Location: PHPKode > projects > AppForMap > appformap/wfs/wfs.php
<?
header('Content-type: text/xml', true);
include("../libs/phpgeometry_class.php");
include ("../functions.php");
include ("../conf/maplinkconfig.php");
$postgisObject = new postgis();
$postgisObject->open();
//added NR

//end added
include('convertgeom.php');
//: $data = $GLOBALS["HTTP_RAW_POST_DATA"];
$thePath= "http://".$SERVER_NAME.$PHP_SELF;
$server="http://".$SERVER_NAME;
$BBox=null;
//added NR
$disjoints=array();
$NotDisjoints=array();
$disjointCoords=array();
$notDisjointCoords=array();
//end added
$currentTable=null;
$currentTag=null;
$gen=array();
$gen[0]="";
$level=0;
$depth=0;
$tables=array();
$fields=array();
$wheres=array();
$limits=array();

function getCapabilities($postgisObject)
{
	global $thePath;
	global $db;
	include('capabilities.php');
}
function intersects($Box)
{
	$Box=str_replace (","," ", $Box);
	$coords = explode(" ", $Box);
	$XMin=$coords[0];
	$YMin=$coords[1];
	$XMax=$coords[2];
	$YMax=$coords[3];
	return "((XMin BETWEEN  $XMin  AND  $XMax ) AND (YMin BETWEEN  $YMin  AND  $YMax )) OR ((XMin BETWEEN  $XMin  AND  $XMax ) AND (YMax BETWEEN  $YMin  AND  $YMax )) OR ((XMax BETWEEN  $XMin  AND  $XMax ) AND (YMin BETWEEN  $YMin  AND  $YMax )) OR ((XMax BETWEEN  $XMin  AND  $XMax ) AND (YMax BETWEEN  $YMin  AND  $YMax ))  OR ((XMin <  $XMin ) AND (YMin <  $YMin ) AND (XMax >  $XMax ) AND (YMax >  $YMax )) OR ((XMin BETWEEN  $XMin  AND  $XMax ) AND (YMin <  $YMin ) AND (YMax >  $YMax )) OR ((XMax BETWEEN  $XMin  AND  $XMax ) AND (YMin <  $YMin ) AND (YMax >  $YMax )) OR ((YMin BETWEEN  $YMin  AND  $YMax ) AND (XMin <  $XMin ) AND (XMax >  $XMax )) OR ((YMax BETWEEN  $YMin  AND  $YMax ) AND (XMin <  $XMin ) AND (XMax >  $XMax ))";
}
function getXSD($postgisObject)
{
	global $server;
	global $depth;
	global $db;
	global $tables;
	include('XSD.php');
}
function doQuery($queryType)
{
	global $currentTag;
	global $BBox;
	global $tables;
	global $fields;
	global $values;
	global $wheres;
	global $limits;
	//added NR
	global $disjoints;
	global $notDisjoints;
	global $disjointCoords;
	global $notDisjointCoords;
	global $WKTfilters;
	global $filterPropertyNames;
	global $postgisObject;
	global $sridOfFilter;
	//end added
	switch ($queryType)
	{
		case "Select":
		foreach($tables as $table)
		{
			$sql="SELECT ";
			if (!(empty($fields[$table])))
			{
				$sql = $sql. $fields[$table];
				$sql=substr($sql, 0, strlen($sql) - 1);
			}
			else
			{
				$sql .= " * ";
			}
			$sridOfTable=$postgisObject->getGeometryColumns($table, "srid");
			$sridOfFilter=$WKTfilters[1][$filterPropertyNames[$table]];
			if ($sridOfFilter=="") $sridOfFilter=$sridOfTable;
			$sql.=",asText(transform(the_geom,".$sridOfFilter.")) as geometry ";
			$sql2 = "SELECT xmin(EXTENT(the_geom)) AS TXMin,xmax(EXTENT(the_geom)) AS TXMax, ymin(EXTENT(the_geom)) AS TYMin,ymax(EXTENT(the_geom)) AS TYMax ";
			$from = " FROM ".$table;
			if ((!(empty($BBox)))||(!(empty($wheres[$table])))||(sizeof($disjointCoords))||(sizeof($notDisjointCoords))){
				$from .= " WHERE ";
			}
			if (!(empty($BBox)))
			{
				$from .= intersects($BBox)." AND ";
			}
			if (sizeof($notDisjoints)){
				for($i=0;$i<sizeof($notDisjoints);$i++)
				{
					foreach($notDisjoints[$i] as $notDisjoint)
					{
						if($notDisjoint==$table){
							$from .= " (".intersects($notDisjointCoords[$i]).") AND ";
							break;
						}
					}
				}
			}
			if (sizeof($disjoints))
			{
				for($i=0;$i<sizeof($disjoints);$i++)
				{
					foreach($disjoints[$i] as $disjoint)
					{
						if($disjoint==$table){
							$from .= " (NOT(".intersects($disjointCoords[$i]).")) AND ";
							break;
						}
					}
				}
			}
			if (!(empty($wheres[$table])))
			{
				$from .= " (" . $wheres[$table] . ")";

			}
			if ((!(empty($BBox)))||(!(empty($wheres[$table])))||(sizeof($disjointCoords))||(sizeof($notDisjointCoords)))
			{
				$from=dropLastChrs($from,5);
				$from.=")";
			}
			if (!(empty($limits[$table])))
			{
				//$from .= " LIMIT " . $limits[$table];
			}
			//echo $filterPropertyNames[$table];
			if (sizeof($filterPropertyNames[$table]))
			{
			$from.= " where intersects"
			."(transform(GeometryFromText('".$WKTfilters[0][$filterPropertyNames[$table]]."',"
			.$sridOfFilter
			."),$sridOfTable),"
			."the_geom)";
			}
			doSelect($table,$sql,$sql2,$from);
		}
		break;
		case "Insert":
		$sql="INSERT INTO " . $table;
		//echo $sql;
		break;
		default:
		break;
	}
}

//added NR
function disjoint($data,$Not)
{
	global $disjoints;
	global $notDisjoints;
	$posOr = strpos($data, "|");
	if($Not==true){
		$num=count($notDisjoints);
		$notDisjoints[$num]=array();
		if(!(empty($posOr))){
			$tables = explode("|", $data);
			foreach($tables as $table){
				$posSlash = strpos($table, "/");
				$table = substr($table, 0, $posSlash);
				$notDisjoints[$num][]=$table;
			}
		}
		else{
			$posSlash = strpos($data, "/");
			$table = substr($data, 0, $posSlash);
			$notDisjoints[$num][]=$table;
		}
	}
	else{
		$num=count($disjoints);
		$disjoints[$num]=array();
		if(!(empty($posOr))){
			$tables = explode("|", $data);
			foreach($tables as $table){
				$posSlash = strpos($table, "/");
				$table = substr($table, 0, $posSlash);
				$disjoints[$num][]=$table;
			}
		}
		else{
			$posSlash = strpos($data, "/");
			$table = substr($data, 0, $posSlash);
			$disjoints[$num][]=$table;
		}
	}
}

function genBBox($XMin,$YMin,$XMax,$YMax)
{
	global $depth;
	global $tables;
	global $db;
	writeTag("open","gml","boundedBy",null,True,True);
	$depth++;
	writeTag("open","gml","Box",null,True,True);
	$depth++;
	writeTag("open","gml","coordinates",null,True,False);
	print $XMin.",".$YMin." ".$XMax.",".$YMax;
	writeTag("close","gml","coordinates",null,False,True);
	$depth--;
	writeTag("close","gml","Box",null,True,True);
	$depth--;
	writeTag("close","gml","boundedBy",null,True,True);
}
function doSelect($table,$sql,$sql2,$from)
{
	global $db;
	global $depth;
	global $postgisObject;
	global $sridOfFilter;
	$sql.=$from;
	$sql2.=$from;
	//echo $sql;
	//echo $sql2;
	$result = $postgisObject->execQuery($sql2);

	while ($myrow = pg_fetch_array($result))
	{
		if(!(empty($myrow["txmin"])))
		{
			//added NR
			genBBox($myrow["txmin"],$myrow["tymin"],$myrow["txmax"],$myrow["tymax"]);
		}
		else
		{
			return;
		}
	}
	$result = $postgisObject->execQuery($sql);
	while ($myrow = pg_fetch_array($result)){
		writeTag("open","gml","featureMember",null,True,True);
		$depth++;
		writeTag("open",null,$table,null,True,True);
		$depth++;
		for ($i=0;($i < pg_num_fields ($result)) ;$i++)
		{
			$FieldName=pg_field_name($result, $i);
			$FieldValue=$myrow[$FieldName];
			if (($FieldName!="geometry") && ($FieldName!="the_geom") &&($FieldName!="txmin")&&($FieldName!="tymin")&&($FieldName!="txmax")&&($FieldName!="tymax")){
				writeTag("open",null,$table.".".$FieldName,null,True,False);
				echo $FieldValue;
				writeTag("close",null,$table.".".$FieldName,null,False,True);
			}
			elseif ($FieldName=="geometry")
			{
				writeTag("open",null,$table.".".$FieldName,null,True,True);
				$depth++;
				$__geofactory=new geometryfactory;
				$__geoObj=$__geofactory->createGeometry($myrow[$FieldName],$sridOfFilter);
				echo $__geoObj->getGML();
				//convertGeom($FieldValue);
				$depth--;
				writeTag("close",null,$table.".".$FieldName,null,True,True);
			}
		}
		$depth--;
		writeTag("close",null,$table,null,True,True);
		$depth--;
		writeTag("close","gml","featureMember",null,True,True);
	}

}
function dropLastChrs($str,$no) {
	$strLen=strlen($str);
	return (substr($str,0,($strLen)-$no));
}
if (sizeof($_POST) > 0)
{
	$HTTP_FORM_VARS = $_POST;
}
else
{
	if (sizeof($_GET) > 0)
	{
		$HTTP_FORM_VARS = $_GET;
	}
	else
	$HTTP_FORM_VARS = array("");
}
// get name/value pairs
$tables=explode(",",$HTTP_FORM_VARS["typename"]);
$properties=explode(",",$HTTP_FORM_VARS["propertyname"]);
$featureids=explode(",",$HTTP_FORM_VARS["featureid"]);
$filterGML=$HTTP_FORM_VARS["filter"];
$filterGML = str_replace("(","",$filterGML);
$filterGML = str_replace(")","",$filterGML);
$filterGML=urldecode($filterGML);
$filterGML = "<Filters>".$filterGML."</Filters>";

