<?php
#
# createDrawing.php - (c) 2008 by Dan "Ducky" Little
# 
# This script is used in Conjunction with renderWKT.php
#


# 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);


$shapeType = $_REQUEST['shape'];	# point,line,polygon
$points = $_REQUEST['points'];		# should be a csv

### These essentially just get passed on to renderWKT.php ###

$keys = array('title','buffer','linewidth','linecolor','fillcolor','symbol');

$prefixes = array();
$prefixes['point'] = 'POINT(';
$prefixes['line'] = 'LINESTRING(';
$prefixes['polygon'] = 'POLYGON((';
$suffixes = array('point' => ')', 'line' => ')', 'polygon' => '))');

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

# Title is free text input, but if it's not title, return it with a 
# default title, but DO NOT pass it on to the drawing.
$title = 'Untitled Drawing';
if(isset($_REQUEST['title']) && $_REQUEST['title'] != '') {
	$title = $_REQUEST['title'];
}

if(!isset($opacity)) {
	$opacity = 75;		# Default to 75% opacity
}

$wktString = "";
$points = str_replace(' ',',',$points);
$points = split(',', $points);

$wktString = "";
$pointArray = array();
for($i = 0; $i < (count($points) - 1); $i+=2) {
	array_push($pointArray, $points[$i].' '.$points[$i+1]);
}
$wktString = join(',', $pointArray);

header('Content-type: text/xml');
print "<return title='create drawing'>\n";
print "<results title='create drawing'></results>\n";
print "<mapbook>";
print "<map default='true' title='".$title."' opacity='".$opacity."'>\n";

$scriptPath = "http://".$_SERVER['SERVER_NAME'].str_replace('createDrawing','renderWKT', $_SERVER['SCRIPT_NAME']);


#print "<param name='wkt' value='".$prefixes[$shapeType].$wktString.$suffixes[$shapeType]."'/>";

$paramString = '?mapext=%EXTENTS%&mapsize=%SIZE%&';
foreach($keys as $key) {
	if(isset($_REQUEST[$key])) {
		#print "<param name='".$key."' value='".$_REQUEST[$key]."'/>\n";
		$paramString = $paramString . $key. '=' . $_REQUEST[$key].'&';
	}
}
$paramString = $paramString . 'wkt=' . $prefixes[$shapeType].$wktString.$suffixes[$shapeType];
print "<file><![CDATA[".$scriptPath.$paramString."]]></file>\n";
print "</map>";
print "</mapbook>";
print "</return>";

?>
