WfsDataServer.php 10.0 KB

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