Created
August 1, 2014 17:55
Star
You must be signed in to star a gist
Trecho de código para ler os dados contidos no arquivo KML para popular o GoogleMaps.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process_file_kml(sender, created, instance, **kwargs): | |
try: | |
if created: | |
xmldoc = minidom.parse(instance.shape_file) | |
document_name = \ | |
xmldoc.getElementsByTagName("kml")[0].getElementsByTagName("Document")[0].getElementsByTagName("name")[ | |
0] | |
type_shape = None | |
for place in xmldoc.getElementsByTagName("Placemark"): | |
name_place = place.getElementsByTagName("name")[0].firstChild.data | |
type_element = str(place.getElementsByTagName("styleUrl")[0].firstChild.data) | |
if type_element.find("PolyStyle") == 1: | |
# TODO Verificar como recuperar todos os elementos Polygon | |
type_shape = "Polygon" | |
for polygon in place.getElementsByTagName("Polygon"): | |
coordinates = \ | |
polygon.getElementsByTagName("outerBoundaryIs")[0].getElementsByTagName("LinearRing")[ | |
0].getElementsByTagName("coordinates")[0] | |
list_coordinates = coordinates.firstChild.data.strip() | |
Placemark.objects.create(shape=instance, | |
name=name_place, | |
coordinate=list_coordinates, type_shape=type_shape) | |
elif type_element.find("LineStyle") == 1: | |
coordinates = place.getElementsByTagName("LineString")[0].getElementsByTagName("coordinates")[0] | |
type_shape = "Line" | |
list_coordinates = coordinates.firstChild.data.strip() | |
Placemark.objects.create(shape=instance, | |
name=name_place, | |
coordinate=list_coordinates, type_shape=type_shape) | |
elif type_element.find("IconStyle") == 1: | |
coordinates = place.getElementsByTagName("Point")[0].getElementsByTagName("coordinates")[0] | |
type_shape = "Point" | |
list_coordinates = coordinates.firstChild.data.strip() | |
Placemark.objects.create(shape=instance, | |
name=name_place, | |
coordinate=list_coordinates, type_shape=type_shape) | |
instance.name = document_name.firstChild.data | |
instance.type_shape = type_shape | |
instance.save(force_update=True) | |
except Exception, e: | |
print e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment