#!/usr/bin/python

import string

#####################

def generate_map():
	text = """Content-type: text/html

<!doctype html public "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">

<html>
<head>
<title>
script output
</title>
<meta name="Author" content="Dave Pape">
</head>

<body>
<h1>
Script results
</h1>

<p>lat = %s - %s</p>
<p>lon = %s - %s</p>
<p>resolution scale = '%s'</p>
<p>size = '%s'</p>
<p>exaggeration = '%s'</p>
<p>units scale = '%s'</p>
<p>texmapfile = '%s'</p>
<p>texres = '%s'</p>
<p>compress = '%s'</p>

<p></p><hr>
<div>
Last modified 1 October 2000.
</div>
<address>
<a href="/pape/">Dave Pape</a>, <a href="mailto:pape@evl.uic.edu">pape@evl.uic.edu</a>
</address>

</body>
</html>
"""
	lat0 = latitude - size / 2
	if lat0 < -90: lat0 = -90
	if lat0 > 90-size: lat0 = 90 - size
	lat1 = lat0 + size
	
	lon0 = longitude - size / 2
	if lon0 < -180: lon0 = -180
	if lon0 > 180-size: lon0 = 180 - size
	lon1 = lon0 + size
	
	print text % (lat0,lat1,lon0,lon1,scale,size,exaggeration,unitsScale,texmapfile,texres,compress)



#####################

def estimate_size(size,scale):
	text = """Content-type: text/html

<!doctype html public "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">

<html>
<head>
<title>
size estimate
</title>
<meta name="Author" content="Dave Pape">
</head>

<body>
<h1>
Size estimate
</h1>
The resulting VRML file will contain approximately %d polygons
and be %d kilobytes (roughly %d kilobytes compressed).
<p>
It should take about 6 seconds to generate the file (assuming
no heavy load on the server).

<p></p><hr>
<div>
Last modified 1 October 2000.
</div>
<address>
<a href="/pape/">Dave Pape</a>, <a href="mailto:pape@evl.uic.edu">pape@evl.uic.edu</a>
</address>

</body>
</html>
"""
	npolys = size * size * 288 / scale / scale
	kbytes = npolys * 28 / 10 / 1000
	kbytes_c = npolys * 9 / 10 / 1000
	print text % (npolys, kbytes, kbytes_c)


#####################

import cgi
form = cgi.FieldStorage()

# Get all form values, assigning defaults for unset options

if form.has_key('location.x'):
	x = string.atoi(form['location.x'].value)
	longitude = x * 360 / 512 - 180
else:
	longitude = 0
if form.has_key('location.y'):
	y = string.atoi(form['location.y'].value)
	latitude = 90 - ( y * 180 / 256 )
else:
	latitude = 0

resolutionDict = { '5 minute': 1, '10 minute': 2, '15 minute': 3, '30 minute': 6, '1 degree': 12, '2 degree': 24 }
if form.has_key('resolution'):
	scale = resolutionDict[form['resolution'].value]
else:
	scale = 3

sizeDict = { '5 degrees': 5, '10 degrees': 10, '20 degrees': 20, '30 degrees': 30, '45 degrees': 45 }
if form.has_key('size'):
	size = sizeDict[form['size'].value]
else:
	size = 10

exaggerationDict = { '1x': 1, '5x': 5, '10x': 10, '20x': 20, '30x': 30 }
if form.has_key('exaggeration'):
	exaggeration = exaggerationDict[form['exaggeration'].value]
else:
	exaggeration = 1

unitsDict = { 'meters': 1, 'kilometers': .001, 'miles': .0006214 }
if form.has_key('size'):
	unitsScale = unitsDict[form['units'].value]
else:
	unitsScale = 1

if form.has_key('texmapfile'):
	texmapfile = form['texmapfile'].value
else:
	texmapfile = 0

texresDict = { '64x64': 64, '128x128': 128, '256x256': 256, '512x512': 512 }
if form.has_key('texres'):
	texres = texresDict[form['texres'].value]
else:
	texres = 256

if form.has_key('compress'):
	compress = form['compress'].value
else:
	compress = 0

if form.has_key('compute'):
	compute = form['compute'].value
else:
	compute = 0

if compute:
	estimate_size(size,scale)
else:
	generate_map()

