WfsDataServer.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. Lib::loadClass('Api_WfsServerBase');
  3. Lib::loadClass('Api_WfsException');
  4. Lib::loadClass('Api_WfsGeomTypeConverter');
  5. class Api_WfsDataServer extends Api_WfsServerBase {
  6. public function parseXMLRequest() {
  7. $data = array();
  8. $reqContent = Request::getRequestBody();
  9. if (empty($reqContent)) {
  10. throw new Exception("Empty request");
  11. }
  12. $parserXml = xml_parser_create();
  13. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  14. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  15. if (0 == xml_parse_into_struct($parserXml, $reqContent, $tags)) {
  16. throw new Exception("Error parsing xml");
  17. }
  18. xml_parser_free($parserXml);
  19. if (empty($tags)) {
  20. throw new Exception("Empty structure from request");
  21. }
  22. $rootTagName = V::get('tag', '', $tags[0]);
  23. if ('Transaction' == $rootTagName) {
  24. return $this->_parseTransactionXmlStruct($reqContent, $tags);
  25. }
  26. throw new Api_WfsException("TODO ... L." . __LINE__, 501);
  27. $xml = new SimpleXMLElement($reqContent);
  28. $namespaces = $xml->getNameSpaces(true);
  29. if ('Transaction' == $xml->getName()) {
  30. $this->_parseTransactionXml($xml);
  31. }
  32. else {
  33. throw new Api_WfsException("Not Implemented " . htmlspecialchars($xml->getName()), 501);
  34. }
  35. }
  36. public function getFeatureAction() {
  37. $type = V::get('TYPENAME', '', $_REQUEST);
  38. $typeEx = explode(':', $type);
  39. $maxFeatures = '10000';// TODO: Set Deafult Limit
  40. $maxFeatures = V::get('MAXFEATURES', $maxFeatures, $_REQUEST, 'int');
  41. $maxFeatures = V::get('maxFeatures', $maxFeatures, $_REQUEST, 'int');
  42. $maxFeatures = V::get('count', $maxFeatures, $_REQUEST, 'int');
  43. $startIndex = V::get('startIndex', 0, $_REQUEST, 'int');// sql offset
  44. $ogcFilter = V::get('Filter', '', $_REQUEST);
  45. $sortBy = V::get('sortBy', '', $_REQUEST);
  46. $propertyName = V::get('propertyName', '', $_REQUEST);
  47. $propertyName = trim($propertyName);
  48. $srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
  49. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  50. if (empty($ogcFilter)) {// read ogc:Filter from POST body if not defined in GET
  51. $reqBody = Request::getRequestBody();
  52. if (!empty($reqBody)) {
  53. $ogcFilter = $this->convertOgcFilterFromRequestBody($reqBody);
  54. if (empty($ogcFilter)) throw new Api_WfsException("Error Processing ogc:Filter", 501);
  55. }
  56. }
  57. if ('hits' == V::get('resultType', '', $_REQUEST)) {// resultType=hits
  58. return $this->getTotalFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex, $propertyName, $simple = true);
  59. } else {
  60. return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex, $propertyName, $simple = true);
  61. }
  62. }
  63. public function getFeatureAdvancedAction() {
  64. $type = V::get('TYPENAME', '', $_REQUEST);
  65. $typeEx = explode(':', $type);
  66. $maxFeatures = '10000';// TODO: Set Deafult Limit
  67. $maxFeatures = V::get('MAXFEATURES', $maxFeatures, $_REQUEST, 'int');
  68. $maxFeatures = V::get('maxFeatures', $maxFeatures, $_REQUEST, 'int');
  69. $maxFeatures = V::get('count', $maxFeatures, $_REQUEST, 'int');
  70. $startIndex = V::get('startIndex', 0, $_REQUEST, 'int');// sql offset
  71. $ogcFilter = V::get('Filter', '', $_REQUEST);
  72. $sortBy = V::get('sortBy', '', $_REQUEST);
  73. $propertyName = V::get('propertyName', '', $_REQUEST);
  74. $propertyName = trim($propertyName);
  75. $srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
  76. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  77. if (empty($ogcFilter)) {// read ogc:Filter from POST body if not defined in GET
  78. $reqBody = Request::getRequestBody();
  79. if (!empty($reqBody)) {
  80. $ogcFilter = $this->convertOgcFilterFromRequestBody($reqBody);
  81. }
  82. }
  83. if ('hits' == V::get('resultType', '', $_REQUEST)) {// resultType=hits
  84. return $this->getTotalFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex, $propertyName, $simple = false);
  85. } else {
  86. return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex, $propertyName, $simple = false);
  87. }
  88. }
  89. public function testOgcFilterAction() {
  90. $type = V::get('TYPENAME', '', $_REQUEST);
  91. $typeEx = explode(':', $type);
  92. $maxFeatures = V::get('MAXFEATURES', '10000', $_REQUEST, 'int');// TODO: Set Deafult Limit
  93. $ogcFilter = V::get('Filter', '', $_REQUEST);
  94. $srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
  95. if (count($typeEx) == 2) {
  96. Lib::loadClass('ParseOgcFilter');
  97. $parser = new ParseOgcFilter();
  98. $parser->loadOgcFilter($ogcFilter);
  99. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  100. echo $queryWhereBuilder->getQueryWhere('t');
  101. } else {
  102. throw new HttpException("Wrong param TYPENAME", 400);
  103. }
  104. }
  105. public function getTotalFeatures($nsPrefix, $type, $maxFeatures, $srsname, $ogcFilter = '', $sortBy = '', $startIndex = 0, $propertyName = '', $simple = true) {
  106. $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
  107. $typeName = "{$nsPrefix}:{$type}";
  108. if($DBG){echo "typeName($typeName})\n";}
  109. $acl = $this->getAclFromTypeName($typeName);
  110. $fldList = $this->_getFieldListFromAcl($acl);
  111. $baseNsUri = $this->getBaseNamespaceUri();
  112. $rootWfsNs = 'p5';
  113. $rootWfsNsUri = "{$baseNsUri}";
  114. //$wfsNs = 'p5_default_db_' . $type;//$nsPrefix;
  115. $wfsNs = $nsPrefix;//'p5_default_db';
  116. $wfsNsUri = "{$baseNsUri}/" . substr($nsPrefix, 3);
  117. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$typeName}&REQUEST=DescribeFeatureType";
  118. // get BBox from geom_field (only one geom fld is allowed)
  119. $geomFld = null;
  120. {
  121. foreach ($fldList as $idZasob => $fldName) {
  122. if ($acl->isGeomField($fldName)) {
  123. $geomFld = $fldName;
  124. }
  125. }
  126. }
  127. if($DBG){echo "ogcFilter(" . strlen($ogcFilter) . "): {$ogcFilter}\n";}
  128. $searchParams = array();
  129. $searchParams['limit'] = $maxFeatures;
  130. $searchParams['limitstart'] = $startIndex;
  131. if (!empty($sortBy)) {
  132. $searchParams['sortBy'] = $sortBy;
  133. } else {
  134. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  135. $searchParams['order_dir'] = 'DESC';
  136. }
  137. if (strlen($ogcFilter) > 0) $searchParams['ogc:Filter'] = $ogcFilter;
  138. if (strlen($propertyName) > 0) {
  139. $propertyNamesEx = explode(',', $propertyName);
  140. $onlyCols = array();
  141. foreach ($propertyNamesEx as $colName) {
  142. $colName = trim($colName);
  143. $onlyCols[] = $colName;
  144. }
  145. if (!empty($onlyCols)) $searchParams['cols'] = $onlyCols;
  146. }
  147. {// BBOX
  148. // 54.26931096743426,18.48242909824306,54.26738118403914,18.478738378639246
  149. $bbox = V::get('BBOX', '', $_GET);
  150. if (!empty($bbox)) {
  151. if (preg_match("/^\d+(.\d+)?,\d+(.\d+)?,\d+(.\d+)?,\d+(.\d+)?$/", $bbox, $matches)) {
  152. $searchParams['f_the_geom'] = "BBOX:{$bbox}";
  153. } else {
  154. // throw new Exception("Error Processing Request", 1);// ?
  155. }
  156. }
  157. }
  158. if($DBG){echo 'getItems() searchParams:';print_r($searchParams);echo "\n";}
  159. $totalItems = $acl->getTotal($searchParams);
  160. if($DBG){echo 'totalItems:';print_r($totalItems);echo "\n";}
  161. $xmlWriter = new XMLWriter();
  162. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  163. $xmlWriter->openUri('php://output');
  164. $xmlWriter->setIndent(true);
  165. $xmlWriter->startDocument('1.0','UTF-8');
  166. $xmlWriter->startElement('wfs:FeatureCollection');
  167. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs/2.0');
  168. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs/2.0');
  169. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  170. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  171. // $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  172. $xmlWriter->writeAttribute('numberMatched', $totalItems);
  173. $xmlWriter->writeAttribute('numberReturned', 0);
  174. // $xmlWriter->writeAttribute('timeStamp', "TODO: timestamp like '2011-12-09T11:30:16'");
  175. $xmlWriter->endElement();// wfs:FeatureCollection
  176. $xmlWriter->endDocument();
  177. exit;
  178. }
  179. public function getFeatures($nsPrefix, $type, $maxFeatures, $srsname, $ogcFilter = '', $sortBy = '', $startIndex = 0, $propertyName = '', $simple = true) {
  180. $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
  181. $typeName = "{$nsPrefix}:{$type}";
  182. if($DBG){echo "typeName($typeName})\n";}
  183. $acl = $this->getAclFromTypeName($typeName);
  184. $fldList = $this->_getFieldListFromAcl($acl);
  185. $baseNsUri = $this->getBaseNamespaceUri();
  186. $rootWfsNs = 'p5';
  187. $rootWfsNsUri = "{$baseNsUri}";
  188. //$wfsNs = 'p5_default_db_' . $type;//$nsPrefix;
  189. $wfsNs = $nsPrefix;//'p5_default_db';
  190. $wfsNsUri = "{$baseNsUri}/" . substr($nsPrefix, 3);
  191. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$typeName}&REQUEST=DescribeFeatureType";
  192. // get BBox from geom_field (only one geom fld is allowed)
  193. $geomFld = null;
  194. {
  195. foreach ($fldList as $idZasob => $fldName) {
  196. if ($acl->isGeomField($fldName)) {
  197. $geomFld = $fldName;
  198. }
  199. }
  200. }
  201. if($DBG){echo "ogcFilter(" . strlen($ogcFilter) . "): {$ogcFilter}\n";}
  202. $searchParams = array();
  203. $searchParams['limit'] = $maxFeatures;
  204. $searchParams['limitstart'] = $startIndex;
  205. if (!empty($sortBy)) {
  206. $searchParams['sortBy'] = $sortBy;
  207. } else {
  208. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  209. $searchParams['order_dir'] = 'DESC';
  210. }
  211. if (strlen($ogcFilter) > 0) $searchParams['ogc:Filter'] = $ogcFilter;
  212. if (strlen($propertyName) > 0) {
  213. $propertyNamesEx = explode(',', $propertyName);
  214. $onlyCols = array();
  215. foreach ($propertyNamesEx as $colName) {
  216. $colName = trim($colName);
  217. $onlyCols[] = $colName;
  218. }
  219. if (!empty($onlyCols)) $searchParams['cols'] = $onlyCols;
  220. }
  221. {// BBOX
  222. // 54.26931096743426,18.48242909824306,54.26738118403914,18.478738378639246
  223. $bbox = V::get('BBOX', '', $_GET);
  224. if (!empty($bbox)) {
  225. if (preg_match("/^\d+(.\d+)?,\d+(.\d+)?,\d+(.\d+)?,\d+(.\d+)?$/", $bbox, $matches)) {
  226. $searchParams['f_the_geom'] = "BBOX:{$bbox}";
  227. } else {
  228. // throw new Exception("Error Processing Request", 1);// ?
  229. }
  230. }
  231. }
  232. if($DBG){echo 'getItems:';print_r($searchParams);echo "\n";}
  233. $items = $acl->getItems($searchParams);
  234. $dom = new DOMDocument('1.0', 'utf-8');
  235. $dom->formatOutput = true;
  236. $dom->preserveWhiteSpace = false;
  237. $rootNode = $dom->createElementNS('http://www.opengis.net/wfs', 'wfs:FeatureCollection');
  238. $dom->appendChild($rootNode);
  239. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.opengis.net/wfs');
  240. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:wfs', 'http://www.opengis.net/wfs');
  241. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  242. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  243. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  244. if (!$simple) $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:{$rootWfsNs}", $rootWfsNsUri);
  245. //$rootNode->setAttribute('xsi:schemaLocation', 'http://www.opengis.net/wfs');
  246. $rootNode->setAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  247. if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
  248. if (empty($items)) {
  249. $pKeyField = $acl->getPrimaryKeyField();
  250. $fakeItem = new stdClass();
  251. $fakeItem->{$pKeyField} = 0;
  252. $items[0] = $fakeItem;
  253. }
  254. foreach ($items as $itemKey => $item) {
  255. if($DBG && !empty($geomFld)){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item->{$geomFld}).'):';print_r($item->{$geomFld});echo "\n";}
  256. $featureMemberNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:featureMember');
  257. $rootNode->appendChild($featureMemberNode);
  258. $featureNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$type}");
  259. $featureMemberNode->appendChild($featureNode);
  260. $featureNode->setAttribute('fid', "{$type}.{$itemKey}");
  261. foreach ($fldList as $idZasob => $fldName) {
  262. $featureFldNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$fldName}");
  263. if ($acl->isAllowed($idZasob, 'R', $item)) {
  264. if ($geomFld != null && $geomFld == $fldName) {
  265. $geomNode = $this->_typeConverter->createGmlFromWkt($item->{$fldName}, $dom);
  266. if (!$geomNode) continue;
  267. $featureFldNode->appendChild($geomNode);
  268. } else {
  269. $featureFldNode->nodeValue = str_replace('&', '&amp;', $item->{$fldName});
  270. if (empty($featureFldNode->nodeValue) && '0' !== $featureFldNode->nodeValue) {
  271. continue;
  272. }
  273. }
  274. }
  275. if (!$simple) {
  276. if (!$acl->isAllowed($idZasob, 'R', $item)) {
  277. $featureFldNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_read", "false");
  278. }
  279. if ($acl->isAllowed($idZasob, 'W', $item)) {
  280. $featureFldNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");
  281. }
  282. }
  283. $featureNode->appendChild($featureFldNode);
  284. }
  285. }
  286. return $dom->saveXml();
  287. }
  288. public function describeFeatureTypeAction() {
  289. $type = V::get('TYPENAME', '', $_REQUEST);
  290. if (empty($type)) {
  291. $reqContent = Request::getRequestBody();
  292. if (!empty($reqContent)) {
  293. return $this->_parseDescribeFeatureTypeRequest($reqContent);
  294. } else {
  295. return $this->_getDescribeFeatureAllTypes();
  296. }
  297. //throw new HttpException("Wrong param TYPENAME", 400);
  298. }
  299. $typeEx = explode(':', $type);
  300. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  301. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
  302. }
  303. public function describeFeatureTypeAdvancedAction() {
  304. $type = V::get('TYPENAME', '', $_REQUEST);
  305. if (empty($type)) {
  306. $reqContent = Request::getRequestBody();
  307. if (!empty($reqContent)) {
  308. return $this->_parseDescribeFeatureTypeRequest($reqContent, $simple = false);
  309. } else {
  310. return $this->_getDescribeFeatureAllTypes($simple = false);
  311. }
  312. //throw new HttpException("Wrong param TYPENAME", 400);
  313. }
  314. $typeEx = explode(':', $type);
  315. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  316. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1], $simple = false);
  317. }
  318. public function getCapabilitiesAction() {
  319. $wfsServerUrl = $this->getBaseUri();
  320. $serviceTitle = "Web Feature Service";
  321. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  322. //header('Content-type: application/xml; charset="utf-8"');
  323. header('Content-type: application/xml');
  324. $this->_getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription);
  325. }
  326. }