WfsDataServer.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 run($request) {
  8. $document = '';
  9. if ('WFS' != V::get('SERVICE', '', $request->query) && ('WFS' != V::get('service', '', $request->query))) {
  10. throw new Api_WfsException("Only WFS Service is allowed");
  11. }
  12. $req = V::get('REQUEST', '', $request->query);
  13. if (!empty($req)) {
  14. $methodName = "{$req}Action";
  15. if (!method_exists($this, $methodName)) {
  16. throw new Api_WfsException("Not Implemented " . htmlspecialchars($req), 501);
  17. }
  18. $this->DBG("WfsServer->{$methodName}() ...", __LINE__);
  19. $document = $this->$methodName($urlQuery);
  20. }
  21. else {
  22. $this->DBG("WfsServer->parseXMLRequest() ...", __LINE__);
  23. $document = $this->parseXMLRequest();
  24. header('Content-type: application/xml');
  25. echo '<?xml version="1.0" encoding="UTF-8"?>';
  26. echo $document; exit;// TODO: return $document;
  27. }
  28. IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">$document (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($document);echo'</pre>';}
  29. if ('raw' == V::get('outputFormat', '', $request->query)) {
  30. header('Content-type: text/plain; charset=utf-8');
  31. echo $document;
  32. } else {
  33. header('Content-type: application/xml');
  34. echo $document;
  35. }
  36. }
  37. public function parseXMLRequest() {
  38. $data = array();
  39. $reqContent = Request::getRequestBody();
  40. if (empty($reqContent)) {
  41. throw new Exception("Empty request");
  42. }
  43. $parserXml = xml_parser_create();
  44. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  45. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  46. if (0 == xml_parse_into_struct($parserXml, $reqContent, $tags)) {
  47. throw new Exception("Error parsing xml");
  48. }
  49. xml_parser_free($parserXml);
  50. if (empty($tags)) {
  51. throw new Exception("Empty structure from request");
  52. }
  53. $rootTagName = V::get('tag', '', $tags[0]);
  54. if ('Transaction' == $rootTagName) return $this->_parseTransactionXmlStruct($reqContent, $tags);
  55. throw new Api_WfsException("Not implemented '{$rootTagName}' #L." . __LINE__, 501);
  56. }
  57. public function getFeatureAction() {
  58. $args = $this->parseGetFeatureArgsFromRequest();
  59. if ('hits' == $args['resultType']) {
  60. return $this->getTotalFeatures($args, $simple = true);
  61. } else {
  62. return $this->getFeatures($args, $simple = true);
  63. }
  64. }
  65. public function getFeatureAdvancedAction() {
  66. $args = $this->parseGetFeatureArgsFromRequest();
  67. if ('hits' == $args['resultType']) {
  68. return $this->getTotalFeatures($args, $simple = false);
  69. } else {
  70. return $this->getFeatures($args, $simple = false);
  71. }
  72. }
  73. public function testOgcFilterAction() {
  74. $type = V::get('TYPENAME', '', $_REQUEST);
  75. $typeEx = explode(':', $type);
  76. $maxFeatures = V::get('MAXFEATURES', '10000', $_REQUEST, 'int');// TODO: Set Deafult Limit
  77. $ogcFilter = V::get('Filter', '', $_REQUEST);
  78. $srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
  79. if (count($typeEx) == 2) {
  80. Lib::loadClass('ParseOgcFilter');
  81. $parser = new ParseOgcFilter();
  82. $parser->loadOgcFilter($ogcFilter);
  83. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  84. echo $queryWhereBuilder->getQueryWhere('t');
  85. } else {
  86. throw new HttpException("Wrong param TYPENAME", 400);
  87. }
  88. }
  89. public function getTotalFeatures($args, $simple = true) {
  90. $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
  91. if($DBG){echo "typeName({$args['xsd:type']})\n";}
  92. $acl = $this->getAclFromTypeName($args['xsd:type']);
  93. $fldList = $this->_getFieldListFromAcl($acl);
  94. $baseNsUri = Api_WfsNs::getBaseWfsUri();
  95. $rootWfsNs = 'p5';
  96. $rootWfsNsUri = "{$baseNsUri}";
  97. $wfsNs = $args['typePrefix'];
  98. $wfsNsUri = "{$baseNsUri}/" . substr($args['typePrefix'], 3);
  99. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&REQUEST=DescribeFeatureType";
  100. // get BBox from geom_field (only one geom fld is allowed)
  101. $geomFld = null;
  102. {
  103. foreach ($fldList as $fldName) {
  104. if ($acl->isGeomField($fldName)) {
  105. $geomFld = $fldName;
  106. }
  107. }
  108. }
  109. if($DBG){echo "ogcFilter(" . strlen($args['ogc:filter']) . "): {$args['ogc:filter']}\n";}
  110. $searchParams = array();
  111. $searchParams['limit'] = $args['limit'];
  112. $searchParams['limitstart'] = $args['offset'];
  113. if (!empty($args['sortBy'])) {
  114. $searchParams['sortBy'] = $args['sortBy'];
  115. } else {
  116. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  117. $searchParams['order_dir'] = 'DESC';
  118. }
  119. if (strlen($args['ogc:filter']) > 0) $searchParams['ogc:Filter'] = $args['ogc:filter'];
  120. if (!empty($args['filterFields'])) $searchParams['cols'] = $args['filterFields'];// propertyName
  121. if (!empty($args['primaryKey'])) $searchParams['primaryKey'] = $args['primaryKey'];// featureID
  122. if (!empty($args['bbox'])) $searchParams['f_the_geom'] = "BBOX:{$args['bbox']}";
  123. if($DBG){echo 'getItems() searchParams:';print_r($searchParams);echo "\n";}
  124. $totalItems = $acl->getTotal($searchParams);
  125. if($DBG){echo 'totalItems:';print_r($totalItems);echo "\n";}
  126. $xmlWriter = new XMLWriter();
  127. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  128. $xmlWriter->openUri('php://output');
  129. $xmlWriter->setIndent(true);
  130. $xmlWriter->startDocument('1.0','UTF-8');
  131. $xmlWriter->startElement('wfs:FeatureCollection');
  132. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs/2.0');
  133. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs/2.0');
  134. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  135. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  136. // $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  137. $xmlWriter->writeAttribute('numberMatched', $totalItems);
  138. $xmlWriter->writeAttribute('numberReturned', 0);
  139. // $xmlWriter->writeAttribute('timeStamp', "TODO: timestamp like '2011-12-09T11:30:16'");
  140. $xmlWriter->endElement();// wfs:FeatureCollection
  141. $xmlWriter->endDocument();
  142. exit;
  143. }
  144. public function getFeatures($args, $simple = true) {
  145. $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
  146. $DBG_DS = V::get('DBG_DS', 0, $_GET, 'int');
  147. $type = $args['typeName'];
  148. if($DBG){echo "typeName({$args['xsd:type']})\n";}
  149. $acl = $this->getAclFromTypeName($args['xsd:type']);
  150. $fldList = $this->_getFieldListFromAcl($acl);
  151. $baseNsUri = Api_WfsNs::getBaseWfsUri();
  152. $rootWfsNs = 'p5';
  153. $rootWfsNsUri = "{$baseNsUri}";
  154. $wfsNs = $args['typePrefix'];
  155. $wfsNsUri = "{$baseNsUri}/" . substr($args['typePrefix'], 3);
  156. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&REQUEST=DescribeFeatureType";
  157. // get BBox from geom_field (only one geom fld is allowed)
  158. $geomFld = null;
  159. {
  160. foreach ($fldList as $fldName) {
  161. if ($acl->isGeomField($fldName)) {
  162. $geomFld = $fldName;
  163. }
  164. }
  165. }
  166. if($DBG){echo "ogcFilter(" . strlen($args['ogc:filter']) . "): {$args['ogc:filter']}\n";}
  167. $searchParams = array();
  168. $searchParams['limit'] = $args['limit'];
  169. $searchParams['limitstart'] = $args['offset'];
  170. if (!empty($args['sortBy'])) {
  171. $searchParams['sortBy'] = $args['sortBy'];
  172. } else {
  173. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  174. $searchParams['order_dir'] = 'DESC';
  175. }
  176. if (strlen($args['ogc:filter']) > 0) $searchParams['ogc:Filter'] = $args['ogc:filter'];
  177. if (!empty($args['filterFields'])) $searchParams['cols'] = $args['filterFields'];// PropertyName
  178. if (!empty($args['primaryKey'])) $searchParams['primaryKey'] = $args['primaryKey'];// featureID
  179. if (!empty($args['bbox'])) $searchParams['f_the_geom'] = "BBOX:{$args['bbox']}";
  180. if($DBG){echo 'getItems:';print_r($searchParams);echo "\n";}
  181. $items = $acl->getItems($searchParams);
  182. header('Content-type: application/xml; charset=utf-8');
  183. $xmlWriter = new XMLWriter();
  184. $xmlWriter->openUri('php://output');
  185. // $xmlWriter->openMemory();// DBG
  186. $xmlWriter->setIndent(true);
  187. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  188. $xmlWriter->startDocument('1.0','UTF-8');
  189. //$xmlWriter->startElementNS('wfs', 'FeatureCollection', 'http://www.opengis.net/wfs');
  190. $xmlWriter->startElement('wfs:FeatureCollection');
  191. // $xmlWriter->writeAttributeNS('xmlns', 'wfs', 'http://www.w3.org/2000/xmlns/', 'http://www.opengis.net/wfs');
  192. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs');
  193. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs');
  194. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  195. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  196. $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
  197. $xmlWriter->writeAttribute("xmlns:{$wfsNs}", $wfsNsUri);
  198. if (!$simple) $xmlWriter->writeAttribute("xmlns:{$rootWfsNs}", $rootWfsNsUri);
  199. $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  200. if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
  201. $tblName = $acl->getName();
  202. foreach ($items as $itemKey => $item) {
  203. if (!is_array($item)) $item = (array)$item;
  204. if($DBG && !empty($geomFld)){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item[$geomFld]).'):';print_r($item[$geomFld]);echo "\n";}
  205. if($DBG_DS){echo ">>> loop({$itemKey}) item: ";print_r($item);echo "\n";}
  206. $xmlWriter->startElement('gml:featureMember');
  207. $xmlWriter->startElement("{$wfsNs}:{$type}");
  208. $xmlWriter->writeAttribute('fid', "{$type}.{$itemKey}");
  209. if (!$simple) $xmlWriter->writeAttribute("{$rootWfsNs}:web_link", Request::getPathUri() . "index.php?_route=ViewTableAjax&namespace=" . $acl->getNamespace() . "#EDIT/{$itemKey}");
  210. foreach ($fldList as $idZasob => $fldName) {
  211. if($DBG_DS){echo ">>> loop({$itemKey}) item({$item['ID']}) fld({$fldName}): ";var_dump($item[$fldName]);echo"\n";}
  212. $fldType = $acl->getXsdFieldType($fldName);
  213. $canReadRecord = $acl->canReadObjectField($fldName, (object)$item);
  214. if ($canReadRecord) {
  215. if ($geomFld != null && $fldName == $geomFld) {
  216. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  217. if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  218. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  219. }
  220. if (!$simple && !$acl->canWriteObjectField($fldName, (object)$item)) {
  221. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  222. }
  223. $this->_typeConverter->createGmlFromWkt_xmlWriter($item[$fldName], $xmlWriter);
  224. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  225. } else if (is_array($item[$fldName])) {// TODO: by struct - REF field
  226. if($DBG_DS){echo">>> TODO({$fldName}) REF item[{$itemKey}][{$fldName}]: ";print_r($item[$fldName]);echo "\n";}
  227. if (1 == count($item[$fldName])) {
  228. $xlink = $item[$fldName][0]['xlink'];
  229. $xlinkParts = explode(':', $xlink);
  230. if (2 != count($xlinkParts)) throw new Exception("Error Processing Reques - wrong xlink format for ".$acl->getName().".{$itemKey}/{$fldName}");
  231. $xlinkParts[0] = Api_WfsNs::getNsUri($xlinkParts[0]);
  232. $xlink = implode('#', $xlinkParts);
  233. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  234. if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  235. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  236. }
  237. if (!$simple && !$acl->canWriteObjectField($fldName, (object)$item)) {
  238. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  239. }
  240. $xmlWriter->writeAttribute('xlink:href', $xlink);
  241. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  242. } else {
  243. throw new Exception("Error Processing Request - too many refs for ".$acl->getName().".{$itemKey}/{$fldName}");
  244. }
  245. } else {
  246. $value = str_replace('&', '&amp;', $item[$fldName]);
  247. if (empty($value) && '0' !== $value) {
  248. continue;
  249. } else {
  250. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  251. if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  252. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  253. }
  254. if (!$simple && !$acl->canWriteObjectField($fldName, (object)$item)) {
  255. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  256. }
  257. $xmlWriter->text($value);
  258. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  259. }
  260. }
  261. }
  262. }
  263. $xmlWriter->endElement();// {$wfsNs}:{$type}
  264. $xmlWriter->endElement();// gml:featureMember
  265. }
  266. $xmlWriter->endElement();// wfs:FeatureCollection
  267. $xmlWriter->endDocument();
  268. exit;
  269. }
  270. public function describeFeatureTypeAction() {
  271. $type = V::get('TYPENAME', '', $_REQUEST);
  272. if (empty($type)) {
  273. $reqContent = Request::getRequestBody();
  274. if (!empty($reqContent)) {
  275. return $this->_parseDescribeFeatureTypeRequest($reqContent);
  276. } else {
  277. return $this->_getDescribeFeatureAllTypes();
  278. }
  279. //throw new HttpException("Wrong param TYPENAME", 400);
  280. }
  281. $typeEx = explode(':', $type);
  282. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  283. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
  284. }
  285. public function describeFeatureTypeAdvancedAction() {
  286. $type = V::get('TYPENAME', '', $_REQUEST);
  287. if (empty($type)) {
  288. $reqContent = Request::getRequestBody();
  289. if (!empty($reqContent)) {
  290. return $this->_parseDescribeFeatureTypeRequest($reqContent, $simple = false);
  291. } else {
  292. return $this->_getDescribeFeatureAllTypes($simple = false);
  293. }
  294. //throw new HttpException("Wrong param TYPENAME", 400);
  295. }
  296. $typeEx = explode(':', $type);
  297. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  298. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1], $simple = false);
  299. }
  300. public function getCapabilitiesAction() {
  301. $wfsServerUrl = $this->getBaseUri();
  302. $serviceTitle = "Web Feature Service";
  303. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  304. //header('Content-type: application/xml; charset="utf-8"');
  305. header('Content-type: application/xml');
  306. $this->_getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription);
  307. }
  308. }