WfsDataServer.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. if (empty($items)) {
  202. $pKeyField = $acl->getPrimaryKeyField();
  203. $fakeItem = new stdClass();
  204. $fakeItem->{$pKeyField} = 0;
  205. $items[0] = $fakeItem;
  206. }
  207. $sourceName = $acl->getSourceName();
  208. $tblName = $acl->getName();
  209. foreach ($items as $itemKey => $item) {
  210. if (!is_array($item)) $item = (array)$item;
  211. if($DBG && !empty($geomFld)){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item[$geomFld]).'):';print_r($item[$geomFld]);echo "\n";}
  212. if($DBG_DS){echo ">>> loop({$itemKey}) item: ";print_r($item);echo "\n";}
  213. $xmlWriter->startElement('gml:featureMember');
  214. $xmlWriter->startElement("{$wfsNs}:{$type}");
  215. $xmlWriter->writeAttribute('fid', "{$type}.{$itemKey}");
  216. if (!$simple) $xmlWriter->writeAttribute("{$rootWfsNs}:web_link", Request::getPathUri() . "index.php?_route=ViewTableAjax&typeName=p5_{$sourceName}:{$tblName}#EDIT/{$itemKey}");
  217. foreach ($fldList as $idZasob => $fldName) {
  218. if($DBG_DS){echo ">>> loop({$itemKey}) item({$item['ID']}) fld({$fldName}): ";var_dump($item[$fldName]);echo"\n";}
  219. $fldType = $acl->getXsdFieldType($fldName);
  220. $canReadRecord = $acl->canReadObjectField($fldName, (object)$item);
  221. if ($canReadRecord) {
  222. if ($geomFld != null && $fldName == $geomFld) {
  223. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  224. if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  225. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  226. }
  227. if (!$simple && !$acl->canWriteObjectField($fldName, (object)$item)) {
  228. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  229. }
  230. $this->_typeConverter->createGmlFromWkt_xmlWriter($item[$fldName], $xmlWriter);
  231. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  232. } else if (is_array($item[$fldName])) {// TODO: by struct - REF field
  233. if($DBG_DS){echo">>> TODO({$fldName}) REF item[{$itemKey}][{$fldName}]: ";print_r($item[$fldName]);echo "\n";}
  234. if (1 == count($item[$fldName])) {
  235. $xlink = $item[$fldName][0]['xlink'];
  236. $xlinkParts = explode(':', $xlink);
  237. if (2 != count($xlinkParts)) throw new Exception("Error Processing Reques - wrong xlink format for ".$acl->getName().".{$itemKey}/{$fldName}");
  238. $xlinkParts[0] = Api_WfsNs::getNsUri($xlinkParts[0]);
  239. $xlink = implode('#', $xlinkParts);
  240. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  241. if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  242. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  243. }
  244. if (!$simple && !$acl->canWriteObjectField($fldName, (object)$item)) {
  245. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  246. }
  247. $xmlWriter->writeAttribute('xlink:href', $xlink);
  248. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  249. } else {
  250. throw new Exception("Error Processing Request - too many refs for ".$acl->getName().".{$itemKey}/{$fldName}");
  251. }
  252. } else {
  253. $value = str_replace('&', '&amp;', $item[$fldName]);
  254. if (empty($value) && '0' !== $value) {
  255. continue;
  256. } else {
  257. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  258. if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  259. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  260. }
  261. if (!$simple && !$acl->canWriteObjectField($fldName, (object)$item)) {
  262. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  263. }
  264. $xmlWriter->text($value);
  265. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  266. }
  267. }
  268. }
  269. }
  270. $xmlWriter->endElement();// {$wfsNs}:{$type}
  271. $xmlWriter->endElement();// gml:featureMember
  272. }
  273. $xmlWriter->endElement();// wfs:FeatureCollection
  274. $xmlWriter->endDocument();
  275. exit;
  276. }
  277. public function describeFeatureTypeAction() {
  278. $type = V::get('TYPENAME', '', $_REQUEST);
  279. if (empty($type)) {
  280. $reqContent = Request::getRequestBody();
  281. if (!empty($reqContent)) {
  282. return $this->_parseDescribeFeatureTypeRequest($reqContent);
  283. } else {
  284. return $this->_getDescribeFeatureAllTypes();
  285. }
  286. //throw new HttpException("Wrong param TYPENAME", 400);
  287. }
  288. $typeEx = explode(':', $type);
  289. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  290. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
  291. }
  292. public function describeFeatureTypeAdvancedAction() {
  293. $type = V::get('TYPENAME', '', $_REQUEST);
  294. if (empty($type)) {
  295. $reqContent = Request::getRequestBody();
  296. if (!empty($reqContent)) {
  297. return $this->_parseDescribeFeatureTypeRequest($reqContent, $simple = false);
  298. } else {
  299. return $this->_getDescribeFeatureAllTypes($simple = false);
  300. }
  301. //throw new HttpException("Wrong param TYPENAME", 400);
  302. }
  303. $typeEx = explode(':', $type);
  304. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  305. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1], $simple = false);
  306. }
  307. public function getCapabilitiesAction() {
  308. $wfsServerUrl = $this->getBaseUri();
  309. $serviceTitle = "Web Feature Service";
  310. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  311. //header('Content-type: application/xml; charset="utf-8"');
  312. header('Content-type: application/xml');
  313. $this->_getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription);
  314. }
  315. }