WfsQgisServer.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. Lib::loadClass('Api_WfsServerBase');
  3. Lib::loadClass('Api_WfsException');
  4. Lib::loadClass('Api_WfsGeomTypeConverter');
  5. Lib::loadClass('DBG');
  6. Lib::loadClass('Api_Wfs_GetCapabilities');
  7. Lib::loadClass('Api_Wfs_GetFeature');
  8. Lib::loadClass('Core_AclBase');
  9. class Api_WfsQgisServer extends Api_WfsServerBase {
  10. public function parseXMLRequest() {
  11. $data = array();
  12. $reqContent = Request::getRequestBody();
  13. if (empty($reqContent)) {
  14. throw new Exception("Empty request");
  15. }
  16. $parserXml = xml_parser_create();
  17. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  18. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  19. if (0 == xml_parse_into_struct($parserXml, $reqContent, $tags)) {
  20. throw new Exception("Error parsing xml");
  21. }
  22. xml_parser_free($parserXml);
  23. if (empty($tags)) {
  24. throw new Exception("Empty structure from request");
  25. }
  26. $rootTagName = V::get('tag', '', $tags[0]);
  27. if ('Transaction' == $rootTagName) {
  28. return $this->_parseTransactionXmlStruct($reqContent, $tags);
  29. }
  30. throw new Api_WfsException("TODO ... L." . __LINE__, 501);
  31. $xml = new SimpleXMLElement($reqContent);
  32. $namespaces = $xml->getNameSpaces(true);
  33. if ('Transaction' == $xml->getName()) {
  34. $this->_parseTransactionXml($xml);
  35. }
  36. else {
  37. throw new Api_WfsException("Not Implemented " . htmlspecialchars($xml->getName()), 501);
  38. }
  39. }
  40. public function getFeatureAction() {
  41. $args = Api_Wfs_GetFeature::parseGetFeatureArgsFromRequest();
  42. // TODO: if ('hits' == $args['resultType']) ...
  43. return $this->getFeatures($args);
  44. }
  45. public function getFeatures($args) {
  46. $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
  47. $type = $args['typeName'];
  48. $acl = $this->getAclFromTypeName($args['xsd:type']);
  49. $fldList = $this->_getFieldListFromAcl($acl);
  50. $baseNsUri = Api_WfsNs::getBaseWfsUri();
  51. $wfsNs = 'p5_default_db';//$args['typePrefix'];
  52. $wfsNsUri = "{$baseNsUri}/" . substr($args['typePrefix'], 3);
  53. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&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 $idZasob => $fldName) {
  60. if ($acl->isGeomField($fldName)) {
  61. $geomFld = $fldName;
  62. }
  63. }
  64. }
  65. $dbGeomType = $acl->getGeomFieldType($geomFld);
  66. $searchParams = array();
  67. $searchParams['limit'] = $args['limit'];
  68. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  69. $searchParams['order_dir'] = 'DESC';
  70. if (!empty($args['bbox'])) $searchParams['f_the_geom'] = "BBOX:{$args['bbox']}";
  71. if (!empty($args['primaryKey'])) $searchParams['primaryKey'] = $args['primaryKey'];
  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 'args:';print_r($args);echo "\n";}
  93. if($DBG){echo 'getItems(params) \$params:';print_r($searchParams);echo "\n";}
  94. $this->DBG("getItems:" . json_encode($searchParams), __LINE__, __FUNCTION__, __CLASS__);
  95. $queryFeatures = $acl->buildQuery($searchParams);
  96. $items = $queryFeatures->getItems();
  97. $this->DBG("items(" . count($items) . ")", __LINE__, __FUNCTION__, __CLASS__);
  98. header('Content-type: application/xml; charset=utf-8');
  99. $xmlWriter = new XMLWriter();
  100. $xmlWriter->openUri('php://output');
  101. // $xmlWriter->openMemory();// DBG
  102. $xmlWriter->setIndent(true);
  103. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  104. $xmlWriter->startDocument('1.0','UTF-8');
  105. //$xmlWriter->startElementNS('wfs', 'FeatureCollection', 'http://www.opengis.net/wfs');
  106. $xmlWriter->startElement('wfs:FeatureCollection');
  107. // $xmlWriter->writeAttributeNS('xmlns', 'wfs', 'http://www.w3.org/2000/xmlns/', 'http://www.opengis.net/wfs');
  108. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs');
  109. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs');
  110. // $xmlWriter->writeAttributeNS('xmlns', 'gml', 'http://www.w3.org/2000/xmlns/', 'http://www.opengis.net/gml');
  111. // $xmlWriter->writeAttributeNS('xmlns', 'xsi', 'http://www.w3.org/2000/xmlns/', 'http://www.w3.org/2001/XMLSchema-instance');
  112. // $xmlWriter->writeAttributeNS('xmlns', $wfsNs, 'http://www.w3.org/2000/xmlns/', $wfsNsUri);
  113. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  114. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  115. $xmlWriter->writeAttribute("xmlns:{$wfsNs}", $wfsNsUri);
  116. $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  117. $xmlWriter->writeAttribute('numberMatched', 'unknown'); // TODO: return total items if simple query (without prefix, small total number, maxFeatures set, etc.)
  118. // NOTE: for client: if numberMatched == 'unknown' then request with resultType = 'hits'
  119. $xmlWriter->writeAttribute('numberReturned', count($items));
  120. if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
  121. $dbgLoop = 0;
  122. $this->DBG("before loop...", __LINE__, __FUNCTION__, __CLASS__);
  123. $primaryKeyField = $acl->getPrimaryKeyField();
  124. foreach ($items as $item) {
  125. $itemKey = V::get($primaryKeyField, '', $item);
  126. $item = (array)$item;
  127. if (0 == (++$dbgLoop) % 500) $this->DBG("items loop:{$dbgLoop}", __LINE__, __FUNCTION__, __CLASS__);
  128. if($DBG){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item[$geomFld]).'):' . var_export($item[$geomFld], true) . "\n";}
  129. if ($geomFld) {
  130. if (empty($item[$geomFld])) {
  131. continue;// QGIS crash when WFS contain features with empty geom field
  132. }
  133. }
  134. $xmlWriter->startElement('gml:featureMember');
  135. $xmlWriter->startElement("{$wfsNs}:{$type}");
  136. $xmlWriter->writeAttribute('fid', "{$type}.{$itemKey}");
  137. foreach ($fldList as $idZasob => $fldName) {
  138. // if (!$acl->isAllowed($idZasob, 'R', $item)) {echo '<!-- '."[{$idZasob}]({$fldName}) not allowed to read: ";print_r($item);echo' -->';}
  139. if (!$acl->canReadObjectField($fldName, (object)$item)) continue;
  140. // if ($acl->isGeomField($fldName)) // BUG: wolno
  141. if ($geomFld != null && $fldName == $geomFld) {
  142. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  143. $this->_typeConverter->createGmlFromWkt_xmlWriter($item[$fldName], $xmlWriter);
  144. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  145. } else if (is_array($item[$fldName])) {// TODO: by struct - REF field
  146. // if($DBG_DS){echo">>> TODO({$fldName}) REF item[{$itemKey}][{$fldName}]: ";print_r($item[$fldName]);echo "\n";}
  147. } else {
  148. $value = str_replace('&', '&amp;', $item[$fldName]);
  149. if (empty($value) && '0' !== $value) {
  150. continue;
  151. } else {
  152. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  153. $xmlWriter->text($value);
  154. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  155. }
  156. }
  157. }
  158. $xmlWriter->endElement();// {$wfsNs}:{$type}
  159. $xmlWriter->endElement();// gml:featureMember
  160. }
  161. $xmlWriter->endElement();// wfs:FeatureCollection
  162. $xmlWriter->endDocument();
  163. $this->DBG("items loop END", __LINE__, __FUNCTION__, __CLASS__);
  164. exit;
  165. }
  166. public function describeFeatureTypeAction() {
  167. $type = V::get('TYPENAME', '', $_REQUEST);
  168. if (empty($type)) {
  169. $reqContent = Request::getRequestBody();
  170. if (!empty($reqContent)) {
  171. return $this->_parseDescribeFeatureTypeRequest($reqContent);
  172. } else {
  173. return $this->_getDescribeFeatureAllTypes();
  174. }
  175. //throw new HttpException("Wrong param TYPENAME", 400);
  176. }
  177. $typeEx = explode(':', $type);
  178. if (count($typeEx) != 2) {
  179. throw new HttpException("Wrong param TYPENAME", 400);
  180. }
  181. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
  182. }
  183. public function getCapabilitiesAction() {
  184. $wfsServerUrl = $this->getBaseUri();
  185. $serviceTitle = "Web Feature Service for QGIS";
  186. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  187. $idDefaultDB = DB::getPDO()->getZasobId();
  188. $aclList = array_filter($this->_usrAcl->getTablesAcl(), function ($acl) use ($idDefaultDB) {
  189. // if ($idDefaultDB != $tblAcl->getDB()) {// hide non default_db tables
  190. // return false;
  191. // }
  192. // $dataSourceName = 'default_db';// TODO: getSourceName
  193. // $tblName = $tblAcl->getName();
  194. // try {
  195. // $acl = $this->getAclFromTypeName($typeName = "p5_" . $tblAcl->getSourceName() . ":" . $tblAcl->getName());
  196. // } catch (Exception $e) {
  197. // // TODO: error log $e->getMessage();
  198. // }
  199. // if (!$acl) {
  200. // // TODO: error log msg
  201. // continue;
  202. // }
  203. // $fldList = $acl->getRealFieldList();
  204. // if (!in_array('the_geom', $fldList)) {
  205. // continue;
  206. // }
  207. return ($acl instanceof Core_AclBase && $idDefaultDB == $acl->getDB()); // hide non default_db tables
  208. });
  209. switch (V::get('outputFormat', 'xml', $_GET)) {
  210. case 'csv': {
  211. (new Api_Wfs_GetCapabilities)->getCapabilitiesCsv($wfsServerUrl, $serviceTitle, $serviceDescription, $aclList);
  212. } break;
  213. case 'xml': {
  214. (new Api_Wfs_GetCapabilities)->getCapabilitiesXml($wfsServerUrl, $serviceTitle, $serviceDescription, $aclList);
  215. } break;
  216. default: throw new Api_WfsException("Not Implemented outputFormat", 501); // , null, 'NotImplemented', 'request');
  217. }
  218. exit;
  219. }
  220. public function _getTableAclList() {// Use only Tables from default_db with the_geom field
  221. $tblAclList = array();
  222. $idDefaultDB = DB::getPDO()->getZasobId();
  223. $fullTblAclList = $this->_usrAcl->getTablesAcl();
  224. foreach ($fullTblAclList as $tblAcl) {
  225. if ($idDefaultDB != $tblAcl->getDB()) {// hide non default_db tables
  226. continue;
  227. }
  228. // try {
  229. // $acl = $this->getAclFromTypeName($typeName = "p5_" . $tblAcl->getSourceName() . ":" . $tblAcl->getName());
  230. // } catch (Exception $e) {
  231. // // TODO: error log $e->getMessage();
  232. // }
  233. // if (!$acl) {
  234. // // TODO: error log msg
  235. // continue;
  236. // }
  237. // $fldList = $acl->getRealFieldList();
  238. // if (!in_array('the_geom', $fldList)) {
  239. // continue;
  240. // }
  241. $tblAclList[] = $tblAcl;
  242. }
  243. return $tblAclList;
  244. }
  245. public function _getFieldListFromAcl($acl) {
  246. $fldList = $acl->getRealFieldListByIdZasob();
  247. // mv the_geom to the first place
  248. $orderedFldList = array();
  249. foreach ($fldList as $idZasob => $fldName) {
  250. if ('the_geom' == $fldName) $orderedFldList[$idZasob] = $fldName;
  251. }
  252. foreach ($fldList as $idZasob => $fldName) {
  253. if ('the_geom' == $fldName) continue;
  254. $orderedFldList[$idZasob] = $fldName;
  255. }
  256. return $orderedFldList;
  257. }
  258. }