# Display requested area of file roads.shp in a PNG file.
#
let imageFile = tempname("PNG")
newpage "png", imageFile, 100, 100

dataset "shapefile", "/tmp/roads.shp", "dbffields=FEATURE"
if X1 eq ""
then
	let X1 = Mapyrus.dataset.min.x
	let Y1 = Mapyrus.dataset.min.y
	let X2 = Mapyrus.dataset.max.x
	let Y2 = Mapyrus.dataset.max.y
endif

worlds X1, Y1, X2, Y2
while Mapyrus.fetch.more
do
	clearpath
	fetch
	addpath GEOMETRY

	# Display different classes of roads in different colors.
	#
	if FEATURE eq 'Principal Highway'
	then
		color "red"
	else
		color "black"
	endif
	stroke
done

let X1 = Mapyrus.worlds.min.x
let Y1 = Mapyrus.worlds.min.y
let X2 = Mapyrus.worlds.max.x
let Y2 = Mapyrus.worlds.max.y
let xMid = (X1 + X2) / 2
let yMid = (Y1 + Y2) / 2

# Ratio of area being displayed to zoom or pan each time.
#
let panFactor = 0.2
let zoomFactor = 0.2

let xPan = panFactor * Mapyrus.worlds.width
let yPan = panFactor * Mapyrus.worlds.height
let xZoom = zoomFactor * Mapyrus.worlds.width
let yZoom = zoomFactor * Mapyrus.worlds.height

# Set URLs for navigating around current display.
#
let upURL = Mapyrus.filename . '?X1=' . X1 . '&Y1=' . (Y1 + yPan) . '&X2=' . X2 . '&Y2=' . (Y2 + yPan)
let downURL = Mapyrus.filename . '?X1=' . X1 . '&Y1=' . (Y1 - yPan) . '&X2=' . X2 . '&Y2=' . (Y2 - yPan)
let leftURL = Mapyrus.filename . '?X1=' . (X1 - xPan) . '&Y1=' . Y1 . '&X2=' . (X2 - xPan) . '&Y2=' . Y2
let rightURL = Mapyrus.filename . '?X1=' . (X1 + xPan) . '&Y1=' . Y1 . '&X2=' . (X2 + xPan) . '&Y2=' . Y2
let zoomInURL = Mapyrus.filename . '?X1=' . (X1 + xZoom) . '&Y1=' . (Y1 + yZoom) . '&X2=' . (X2 - xZoom) . '&Y2=' . (Y2 - yZoom)
let zoomOutURL = Mapyrus.filename . '?X1=' . (X1 - xZoom) . '&Y1=' . (Y1 - yZoom) . '&X2=' . (X2 + xZoom) . '&Y2=' . (Y2 + yZoom)

# Spool HTML file to standard output, replacing placeholder tokens
# with actual values for this request.
#
httpresponse "HTTP/1.0 200 OK
Content-Type: text/html; charset=UTF-8"

let r = spool("tutorialhtmlpage2.txt")
let r = replace(r, '@placeholder_title@', 'Real Title')
let r = replace(r, '@placeholder_coordinates@', xMid . ', ' . yMid)
let r = replace(r, '@placeholder_image@', imageFile)
let r = replace(r, '@placeholder_up_url@', upURL)
let r = replace(r, '@placeholder_down_url@', downURL)
let r = replace(r, '@placeholder_left_url@', leftURL)
let r = replace(r, '@placeholder_right_url@', rightURL)
let r = replace(r, '@placeholder_zoom_in_url@', zoomInURL)
let r = replace(r, '@placeholder_zoom_out_url@', zoomOutURL)
print r
