WfsDataServer.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. header('Content-type: application/xml; charset=utf-8');
  153. $xmlWriter = new XMLWriter();
  154. $xmlWriter->openUri('php://output');
  155. // $xmlWriter->openMemory();// DBG
  156. $xmlWriter->setIndent(true);
  157. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  158. $xmlWriter->startDocument('1.0','UTF-8');
  159. //$xmlWriter->startElementNS('wfs', 'FeatureCollection', 'http://www.opengis.net/wfs');
  160. $xmlWriter->startElement('wfs:FeatureCollection');
  161. // $xmlWriter->writeAttributeNS('xmlns', 'wfs', 'http://www.w3.org/2000/xmlns/', 'http://www.opengis.net/wfs');
  162. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs');
  163. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs');
  164. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  165. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  166. $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
  167. $xmlWriter->writeAttribute("xmlns:{$wfsNs}", $wfsNsUri);
  168. if (!$simple) $xmlWriter->writeAttribute("xmlns:{$rootWfsNs}", $rootWfsNsUri);
  169. $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  170. if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
  171. if (empty($items)) {
  172. $pKeyField = $acl->getPrimaryKeyField();
  173. $fakeItem = new stdClass();
  174. $fakeItem->{$pKeyField} = 0;
  175. $items[0] = $fakeItem;
  176. }
  177. $sourceName = $acl->getSourceName();
  178. $tblName = $acl->getName();
  179. foreach ($items as $itemKey => $item) {
  180. if (!is_array($item)) $item = (array)$item;
  181. if($DBG && !empty($geomFld)){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item[$geomFld]).'):';print_r($item[$geomFld]);echo "\n";}
  182. if($DBG_DS){echo ">>> loop({$itemKey}) item: ";print_r($item);echo "\n";}
  183. $xmlWriter->startElement('gml:featureMember');
  184. $xmlWriter->startElement("{$wfsNs}:{$type}");
  185. $xmlWriter->writeAttribute('fid', "{$type}.{$itemKey}");
  186. if (!$simple) $xmlWriter->writeAttribute("{$rootWfsNs}:web_link", Request::getPathUri() . "index.php?_route=ViewTableAjax&typeName=p5_{$sourceName}:{$tblName}#EDIT/{$itemKey}");
  187. foreach ($fldList as $idZasob => $fldName) {
  188. if($DBG_DS){echo ">>> loop({$itemKey}) item({$item['ID']}) fld({$fldName}): ";var_dump($item[$fldName]);echo"\n";}
  189. $fldType = $acl->getXsdFieldType($fldName);
  190. $canReadRecord = $acl->canReadObjectField($fldName, (object)$item);
  191. if ($canReadRecord) {
  192. if ($geomFld != null && $fldName == $geomFld) {
  193. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  194. if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  195. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  196. }
  197. if (!$simple && !$acl->canWriteObjectField($fldName, (object)$item)) {
  198. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  199. }
  200. $this->_typeConverter->createGmlFromWkt_xmlWriter($item[$fldName], $xmlWriter);
  201. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  202. } else if (is_array($item[$fldName])) {// TODO: by struct - REF field
  203. if($DBG_DS){echo">>> TODO({$fldName}) REF item[{$itemKey}][{$fldName}]: ";print_r($item[$fldName]);echo "\n";}
  204. if (1 == count($item[$fldName])) {
  205. $xlink = $item[$fldName][0]['xlink'];
  206. $xlinkParts = explode(':', $xlink);
  207. if (2 != count($xlinkParts)) throw new Exception("Error Processing Reques - wrong xlink format for ".$acl->getName().".{$itemKey}/{$fldName}");
  208. $xlinkParts[0] = Api_WfsNs::getNsUri($xlinkParts[0]);
  209. $xlink = implode('#', $xlinkParts);
  210. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  211. if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  212. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  213. }
  214. if (!$simple && !$acl->canWriteObjectField($fldName, (object)$item)) {
  215. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  216. }
  217. $xmlWriter->writeAttribute('xlink:href', $xlink);
  218. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  219. } else {
  220. throw new Exception("Error Processing Request - too many refs for ".$acl->getName().".{$itemKey}/{$fldName}");
  221. }
  222. } else {
  223. $value = str_replace('&', '&amp;', $item[$fldName]);
  224. if (empty($value) && '0' !== $value) {
  225. continue;
  226. } else {
  227. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  228. if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
  229. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
  230. }
  231. if (!$simple && !$acl->canWriteObjectField($fldName, (object)$item)) {
  232. $xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
  233. }
  234. $xmlWriter->text($value);
  235. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  236. }
  237. }
  238. }
  239. }
  240. $xmlWriter->endElement();// {$wfsNs}:{$type}
  241. $xmlWriter->endElement();// gml:featureMember
  242. }
  243. $xmlWriter->endElement();// wfs:FeatureCollection
  244. $xmlWriter->endDocument();
  245. exit;
  246. }
  247. public function describeFeatureTypeAction() {
  248. $type = V::get('TYPENAME', '', $_REQUEST);
  249. if (empty($type)) {
  250. $reqContent = Request::getRequestBody();
  251. if (!empty($reqContent)) {
  252. return $this->_parseDescribeFeatureTypeRequest($reqContent);
  253. } else {
  254. return $this->_getDescribeFeatureAllTypes();
  255. }
  256. //throw new HttpException("Wrong param TYPENAME", 400);
  257. }
  258. $typeEx = explode(':', $type);
  259. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  260. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
  261. }
  262. public function describeFeatureTypeAdvancedAction() {
  263. $type = V::get('TYPENAME', '', $_REQUEST);
  264. if (empty($type)) {
  265. $reqContent = Request::getRequestBody();
  266. if (!empty($reqContent)) {
  267. return $this->_parseDescribeFeatureTypeRequest($reqContent, $simple = false);
  268. } else {
  269. return $this->_getDescribeFeatureAllTypes($simple = false);
  270. }
  271. //throw new HttpException("Wrong param TYPENAME", 400);
  272. }
  273. $typeEx = explode(':', $type);
  274. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  275. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1], $simple = false);
  276. }
  277. public function getCapabilitiesAction() {
  278. $wfsServerUrl = $this->getBaseUri();
  279. $serviceTitle = "Web Feature Service";
  280. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  281. //header('Content-type: application/xml; charset="utf-8"');
  282. header('Content-type: application/xml');
  283. $this->_getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription);
  284. }
  285. }