Files
ThingsBoard/Code Snippets/csv-to-kmz.py
2024-07-31 14:02:59 -05:00

37 lines
1.2 KiB
Python

import csv
import simplekml
# Create a new KML document
kml = simplekml.Kml()
# Open the CSV file and read its contents
with open('inputcoords.csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=';')
next(reader) # Skip the header row
# Loop through each device in the CSV file
for row in reader:
name = row[0]
lat_tel = float(row[1]) if row[1] else None
lon_tel = float(row[2]) if row[2] else None
lat_att = float(row[3]) if row[3] else None
lon_att = float(row[4]) if row[4] else None
# Ignore devices with "Gateway" in the name
if "Gateway" in name or "Camera Trailer" in name:
continue
# Prefer latitude and longitude from "tel" columns if available
lat = lat_tel or lat_att
lon = lon_tel or lon_att
# If no coordinates are available, print a warning message
if not (lat and lon):
print(f"No coordinates for device: {name}")
continue
# Create a new placemark for the device
pnt = kml.newpoint(name=name, coords=[(lon, lat)])
# Save the KML document to a file (KMZ file)
kml.savekmz("devices_tb.kmz")