예제 #1
0
def make_kml_file(geofence):

	fld = KML.Folder()
	geometry_coords = []

	# create a KML file skeleton
	stylename = "sn_shaded_dot"
	doc = KML.kml(
		KML.Document(
			KML.Name("Sun Position"),
			KML.Style(
				KML.IconStyle(
					KML.scale(1.2),
					KML.Icon(
						KML.href("http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png")
					),
				),
				id=stylename,
			)
			KML.Folder(
				KML.name("Shrunk Geofence")
			)
		)
	)

	for coord in geofence:
		str_lat = str(coord.lat)
		str_lon = str(coord.lon)
		str_comb = "%s,%s,0 " % (str_lat,str_lon)
		geometry_coords.append(str_comb)


	geometry_string = ''.join(geometry_coords)

	doc.Document.Folder.append(KML.Placemark(
		KML.Polygon(
			KML.outerBoundaryIs(
				KML.linearRing(
					KML.coordinates(geometry_string)
				)
			)
		)
	))

	with open("./kml_modGeofence/Shrunk_Geofence.kml", "w") as text_file:
		text_file.write(etree.tostring(doc, pretty_print=True))