WfsDataServer.php 11 KB

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