0

我想将代码更改为 for-loop,以便我可以更改每个点的样式。

下面的代码在没有 for 循环的情况下工作正常:

import simplekml
import pandas as pd

excel_file = 'sample.xlsx'
df=pd.read_excel(excel_file)
kml = simplekml.Kml()
df.apply(lambda X: kml.newpoint( coords=[( X["Long"],X["Lat"])]) ,axis=1)
kml.save(path = "data.kml")

我想在 for 循环中这样做,以便我可以为每个点添加样式,但是我的 for 循环不起作用

import simplekml
import pandas as pd
kml = simplekml.Kml()
style = simplekml.Style()
excel_file = 'sample1.xlsx'
df=pd.read_excel(excel_file)
y=df.Long
x=df.Lat

MinLat=int(df.Lat.min())
MaxLat=int(df.Lat.max())
MinLong=int(df.Long.min())
MaxLong=int(df.Long.max())
multipnt =kml.newmultigeometry()

for long in range(MinLong,MaxLong):  # Generate longitude values
    for lat in  range(MaxLat,MinLat): # Generate latitude values
         multipnt.newpoint(coords=[(y,x)])
        #kml.newpoint(coords=[(y,x)])
kml.save("Point Shared Style.kml")
4

1 回答 1

1

如果想要遍历 Excel 文件中的点集合并使用 for 循环将它们作为 MultiGeometry 添加到单个 Placemark 中,请尝试此操作。

import simplekml
import pandas as pd

kml = simplekml.Kml()
style = simplekml.Style()
excel_file = 'sample1.xlsx'
df = pd.read_excel(excel_file)
multipnt = kml.newmultigeometry()
for row in df.itertuples(index=False):
    multipnt.newpoint(coords=[(row.Lat, row.Long)])
kml.save("PointSharedStyle.kml")

如果要为点的边界框每隔十进制度生成一个点网格,那么您可以尝试以下操作:

import simplekml
import pandas as pd

kml = simplekml.Kml()
style = simplekml.Style()
excel_file = 'sample1.xlsx'
df = pd.read_excel(excel_file)

MinLat = int(df.Lat.min())
MaxLat = int(df.Lat.max())
MinLong = int(df.Long.min())
MaxLong = int(df.Long.max())

for long in range(MinLong, MaxLong+1):  # Generate longitude values
    for lat in range(MinLat, MaxLat+1): # Generate latitude values
        multipnt.newpoint(coords=[(long, lat)])
        #kml.newpoint(coords=[(long,lat)])

kml.save("PointSharedStyle.kml")

请注意,样式被分配给地标而不是几何体,因此只能为所有点为 MultiGeometry 分配一个样式。如果要为每个点创建不同的样式,则需要为每个点创建一个地标,并为每个点分配自己的样式。

有关设置样式的帮助,请参阅https://simplekml.readthedocs.io/en/latest/styles.html

于 2020-09-11T15:48:08.987 回答