WfsQgisServer.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 = $acl->getRealFieldList();
  52. $baseNsUri = $this->getBaseNamespaceUri();
  53. $wfsNs = 'p5_default_db_' . $type;//$nsPrefix;
  54. $wfsNsUri = "{$baseNsUri}/" . substr($nsPrefix, 3) . '/' . $type;
  55. // get BBox from geom_field (only one geom fld is allowed)
  56. $geomFld = null;
  57. {
  58. foreach ($fldList as $fldName) {
  59. if ($acl->isGeomField($fldName)) {
  60. $geomFld = $fldName;
  61. }
  62. }
  63. }
  64. $dbGeomType = $acl->getGeomFieldType($geomFld);
  65. $searchParams = array();
  66. $searchParams['limit'] = $maxFeatures;
  67. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  68. $searchParams['order_dir'] = 'DESC';
  69. if ($geomFld) $searchParams["f_{$geomFld}"] = 'IS NOT NULL';
  70. if ($geomFld) $searchParams["f_{$geomFld}"] = 'GeometryType=' . strtoupper($dbGeomType);
  71. //if ($geomFld) $searchParams["f_{$geomFld}"] = 'GeometryType=LINESTRING';
  72. if($DBG){echo 'getItems:';print_r($searchParams);echo "\n";}
  73. $items = $acl->getItems($searchParams);
  74. $dom = new DOMDocument('1.0', 'utf-8');
  75. $dom->formatOutput = true;
  76. $dom->preserveWhiteSpace = false;
  77. $rootNode = $dom->createElementNS('http://www.opengis.net/wfs', 'wfs:FeatureCollection');
  78. $dom->appendChild($rootNode);
  79. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.opengis.net/wfs');
  80. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:wfs', 'http://www.opengis.net/wfs');
  81. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  82. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  83. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  84. $rootNode->setAttribute('xsi:schemaLocation', 'http://www.opengis.net/wfs');// TODO: add DescribeFeatureType xsd uri
  85. if(0){// TODO: get BBOX for add features
  86. $boundedByNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:boundedBy');
  87. $rootNode->appendChild($boundedByNode);
  88. $boxNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:Box');
  89. $boundedByNode->appendChild($boxNode);
  90. $boxNode->setAttribute('srsName', "http://www.opengis.net/gml/srs/epsg.xml#4326");// TODO: EPSG
  91. $coordinatesNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:coordinates');
  92. $boxNode->appendChild($coordinatesNode);
  93. $coordinatesNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  94. $coordinatesNode->setAttribute('decimal', '.');
  95. $coordinatesNode->setAttribute('cs', ',');
  96. $coordinatesNode->setAttribute('ts', ' ');
  97. $coordinatesNode->nodeValue = '1544947.6295,4322758.105 1548002.2259,4330464.1001';// TODO: coordinates for all items?
  98. }
  99. if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
  100. if (empty($items)) {
  101. $pKeyField = $acl->getPrimaryKeyField();
  102. $fakeItem = new stdClass();
  103. $fakeItem->{$pKeyField} = 0;
  104. if ('polygon' == $dbGeomType) {
  105. $fakeItem->the_geom = "POLYGON(())";
  106. } else if ('linestring' == $dbGeomType) {
  107. $fakeItem->the_geom = "LINESTRING()";
  108. } else if ('point' == $dbGeomType) {
  109. $fakeItem->the_geom = "POINT(0,0)";
  110. }
  111. $items[0] = $fakeItem;
  112. }
  113. foreach ($items as $itemKey => $item) {
  114. //if($item->ID == 19)continue;
  115. if($DBG){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item->{$geomFld}).'):';print_r($item->{$geomFld});echo "\n";}
  116. if ($geomFld) {
  117. if (empty($item->{$geomFld})) {
  118. continue;// QGIS crash when WFS contain features with empty geom field
  119. }
  120. }
  121. $featureMemberNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:featureMember');
  122. $rootNode->appendChild($featureMemberNode);
  123. $featureNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$type}");
  124. $featureMemberNode->appendChild($featureNode);
  125. $featureNode->setAttribute('fid', "{$type}.{$itemKey}");
  126. if(0){// TODO: get BBOX
  127. $boundedByNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:boundedBy');
  128. $featureNode->appendChild($boundedByNode);
  129. $boxNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:Box');
  130. $boundedByNode->appendChild($boxNode);
  131. $boxNode->setAttribute('srsName', "http://www.opengis.net/gml/srs/epsg.xml#4326");// TODO: EPSG
  132. $coordinatesNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:coordinates');
  133. $boxNode->appendChild($coordinatesNode);
  134. $coordinatesNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  135. $coordinatesNode->setAttribute('decimal', '.');
  136. $coordinatesNode->setAttribute('cs', ',');
  137. $coordinatesNode->setAttribute('ts', ' ');
  138. $coordinatesNode->nodeValue = '1546472.2363,4328949.5775 1548002.2259,4330464.1001';// TODO: coordinates for item?
  139. }
  140. foreach ($fldList as $fldName) {
  141. $featureFldNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$fldName}");
  142. if ($acl->isGeomField($fldName)) {
  143. $geomNode = $this->_typeConverter->createGmlFromWkt($item->{$fldName}, $dom);
  144. if (!$geomNode) continue;
  145. $featureFldNode->appendChild($geomNode);
  146. } else {
  147. $featureFldNode->nodeValue = str_replace('&', '&amp;', $item->{$fldName});
  148. if (empty($featureFldNode->nodeValue)) {
  149. continue;
  150. }
  151. }
  152. $featureNode->appendChild($featureFldNode);
  153. }
  154. }
  155. return $dom->saveXml();
  156. }
  157. public function describeFeatureTypeAction() {
  158. $type = V::get('TYPENAME', '', $_REQUEST);
  159. if (empty($type)) {
  160. throw new HttpException("Wrong param TYPENAME", 400);
  161. }
  162. $typeEx = explode(':', $type);
  163. if (count($typeEx) != 2) {
  164. throw new HttpException("Wrong param TYPENAME", 400);
  165. }
  166. return $this->getDescribeFeatureType($typeEx[0], $typeEx[1]);
  167. }
  168. public function getDescribeFeatureType($nsPrefix, $type) {
  169. $typeName = "{$nsPrefix}:{$type}";
  170. $acl = $this->getAclFromTypeName($typeName);
  171. $baseNsUri = $this->getBaseNamespaceUri();
  172. $wfsNs = 'p5_default_db_' . $type;//$nsPrefix;
  173. $wfsNsUri = "{$baseNsUri}/" . substr($nsPrefix, 3) . '/' . $type;
  174. if (empty($type)) {
  175. throw new HttpException("Feature Type Name not defined", 400);
  176. }
  177. if (!$this->isAllowedFeatureType($nsPrefix, $type)) {
  178. throw new Api_WfsException("Could not find type: " . htmlspecialchars($type));
  179. }
  180. $typeName = $type . 'Type';
  181. $fldList = $acl->getRealFieldList();
  182. header('Content-type: application/xml');
  183. // <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="http://webgis.regione.sardegna.it:80/geoserver/schemas/gml/2.1.2/feature.xsd"/>
  184. // <xsd:element name="{type}" substitutionGroup="gml:_Feature" type="dbu:{typeName}"/>
  185. $dom = new DOMDocument('1.0', 'utf-8');
  186. $dom->formatOutput = true;
  187. $dom->preserveWhiteSpace = false;
  188. $rootNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:schema');
  189. $dom->appendChild($rootNode);
  190. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  191. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  192. $rootNode->setAttribute('elementFormDefault', 'qualified');
  193. $rootNode->setAttribute('targetNamespace', $wfsNsUri);
  194. $cTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  195. $rootNode->appendChild($cTypeNode);
  196. $cTypeNode->setAttribute('name', $typeName);
  197. $cConNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexContent');
  198. $cTypeNode->appendChild($cConNode);
  199. $extNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:extension');
  200. $cConNode->appendChild($extNode);
  201. $extNode->setAttribute('base', 'gml:AbstractFeatureType');
  202. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:sequence');
  203. $extNode->appendChild($seqNode);
  204. // <xsd:element maxOccurs="1" minOccurs="0" name="{$fldName}" nillable="true" type="xsd:integer"/>
  205. $pKeyField = $acl->getPrimaryKeyField();
  206. foreach ($fldList as $fldName) {
  207. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  208. $seqNode->appendChild($elNode);
  209. $elNode->setAttribute('name', $fldName);
  210. $minOccurs = 0;
  211. if ($pKeyField == $fldName) {
  212. $minOccurs = '1';
  213. } else {
  214. $minOccurs = '0';
  215. }
  216. $elNode->setAttribute('minOccurs', $minOccurs);
  217. if ($acl->isIntegerField($fldName)) {
  218. $fldType = 'xsd:integer';
  219. }
  220. else if ($acl->isDecimalField($fldName)) {
  221. $fldType = 'xsd:decimal';
  222. }
  223. else if ($acl->isDateField($fldName)) {
  224. $fldType = 'xsd:date';
  225. }
  226. else if ($acl->isDateTimeField($fldName)) {
  227. $fldType = 'xsd:dateTime';
  228. }
  229. else if ($acl->isGeomField($fldName)) {
  230. $fldType = 'gml:GeometryPropertyType';
  231. //$fldType = 'gml:Polygon';// nie działa musi być gml:GeometryPropertyType
  232. }
  233. else {
  234. $fldType = 'xsd:string';
  235. }
  236. $elNode->setAttribute('type', $fldType);
  237. $elNode->setAttribute('nillable', 'true');
  238. }
  239. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  240. $rootNode->appendChild($elNode);
  241. $elNode->setAttribute('name', $type);
  242. $elNode->setAttribute('type', $wfsNs . ':' . $typeName);
  243. echo $dom->saveXML();
  244. }
  245. public function getCapabilitiesAction() {
  246. $wfsServerUrl = $this->getBaseUri();
  247. $serviceTitle = "Web Feature Service for QGIS";
  248. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  249. //header('Content-type: application/xml; charset="utf-8"');
  250. header('Content-type: application/xml');
  251. $this->_getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription);
  252. }
  253. }