WfsDataServer.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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) return $this->_parseTransactionXmlStruct($reqContent, $tags);
  25. throw new Api_WfsException("Not implemented '{$rootTagName}' #L." . __LINE__, 501);
  26. }
  27. public function getFeatureAction() {
  28. $args = $this->parseGetFeatureArgsFromRequest();
  29. if ('hits' == $args['resultType']) {
  30. return $this->getTotalFeatures($args, $simple = true);
  31. } else {
  32. return $this->getFeatures($args, $simple = true);
  33. }
  34. }
  35. public function getFeatureAdvancedAction() {
  36. $args = $this->parseGetFeatureArgsFromRequest();
  37. if ('hits' == $args['resultType']) {
  38. return $this->getTotalFeatures($args, $simple = false);
  39. } else {
  40. return $this->getFeatures($args, $simple = false);
  41. }
  42. }
  43. public function testOgcFilterAction() {
  44. $type = V::get('TYPENAME', '', $_REQUEST);
  45. $typeEx = explode(':', $type);
  46. $maxFeatures = V::get('MAXFEATURES', '10000', $_REQUEST, 'int');// TODO: Set Deafult Limit
  47. $ogcFilter = V::get('Filter', '', $_REQUEST);
  48. $srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
  49. if (count($typeEx) == 2) {
  50. Lib::loadClass('ParseOgcFilter');
  51. $parser = new ParseOgcFilter();
  52. $parser->loadOgcFilter($ogcFilter);
  53. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  54. echo $queryWhereBuilder->getQueryWhere('t');
  55. } else {
  56. throw new HttpException("Wrong param TYPENAME", 400);
  57. }
  58. }
  59. public function getTotalFeatures($args, $simple = true) {
  60. $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
  61. if($DBG){echo "typeName({$args['xsd:type']})\n";}
  62. $acl = $this->getAclFromTypeName($args['xsd:type']);
  63. $fldList = $this->_getFieldListFromAcl($acl);
  64. $baseNsUri = Api_WfsNs::getBaseWfsUri();
  65. $rootWfsNs = 'p5';
  66. $rootWfsNsUri = "{$baseNsUri}";
  67. $wfsNs = $args['typePrefix'];
  68. $wfsNsUri = "{$baseNsUri}/" . substr($args['typePrefix'], 3);
  69. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&REQUEST=DescribeFeatureType";
  70. // get BBox from geom_field (only one geom fld is allowed)
  71. $geomFld = null;
  72. {
  73. foreach ($fldList as $fldName) {
  74. if ($acl->isGeomField($fldName)) {
  75. $geomFld = $fldName;
  76. }
  77. }
  78. }
  79. if($DBG){echo "ogcFilter(" . strlen($args['ogc:Filter']) . "): {$args['ogc:Filter']}\n";}
  80. $searchParams = array();
  81. $searchParams['limit'] = $args['limit'];
  82. $searchParams['limitstart'] = $args['offset'];
  83. if (!empty($args['sortBy'])) {
  84. $searchParams['sortBy'] = $args['sortBy'];
  85. } else {
  86. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  87. $searchParams['order_dir'] = 'DESC';
  88. }
  89. if (strlen($args['ogc:Filter']) > 0) $searchParams['ogc:Filter'] = $args['ogc:Filter'];
  90. if (!empty($args['filterFields'])) $searchParams['cols'] = $args['filterFields'];// propertyName
  91. if (!empty($args['primaryKey'])) $searchParams['primaryKey'] = $args['primaryKey'];// featureID
  92. if (!empty($args['bbox'])) $searchParams['f_the_geom'] = "BBOX:{$args['bbox']}";
  93. if($DBG){echo 'getItems() searchParams:';print_r($searchParams);echo "\n";}
  94. $totalItems = $acl->getTotal($searchParams);
  95. if($DBG){echo 'totalItems:';print_r($totalItems);echo "\n";}
  96. $xmlWriter = new XMLWriter();
  97. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  98. $xmlWriter->openUri('php://output');
  99. $xmlWriter->setIndent(true);
  100. $xmlWriter->startDocument('1.0','UTF-8');
  101. $xmlWriter->startElement('wfs:FeatureCollection');
  102. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs/2.0');
  103. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs/2.0');
  104. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  105. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  106. // $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  107. $xmlWriter->writeAttribute('numberMatched', $totalItems);
  108. $xmlWriter->writeAttribute('numberReturned', 0);
  109. // $xmlWriter->writeAttribute('timeStamp', "TODO: timestamp like '2011-12-09T11:30:16'");
  110. $xmlWriter->endElement();// wfs:FeatureCollection
  111. $xmlWriter->endDocument();
  112. exit;
  113. }
  114. public function getFeatures($args, $simple = true) {
  115. $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
  116. $DBG_DS = V::get('DBG_DS', 0, $_GET, 'int');
  117. $type = $args['typeName'];
  118. if($DBG){echo "typeName({$args['xsd:type']})\n";}
  119. $acl = $this->getAclFromTypeName($args['xsd:type']);
  120. $fldList = $this->_getFieldListFromAcl($acl);
  121. $baseNsUri = Api_WfsNs::getBaseWfsUri();
  122. $rootWfsNs = 'p5';
  123. $rootWfsNsUri = "{$baseNsUri}";
  124. $wfsNs = $args['typePrefix'];
  125. $wfsNsUri = "{$baseNsUri}/" . substr($args['typePrefix'], 3);
  126. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&REQUEST=DescribeFeatureType";
  127. // get BBox from geom_field (only one geom fld is allowed)
  128. $geomFld = null;
  129. {
  130. foreach ($fldList as $fldName) {
  131. if ($acl->isGeomField($fldName)) {
  132. $geomFld = $fldName;
  133. }
  134. }
  135. }
  136. if($DBG){echo "ogcFilter(" . strlen($args['ogc:Filter']) . "): {$args['ogc:Filter']}\n";}
  137. $searchParams = array();
  138. $searchParams['limit'] = $args['limit'];
  139. $searchParams['limitstart'] = $args['offset'];
  140. if (!empty($args['sortBy'])) {
  141. $searchParams['sortBy'] = $args['sortBy'];
  142. } else {
  143. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  144. $searchParams['order_dir'] = 'DESC';
  145. }
  146. if (strlen($args['ogc:Filter']) > 0) $searchParams['ogc:Filter'] = $args['ogc:Filter'];
  147. if (!empty($args['filterFields'])) $searchParams['cols'] = $args['filterFields'];// PropertyName
  148. if (!empty($args['primaryKey'])) $searchParams['primaryKey'] = $args['primaryKey'];// featureID
  149. if (!empty($args['bbox'])) $searchParams['f_the_geom'] = "BBOX:{$args['bbox']}";
  150. if($DBG){echo 'getItems:';print_r($searchParams);echo "\n";}
  151. $items = $acl->getItems($searchParams);
  152. $dom = new DOMDocument('1.0', 'utf-8');
  153. $dom->formatOutput = true;
  154. $dom->preserveWhiteSpace = false;
  155. $rootNode = $dom->createElementNS('http://www.opengis.net/wfs', 'wfs:FeatureCollection');
  156. $dom->appendChild($rootNode);
  157. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.opengis.net/wfs');
  158. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:wfs', 'http://www.opengis.net/wfs');
  159. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  160. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  161. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', 'http://www.w3.org/1999/xlink');
  162. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  163. if (!$simple) $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:{$rootWfsNs}", $rootWfsNsUri);
  164. //$rootNode->setAttribute('xsi:schemaLocation', 'http://www.opengis.net/wfs');
  165. $rootNode->setAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  166. if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
  167. if (empty($items)) {
  168. $pKeyField = $acl->getPrimaryKeyField();
  169. $fakeItem = new stdClass();
  170. $fakeItem->{$pKeyField} = 0;
  171. $items[0] = $fakeItem;
  172. }
  173. $sourceName = $acl->getSourceName();
  174. $tblName = $acl->getName();
  175. foreach ($items as $itemKey => $item) {
  176. if (!is_array($item)) $item = (array)$item;
  177. if($DBG && !empty($geomFld)){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item[$geomFld]).'):';print_r($item[$geomFld]);echo "\n";}
  178. if($DBG_DS){echo ">>> loop({$itemKey}) item: ";print_r($item);echo "\n";}
  179. $featureMemberNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:featureMember');
  180. $rootNode->appendChild($featureMemberNode);
  181. $featureNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$type}");
  182. $featureMemberNode->appendChild($featureNode);
  183. $featureNode->setAttribute('fid', "{$type}.{$itemKey}");
  184. if (!$simple) $featureNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:web_link", Request::getPathUri() . "index.php?_route=ViewTableAjax&typeName=p5_{$sourceName}:{$tblName}#EDIT/{$itemKey}");
  185. foreach ($fldList as $fldName) {
  186. $fldType = $acl->getXsdFieldType($fldName);
  187. $featureFldNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$fldName}");
  188. if($DBG_DS){echo">>> acl->canReadObjectField('{$fldName}', \$item) ...\n";}
  189. if ($acl->canReadObjectField($fldName, (object)$item)) {
  190. if ($geomFld != null && $geomFld == $fldName) {
  191. $geomNode = $this->_typeConverter->createGmlFromWkt($item[$fldName], $dom);
  192. if (!$geomNode) continue;
  193. $featureFldNode->appendChild($geomNode);
  194. } else if (is_array($item[$fldName])) {// TODO: by struct - REF field
  195. if($DBG_DS){echo">>> TODO({$fldName}) REF item[{$itemKey}][{$fldName}]: ";print_r($item[$fldName]);echo "\n";}
  196. if (1 == count($item[$fldName])) {
  197. $xlink = $item[$fldName][0]['xlink'];
  198. $xlinkParts = explode(':', $xlink);
  199. if (2 != count($xlinkParts)) throw new Exception("Error Processing Reques - wrong xlink format for ".$acl->getName().".{$itemKey}/{$fldName}");
  200. $xlinkParts[0] = Api_WfsNs::getNsUri($xlinkParts[0]);
  201. $xlink = implode('#', $xlinkParts);
  202. $featureFldNode->setAttribute('xlink:href', $xlink);
  203. } else {
  204. throw new Exception("Error Processing Request - too many refs for ".$acl->getName().".{$itemKey}/{$fldName}");
  205. }
  206. // foreach ($item[$fldName] as $ref) {
  207. // $refNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$fldName}");
  208. // $featureFldNode->appendChild($refNode);
  209. // }
  210. } else {
  211. $featureFldNode->nodeValue = str_replace('&', '&amp;', $item[$fldName]);
  212. if (empty($featureFldNode->nodeValue) && '0' !== $featureFldNode->nodeValue) {
  213. continue;
  214. }
  215. }
  216. }
  217. if (!$simple) {
  218. if (!$acl->canReadObjectField($fldName, (object)$item)) {
  219. $featureFldNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_read", "false");
  220. }
  221. if ($acl->canWriteObjectField($fldName, (object)$item)) {
  222. $featureFldNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");
  223. }
  224. }
  225. $featureNode->appendChild($featureFldNode);
  226. }
  227. }
  228. return $dom->saveXml();
  229. }
  230. public function describeFeatureTypeAction() {
  231. $type = V::get('TYPENAME', '', $_REQUEST);
  232. if (empty($type)) {
  233. $reqContent = Request::getRequestBody();
  234. if (!empty($reqContent)) {
  235. return $this->_parseDescribeFeatureTypeRequest($reqContent);
  236. } else {
  237. return $this->_getDescribeFeatureAllTypes();
  238. }
  239. //throw new HttpException("Wrong param TYPENAME", 400);
  240. }
  241. $typeEx = explode(':', $type);
  242. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  243. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
  244. }
  245. public function describeFeatureTypeAdvancedAction() {
  246. $type = V::get('TYPENAME', '', $_REQUEST);
  247. if (empty($type)) {
  248. $reqContent = Request::getRequestBody();
  249. if (!empty($reqContent)) {
  250. return $this->_parseDescribeFeatureTypeRequest($reqContent, $simple = false);
  251. } else {
  252. return $this->_getDescribeFeatureAllTypes($simple = false);
  253. }
  254. //throw new HttpException("Wrong param TYPENAME", 400);
  255. }
  256. $typeEx = explode(':', $type);
  257. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  258. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1], $simple = false);
  259. }
  260. public function getCapabilitiesAction() {
  261. $wfsServerUrl = $this->getBaseUri();
  262. $serviceTitle = "Web Feature Service";
  263. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  264. //header('Content-type: application/xml; charset="utf-8"');
  265. header('Content-type: application/xml');
  266. $this->_getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription);
  267. }
  268. }