WfsDataServer.php 15 KB

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