Ver Mensaje Individual
  #1 (permalink)  
Antiguo 21/10/2011, 06:39
mferril
 
Fecha de Ingreso: octubre-2011
Mensajes: 1
Antigüedad: 12 años, 6 meses
Puntos: 0
Crear un .kml a partir de un .csv

Buenas,
soy una novata de Python...
necesito crear un fichero kml a partir de otro csv.
Me descargué la versión 2.7 de Python y compile este programa que encontré en internet.

Al compilarlo, me genera un fichero .pyc
y al darle doble click al .pyc se megenera el .kml deseado.
El problema es que cuando introduzco alguna modificación el el fichero .py inicial, al compilarlo se me genera el nuevo .pyc pero al darle doble click ya no se genera el nuevo .kml

Lo intentado mil veces, pero no me sale nunca.
¿sabeis por qué ocurre esto?

gracias

Código Python:
Ver original
  1. import geocoding_for_kml
  2. import csv
  3. import xml.dom.minidom
  4. import sys
  5.  
  6.  
  7. def extractAddress(row):
  8.   # This extracts an address from a row and returns it as a string. This requires knowing
  9.   # ahead of time what the columns are that hold the address information.
  10.   return '%s,%s,%s,%s,%s' % (row['Address1'], row['Address2'], row['City'], row['State'], row['Zip'])
  11.  
  12. def createPlacemark(kmlDoc, row, order):
  13.   # This creates a <Placemark> element for a row of data.
  14.   # A row is a dict.
  15.   placemarkElement = kmlDoc.createElement('Placemark')
  16.   extElement = kmlDoc.createElement('ExtendedData')
  17.   placemarkElement.appendChild(extElement)
  18.  
  19.   # Loop through the columns and create a <Data> element for every field that has a value.
  20.   for key in order:
  21.     if row[key]:
  22.       dataElement = kmlDoc.createElement('Data')
  23.       dataElement.setAttribute('name', key)
  24.       valueElement = kmlDoc.createElement('value')
  25.       dataElement.appendChild(valueElement)
  26.       valueText = kmlDoc.createTextNode(row[key])
  27.       valueElement.appendChild(valueText)
  28.       extElement.appendChild(dataElement)
  29.  
  30.   pointElement = kmlDoc.createElement('Point')
  31.   placemarkElement.appendChild(pointElement)
  32.   coordinates = geocoding_for_kml.geocode(extractAddress(row))
  33.   coorElement = kmlDoc.createElement('coordinates')
  34.   coorElement.appendChild(kmlDoc.createTextNode(coordinates))
  35.   pointElement.appendChild(coorElement)
  36.   return placemarkElement
  37.  
  38. def createKML(csvReader, fileName, order):
  39.   # This constructs the KML document from the CSV file.
  40.   kmlDoc = xml.dom.minidom.Document()
  41.  
  42.   kmlElement = kmlDoc.createElementNS('http://earth.google.com/kml/2.2', 'kml')
  43.   kmlElement.setAttribute('xmlns', 'http://earth.google.com/kml/2.2')
  44.   kmlElement = kmlDoc.appendChild(kmlElement)
  45.   documentElement = kmlDoc.createElement('Document')
  46.   documentElement = kmlElement.appendChild(documentElement)
  47.  
  48.   # Skip the header line.
  49.   csvReader.next()
  50.  
  51.   for row in csvReader:
  52.     placemarkElement = createPlacemark(kmlDoc, row, order)
  53.     documentElement.appendChild(placemarkElement)
  54.   kmlFile = open(fileName, 'w')
  55.   kmlFile.write(kmlDoc.toprettyxml('  ', newl = '\n', encoding = 'utf-8'))
  56.  
  57. def main():
  58.   # This reader opens up 'google-addresses.csv', which should be replaced with your own.
  59.   # It creates a KML file called 'google.kml'.
  60.  
  61.   # If an argument was passed to the script, it splits the argument on a comma
  62.   # and uses the resulting list to specify an order for when columns get added.
  63.   # Otherwise, it defaults to the order used in the sample.
  64.  
  65.   if len(sys.argv) >1: order = sys.argv[1].split(',')
  66.   else: order = ['Office','Address1','Address2','Address3','City','State','Zip','Phone','Fax']
  67.   csvreader = csv.DictReader(open('google-addresses.csv'),order)
  68.   kml = createKML(csvreader, 'google-addresses.kml', order)
  69.  
  70. if __name__ == '__main__':
  71.   main()

Última edición por AlvaroG; 21/10/2011 a las 08:00 Razón: resaltado de código