| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <?php
- Lib::loadClass('Api_WfsServerBase');
- Lib::loadClass('Api_WfsException');
- Lib::loadClass('Api_WfsGeomTypeConverter');
- class Api_WfsQgisServer extends Api_WfsServerBase {
- public function parseXMLRequest() {
- $data = array();
- $reqContent = Request::getRequestBody();
- if (empty($reqContent)) {
- throw new Exception("Empty request");
- }
- $parserXml = xml_parser_create();
- xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
- xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
- if (0 == xml_parse_into_struct($parserXml, $reqContent, $tags)) {
- throw new Exception("Error parsing xml");
- }
- xml_parser_free($parserXml);
- if (empty($tags)) {
- throw new Exception("Empty structure from request");
- }
- $rootTagName = V::get('tag', '', $tags[0]);
- if ('Transaction' == $rootTagName) {
- return $this->_parseTransactionXmlStruct($reqContent, $tags);
- }
- throw new Api_WfsException("TODO ... L." . __LINE__, 501);
- $xml = new SimpleXMLElement($reqContent);
- $namespaces = $xml->getNameSpaces(true);
- if ('Transaction' == $xml->getName()) {
- $this->_parseTransactionXml($xml);
- }
- else {
- throw new Api_WfsException("Not Implemented " . htmlspecialchars($xml->getName()), 501);
- }
- }
- public function getFeatureAction() {
- $type = V::get('TYPENAME', '', $_REQUEST);
- $typeEx = explode(':', $type);
- $maxFeatures = V::get('MAXFEATURES', '10000', $_REQUEST, 'int');// TODO: Set Deafult Limit
- $srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
- if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
- return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname);
- }
- public function getFeatures($nsPrefix, $type, $maxFeatures, $srsname) {
- $DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
- $typeName = "{$nsPrefix}:{$type}";
- $acl = $this->getAclFromTypeName($typeName);
- $fldList = $this->_getFieldListFromAcl($acl);
- $baseNsUri = $this->getBaseNamespaceUri();
- //$wfsNs = 'p5_default_db_' . $type;//$nsPrefix;
- $wfsNs = 'p5_default_db';//$nsPrefix;
- $wfsNsUri = "{$baseNsUri}/" . substr($nsPrefix, 3);
- $featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$typeName}&REQUEST=DescribeFeatureType";
- // https://biuro.biall-net.pl/dev-pl/se-master/wfs-qgis.php/default_db/
- // https://biuro.biall-net.pl/dev-pl/se-master/wfs-data.php/default_db/TEST_PERMS/?SERVICE=WFS&VERSION=1.0.0&TYPENAME=p5_default_db:TEST_PERMS&REQUEST=DescribeFeatureType
- // get BBox from geom_field (only one geom fld is allowed)
- $geomFld = null;
- {
- foreach ($fldList as $fldName) {
- if ($acl->isGeomField($fldName)) {
- $geomFld = $fldName;
- }
- }
- }
- $dbGeomType = $acl->getGeomFieldType($geomFld);
- $searchParams = array();
- $searchParams['limit'] = $maxFeatures;
- $searchParams['order_by'] = $acl->getPrimaryKeyField();
- $searchParams['order_dir'] = 'DESC';
- {// BBOX
- // 54.26931096743426,18.48242909824306,54.26738118403914,18.478738378639246
- $bbox = V::get('BBOX', '', $_GET);
- if (!empty($bbox)) {
- if (preg_match("/^\d+(.\d+)?,\d+(.\d+)?,\d+(.\d+)?,\d+(.\d+)?$/", $bbox, $matches)) {
- $searchParams['f_the_geom'] = "BBOX:{$bbox}";
- } else {
- // throw new Exception("Error Processing Request", 1);// ?
- $this->DBG("Error BBOX wrong format", __LINE__, __FUNCTION__, __CLASS__);
- }
- }
- }
- //if ($geomFld) $searchParams["f_{$geomFld}"] = 'IS NOT NULL';
- //if ($geomFld) $searchParams["f_{$geomFld}"] = 'GeometryType=' . strtoupper($dbGeomType);
- $geomType = strtoupper($dbGeomType);
- if ($geomFld) $searchParams["ogc:Filter"] = <<<OGC_FILTER
- <ogc:Filter>
- <ogc:And>
- <ogc:Not>
- <ogc:PropertyIsNull>
- <ogc:PropertyName>{$geomFld}</ogc:PropertyName>
- </ogc:PropertyIsNull>
- </ogc:Not>
- <ogc:PropertyIsEqualTo>
- <ogc:Function name="GeometryType">
- <ogc:PropertyName>{$geomFld}</ogc:PropertyName>
- </ogc:Function>
- <ogc:Literal>{$geomType}</ogc:Literal>
- </ogc:PropertyIsEqualTo>
- </ogc:And>
- </ogc:Filter>
- OGC_FILTER;
- if($DBG){echo 'getItems:';print_r($searchParams);echo "\n";}
- $this->DBG("getItems:" . json_encode($searchParams), __LINE__, __FUNCTION__, __CLASS__);
- $items = $acl->getItems($searchParams);
- $this->DBG("items(" . count($items) . ")", __LINE__, __FUNCTION__, __CLASS__);
- if (true) {//1 == V::get('XML_WRITER', 0, $_GET, 'int')) {
- header('Content-type: application/xml; charset=utf-8');
- $xmlWriter = new XMLWriter();
- $xmlWriter->openUri('php://output');
- // $xmlWriter->openMemory();// DBG
- $xmlWriter->setIndent(true);
- if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
- $xmlWriter->startDocument('1.0','UTF-8');
- //$xmlWriter->startElementNS('wfs', 'FeatureCollection', 'http://www.opengis.net/wfs');
- $xmlWriter->startElement('wfs:FeatureCollection');
- // $xmlWriter->writeAttributeNS('xmlns', 'wfs', 'http://www.w3.org/2000/xmlns/', 'http://www.opengis.net/wfs');
- $xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs');
- $xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs');
- // $xmlWriter->writeAttributeNS('xmlns', 'gml', 'http://www.w3.org/2000/xmlns/', 'http://www.opengis.net/gml');
- // $xmlWriter->writeAttributeNS('xmlns', 'xsi', 'http://www.w3.org/2000/xmlns/', 'http://www.w3.org/2001/XMLSchema-instance');
- // $xmlWriter->writeAttributeNS('xmlns', $wfsNs, 'http://www.w3.org/2000/xmlns/', $wfsNsUri);
- $xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
- $xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
- $xmlWriter->writeAttribute("xmlns:{$wfsNs}", $wfsNsUri);
- $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
- if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
- if (empty($items)) {
- $pKeyField = $acl->getPrimaryKeyField();
- $fakeItem = new stdClass();
- $fakeItem->{$pKeyField} = 0;
- if ('polygon' == $dbGeomType) {
- $fakeItem->the_geom = "POLYGON(())";
- } else if ('linestring' == $dbGeomType) {
- $fakeItem->the_geom = "LINESTRING()";
- } else if ('point' == $dbGeomType) {
- $fakeItem->the_geom = "POINT(0,0)";
- }
- $items[0] = $fakeItem;
- }
- $dbgLoop = 0;
- $this->DBG("before loop...", __LINE__, __FUNCTION__, __CLASS__);
- foreach ($items as $itemKey => $item) {
- if (0 == (++$dbgLoop) % 500) $this->DBG("items loop:{$dbgLoop}", __LINE__, __FUNCTION__, __CLASS__);
- if($DBG){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item->{$geomFld}).'):';print_r($item->{$geomFld});echo "\n";}
- if ($geomFld) {
- if (empty($item->{$geomFld})) {
- continue;// QGIS crash when WFS contain features with empty geom field
- }
- }
- $xmlWriter->startElement('gml:featureMember');
- $xmlWriter->startElement("{$wfsNs}:{$type}");
- $xmlWriter->writeAttribute('fid', "{$type}.{$itemKey}");
- foreach ($fldList as $fldName) {
- // if ($acl->isGeomField($fldName)) {// BUG: wolno
- // if($fldName == 'the_geom'){// OK szybko
- if ($geomFld != null && $fldName == $geomFld){
- $xmlWriter->startElement("{$wfsNs}:{$fldName}");
- $this->_typeConverter->createGmlFromWkt_xmlWriter($item->{$fldName}, $xmlWriter);
- $xmlWriter->endElement();// {$wfsNs}:{$fldName}
- } else {
- $value = str_replace('&', '&', $item->{$fldName});
- if (empty($value) && '0' !== $value) {
- continue;
- } else {
- $xmlWriter->startElement("{$wfsNs}:{$fldName}");
- $xmlWriter->text($value);
- $xmlWriter->endElement();// {$wfsNs}:{$fldName}
- }
- }
- }
- $xmlWriter->endElement();// {$wfsNs}:{$type}
- $xmlWriter->endElement();// gml:featureMember
- }
- $xmlWriter->endElement();// wfs:FeatureCollection
- $xmlWriter->endDocument();
- $this->DBG("items loop END", __LINE__, __FUNCTION__, __CLASS__);
- //'<!-- .EOF -->';
- exit;
- }
- $dom = new DOMDocument('1.0', 'utf-8');
- $dom->formatOutput = true;
- $dom->preserveWhiteSpace = false;
- $rootNode = $dom->createElementNS('http://www.opengis.net/wfs', 'wfs:FeatureCollection');
- $dom->appendChild($rootNode);
- $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.opengis.net/wfs');
- $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:wfs', 'http://www.opengis.net/wfs');
- $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
- $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
- $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
- $rootNode->setAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
- if(0){// TODO: get BBOX for add features
- $boundedByNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:boundedBy');
- $rootNode->appendChild($boundedByNode);
- $boxNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:Box');
- $boundedByNode->appendChild($boxNode);
- $boxNode->setAttribute('srsName', "http://www.opengis.net/gml/srs/epsg.xml#4326");// TODO: EPSG
- $coordinatesNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:coordinates');
- $boxNode->appendChild($coordinatesNode);
- $coordinatesNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
- $coordinatesNode->setAttribute('decimal', '.');
- $coordinatesNode->setAttribute('cs', ',');
- $coordinatesNode->setAttribute('ts', ' ');
- $coordinatesNode->nodeValue = '1544947.6295,4322758.105 1548002.2259,4330464.1001';// TODO: coordinates for all items?
- }
- if($DBG){echo '(geomFld: '.$geomFld.'):';print_r($acl->getFieldType($geomFld));echo "\n";}
- if (empty($items)) {
- $pKeyField = $acl->getPrimaryKeyField();
- $fakeItem = new stdClass();
- $fakeItem->{$pKeyField} = 0;
- if ('polygon' == $dbGeomType) {
- $fakeItem->the_geom = "POLYGON(())";
- } else if ('linestring' == $dbGeomType) {
- $fakeItem->the_geom = "LINESTRING()";
- } else if ('point' == $dbGeomType) {
- $fakeItem->the_geom = "POINT(0,0)";
- }
- $items[0] = $fakeItem;
- }
- $dbgLoop = 0;
- foreach ($items as $itemKey => $item) {
- $dbgLoop++; if (0 == $dbgLoop % 100) $this->DBG("items loop:{$dbgLoop}", __LINE__, __FUNCTION__, __CLASS__);
- if($DBG){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item->{$geomFld}).'):';print_r($item->{$geomFld});echo "\n";}
- if ($geomFld) {
- if (empty($item->{$geomFld})) {
- continue;// QGIS crash when WFS contain features with empty geom field
- }
- }
- $featureMemberNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:featureMember');
- $rootNode->appendChild($featureMemberNode);
- $featureNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$type}");
- $featureMemberNode->appendChild($featureNode);
- $featureNode->setAttribute('fid', "{$type}.{$itemKey}");
- if(0){// TODO: get BBOX
- $boundedByNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:boundedBy');
- $featureNode->appendChild($boundedByNode);
- $boxNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:Box');
- $boundedByNode->appendChild($boxNode);
- $boxNode->setAttribute('srsName', "http://www.opengis.net/gml/srs/epsg.xml#4326");// TODO: EPSG
- $coordinatesNode = $dom->createElementNS('http://www.opengis.net/gml', 'gml:coordinates');
- $boxNode->appendChild($coordinatesNode);
- $coordinatesNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
- $coordinatesNode->setAttribute('decimal', '.');
- $coordinatesNode->setAttribute('cs', ',');
- $coordinatesNode->setAttribute('ts', ' ');
- $coordinatesNode->nodeValue = '1546472.2363,4328949.5775 1548002.2259,4330464.1001';// TODO: coordinates for item?
- }
- foreach ($fldList as $fldName) {
- $featureFldNode = $dom->createElementNS($wfsNsUri, "{$wfsNs}:{$fldName}");
- if ($acl->isGeomField($fldName)) {
- $geomNode = $this->_typeConverter->createGmlFromWkt($item->{$fldName}, $dom);
- if (!$geomNode) continue;
- $featureFldNode->appendChild($geomNode);
- } else {
- $featureFldNode->nodeValue = str_replace('&', '&', $item->{$fldName});
- if (empty($featureFldNode->nodeValue) && '0' !== $featureFldNode->nodeValue) {
- continue;
- }
- }
- $featureNode->appendChild($featureFldNode);
- }
- }
- $this->DBG("items loop END", __LINE__, __FUNCTION__, __CLASS__);
- return $dom->saveXml();
- }
- public function describeFeatureTypeAction() {
- $type = V::get('TYPENAME', '', $_REQUEST);
- if (empty($type)) {
- $reqContent = Request::getRequestBody();
- if (!empty($reqContent)) {
- return $this->_parseDescribeFeatureTypeRequest($reqContent);
- } else {
- return $this->_getDescribeFeatureAllTypes();
- }
- //throw new HttpException("Wrong param TYPENAME", 400);
- }
- $typeEx = explode(':', $type);
- if (count($typeEx) != 2) {
- throw new HttpException("Wrong param TYPENAME", 400);
- }
- return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
- }
- public function getCapabilitiesAction() {
- $wfsServerUrl = $this->getBaseUri();
- $serviceTitle = "Web Feature Service for QGIS";
- $serviceDescription = "This is the reference implementation of WFS 1.0.0 and WFS 1.1.0, supports all WFS operations including Transaction.";
- //header('Content-type: application/xml; charset="utf-8"');
- header('Content-type: application/xml');
- $this->_getCapabilities($wfsServerUrl, $serviceTitle, $serviceDescription);
- exit;
- }
- public function _getTableAclList() {// Use only Tables from default_db with the_geom field
- $tblAclList = array();
- $db = DB::getDB();
- $idDefaultDB = $db->_zasob_id;
- $fullTblAclList = $this->_usrAcl->getTablesAcl();
- foreach ($fullTblAclList as $tblAcl) {
- $dataSourceName = 'default_db';// TODO: getSourceName
- $tblName = $tblAcl->getName();
- if ($idDefaultDB != $tblAcl->getDB()) {// hide non default_db tables
- continue;
- }
- try {
- $acl = $this->getAclFromTypeName($typeName = "p5_{$dataSourceName}:{$tblName}");
- } catch (Exception $e) {
- // TODO: error log $e->getMessage();
- }
- if (!$acl) {
- // TODO: error log msg
- continue;
- }
- $fldList = $acl->getRealFieldList();
- if (!in_array('the_geom', $fldList)) {
- continue;
- }
- $tblAclList[] = $tblAcl;
- }
- return $tblAclList;
- }
- public function _getFieldListFromAcl($acl) {
- $fldList = $acl->getRealFieldList();
- {// mv the_geom to the first place
- array_unshift($fldList, 'the_geom');
- $fldList = array_unique($fldList);
- }
- return $fldList;
- }
- }
|