Issue reproducing "Create a kml in python that has both points and lines" post

305 Views Asked by At

I'm trying to replicate a post I came across in the link below and had an error message come up I'm a bit unsure how to resolve: Link to Prior Post I'm trying to replicate

I'm getting an error on the following line:

coord = (row[5], row[6]) # lon, lat order

The error message reads IndexError: string index out of range. I'm calling the column numbers that have my lat and long, which is 5 & 6. Any idea what this error message is referring to?

Here's the script I have at this point:

import geopandas as gpd
import simplekml
kml = simplekml.Kml()


inputfile = gpd.read_file("C:/Users/CombineKMLs_AddLabels/Data/ScopePoles.shp") 
points = []
for row in inputfile:
    coord = (row[5], row[6]) # lon, lat order
    pnt = kml.newpoint(name=row[2], coords=[coord])
    points.append(coord)    
    pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_square.png'

ls = kml.newlinestring(name='A LineString')
ls.coords = np.array(points)
ls.altitudemode = simplekml.AltitudeMode.relativetoground
ls.extrude = 1

kml.save("C:/Users/CombineKMLs_AddLabels/Data/PolesandLines.shp")
1

There are 1 best solutions below

2
On BEST ANSWER

Use df.iterrows() to iterate over the data frame and df.loc[index, 'geometry'] to access the point.

import geopandas as gpd
import simplekml

kml = simplekml.Kml()

df = gpd.read_file("C:/Users/CombineKMLs_AddLabels/Data/ScopePoles.shp")

points = []
for index, poi in df.iterrows():
  pt = df.loc[index, 'geometry']
  coord = (pt.x, pt.y)
  pnt = kml.newpoint(name=index, coords=[coord])
  points.append(coord)
  pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_square.png'

ls = kml.newlinestring(name='A LineString', coords=points)

kml.save('PolesandLines.kml')