WfsQgisServer.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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) {
  42. return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname);
  43. } else {
  44. throw new HttpException("Wrong param TYPENAME", 400);
  45. }
  46. }
  47. public function getFeatures($nsPrefix, $type, $maxFeatures, $srsname) {
  48. $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
  49. $typeName = "{$nsPrefix}:{$type}";
  50. $acl = $this->getAclFromTypeName($typeName);
  51. $fldList = $this->_getFieldListFromAcl($acl);
  52. $baseNsUri = $this->getBaseNamespaceUri();
  53. //$wfsNs = 'p5_default_db_' . $type;//$nsPrefix;
  54. $wfsNs = 'p5_default_db';//$nsPrefix;
  55. $wfsNsUri = "{$baseNsUri}/" . substr($nsPrefix, 3);
  56. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$typeName}&REQUEST=DescribeFeatureType";
  57. // https://biuro.biall-net.pl/dev-pl/se-master/wfs-qgis.php/default_db/
  58. // 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
  59. // get BBox from geom_field (only one geom fld is allowed)
  60. $geomFld = null;
  61. {
  62. foreach ($fldList as $fldName) {
  63. if ($acl->isGeomField($fldName)) {
  64. $geomFld = $fldName;
  65. }
  66. }
  67. }
  68. $dbGeomType = $acl->getGeomFieldType($geomFld);
  69. $searchParams = array();
  70. $searchParams['limit'] = $maxFeatures;
  71. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  72. $searchParams['order_dir'] = 'DESC';
  73. if ($geomFld) $searchParams["f_{$geomFld}"] = 'IS NOT NULL';
  74. if ($geomFld) $searchParams["f_{$geomFld}"] = 'GeometryType=' . strtoupper($dbGeomType);
  75. //if ($geomFld) $searchParams["f_{$geomFld}"] = 'GeometryType=LINESTRING';
  76. if($DBG){echo 'getItems:';print_r($searchParams);echo "\n";}
  77. $items = $acl->getItems($searchParams);
  78. $dom = new DOMDocument('1.0', 'utf-8');
  79. $dom->formatOutput = true;
  80. $dom->preserveWhiteSpace = false;
  81. $rootNode = $dom->createElementNS('http://www.opengis.net/wfs', 'wfs:FeatureCollection');
  82. $dom->appendChild($rootNode);
  83. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.opengis.net/wfs');
  84. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:wfs', 'http://www.opengis.net/wfs');
  85. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  86. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  87. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  88. $rootNode->setAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  89. if(0){// TODO: get BBOX for add features
  90. $boundedByNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:boundedBy');
  91. $rootNode->appendChild($boundedByNode);
  92. $boxNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:Box');
  93. $boundedByNode->appendChild($boxNode);
  94. $boxNode->setAttribute('srsName', "http://www.opengis.net/gml/srs/epsg.xml#4326");// TODO: EPSG
  95. $coordinatesNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:coordinates');
  96. $boxNode->appendChild($coordinatesNode);
  97. $coordinatesNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  98. $coordinatesNode->setAttribute('decimal', '.');
  99. $coordinatesNode->setAttribute('cs', ',');
  100. $coordinatesNode->setAttribute('ts', ' ');
  101. $coordinatesNode->nodeValue = '1544947.6295,4322758.105 1548002.2259,4330464.1001';// TODO: coordinates for all items?
  102. }
  103. if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
  104. if (empty($items)) {
  105. $pKeyField = $acl->getPrimaryKeyField();
  106. $fakeItem = new stdClass();
  107. $fakeItem->{$pKeyField} = 0;
  108. if ('polygon' == $dbGeomType) {
  109. $fakeItem->the_geom = "POLYGON(())";
  110. } else if ('linestring' == $dbGeomType) {
  111. $fakeItem->the_geom = "LINESTRING()";
  112. } else if ('point' == $dbGeomType) {
  113. $fakeItem->the_geom = "POINT(0,0)";
  114. }
  115. $items[0] = $fakeItem;
  116. }
  117. foreach ($items as $itemKey => $item) {
  118. if($DBG){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item->{$geomFld}).'):';print_r($item->{$geomFld});echo "\n";}
  119. if ($geomFld) {
  120. if (empty($item->{$geomFld})) {
  121. continue;// QGIS crash when WFS contain features with empty geom field
  122. }
  123. }
  124. $featureMemberNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:featureMember');
  125. $rootNode->appendChild($featureMemberNode);
  126. $featureNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$type}");
  127. $featureMemberNode->appendChild($featureNode);
  128. $featureNode->setAttribute('fid', "{$type}.{$itemKey}");
  129. if(0){// TODO: get BBOX
  130. $boundedByNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:boundedBy');
  131. $featureNode->appendChild($boundedByNode);
  132. $boxNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:Box');
  133. $boundedByNode->appendChild($boxNode);
  134. $boxNode->setAttribute('srsName', "http://www.opengis.net/gml/srs/epsg.xml#4326");// TODO: EPSG
  135. $coordinatesNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:coordinates');
  136. $boxNode->appendChild($coordinatesNode);
  137. $coordinatesNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  138. $coordinatesNode->setAttribute('decimal', '.');
  139. $coordinatesNode->setAttribute('cs', ',');
  140. $coordinatesNode->setAttribute('ts', ' ');
  141. $coordinatesNode->nodeValue = '1546472.2363,4328949.5775 1548002.2259,4330464.1001';// TODO: coordinates for item?
  142. }
  143. foreach ($fldList as $fldName) {
  144. $featureFldNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$fldName}");
  145. if ($acl->isGeomField($fldName)) {
  146. $geomNode = $this->_typeConverter->createGmlFromWkt($item->{$fldName}, $dom);
  147. if (!$geomNode) continue;
  148. $featureFldNode->appendChild($geomNode);
  149. } else {
  150. $featureFldNode->nodeValue = str_replace('&', '&amp;', $item->{$fldName});
  151. if (empty($featureFldNode->nodeValue) && '0' !== $featureFldNode->nodeValue) {
  152. continue;
  153. }
  154. }
  155. $featureNode->appendChild($featureFldNode);
  156. }
  157. }
  158. return $dom->saveXml();
  159. }
  160. public function describeFeatureTypeAction() {
  161. $type = V::get('TYPENAME', '', $_REQUEST);
  162. if (empty($type)) {
  163. $reqContent = Request::getRequestBody();
  164. if (!empty($reqContent)) {
  165. return $this->_parseDescribeFeatureTypeRequest($reqContent);
  166. } else {
  167. return $this->_getDescribeFeatureAllTypes();
  168. }
  169. //throw new HttpException("Wrong param TYPENAME", 400);
  170. }
  171. $typeEx = explode(':', $type);
  172. if (count($typeEx) != 2) {
  173. throw new HttpException("Wrong param TYPENAME", 400);
  174. }
  175. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
  176. }
  177. public function getCapabilitiesAction() {
  178. $wfsServerUrl = $this->getBaseUri();
  179. $serviceTitle = "Web Feature Service for QGIS";
  180. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  181. //header('Content-type: application/xml; charset="utf-8"');
  182. header('Content-type: application/xml');
  183. $this->_getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription);
  184. exit;
  185. }
  186. public function _getTableAclList() {// Use only Tables from default_db with the_geom field
  187. $tblAclList = array();
  188. $db = DB::getDB();
  189. $idDefaultDB = $db->_zasob_id;
  190. $fullTblAclList = $this->_usrAcl->getTablesAcl();
  191. foreach ($fullTblAclList as $tblAcl) {
  192. $dataSourceName = 'default_db';// TODO: getSourceName
  193. $tblName = $tblAcl->getName();
  194. if ($idDefaultDB != $tblAcl->getDB()) {// hide non default_db tables
  195. continue;
  196. }
  197. try {
  198. $acl = $this->getAclFromTypeName($typeName = "p5_{$dataSourceName}:{$tblName}");
  199. } catch (Exception $e) {
  200. // TODO: error log $e->getMessage();
  201. }
  202. if (!$acl) {
  203. // TODO: error log msg
  204. continue;
  205. }
  206. $fldList = $acl->getRealFieldList();
  207. if (!in_array('the_geom', $fldList)) {
  208. continue;
  209. }
  210. $tblAclList[] = $tblAcl;
  211. }
  212. return $tblAclList;
  213. }
  214. public function _getFieldListFromAcl($acl) {
  215. $fldList = $acl->getRealFieldList();
  216. {// mv the_geom to the first place
  217. array_unshift($fldList, 'the_geom');
  218. $fldList = array_unique($fldList);
  219. }
  220. return $fldList;
  221. }
  222. }