WfsDataServer.php 15 KB

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