<?php
# 
# renderWKT.php - (c) 2008 Dan "Ducky" Little
# Written for the OpenMNND Organization
#
# This inputs some basic styles, map coordinates, and a wkt string to draw an object
#
#

# Include GeoMOOSE PHP Utility Function Library
include('geomoose_php_util.php');
include('Configuration.php');

# Make the PHP Script Shut Up, but still give something useful if there
# is a legit error or parsing bug.
error_reporting(E_ERROR | E_PARSE);


#$queryId = $_REQUEST['query'];

$wkt = $_REQUEST['wkt'];
$buffer = $_REQUEST['buffer']; # The shape can also be buffered!

$lineWidth = $_REQUEST['linewidth'];	# In pixels
$lineColor = $_REQUEST['linecolor'];	# In mapserver RGB triplet (255 0 0)
$fillColor = $_REQUEST['fillcolor'];	# In mapserver RGB triplet (0 255 0)

$title = $_REQUEST['title'];

# This is so that the script responds like mapserver
$extent = $_REQUEST['mapext'];
$imagesize = $_REQUEST['mapsize'];

$extent = split(' ',$extent);

#header('Content-type: image/gif')

$shape = ms_shapeObjFromWkt($wkt);

# Prevent negative and 0 buffers
if($buffer > 0) { $shape = $shape->buffer($buffer); }

# Open the mapfile
$map = ms_newMapObj('resources/renderWKT.map');

# Set up the extents and the mapsize
$map->setExtent($extent[0], $extent[1], $extent[2], $extent[3]);
if(isset($imagesize)) {
	$imagesize = split(' ',$imagesize);
	$map->setSize($imagesize[0],$imagesize[1]);
} else {
	$map->setSize(500,500);
}

$layer = $map->getLayer(0);

# Set the layer type to match the object that will be drawn
if($shape->{type} == MS_SHAPE_POINT) {
	$layer->set('type', MS_LAYER_POINT);
	if(isset($_REQUEST['symbol'])) {
		$cl = $layer->getClass(0);
		$st = $cl->getStyle(1);
		$st->set('symbol', $map->getsymbolbyname($_REQUEST['symbol']));
#		print $_REQUEST['symbol'];
	}
} elseif ($shape->{type} == MS_SHAPE_LINE) {
	$layer->set('type', MS_LAYER_LINE);
} elseif ($shape->{type} == MS_SHAPE_POLYGON) {
	$layer->set('type', MS_LAYER_POLYGON);
}

# Now setup the coloring from the CGI request
$class = $layer->getClass(0); # there can be only 1
if(isset($title)) {
	$class->settext($title);
}

if(isset($lineWidth)) {
	$class->getStyle(1)->set('size', $lineWidth);
}

if(isset($lineColor)) {
	$lineColor = split(' ', $lineColor);
	$class->getStyle(1)->outlinecolor->setRGB($lineColor[0],$lineColor[1],$lineColor[2]);
}

if(isset($fillColor)) {
	$fillColor = split(' ', $fillColor);
	$class->getStyle(0)->color->setRGB($fillColor[0],$fillColor[1],$fillColor[2]);
}

# Points only get one style applied to them, so this makes sure the point is filled properly
if($shape->{type} == MS_SHAPE_POINT) {
	if(isset($fillColor)) {
		$class->getStyle(1)->color->setRGB($fillColor[0],$fillColor[1],$fillColor[2]);
	}

}


$layer->addFeature($shape);
$image= $map->prepareImage();
$image = $map->draw();

header('Content-type: image/gif');
#$layer->{status} = MS_OFF;
$image->saveImage(''); # No file name means it goes to stdout
#$image->saveImage();

#print "completed";
#print "<br>";
#print $shape->toWkt();
#readfile('/tmp/www/out.gif');
#readfile("../shared/images/blank.gif");

?>
