WfsDataServer.php 16 KB

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