WfsDataServer.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. $maxFeatures = V::get('count', $maxFeatures, $_REQUEST, 'int');
  43. $startIndex = V::get('startIndex', 0, $_REQUEST, 'int');// sql offset
  44. $ogcFilter = V::get('Filter', '', $_REQUEST);
  45. $sortBy = V::get('sortBy', '', $_REQUEST);
  46. $propertyName = V::get('propertyName', '', $_REQUEST);
  47. $propertyName = trim($propertyName);
  48. $srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
  49. if (count($typeEx) == 2) {
  50. if (empty($ogcFilter)) {// read ogc:Filter from POST body if not defined in GET
  51. $reqBody = Request::getRequestBody();
  52. if (!empty($reqBody)) {
  53. $ogcFilter = $this->convertOgcFilterFromRequestBody($reqBody);
  54. }
  55. }
  56. return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex, $propertyName);
  57. } else {
  58. throw new HttpException("Wrong param TYPENAME", 400);
  59. }
  60. }
  61. public function testOgcFilterAction() {
  62. $type = V::get('TYPENAME', '', $_REQUEST);
  63. $typeEx = explode(':', $type);
  64. $maxFeatures = V::get('MAXFEATURES', '10000', $_REQUEST, 'int');// TODO: Set Deafult Limit
  65. $ogcFilter = V::get('Filter', '', $_REQUEST);
  66. $srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
  67. if (count($typeEx) == 2) {
  68. Lib::loadClass('ParseOgcFilter');
  69. $parser = new ParseOgcFilter();
  70. $parser->loadOgcFilter($ogcFilter);
  71. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  72. echo $queryWhereBuilder->getQueryWhere('t');
  73. } else {
  74. throw new HttpException("Wrong param TYPENAME", 400);
  75. }
  76. }
  77. public function getFeatures($nsPrefix, $type, $maxFeatures, $srsname, $ogcFilter = '', $sortBy = '', $startIndex = 0, $propertyName = '') {
  78. $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
  79. $typeName = "{$nsPrefix}:{$type}";
  80. $acl = $this->getAclFromTypeName($typeName);
  81. $fldList = $this->_getFieldListFromAcl($acl);
  82. $baseNsUri = $this->getBaseNamespaceUri();
  83. //$wfsNs = 'p5_default_db_' . $type;//$nsPrefix;
  84. $wfsNs = 'p5_default_db';//$nsPrefix;
  85. $wfsNsUri = "{$baseNsUri}/" . substr($nsPrefix, 3);
  86. $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$typeName}&REQUEST=DescribeFeatureType";
  87. // get BBox from geom_field (only one geom fld is allowed)
  88. $geomFld = null;
  89. {
  90. foreach ($fldList as $fldName) {
  91. if ($acl->isGeomField($fldName)) {
  92. $geomFld = $fldName;
  93. }
  94. }
  95. }
  96. if($DBG){echo "ogcFilter(" . strlen($ogcFilter) . "): {$ogcFilter}\n";}
  97. $searchParams = array();
  98. $searchParams['limit'] = $maxFeatures;
  99. $searchParams['limitstart'] = $startIndex;
  100. if (!empty($sortBy)) {
  101. $searchParams['sortBy'] = $sortBy;
  102. } else {
  103. $searchParams['order_by'] = $acl->getPrimaryKeyField();
  104. $searchParams['order_dir'] = 'DESC';
  105. }
  106. if (strlen($ogcFilter) > 0) $searchParams['ogc:Filter'] = $ogcFilter;
  107. if (strlen($propertyName) > 0) {
  108. $propertyNamesEx = explode(',', $propertyName);
  109. $onlyCols = array();
  110. foreach ($propertyNamesEx as $colName) {
  111. $colName = trim($colName);
  112. $onlyCols[] = $colName;
  113. }
  114. if (!empty($onlyCols)) $searchParams['cols'] = $onlyCols;
  115. }
  116. if($DBG){echo 'getItems:';print_r($searchParams);echo "\n";}
  117. $items = $acl->getItems($searchParams);
  118. $dom = new DOMDocument('1.0', 'utf-8');
  119. $dom->formatOutput = true;
  120. $dom->preserveWhiteSpace = false;
  121. $rootNode = $dom->createElementNS('http://www.opengis.net/wfs', 'wfs:FeatureCollection');
  122. $dom->appendChild($rootNode);
  123. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.opengis.net/wfs');
  124. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:wfs', 'http://www.opengis.net/wfs');
  125. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
  126. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  127. $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
  128. //$rootNode->setAttribute('xsi:schemaLocation', 'http://www.opengis.net/wfs');
  129. $rootNode->setAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
  130. if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
  131. if (empty($items)) {
  132. $pKeyField = $acl->getPrimaryKeyField();
  133. $fakeItem = new stdClass();
  134. $fakeItem->{$pKeyField} = 0;
  135. $items[0] = $fakeItem;
  136. }
  137. foreach ($items as $itemKey => $item) {
  138. if($DBG){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item->{$geomFld}).'):';print_r($item->{$geomFld});echo "\n";}
  139. $featureMemberNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:featureMember');
  140. $rootNode->appendChild($featureMemberNode);
  141. $featureNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$type}");
  142. $featureMemberNode->appendChild($featureNode);
  143. $featureNode->setAttribute('fid', "{$type}.{$itemKey}");
  144. foreach ($fldList as $fldName) {
  145. $featureFldNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$fldName}");
  146. if ($acl->isGeomField($fldName)) {
  147. $geomNode = $this->_typeConverter->createGmlFromWkt($item->{$fldName}, $dom);
  148. if (!$geomNode) continue;
  149. $featureFldNode->appendChild($geomNode);
  150. } else {
  151. $featureFldNode->nodeValue = str_replace('&', '&amp;', $item->{$fldName});
  152. if (empty($featureFldNode->nodeValue) && '0' !== $featureFldNode->nodeValue) {
  153. continue;
  154. }
  155. }
  156. $featureNode->appendChild($featureFldNode);
  157. }
  158. }
  159. return $dom->saveXml();
  160. }
  161. public function describeFeatureTypeAction() {
  162. $type = V::get('TYPENAME', '', $_REQUEST);
  163. if (empty($type)) {
  164. $reqContent = Request::getRequestBody();
  165. if (!empty($reqContent)) {
  166. return $this->_parseDescribeFeatureTypeRequest($reqContent);
  167. } else {
  168. return $this->_getDescribeFeatureAllTypes();
  169. }
  170. //throw new HttpException("Wrong param TYPENAME", 400);
  171. }
  172. $typeEx = explode(':', $type);
  173. if (count($typeEx) != 2) {
  174. throw new HttpException("Wrong param TYPENAME", 400);
  175. }
  176. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
  177. }
  178. public function describeFeatureTypeAdvancedAction() {
  179. $type = V::get('TYPENAME', '', $_REQUEST);
  180. if (empty($type)) {
  181. $reqContent = Request::getRequestBody();
  182. if (!empty($reqContent)) {
  183. return $this->_parseDescribeFeatureTypeRequest($reqContent, $simple = false);
  184. } else {
  185. return $this->_getDescribeFeatureAllTypes($simple = false);
  186. }
  187. //throw new HttpException("Wrong param TYPENAME", 400);
  188. }
  189. $typeEx = explode(':', $type);
  190. if (count($typeEx) != 2) {
  191. throw new HttpException("Wrong param TYPENAME", 400);
  192. }
  193. return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1], $simple = false);
  194. }
  195. public function getCapabilitiesAction() {
  196. $wfsServerUrl = $this->getBaseUri();
  197. $serviceTitle = "Web Feature Service";
  198. $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
  199. //header('Content-type: application/xml; charset="utf-8"');
  200. header('Content-type: application/xml');
  201. $this->_getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription);
  202. }
  203. }