WfsDataServer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. Lib::loadClass('Api_WfsServerBase');
  3. Lib::loadClass('Api_WfsException');
  4. Lib::loadClass('Api_WfsGeomTypeConverter');
  5. class Api_WfsDataServer 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 = '10000';// TODO: Set Deafult Limit
  40. $maxFeatures = V::get('MAXFEATURES', $maxFeatures, $_REQUEST, 'int');
  41. $maxFeatures = V::get('maxFeatures', $maxFeatures, $_REQUEST, 'int');
  42. $startIndex = V::get('startIndex', 0, $_REQUEST, 'int');// sql offset
  43. $ogcFilter = V::get('Filter', '', $_REQUEST);
  44. $sortBy = V::get('sortBy', '', $_REQUEST);
  45. $srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
  46. if (count($typeEx) == 2) {
  47. return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex);
  48. } else {
  49. throw new HttpException("Wrong param TYPENAME", 400);
  50. }
  51. }
  52. public function testOgcFilterAction() {
  53. $type = V::get('TYPENAME', '', $_REQUEST);
  54. $typeEx = explode(':', $type);
  55. $maxFeatures = V::get('MAXFEATURES', '10000', $_REQUEST, 'int');// TODO: Set Deafult Limit
  56. $ogcFilter = V::get('Filter', '', $_REQUEST);
  57. $srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
  58. if (count($typeEx) == 2) {
  59. Lib::loadClass('ParseOgcFilter');
  60. $parser = new ParseOgcFilter();
  61. $parser->loadOgcFilter($ogcFilter);
  62. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  63. echo $queryWhereBuilder->getQueryWhere('t');
  64. } else {
  65. throw new HttpException("Wrong param TYPENAME", 400);
  66. }
  67. }
  68. public function getFeatures($nsPrefix, $type, $maxFeatures, $srsname, $ogcFilter = '', $sortBy = '', $startIndex = 0) {
  69. $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
  70. $typeName = "{$nsPrefix}:{$type}";
  71. $acl = $this->getAclFromTypeName($typeName);
  72. $fldList = $acl->getRealFieldList();
  73. $baseNsUri = $this->getBaseNamespaceUri();
  74. $wfsNs = 'p5_default_db_' . $type;//$nsPrefix;
  75. $wfsNsUri = "{$baseNsUri}/" . substr($nsPrefix, 3) . '/' . $type;
  76. // get BBox from geom_field (only one geom fld is allowed)
  77. $geomFld = null;
  78. {
  79. foreach ($fldList as $fldName) {
  80. if ($acl->isGeomField($fldName)) {
  81. $geomFld = $fldName;
  82. }
  83. }
  84. }
  85. if($DBG){echo "ogcFilter(" . strlen($ogcFilter) . "): {$ogcFilter}\n";}
  86. $searchParams = array();
  87. $searchParams['limit'] = $maxFeatures;
  88. $searchParams['limitstart'] = $startIndex;
  89. if (!empty($sortBy)) {
  90. $searchParams['sortBy'] = $sortBy;
  91. } else {
  92. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  93. $searchParams['order_dir'] = 'DESC';
  94. }
  95. if (strlen($ogcFilter) > 0) $searchParams['ogc:Filter'] = $ogcFilter;
  96. if($DBG){echo 'getItems:';print_r($searchParams);echo "\n";}
  97. $items = $acl->getItems($searchParams);
  98. $dom = new DOMDocument('1.0', 'utf-8');
  99. $dom->formatOutput = true;
  100. $dom->preserveWhiteSpace = false;
  101. $rootNode = $dom->createElementNS('http://www.opengis.net/wfs', 'wfs:FeatureCollection');
  102. $dom->appendChild($rootNode);
  103. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.opengis.net/wfs');
  104. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:wfs', 'http://www.opengis.net/wfs');
  105. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  106. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  107. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  108. $rootNode->setAttribute('xsi:schemaLocation', 'http://www.opengis.net/wfs');// TODO: add DescribeFeatureType xsd uri
  109. if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
  110. if (empty($items)) {
  111. $pKeyField = $acl->getPrimaryKeyField();
  112. $fakeItem = new stdClass();
  113. $fakeItem->{$pKeyField} = 0;
  114. $items[0] = $fakeItem;
  115. }
  116. foreach ($items as $itemKey => $item) {
  117. if($DBG){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item->{$geomFld}).'):';print_r($item->{$geomFld});echo "\n";}
  118. $featureMemberNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:featureMember');
  119. $rootNode->appendChild($featureMemberNode);
  120. $featureNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$type}");
  121. $featureMemberNode->appendChild($featureNode);
  122. $featureNode->setAttribute('fid', "{$type}.{$itemKey}");
  123. foreach ($fldList as $fldName) {
  124. $featureFldNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$fldName}");
  125. if ($acl->isGeomField($fldName)) {
  126. $geomNode = $this->_typeConverter->createGmlFromWkt($item->{$fldName}, $dom);
  127. if (!$geomNode) continue;
  128. $featureFldNode->appendChild($geomNode);
  129. } else {
  130. $featureFldNode->nodeValue = str_replace('&', '&amp;', $item->{$fldName});
  131. if (empty($featureFldNode->nodeValue)) {
  132. continue;
  133. }
  134. }
  135. $featureNode->appendChild($featureFldNode);
  136. }
  137. }
  138. return $dom->saveXml();
  139. }
  140. public function describeFeatureTypeAction() {
  141. $type = V::get('TYPENAME', '', $_REQUEST);
  142. if (empty($type)) {
  143. throw new HttpException("Wrong param TYPENAME", 400);
  144. }
  145. $typeEx = explode(':', $type);
  146. if (count($typeEx) != 2) {
  147. throw new HttpException("Wrong param TYPENAME", 400);
  148. }
  149. return $this->getDescribeFeatureType($typeEx[0], $typeEx[1]);
  150. }
  151. public function getDescribeFeatureType($nsPrefix, $type) {
  152. $typeName = "{$nsPrefix}:{$type}";
  153. $acl = $this->getAclFromTypeName($typeName);
  154. $baseNsUri = $this->getBaseNamespaceUri();
  155. $wfsNs = 'p5_default_db_' . $type;//$nsPrefix;
  156. $wfsNsUri = "{$baseNsUri}/" . substr($nsPrefix, 3) . '/' . $type;
  157. if (empty($type)) {
  158. throw new HttpException("Feature Type Name not defined", 400);
  159. }
  160. if (!$this->isAllowedFeatureType($nsPrefix, $type)) {
  161. throw new Api_WfsException("Could not find type: " . htmlspecialchars($type));
  162. }
  163. $typeName = $type . 'Type';
  164. $fldList = $acl->getRealFieldList();
  165. header('Content-type: application/xml');
  166. // <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="http://webgis.regione.sardegna.it:80/geoserver/schemas/gml/2.1.2/feature.xsd"/>
  167. // <xsd:element name="{type}" substitutionGroup="gml:_Feature" type="dbu:{typeName}"/>
  168. $dom = new DOMDocument('1.0', 'utf-8');
  169. $dom->formatOutput = true;
  170. $dom->preserveWhiteSpace = false;
  171. $rootNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:schema');
  172. $dom->appendChild($rootNode);
  173. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  174. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  175. $rootNode->setAttribute('elementFormDefault', 'qualified');
  176. $rootNode->setAttribute('targetNamespace', $wfsNsUri);
  177. $cTypeNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexType');
  178. $rootNode->appendChild($cTypeNode);
  179. $cTypeNode->setAttribute('name', $typeName);
  180. $cConNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:complexContent');
  181. $cTypeNode->appendChild($cConNode);
  182. $extNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:extension');
  183. $cConNode->appendChild($extNode);
  184. $extNode->setAttribute('base', 'gml:AbstractFeatureType');
  185. $seqNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:sequence');
  186. $extNode->appendChild($seqNode);
  187. // <xsd:element maxOccurs="1" minOccurs="0" name="{$fldName}" nillable="true" type="xsd:integer"/>
  188. $pKeyField = $acl->getPrimaryKeyField();
  189. foreach ($fldList as $fldName) {
  190. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  191. $seqNode->appendChild($elNode);
  192. $elNode->setAttribute('name', $fldName);
  193. $minOccurs = 0;
  194. if ($pKeyField == $fldName) {
  195. $minOccurs = '1';
  196. } else {
  197. $minOccurs = '0';
  198. }
  199. $elNode->setAttribute('minOccurs', $minOccurs);
  200. if ($acl->isIntegerField($fldName)) {
  201. $fldType = 'xsd:integer';
  202. }
  203. else if ($acl->isDecimalField($fldName)) {
  204. $fldType = 'xsd:decimal';
  205. }
  206. else if ($acl->isDateField($fldName)) {
  207. $fldType = 'xsd:date';
  208. }
  209. else if ($acl->isDateTimeField($fldName)) {
  210. $fldType = 'xsd:dateTime';
  211. }
  212. else if ($acl->isGeomField($fldName)) {
  213. $fldType = 'gml:GeometryPropertyType';
  214. //$fldType = 'gml:Polygon';// nie działa musi być gml:GeometryPropertyType
  215. }
  216. else {
  217. $fldType = 'xsd:string';
  218. }
  219. $elNode->setAttribute('type', $fldType);
  220. $elNode->setAttribute('nillable', 'true');
  221. }
  222. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
  223. $rootNode->appendChild($elNode);
  224. $elNode->setAttribute('name', $type);
  225. $elNode->setAttribute('type', $wfsNs . ':' . $typeName);
  226. echo $dom->saveXML();
  227. }
  228. public function getCapabilitiesAction() {
  229. $wfsServerUrl = $this->getBaseUri();
  230. $serviceTitle = "Web Feature Service";
  231. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  232. //header('Content-type: application/xml; charset="utf-8"');
  233. header('Content-type: application/xml');
  234. $this->_getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription);
  235. }
  236. }