WfsQgisServer.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. Lib::loadClass('Api_WfsServerBase');
  3. Lib::loadClass('Api_WfsException');
  4. Lib::loadClass('Api_WfsGeomTypeConverter');
  5. class Api_WfsQgisServer extends Api_WfsServerBase {
  6. public function parseXMLRequest() {
  7. $data = array();
  8. $reqContent = Request::getRequestBody();
  9. if (empty($reqContent)) {
  10. throw new Exception("Empty request");
  11. }
  12. $parserXml = xml_parser_create();
  13. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  14. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  15. if (0 == xml_parse_into_struct($parserXml, $reqContent, $tags)) {
  16. throw new Exception("Error parsing xml");
  17. }
  18. xml_parser_free($parserXml);
  19. if (empty($tags)) {
  20. throw new Exception("Empty structure from request");
  21. }
  22. $rootTagName = V::get('tag', '', $tags[0]);
  23. if ('Transaction' == $rootTagName) {
  24. return $this->_parseTransactionXmlStruct($reqContent, $tags);
  25. }
  26. throw new Api_WfsException("TODO ... L." . __LINE__, 501);
  27. $xml = new SimpleXMLElement($reqContent);
  28. $namespaces = $xml->getNameSpaces(true);
  29. if ('Transaction' == $xml->getName()) {
  30. $this->_parseTransactionXml($xml);
  31. }
  32. else {
  33. throw new Api_WfsException("Not Implemented " . htmlspecialchars($xml->getName()), 501);
  34. }
  35. }
  36. public function getFeatureAction() {
  37. $type = V::get('TYPENAME', '', $_REQUEST);
  38. $typeEx = explode(':', $type);
  39. $maxFeatures = V::get('MAXFEATURES', '10000', $_REQUEST, 'int');// TODO: Set Deafult Limit
  40. $srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
  41. if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  42. return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname);
  43. }
  44. public function getFeatures($nsPrefix, $type, $maxFeatures, $srsname) {
  45. $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
  46. $typeName = "{$nsPrefix}:{$type}";
  47. $acl = $this->getAclFromTypeName($typeName);
  48. $fldList = $this->_getFieldListFromAcl($acl);
  49. $baseNsUri = $this->getBaseNamespaceUri();
  50. //$wfsNs = 'p5_default_db_' . $type;//$nsPrefix;
  51. $wfsNs = 'p5_default_db';//$nsPrefix;
  52. $wfsNsUri = "{$baseNsUri}/" . substr($nsPrefix, 3);
  53. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$typeName}&REQUEST=DescribeFeatureType";
  54. // https://biuro.biall-net.pl/dev-pl/se-master/wfs-qgis.php/default_db/
  55. // https://biuro.biall-net.pl/dev-pl/se-master/wfs-data.php/default_db/TEST_PERMS/?SERVICE=WFS&VERSION=1.0.0&TYPENAME=p5_default_db:TEST_PERMS&REQUEST=DescribeFeatureType
  56. // get BBox from geom_field (only one geom fld is allowed)
  57. $geomFld = null;
  58. {
  59. foreach ($fldList as $fldName) {
  60. if ($acl->isGeomField($fldName)) {
  61. $geomFld = $fldName;
  62. }
  63. }
  64. }
  65. $dbGeomType = $acl->getGeomFieldType($geomFld);
  66. $searchParams = array();
  67. $searchParams['limit'] = $maxFeatures;
  68. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  69. $searchParams['order_dir'] = 'DESC';
  70. $bbox = V::get('BBOX', '', $_GET);
  71. // TODO: if (!empty($bbox)) $searchParams['f_the_geom'] = "BBOX:{$bbox}";
  72. //if ($geomFld) $searchParams["f_{$geomFld}"] = 'IS NOT NULL';
  73. //if ($geomFld) $searchParams["f_{$geomFld}"] = 'GeometryType=' . strtoupper($dbGeomType);
  74. $geomType = strtoupper($dbGeomType);
  75. if ($geomFld) $searchParams["ogc:Filter"] = <<<OGC_FILTER
  76. <ogc:Filter>
  77. <ogc:And>
  78. <ogc:Not>
  79. <ogc:PropertyIsNull>
  80. <ogc:PropertyName>{$geomFld}</ogc:PropertyName>
  81. </ogc:PropertyIsNull>
  82. </ogc:Not>
  83. <ogc:PropertyIsEqualTo>
  84. <ogc:Function name="GeometryType">
  85. <ogc:PropertyName>{$geomFld}</ogc:PropertyName>
  86. </ogc:Function>
  87. <ogc:Literal>{$geomType}</ogc:Literal>
  88. </ogc:PropertyIsEqualTo>
  89. </ogc:And>
  90. </ogc:Filter>
  91. OGC_FILTER;
  92. if($DBG){echo 'getItems:';print_r($searchParams);echo "\n";}
  93. $this->DBG("getItems:" . json_encode($searchParams), __LINE__, __FUNCTION__, __CLASS__);
  94. $items = $acl->getItems($searchParams);
  95. $this->DBG("items(" . count($items) . ")", __LINE__, __FUNCTION__, __CLASS__);
  96. if (true) {//1 == V::get('XML_WRITER', 0, $_GET, 'int')) {
  97. header('Content-type: application/xml; charset=utf-8');
  98. $xmlWriter = new XMLWriter();
  99. $xmlWriter->openUri('php://output');
  100. // $xmlWriter->openMemory();
  101. $xmlWriter->setIndent(true);
  102. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  103. $xmlWriter->startDocument('1.0','UTF-8');
  104. //$xmlWriter->startElementNS('wfs', 'FeatureCollection', 'http://www.opengis.net/wfs');
  105. $xmlWriter->startElement('wfs:FeatureCollection');
  106. // $xmlWriter->writeAttributeNS('xmlns', 'wfs', 'http://www.w3.org/2000/xmlns/', 'http://www.opengis.net/wfs');
  107. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs');
  108. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs');
  109. // $xmlWriter->writeAttributeNS('xmlns', 'gml', 'http://www.w3.org/2000/xmlns/', 'http://www.opengis.net/gml');
  110. // $xmlWriter->writeAttributeNS('xmlns', 'xsi', 'http://www.w3.org/2000/xmlns/', 'http://www.w3.org/2001/XMLSchema-instance');
  111. // $xmlWriter->writeAttributeNS('xmlns', $wfsNs, 'http://www.w3.org/2000/xmlns/', $wfsNsUri);
  112. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  113. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  114. $xmlWriter->writeAttribute("xmlns:{$wfsNs}", $wfsNsUri);
  115. $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  116. if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
  117. if (empty($items)) {
  118. $pKeyField = $acl->getPrimaryKeyField();
  119. $fakeItem = new stdClass();
  120. $fakeItem->{$pKeyField} = 0;
  121. if ('polygon' == $dbGeomType) {
  122. $fakeItem->the_geom = "POLYGON(())";
  123. } else if ('linestring' == $dbGeomType) {
  124. $fakeItem->the_geom = "LINESTRING()";
  125. } else if ('point' == $dbGeomType) {
  126. $fakeItem->the_geom = "POINT(0,0)";
  127. }
  128. $items[0] = $fakeItem;
  129. }
  130. $dbgLoop = 0;
  131. $this->DBG("before loop...", __LINE__, __FUNCTION__, __CLASS__);
  132. foreach ($items as $itemKey => $item) {
  133. if (0 == (++$dbgLoop) % 500) $this->DBG("items loop:{$dbgLoop}", __LINE__, __FUNCTION__, __CLASS__);
  134. if($DBG){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item->{$geomFld}).'):';print_r($item->{$geomFld});echo "\n";}
  135. if ($geomFld) {
  136. if (empty($item->{$geomFld})) {
  137. continue;// QGIS crash when WFS contain features with empty geom field
  138. }
  139. }
  140. $xmlWriter->startElement('gml:featureMember');
  141. $xmlWriter->startElement("{$wfsNs}:{$type}");
  142. $xmlWriter->writeAttribute('fid', "{$type}.{$itemKey}");
  143. foreach ($fldList as $fldName) {
  144. // if ($acl->isGeomField($fldName)) {// BUG: wolno
  145. // if($fldName == 'the_geom'){// OK szybko
  146. if ($geomFld != null && $fldName == $geomFld){
  147. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  148. $this->_typeConverter->createGmlFromWkt_xmlWriter($item->{$fldName}, $xmlWriter);
  149. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  150. } else {
  151. $value = str_replace('&', '&amp;', $item->{$fldName});
  152. if (empty($value) && '0' !== $value) {
  153. continue;
  154. } else {
  155. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  156. $xmlWriter->text($value);
  157. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  158. }
  159. }
  160. }
  161. $xmlWriter->endElement();// {$wfsNs}:{$type}
  162. $xmlWriter->endElement();// gml:featureMember
  163. }
  164. $xmlWriter->endElement();// wfs:FeatureCollection
  165. $xmlWriter->endDocument();
  166. $this->DBG("items loop END", __LINE__, __FUNCTION__, __CLASS__);
  167. //'<!-- .EOF -->';
  168. exit;
  169. }
  170. $dom = new DOMDocument('1.0', 'utf-8');
  171. $dom->formatOutput = true;
  172. $dom->preserveWhiteSpace = false;
  173. $rootNode = $dom->createElementNS('http://www.opengis.net/wfs', 'wfs:FeatureCollection');
  174. $dom->appendChild($rootNode);
  175. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.opengis.net/wfs');
  176. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:wfs', 'http://www.opengis.net/wfs');
  177. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  178. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  179. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  180. $rootNode->setAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  181. if(0){// TODO: get BBOX for add features
  182. $boundedByNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:boundedBy');
  183. $rootNode->appendChild($boundedByNode);
  184. $boxNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:Box');
  185. $boundedByNode->appendChild($boxNode);
  186. $boxNode->setAttribute('srsName', "http://www.opengis.net/gml/srs/epsg.xml#4326");// TODO: EPSG
  187. $coordinatesNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:coordinates');
  188. $boxNode->appendChild($coordinatesNode);
  189. $coordinatesNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  190. $coordinatesNode->setAttribute('decimal', '.');
  191. $coordinatesNode->setAttribute('cs', ',');
  192. $coordinatesNode->setAttribute('ts', ' ');
  193. $coordinatesNode->nodeValue = '1544947.6295,4322758.105 1548002.2259,4330464.1001';// TODO: coordinates for all items?
  194. }
  195. if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
  196. if (empty($items)) {
  197. $pKeyField = $acl->getPrimaryKeyField();
  198. $fakeItem = new stdClass();
  199. $fakeItem->{$pKeyField} = 0;
  200. if ('polygon' == $dbGeomType) {
  201. $fakeItem->the_geom = "POLYGON(())";
  202. } else if ('linestring' == $dbGeomType) {
  203. $fakeItem->the_geom = "LINESTRING()";
  204. } else if ('point' == $dbGeomType) {
  205. $fakeItem->the_geom = "POINT(0,0)";
  206. }
  207. $items[0] = $fakeItem;
  208. }
  209. $dbgLoop = 0;
  210. foreach ($items as $itemKey => $item) {
  211. $dbgLoop++; if (0 == $dbgLoop % 100) $this->DBG("items loop:{$dbgLoop}", __LINE__, __FUNCTION__, __CLASS__);
  212. if($DBG){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item->{$geomFld}).'):';print_r($item->{$geomFld});echo "\n";}
  213. if ($geomFld) {
  214. if (empty($item->{$geomFld})) {
  215. continue;// QGIS crash when WFS contain features with empty geom field
  216. }
  217. }
  218. $featureMemberNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:featureMember');
  219. $rootNode->appendChild($featureMemberNode);
  220. $featureNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$type}");
  221. $featureMemberNode->appendChild($featureNode);
  222. $featureNode->setAttribute('fid', "{$type}.{$itemKey}");
  223. if(0){// TODO: get BBOX
  224. $boundedByNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:boundedBy');
  225. $featureNode->appendChild($boundedByNode);
  226. $boxNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:Box');
  227. $boundedByNode->appendChild($boxNode);
  228. $boxNode->setAttribute('srsName', "http://www.opengis.net/gml/srs/epsg.xml#4326");// TODO: EPSG
  229. $coordinatesNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:coordinates');
  230. $boxNode->appendChild($coordinatesNode);
  231. $coordinatesNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  232. $coordinatesNode->setAttribute('decimal', '.');
  233. $coordinatesNode->setAttribute('cs', ',');
  234. $coordinatesNode->setAttribute('ts', ' ');
  235. $coordinatesNode->nodeValue = '1546472.2363,4328949.5775 1548002.2259,4330464.1001';// TODO: coordinates for item?
  236. }
  237. foreach ($fldList as $fldName) {
  238. $featureFldNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$fldName}");
  239. if ($acl->isGeomField($fldName)) {
  240. $geomNode = $this->_typeConverter->createGmlFromWkt($item->{$fldName}, $dom);
  241. if (!$geomNode) continue;
  242. $featureFldNode->appendChild($geomNode);
  243. } else {
  244. $featureFldNode->nodeValue = str_replace('&', '&amp;', $item->{$fldName});
  245. if (empty($featureFldNode->nodeValue) && '0' !== $featureFldNode->nodeValue) {
  246. continue;
  247. }
  248. }
  249. $featureNode->appendChild($featureFldNode);
  250. }
  251. }
  252. $this->DBG("items loop END", __LINE__, __FUNCTION__, __CLASS__);
  253. return $dom->saveXml();
  254. }
  255. public function describeFeatureTypeAction() {
  256. $type = V::get('TYPENAME', '', $_REQUEST);
  257. if (empty($type)) {
  258. $reqContent = Request::getRequestBody();
  259. if (!empty($reqContent)) {
  260. return $this->_parseDescribeFeatureTypeRequest($reqContent);
  261. } else {
  262. return $this->_getDescribeFeatureAllTypes();
  263. }
  264. //throw new HttpException("Wrong param TYPENAME", 400);
  265. }
  266. $typeEx = explode(':', $type);
  267. if (count($typeEx) != 2) {
  268. throw new HttpException("Wrong param TYPENAME", 400);
  269. }
  270. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
  271. }
  272. public function getCapabilitiesAction() {
  273. $wfsServerUrl = $this->getBaseUri();
  274. $serviceTitle = "Web Feature Service for QGIS";
  275. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  276. //header('Content-type: application/xml; charset="utf-8"');
  277. header('Content-type: application/xml');
  278. $this->_getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription);
  279. exit;
  280. }
  281. public function _getTableAclList() {// Use only Tables from default_db with the_geom field
  282. $tblAclList = array();
  283. $db = DB::getDB();
  284. $idDefaultDB = $db->_zasob_id;
  285. $fullTblAclList = $this->_usrAcl->getTablesAcl();
  286. foreach ($fullTblAclList as $tblAcl) {
  287. $dataSourceName = 'default_db';// TODO: getSourceName
  288. $tblName = $tblAcl->getName();
  289. if ($idDefaultDB != $tblAcl->getDB()) {// hide non default_db tables
  290. continue;
  291. }
  292. try {
  293. $acl = $this->getAclFromTypeName($typeName = "p5_{$dataSourceName}:{$tblName}");
  294. } catch (Exception $e) {
  295. // TODO: error log $e->getMessage();
  296. }
  297. if (!$acl) {
  298. // TODO: error log msg
  299. continue;
  300. }
  301. $fldList = $acl->getRealFieldList();
  302. if (!in_array('the_geom', $fldList)) {
  303. continue;
  304. }
  305. $tblAclList[] = $tblAcl;
  306. }
  307. return $tblAclList;
  308. }
  309. public function _getFieldListFromAcl($acl) {
  310. $fldList = $acl->getRealFieldList();
  311. {// mv the_geom to the first place
  312. array_unshift($fldList, 'the_geom');
  313. $fldList = array_unique($fldList);
  314. }
  315. return $fldList;
  316. }
  317. }