import os
import string
import math

def GetImageDiagonalSize (alt, zoom):
	a = 0.0023
	b = -0.1230
	c = 1.5300
	z = float (zoom)
	zoomScale = a * z * z + b * z + c
	altf = float (alt)
	return (altf * zoomScale)

class WorldPos:
	def __init__(self, newlon, newlat):
		self.lon = float(newlon)
		self.lat = float(newlat)
	def OffsetByFeet(self, hd, offset):
		feetPerLat = 60.0 * 6086.988
		feetPerLon = feetPerLat * math.cos(self.lat * 3.1415926 / 180.0)
		headingRad = hd * 3.1415926 / 180.0
		changeLon = math.sin(headingRad) * offset / feetPerLon
		changeLat = math.cos(headingRad) * offset / feetPerLat;
		return (WorldPos (self.lon + changeLon, self.lat + changeLat))

outf = open ('test.kml', 'w')
outf.write ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
outf.write ("<kml xmlns=\"http://earth.google.com/kml/2.2\">")
outf.write ( "<Document>")

fileList = os.listdir('.')

for f in fileList:
	sp = string.split (f, '.')
	if (len(sp)>1 and sp[1] == 'JPG'):
		
		fnl = string.split(sp[0], ' ')
		lon = 0.0
		lonstr = fnl[2]
		lonlist = string.split(lonstr, '-')
		if (lonlist[0] == ''):
			lon = -float (lonlist[1]) - float (lonlist[2]) / 10000.0
		else:
			lon = float (lonlist[0]) + float (lonlist[1]) / 10000.0

		latstr = fnl[3]
		lat = 0.0
		latlist = string.split(latstr, '-')
		if (latlist[0] == ''):
			lat = float (latlist[1]) + float (latlist[2]) / 10000.0
		else:
			lat = float (latlist[0]) + float (latlist[1]) / 10000.0

		alt = fnl[4]
		hd = fnl[6]
		zoom = fnl[8]
		fov = GetImageDiagonalSize (alt, zoom)
		fovheight = 0.6 * fov
		fovwidth = 0.8 * fov
		center = WorldPos (lon, lat)

		northfacingtop = center.OffsetByFeet (0.0, (fovheight / 2.0))
		northfacingtopleft = northfacingtop.OffsetByFeet (-90.0, (fovwidth / 2.0))
		northfacingtopright = northfacingtopleft.OffsetByFeet (90.0, fovwidth)
		northfacingbottomleft = northfacingtopleft.OffsetByFeet (180.0, fovheight)

		boundeast = northfacingtopright.lon;
		boundwest = northfacingtopleft.lon;
		boundnorth = northfacingtopleft.lat;
		boundsouth = northfacingbottomleft.lat;

		bottom = center.OffsetByFeet (180.0, (fovheight / 2.0))
		left = center.OffsetByFeet (-90.0, (fovwidth / 2.0))
		right = center.OffsetByFeet (90.0, (fovwidth / 2.0))

#		for cornername in ["TL", "TR", "BL", "BR"]:
#			lon = 0.0
#			lon = 0.0
#			if (cornername == "TL"):
#				lon = topleft.lon
#				lat = topleft.lat
#			if (cornername == "TR"):
#				lon = topright.lon
#				lat = topright.lat
#			if (cornername == "BL"):
#				lon = bottomleft.lon
#				lat = bottomleft.lat
#			if (cornername == "BR"):
#				lon = bottomright.lon
#				lat = bottomright.lat
#			outf.write ("<Placemark>")
#			outf.write ("\t<name>" + cornername + "</name>")
#			outf.write ("\t<Point>")
#			outf.write ("\t<coordinates>" + str(lon) + "," + str(lat) + "</coordinates>")
#			outf.write ("\t</Point>")
#			outf.write ("</Placemark>")

		outf.write ( "<GroundOverlay>")
		outf.write ( "\t<name>" + f + "</name>")
		outf.write ( "\t<Icon>")
		outf.write ( "\t\t<href>" + "http://www.mwilt.org/lookimages/Flying2009-08-08/" + f + "</href>")
		outf.write ( "\t\t<viewBoundScale>0.75</viewBoundScale>")
		outf.write ( "\t</Icon>")
		outf.write ( "\t<LatLonBox>")
		outf.write ( "\t\t<north>" + str(boundnorth) + "</north>")
		outf.write ( "\t\t<south>" + str(boundsouth) + "</south>")
		outf.write ( "\t\t<east>" + str(boundeast) + "</east>")
		outf.write ( "\t\t<west>" + str(boundwest) + "</west>")
		outf.write ( "\t\t<rotation>" + str(-float(hd)) + "</rotation>")
		outf.write ( "\t</LatLonBox>")
		outf.write ( "</GroundOverlay>")
outf.write ( "</Document>")
outf.write ( "</kml>")
outf.close ()
