query) && ('WFS' != V::get('service', '', $request->query))) { throw new Api_WfsException("Only WFS Service is allowed"); } $req = V::get('REQUEST', '', $request->query); if (!empty($req)) { $methodName = "{$req}Action"; if (!method_exists($this, $methodName)) { throw new Api_WfsException("Not Implemented " . htmlspecialchars($req), 501); } $this->DBG("WfsServer->{$methodName}() ...", __LINE__); $document = $this->$methodName($urlQuery); } else { $this->DBG("WfsServer->parseXMLRequest() ...", __LINE__); $document = $this->parseXMLRequest(); header('Content-type: application/xml'); echo ''; echo $document; exit;// TODO: return $document; } IF(V::get('DBG','',$_GET)){echo'
$document (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($document);echo'';}
if ('raw' == V::get('outputFormat', '', $request->query)) {
header('Content-type: text/plain; charset=utf-8');
echo $document;
} else {
header('Content-type: application/xml');
echo $document;
}
}
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("Not implemented '{$rootTagName}' #L." . __LINE__, 501);
}
public function getFeatureAction() {
$args = $this->parseGetFeatureArgsFromRequest();
if ('hits' == $args['resultType']) {
return $this->getTotalFeatures($args, $simple = true);
} else {
return $this->getFeatures($args, $simple = true);
}
}
public function getFeatureAdvancedAction() {
$args = $this->parseGetFeatureArgsFromRequest();
if ('hits' == $args['resultType']) {
return $this->getTotalFeatures($args, $simple = false);
} else {
if ('/@instance' == strtolower(substr($args['typeName'], -1 * strlen('/@instance')))) {
return $this->getInstanceFeatures(substr($args['typeName'], 0, -1 * strlen('/@instance')), $args);
}
return $this->getFeatures($args, $simple = false);
}
}
public function testOgcFilterAction() {
$type = V::get('TYPENAME', '', $_REQUEST);
$typeEx = explode(':', $type);
$maxFeatures = V::get('MAXFEATURES', '10000', $_REQUEST, 'int');// TODO: Set Deafult Limit
$ogcFilter = V::get('Filter', '', $_REQUEST);
$srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
if (count($typeEx) == 2) {
Lib::loadClass('ParseOgcFilter');
$parser = new ParseOgcFilter();
$parser->loadOgcFilter($ogcFilter);
$queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
echo $queryWhereBuilder->getQueryWhere('t');
} else {
throw new HttpException("Wrong param TYPENAME", 400);
}
}
public function getTotalFeatures($args, $simple = true) {
DBG::log("typeName({$args['xsd:type']})");
$acl = $this->getAclFromTypeName($args['xsd:type']);
DBG::log([ 'msg'=>"typeName({$args['xsd:type']}) - acl(".get_class($acl).")", '$acl'=>$acl ]);
$fldList = $this->_getFieldListFromAcl($acl);
$baseNsUri = Api_WfsNs::getBaseWfsUri();
$rootWfsNs = 'p5';
$rootWfsNsUri = "{$baseNsUri}";
$wfsNs = $args['typePrefix'];
$wfsNsUri = "{$baseNsUri}/" . ('p5_' == substr($args['typePrefix'], 0, 3)) ? substr($args['typePrefix'], 3) : $args['typePrefix'];
$featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&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;
}
}
}
DBG::log("ogcFilter(" . strlen($args['ogc:filter']) . "): {$args['ogc:filter']}");
$searchParams = array();
$searchParams['limit'] = $args['limit'];
$searchParams['limitstart'] = $args['offset'];
if (!empty($args['sortBy'])) {
$searchParams['sortBy'] = $args['sortBy'];
} else {
$searchParams['order_by'] = $acl->getPrimaryKeyField();
$searchParams['order_dir'] = 'DESC';
}
if (strlen($args['ogc:filter']) > 0) $searchParams['ogc:Filter'] = $args['ogc:filter'];
if (!empty($args['filterFields'])) $searchParams['cols'] = $args['filterFields'];// propertyName
if (!empty($args['primaryKey'])) $searchParams['primaryKey'] = $args['primaryKey'];// featureID
if (!empty($args['bbox'])) $searchParams['f_the_geom'] = "BBOX:{$args['bbox']}";
DBG::log([ 'msg'=>"getTotal() - searchParams", '$searchParams'=>$searchParams ]);
$totalItems = $acl->getTotal($searchParams);
DBG::log([ 'msg'=>"getTotal() - total", '$totalItems'=>$totalItems ]);
$xmlWriter = new XMLWriter();
if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
$xmlWriter->openUri('php://output');
$xmlWriter->setIndent(true);
$xmlWriter->startDocument('1.0','UTF-8');
$xmlWriter->startElement('wfs:FeatureCollection');
$xmlWriter->writeAttribute('xmlns:wfs', 'http://www.opengis.net/wfs/2.0');
$xmlWriter->writeAttribute('xmlns', 'http://www.opengis.net/wfs/2.0');
$xmlWriter->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
$xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
// $xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
$xmlWriter->writeAttribute('numberMatched', $totalItems);
$xmlWriter->writeAttribute('numberReturned', 0);
// $xmlWriter->writeAttribute('timeStamp', "TODO: timestamp like '2011-12-09T11:30:16'");
$xmlWriter->endElement();// wfs:FeatureCollection
$xmlWriter->endDocument();
exit;
}
public function getFeatures($args, $simple = true) {
$type = $args['typeName'];
DBG::log("typeName({$args['xsd:type']})");
$acl = $this->getAclFromTypeName($args['xsd:type']);
DBG::log([ 'msg'=>"typeName({$args['xsd:type']}) - acl(".get_class($acl).")", '$acl'=>$acl ]);
$fldList = $this->_getFieldListFromAcl($acl);
$baseNsUri = Api_WfsNs::getBaseWfsUri();
$rootWfsNs = 'p5';
$rootWfsNsUri = "{$baseNsUri}";
$wfsNs = $args['typePrefix'];
$wfsNsUri = "{$baseNsUri}/" . substr($args['typePrefix'], 3);
$featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$args['xsd:type']}&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;
}
}
}
DBG::log("ogcFilter(" . strlen($args['ogc:filter']) . "): {$args['ogc:filter']}");
$searchParams = array();
$searchParams['limit'] = $args['limit'];
$searchParams['limitstart'] = $args['offset'];
if (!empty($args['sortBy'])) {
$searchParams['sortBy'] = $args['sortBy'];
} else {
$searchParams['order_by'] = $acl->getPrimaryKeyField();
$searchParams['order_dir'] = 'DESC';
}
if (strlen($args['ogc:filter']) > 0) $searchParams['ogc:Filter'] = $args['ogc:filter'];
if (!empty($args['filterFields'])) $searchParams['cols'] = $args['filterFields'];// PropertyName
if (!empty($args['primaryKey'])) $searchParams['primaryKey'] = $args['primaryKey'];// featureID
if (!empty($args['bbox'])) $searchParams['f_the_geom'] = "BBOX:{$args['bbox']}";
DBG::log([ 'msg'=>'getItems - $searchParams', '$searchParams'=>$searchParams ]);
$items = $acl->getItems($searchParams);
DBG::log([ 'msg'=>'getItems - $items', '$items'=>$items ]);
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->writeAttribute('xmlns:gml', 'http://www.opengis.net/gml');
$xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
$xmlWriter->writeAttribute("xmlns:{$wfsNs}", $wfsNsUri);
if (!$simple) $xmlWriter->writeAttribute("xmlns:{$rootWfsNs}", $rootWfsNsUri);
$xmlWriter->writeAttribute('xsi:schemaLocation', "{$wfsNsUri} {$featureTypeUri}");
DBG::log([ 'msg'=>'$geomFld - getFieldType', 'getFieldType'=>$acl->getFieldType($geomFld) ]);
$tblName = $acl->getName();
foreach ($items as $itemKey => $item) {
if (!is_array($item)) $item = (array)$item;
if (!empty($geomFld)) DBG::log(['msg'=>"item[{$itemKey}] ({$geomFld})isEmpty(".empty($item[$geomFld])."):", '$item['.$geomFld.']'=>$item[$geomFld]]);
DBG::log([ 'msg'=>">>> loop({$itemKey})", '$item'=>$item ]);
$xmlWriter->startElement('gml:featureMember');
$xmlWriter->startElement("{$wfsNs}:{$type}");
$xmlWriter->writeAttribute('fid', "{$type}.{$itemKey}");
if (!$simple) $xmlWriter->writeAttribute("{$rootWfsNs}:web_link", Request::getPathUri() . "index.php?_route=ViewTableAjax&namespace=" . $acl->getNamespace() . "#EDIT/{$itemKey}");
foreach ($fldList as $idZasob => $fldName) {
DBG::log([ 'msg'=>">>> loop({$itemKey}) item({$item['ID']}) fld({$fldName})", '$item'=>$item[$fldName] ]);
$fldType = $acl->getXsdFieldType($fldName);
if (!$acl->canReadObjectField($fldName, (object)$item)) continue;
if ($geomFld != null && $fldName == $geomFld) {
$xmlWriter->startElement("{$wfsNs}:{$fldName}");
if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
$xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
}
if (!$simple && !$acl->canWriteObjectField($fldName, (object)$item)) {
$xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
}
$this->_typeConverter->createGmlFromWkt_xmlWriter($item[$fldName], $xmlWriter);
$xmlWriter->endElement();// {$wfsNs}:{$fldName}
} else if (is_array($item[$fldName])) {// TODO: by struct - REF field
DBG::log([ 'msg'=>">>> loop({$itemKey}) REF item[{$itemKey}][{$fldName}]", '$item'=>$item[$fldName] ]);
if (1 == count($item[$fldName])) {
$xlink = $item[$fldName][0]['xlink'];
$xlinkParts = explode(':', $xlink);
if (2 != count($xlinkParts)) throw new Exception("Error Processing Request - wrong xlink format for ".$acl->getName().".{$itemKey}/{$fldName}");
$xlinkParts[0] = Api_WfsNs::getNsUri($xlinkParts[0]);
$xlink = implode('#', $xlinkParts);
$xmlWriter->startElement("{$wfsNs}:{$fldName}");
if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
$xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
}
if (!$simple && !$acl->canWriteObjectField($fldName, (object)$item)) {
$xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
}
$xmlWriter->writeAttribute('xlink:href', $xlink);
$xmlWriter->endElement();// {$wfsNs}:{$fldName}
} else {
throw new Exception("Error Processing Request - too many refs for ".$acl->getName().".{$itemKey}/{$fldName}");
}
} else {
$value = str_replace('&', '&', $item[$fldName]);
if (empty($value) && '0' !== $value) {
continue;
} else {
$xmlWriter->startElement("{$wfsNs}:{$fldName}");
if (!$simple && !$acl->canReadObjectField($fldName, (object)$item)) {
$xmlWriter->writeAttribute("{$rootWfsNs}:allow_read", "false");
}
if (!$simple && !$acl->canWriteObjectField($fldName, (object)$item)) {
$xmlWriter->writeAttribute("{$rootWfsNs}:allow_write", "true");
}
$xmlWriter->text($value);
$xmlWriter->endElement();// {$wfsNs}:{$fldName}
}
}
}
$xmlWriter->endElement();// {$wfsNs}:{$type}
$xmlWriter->endElement();// gml:featureMember
}
$xmlWriter->endElement();// wfs:FeatureCollection
$xmlWriter->endDocument();
exit;
}
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 describeFeatureTypeAdvancedAction() {
$typeName = V::geti('TYPENAME', '', $_REQUEST);
if (empty($typeName)) {
$reqContent = Request::getRequestBody();
if (!empty($reqContent)) {
return $this->_parseDescribeFeatureTypeRequest($reqContent, $simple = false);
} else {
return $this->_getDescribeFeatureAllTypes($simple = false);
}
//throw new HttpException("Wrong param TYPENAME", 400);
}
if (false === strpos($typeName, ':')) throw new HttpException("Wrong param TYPENAME", 400);
list($nsPrefix, $name) = explode(':', $typeName);
if ('/@instance' == strtolower(substr($name, -1 * strlen('/@instance')))) {
return $this->_describeInstanceAttributeTable($nsPrefix, substr($name, 0, -1 * strlen('/@instance')));
}
return $this->_getDescribeFeatureType($nsPrefix, $name, $simple = false);
}
public function getCapabilitiesAction() {
$wfsServerUrl = $this->getBaseUri();
$serviceTitle = "Web Feature Service";
$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);
}
}