Python script for converting comma-separated values files (.csv) into Keyhole Markup Language (.kml) for Google Earth.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.9 KiB

  1. import csv
  2. import simplekml
  3. def description(*argv):
  4. """Creates a description block with the arguments of the function"""
  5. block = ''
  6. check = 0
  7. for arg in argv:
  8. if arg == argv[0] and check == 0:
  9. block = block + arg
  10. check = 1
  11. else:
  12. block = block + '<BR>' + arg
  13. return block
  14. # Open the CSV file and read its contents
  15. with open(r'D:\aleja\Downloads\cosa\Sample CSV format.csv', 'r', enconding='UTF-8') as file:
  16. reader = csv.reader(file)
  17. headers = next(reader)
  18. # Create a KML document
  19. kml = simplekml.Kml()
  20. # Iterate over the rows in the CSV file
  21. for row in reader:
  22. # Extract the data for each field in the row
  23. name = row[1]
  24. pressure = "Sensor de presion: " + row[2] + " " + row[3]
  25. starter = "Arrancador: " + row[4] + " " + row[5]
  26. flow = "Caudalimetro: " + row[6] + " " + row[7]
  27. power = "Analizador de potencia: " + row[8] + " " + row[9]
  28. switch = "Interruptor de presion: " + row[10] + " " + row[11]
  29. rtu = "RTU: " + row[12] + " " + row[13]
  30. modem = "Modem: " + row[14] + " " + row[15]
  31. ID = "ID: " + row[18]
  32. level = "Sensor de nivel: " + row[19] + " " + row[20]
  33. latitude = float(row[16])
  34. longitude = float(row[17])
  35. # Added function, arguments are data to be placed in description box
  36. datos = description(name, ID, pressure, starter, flow, power, switch, rtu, modem, level)
  37. # Add a new point to the KML document
  38. point = kml.newpoint(name=name, description=datos, coords=[(longitude, latitude)])
  39. point.style.labelstyle.scale = 1
  40. point.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/water.png'
  41. # Write the KML data to a file
  42. print(kml.kml())
  43. kml.save(r'D:\aleja\Downloads\cosa\Output.kml')