WfsQgisServer.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. $args = $this->parseGetFeatureArgsFromRequest();
  38. // TODO: if ('hits' == $args['resultType']) ...
  39. return $this->getFeatures($args);
  40. }
  41. public function getFeatures($args) {
  42. $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
  43. $type = $args['typeName'];
  44. $acl = $this->getAclFromTypeName($args['xsd:type']);
  45. $fldList = $this->_getFieldListFromAcl($acl);
  46. $baseNsUri = Api_WfsNs::getBaseWfsUri();
  47. $wfsNs = 'p5_default_db';//$args['typePrefix'];
  48. $wfsNsUri = "{$baseNsUri}/" . substr($args['typePrefix'], 3);
  49. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&REQUEST=DescribeFeatureType";
  50. // https://biuro.biall-net.pl/dev-pl/se-master/wfs-qgis.php/default_db/
  51. // 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
  52. // get BBox from geom_field (only one geom fld is allowed)
  53. $geomFld = null;
  54. {
  55. foreach ($fldList as $idZasob => $fldName) {
  56. if ($acl->isGeomField($fldName)) {
  57. $geomFld = $fldName;
  58. }
  59. }
  60. }
  61. $dbGeomType = $acl->getGeomFieldType($geomFld);
  62. $searchParams = array();
  63. $searchParams['limit'] = $args['limit'];
  64. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  65. $searchParams['order_dir'] = 'DESC';
  66. if (!empty($args['bbox'])) $searchParams['f_the_geom'] = "BBOX:{$args['bbox']}";
  67. if (!empty($args['primaryKey'])) $searchParams['primaryKey'] = $args['primaryKey'];
  68. //if ($geomFld) $searchParams["f_{$geomFld}"] = 'IS NOT NULL';
  69. //if ($geomFld) $searchParams["f_{$geomFld}"] = 'GeometryType=' . strtoupper($dbGeomType);
  70. $geomType = strtoupper($dbGeomType);
  71. if ($geomFld) $searchParams["ogc:Filter"] = <<<OGC_FILTER
  72. <ogc:Filter>
  73. <ogc:And>
  74. <ogc:Not>
  75. <ogc:PropertyIsNull>
  76. <ogc:PropertyName>{$geomFld}</ogc:PropertyName>
  77. </ogc:PropertyIsNull>
  78. </ogc:Not>
  79. <ogc:PropertyIsEqualTo>
  80. <ogc:Function name="GeometryType">
  81. <ogc:PropertyName>{$geomFld}</ogc:PropertyName>
  82. </ogc:Function>
  83. <ogc:Literal>{$geomType}</ogc:Literal>
  84. </ogc:PropertyIsEqualTo>
  85. </ogc:And>
  86. </ogc:Filter>
  87. OGC_FILTER;
  88. if($DBG){echo 'args:';print_r($args);echo "\n";}
  89. if($DBG){echo 'getItems(params) \$params:';print_r($searchParams);echo "\n";}
  90. $this->DBG("getItems:" . json_encode($searchParams), __LINE__, __FUNCTION__, __CLASS__);
  91. $items = $acl->getItems($searchParams);
  92. $this->DBG("items(" . count($items) . ")", __LINE__, __FUNCTION__, __CLASS__);
  93. header('Content-type: application/xml; charset=utf-8');
  94. $xmlWriter = new XMLWriter();
  95. $xmlWriter->openUri('php://output');
  96. // $xmlWriter->openMemory();// DBG
  97. $xmlWriter->setIndent(true);
  98. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  99. $xmlWriter->startDocument('1.0','UTF-8');
  100. //$xmlWriter->startElementNS('wfs', 'FeatureCollection', 'http://www.opengis.net/wfs');
  101. $xmlWriter->startElement('wfs:FeatureCollection');
  102. // $xmlWriter->writeAttributeNS('xmlns', 'wfs', 'http://www.w3.org/2000/xmlns/', 'http://www.opengis.net/wfs');
  103. $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs');
  104. $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs');
  105. // $xmlWriter->writeAttributeNS('xmlns', 'gml', 'http://www.w3.org/2000/xmlns/', 'http://www.opengis.net/gml');
  106. // $xmlWriter->writeAttributeNS('xmlns', 'xsi', 'http://www.w3.org/2000/xmlns/', 'http://www.w3.org/2001/XMLSchema-instance');
  107. // $xmlWriter->writeAttributeNS('xmlns', $wfsNs, 'http://www.w3.org/2000/xmlns/', $wfsNsUri);
  108. $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
  109. $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  110. $xmlWriter->writeAttribute("xmlns:{$wfsNs}", $wfsNsUri);
  111. $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  112. if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
  113. $dbgLoop = 0;
  114. $this->DBG("before loop...", __LINE__, __FUNCTION__, __CLASS__);
  115. foreach ($items as $itemKey => $item) {
  116. $item = (array)$item;
  117. if (0 == (++$dbgLoop) % 500) $this->DBG("items loop:{$dbgLoop}", __LINE__, __FUNCTION__, __CLASS__);
  118. if($DBG){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item[$geomFld]).'):' . var_export($item[$geomFld], true) . "\n";}
  119. if ($geomFld) {
  120. if (empty($item[$geomFld])) {
  121. continue;// QGIS crash when WFS contain features with empty geom field
  122. }
  123. }
  124. $xmlWriter->startElement('gml:featureMember');
  125. $xmlWriter->startElement("{$wfsNs}:{$type}");
  126. $xmlWriter->writeAttribute('fid', "{$type}.{$itemKey}");
  127. foreach ($fldList as $idZasob => $fldName) {
  128. // if (!$acl->isAllowed($idZasob, 'R', $item)) {echo '<!-- '."[{$idZasob}]({$fldName}) not allowed to read: ";print_r($item);echo' -->';}
  129. if (!$acl->canReadObjectField($fldName, (object)$item)) continue;
  130. // if ($acl->isGeomField($fldName)) // BUG: wolno
  131. if ($geomFld != null && $fldName == $geomFld) {
  132. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  133. $this->_typeConverter->createGmlFromWkt_xmlWriter($item[$fldName], $xmlWriter);
  134. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  135. } else if (is_array($item[$fldName])) {// TODO: by struct - REF field
  136. // if($DBG_DS){echo">>> TODO({$fldName}) REF item[{$itemKey}][{$fldName}]: ";print_r($item[$fldName]);echo "\n";}
  137. } else {
  138. $value = str_replace('&', '&amp;', $item[$fldName]);
  139. if (empty($value) && '0' !== $value) {
  140. continue;
  141. } else {
  142. $xmlWriter->startElement("{$wfsNs}:{$fldName}");
  143. $xmlWriter->text($value);
  144. $xmlWriter->endElement();// {$wfsNs}:{$fldName}
  145. }
  146. }
  147. }
  148. $xmlWriter->endElement();// {$wfsNs}:{$type}
  149. $xmlWriter->endElement();// gml:featureMember
  150. }
  151. $xmlWriter->endElement();// wfs:FeatureCollection
  152. $xmlWriter->endDocument();
  153. $this->DBG("items loop END", __LINE__, __FUNCTION__, __CLASS__);
  154. exit;
  155. }
  156. public function describeFeatureTypeAction() {
  157. $type = V::get('TYPENAME', '', $_REQUEST);
  158. if (empty($type)) {
  159. $reqContent = Request::getRequestBody();
  160. if (!empty($reqContent)) {
  161. return $this->_parseDescribeFeatureTypeRequest($reqContent);
  162. } else {
  163. return $this->_getDescribeFeatureAllTypes();
  164. }
  165. //throw new HttpException("Wrong param TYPENAME", 400);
  166. }
  167. $typeEx = explode(':', $type);
  168. if (count($typeEx) != 2) {
  169. throw new HttpException("Wrong param TYPENAME", 400);
  170. }
  171. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
  172. }
  173. public function getCapabilitiesAction() {
  174. $wfsServerUrl = $this->getBaseUri();
  175. $serviceTitle = "Web Feature Service for QGIS";
  176. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  177. //header('Content-type: application/xml; charset="utf-8"');
  178. header('Content-type: application/xml');
  179. $this->_getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription);
  180. exit;
  181. }
  182. public function _getTableAclList() {// Use only Tables from default_db with the_geom field
  183. $tblAclList = array();
  184. $db = DB::getDB();
  185. $idDefaultDB = $db->_zasob_id;
  186. $fullTblAclList = $this->_usrAcl->getTablesAcl();
  187. foreach ($fullTblAclList as $tblAcl) {
  188. $dataSourceName = 'default_db';// TODO: getSourceName
  189. $tblName = $tblAcl->getName();
  190. if ($idDefaultDB != $tblAcl->getDB()) {// hide non default_db tables
  191. continue;
  192. }
  193. try {
  194. $acl = $this->getAclFromTypeName($typeName = "p5_{$dataSourceName}:{$tblName}");
  195. } catch (Exception $e) {
  196. // TODO: error log $e->getMessage();
  197. }
  198. if (!$acl) {
  199. // TODO: error log msg
  200. continue;
  201. }
  202. $fldList = $acl->getRealFieldList();
  203. if (!in_array('the_geom', $fldList)) {
  204. continue;
  205. }
  206. $tblAclList[] = $tblAcl;
  207. }
  208. return $tblAclList;
  209. }
  210. public function _getFieldListFromAcl($acl) {
  211. $fldList = $acl->getRealFieldListByIdZasob();
  212. // mv the_geom to the first place
  213. $orderedFldList = array();
  214. foreach ($fldList as $idZasob => $fldName) {
  215. if ('the_geom' == $fldName) $orderedFldList[$idZasob] = $fldName;
  216. }
  217. foreach ($fldList as $idZasob => $fldName) {
  218. if ('the_geom' == $fldName) continue;
  219. $orderedFldList[$idZasob] = $fldName;
  220. }
  221. return $orderedFldList;
  222. }
  223. }