Просмотр исходного кода

added resultType=hits to WFS API

Piotr Labudda 9 лет назад
Родитель
Сommit
d28dd38451
1 измененных файлов с 97 добавлено и 16 удалено
  1. 97 16
      SE/se-lib/Api/WfsDataServer.php

+ 97 - 16
SE/se-lib/Api/WfsDataServer.php

@@ -54,16 +54,17 @@ class Api_WfsDataServer extends Api_WfsServerBase {
 		$propertyName = V::get('propertyName', '', $_REQUEST);
 		$propertyName = trim($propertyName);
 		$srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
-		if (count($typeEx) == 2) {
-			if (empty($ogcFilter)) {// read ogc:Filter from POST body if not defined in GET
-				$reqBody = Request::getRequestBody();
-				if (!empty($reqBody)) {
-					$ogcFilter = $this->convertOgcFilterFromRequestBody($reqBody);
-				}
+		if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
+		if (empty($ogcFilter)) {// read ogc:Filter from POST body if not defined in GET
+			$reqBody = Request::getRequestBody();
+			if (!empty($reqBody)) {
+				$ogcFilter = $this->convertOgcFilterFromRequestBody($reqBody);
 			}
-		return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex, $propertyName, $simple = true);
+		}
+		if ('hits' == V::get('resultType', '', $_REQUEST)) {// resultType=hits
+			return $this->getTotalFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex, $propertyName, $simple = true);
 		} else {
-			throw new HttpException("Wrong param TYPENAME", 400);
+			return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex, $propertyName, $simple = true);
 		}
 	}
 
@@ -80,16 +81,17 @@ class Api_WfsDataServer extends Api_WfsServerBase {
 		$propertyName = V::get('propertyName', '', $_REQUEST);
 		$propertyName = trim($propertyName);
 		$srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
-		if (count($typeEx) == 2) {
-			if (empty($ogcFilter)) {// read ogc:Filter from POST body if not defined in GET
-				$reqBody = Request::getRequestBody();
-				if (!empty($reqBody)) {
-					$ogcFilter = $this->convertOgcFilterFromRequestBody($reqBody);
-				}
+		if (count($typeEx) != 2) throw new HttpException("Wrong param TYPENAME", 400);
+		if (empty($ogcFilter)) {// read ogc:Filter from POST body if not defined in GET
+			$reqBody = Request::getRequestBody();
+			if (!empty($reqBody)) {
+				$ogcFilter = $this->convertOgcFilterFromRequestBody($reqBody);
 			}
-			return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex, $propertyName, $simple = false);
+		}
+		if ('hits' == V::get('resultType', '', $_REQUEST)) {// resultType=hits
+			return $this->getTotalFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex, $propertyName, $simple = false);
 		} else {
-			throw new HttpException("Wrong param TYPENAME", 400);
+			return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex, $propertyName, $simple = false);
 		}
 	}
 
@@ -110,6 +112,85 @@ class Api_WfsDataServer extends Api_WfsServerBase {
 		}
 	}
 
+	public function getTotalFeatures($nsPrefix, $type, $maxFeatures, $srsname, $ogcFilter = '', $sortBy = '', $startIndex = 0, $propertyName = '', $simple = true) {
+		$DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
+		$typeName = "{$nsPrefix}:{$type}";
+		if($DBG){echo "typeName($typeName})\n";}
+		$acl = $this->getAclFromTypeName($typeName);
+		$fldList = $this->_getFieldListFromAcl($acl);
+
+		$baseNsUri = $this->getBaseNamespaceUri();
+		$rootWfsNs = 'p5';
+		$rootWfsNsUri = "{$baseNsUri}";
+		//$wfsNs = 'p5_default_db_' . $type;//$nsPrefix;
+		$wfsNs = $nsPrefix;//'p5_default_db';
+		$wfsNsUri = "{$baseNsUri}/" . substr($nsPrefix, 3);
+		$featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$typeName}&REQUEST=DescribeFeatureType";
+
+		// get BBox from geom_field (only one geom fld is allowed)
+		$geomFld = null;
+		{
+			foreach ($fldList as $idZasob => $fldName) {
+				if ($acl->isGeomField($fldName)) {
+					$geomFld = $fldName;
+				}
+			}
+		}
+
+		if($DBG){echo "ogcFilter(" . strlen($ogcFilter) . "): {$ogcFilter}\n";}
+		$searchParams = array();
+		$searchParams['limit'] = $maxFeatures;
+		$searchParams['limitstart'] = $startIndex;
+		if (!empty($sortBy)) {
+			$searchParams['sortBy'] = $sortBy;
+		} else {
+			$searchParams['order_by'] = $acl->getPrimaryKeyField();
+			$searchParams['order_dir'] = 'DESC';
+		}
+		if (strlen($ogcFilter) > 0) $searchParams['ogc:Filter'] = $ogcFilter;
+		if (strlen($propertyName) > 0) {
+			$propertyNamesEx = explode(',', $propertyName);
+			$onlyCols = array();
+			foreach ($propertyNamesEx as $colName) {
+				$colName = trim($colName);
+				$onlyCols[] = $colName;
+			}
+			if (!empty($onlyCols)) $searchParams['cols'] = $onlyCols;
+		}
+		{// 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);// ?
+				}
+			}
+		}
+		if($DBG){echo 'getItems() searchParams:';print_r($searchParams);echo "\n";}
+		$totalItems = $acl->getTotal($searchParams);
+		if($DBG){echo 'totalItems:';print_r($totalItems);echo "\n";}
+
+		$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($nsPrefix, $type, $maxFeatures, $srsname, $ogcFilter = '', $sortBy = '', $startIndex = 0, $propertyName = '', $simple = true) {
 		$DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
 		$typeName = "{$nsPrefix}:{$type}";