简体   繁体   English

问题重现“在 python 中创建一个既有点又有线的 kml”帖子

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

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.错误消息显示IndexError:字符串索引超出范围。 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?我正在调用具有我的纬度和经度的列号,即 5 和 6。知道此错误消息指的是什么吗?

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")

Use df.iterrows() to iterate over the data frame and df.loc[index, 'geometry'] to access the point.使用df.iterrows()迭代数据框和df.loc[index, 'geometry']访问点。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM