Пример #1
0
function createGeoJSON($layer_tablename,$column_names,$file='',$Fixed_Columns='',$projection='900913'){
	require_once 'json/include_geom.php';
	require_once 'json/geojson.php';
	require_once 'json/wkt.php';
	if ($Fixed_Columns == '') {
		$Fixed_Columns = AUTO_DBCOL_PREFIX."id, astext(ST_Transform(".AUTO_DBCOL_PREFIX."topology,". $projection .")) as topology, ";
	}
	$total_column_names =  $Fixed_Columns . $column_names;
	$sql = 'select '.$total_column_names.' from  "'. $layer_tablename . '"';
	$query_args = array($total_column_names, $layer_tablename);
	$strgeoJSON='{"type": "FeatureCollection", "features": [';
	$str='';
	$arr = explode(",",$column_names);
	$cnt = count($arr);
	$data = db_query($sql);
	while($layer_obj = db_fetch_object($data)){
		$geom_wkt = $layer_obj->topology;
		$wkt = new WKT();
		$geom = $wkt->read($geom_wkt);
		$strgeoJSON .= '{"geometry": ' . json_encode($geom->getGeoInterface()) . ', ';
		$strgeoJSON .= '"type": "Feature", ';
		$strgeoJSON .= '"id": '.$layer_obj->{AUTO_DBCOL_PREFIX."id"}.', ';
		$strgeoJSON .= '"properties": {';
		for($i=0;$i<$cnt;$i++){
			$strgeoJSON .= '"'. trim($arr[$i]) .'": "' . $layer_obj->$arr[$i] .'",';
		}
		$strgeoJSON = substr_replace($strgeoJSON,"",-1);
		$strgeoJSON .= '}},';
	}
	$strgeoJSON = substr_replace($strgeoJSON,"",-1);
	$strgeoJSON .= ']}';
	if ($file != '') {
		$myFile = $file;
	}else{
		$myFile = "json/".$layer_tablename .".json";
	}

	$fh = fopen($myFile, 'w') or die("can't open file");

	fwrite($fh, $strgeoJSON);
	fclose($fh);

	return $myFile;
}
Пример #2
0
/**
 * Read WKT string into geometry objects.
 *
 * @param string $text A WKT string.
 *
 * @return Geometry|GeometryCollection.
 */
function treasurehunt_wkt_to_object($text)
{
    $wkt = new WKT();
    return $wkt->read($text);
}