if (!(empty($filterGML)))
{

	$__con=new gmlConverter;
	$filterGML=str_replace("\\","",$filterGML);
	//echo "test".$filterGML;
    $WKTfilters=$__con->gmlToWKT($filterGML,"FILTER");
    $filterPropertyNames=$__con->filterPropertyName;
   //print_r($WKTfilters);
}
if (!(empty($properties[0])))
foreach ($properties as $property)
{
	$__u=explode("/",$property);
	foreach ($tables as $table)
	{
		if ($table==$__u[0])
		{
			$fields[$table].=$__u[1].",";
		}
	}
}
if (!(empty($featureids[0])))
{
	foreach ($featureids as $featureid)
	{
		$__u=explode(".",$featureid);
		foreach ($tables as $table)
		{
			if ($table==$__u[0])
			{
				$wheres[$table].=" gid=".$__u[1]." or ";
			}
		}
	}
}
//get the request
switch (strtoupper($HTTP_FORM_VARS["request"]))
{
	case "GETCAPABILITIES":
	getCapabilities($postgisObject);
	break;
	case "GETFEATURE":
	print '<wfs:FeatureCollection';
	print ' xmlns:wfs="http://www.opengis.net/wfs"';
	print ' xmlns:gml="http://www.opengis.net/gml">';
	doQuery("Select");
	print '</wfs:FeatureCollection>';
	break;
	case "DESCRIBEFEATURETYPE":
	getXSD($postgisObject);
	break;
	default:
	break;
}
?>
Return current item: AppForMap