Python Simplekml 如果文件夹已经存在,将文件夹内的数据附加为地标标签
Python Simplekml if folder already exists append the data inside folder as placemark tag
我有关于 SimpleKML 的问题。如何在具有相同名称标签的文件夹中创建(追加)地标?意思是在一个文件夹中可以看到 2 个数据点。下面是我正在寻找的输出。提前致谢。
输出寻找。
<Folder>
<name>MISCELNOUS</name>
<Placemark>
<name>GLEACHER CENTER</name>
<Snippet maxLines="0"></Snippet>
<description>Center<description>
<LookAt>
<longitude>-87.62224000000001</longitude>
<latitude>41.889645</latitude>
</LookAt>
<Point>
<coordinates>-87.62224000000001,41.889645,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>TRANSUNION BUILDING</name>
<Snippet maxLines="0"></Snippet>
<description>the post</description>
<LookAt>
<longitude>-87.641824</longitude>
<latitude>41.878961</latitude>
</LookAt>
<Point>
<coordinates>-87.641824,41.878961,0</coordinates>
</Point>
</Placemark>
</Folder>
<Folder>
<name>Office</name>
<Placemark>
<name>EAST NORTH ACADEMY</name>
<Snippet maxLines="0"></Snippet>
<description>My hero Academia</description>
<LookAt>
<longitude>-82.38971100000001</longitude>
<latitude>34.852488</latitude>
</LookAt>
<Point>
<coordinates>-82.38971100000001,34.852488,0</coordinates>
</Point>
</Placemark>
</Folder>
我的代码:
kml = simplekml.Kml()
kml.newdocument(name="PCI Data")
for i, j in df_small.iterrows():
try:
fol = kml.newfolder()
fol.name = j['folder']
pnt = fol.newpoint(name=j['Name'], coords=[[j['latitude'], j['longitude']]])
#pnt.lookat = simplekml.LookAt(gxaltitudemode=simplekml.GxAltitudeMode.relativetoseafloor,latitude=j['latitude'], longitude=j['longitude'], range=1000, heading=0, tilt=0)
except Exception as e:
print(e)
print(kml.kml())
我的输出:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document id="635">
<Document id="636">
<name>PCI Data</name>
</Document>
<Folder id="637">
<name>MISCELNOUS</name>
<Placemark id="639">
<name>SOKA BAU GP-FK00</name>
<Point id="638">
<coordinates>32.77815,-96.7954,0.0</coordinates>
</Point>
</Placemark>
</Folder>
<Folder id="640">
<name>MISCELNOUS</name>
<Placemark id="642">
<name>Thematic Platform LP FA01</name>
<Point id="641">
<coordinates>32.77815,-96.7954,0.0</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</kml>
我还有更多的文件夹要创建,里面应该包含更多的地标。
目前,我正在尝试这样解决,但没有成功获得结果。任何指导都会有所帮助。
kml = simplekml.Kml()
doc = kml.newdocument(name="PCI Data")
for i, j in df_small.iterrows():
try:
if 'MISCELNOUS' or 'Office' in [x.name for x in kml.containers]:
fol.name = j['primary_property_type']
pnt = fol.newpoint(name=j['Name'], coords=[[j['latitude'], j['longitude']]])
pnt = fol.newpoint(name=j['Name'], coords=[[j['latitude'], j['longitude']]])
pnt.lookat = simplekml.LookAt(gxaltitudemode=simplekml.GxAltitudeMode.relativetoseafloor,latitude=j['latitude'], longitude=j['longitude'], range=1000, heading=0, tilt=0)
else:
fol = kml.newfolder()
fol.name = j['primary_property_type']
pnt = fol.newpoint(name=j['Name'], coords=[[j['latitude'], j['longitude']]])
pnt.lookat = simplekml.LookAt(gxaltitudemode=simplekml.GxAltitudeMode.relativetoseafloor,latitude=j['latitude'], longitude=j['longitude'], range=1000, heading=0, tilt=0)
except Exception as e:
print(e)
print(kml.kml())
示例数据集:
df_sample = pd.DataFrame({'Name': ['GLEACHER CENTER', 'TRANSUNION BUILDING', 'EAST NORTH ACADEMY'],
'description': ['Center', 'the post', 'My hero Academia'],
'longitude' : [-87.62224000000001, -87.641824, -82.38971100000001],
'latitude' : [41.889645, 41.878961, 34.852488],
'folder' : ['MISCELNOUS', 'MISCELNOUS', 'Office']})
只需在行循环外初始化 simpleKML Folder
。然后将 newpoint
s 添加到这个文件夹。要分离出文件夹标签,运行 一个 groupby
迭代并在 运行ning 数据框按行迭代之前分配文件夹名称。
kml = simplekml.Kml()
doc = kml.newdocument(name="PCI Data")
for grp, sub in df_sample.groupby("folder"):
fol = kml.newfolder(name=grp)
for row in sub.iterrows():
try:
fol.newpoint(
name=row['Name'],
description=row['description'],
coords=[[row['latitude'], row['longitude']]]
)
except Exception as e:
print(e)
print(kml.kml())
谢谢,@Parfait。对您的解决方案进行一些修改。我得到了我需要的东西。你救了我的命。
kml = simplekml.Kml()
doc = kml.newdocument(name="PCI Data")
for grp, sub in df_sample.groupby("folder"):
fol = kml.newfolder(name=grp)
for row in sub.iterrows():
try:
fol.newpoint(
name=row[1]['Name'],
description=row[1]['description'],
coords=[[row[1]['latitude'], row[1]['longitude']]]
)
except Exception as e:
print(e)
print(kml.kml())
我有关于 SimpleKML 的问题。如何在具有相同名称标签的文件夹中创建(追加)地标?意思是在一个文件夹中可以看到 2 个数据点。下面是我正在寻找的输出。提前致谢。
输出寻找。
<Folder>
<name>MISCELNOUS</name>
<Placemark>
<name>GLEACHER CENTER</name>
<Snippet maxLines="0"></Snippet>
<description>Center<description>
<LookAt>
<longitude>-87.62224000000001</longitude>
<latitude>41.889645</latitude>
</LookAt>
<Point>
<coordinates>-87.62224000000001,41.889645,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>TRANSUNION BUILDING</name>
<Snippet maxLines="0"></Snippet>
<description>the post</description>
<LookAt>
<longitude>-87.641824</longitude>
<latitude>41.878961</latitude>
</LookAt>
<Point>
<coordinates>-87.641824,41.878961,0</coordinates>
</Point>
</Placemark>
</Folder>
<Folder>
<name>Office</name>
<Placemark>
<name>EAST NORTH ACADEMY</name>
<Snippet maxLines="0"></Snippet>
<description>My hero Academia</description>
<LookAt>
<longitude>-82.38971100000001</longitude>
<latitude>34.852488</latitude>
</LookAt>
<Point>
<coordinates>-82.38971100000001,34.852488,0</coordinates>
</Point>
</Placemark>
</Folder>
我的代码:
kml = simplekml.Kml()
kml.newdocument(name="PCI Data")
for i, j in df_small.iterrows():
try:
fol = kml.newfolder()
fol.name = j['folder']
pnt = fol.newpoint(name=j['Name'], coords=[[j['latitude'], j['longitude']]])
#pnt.lookat = simplekml.LookAt(gxaltitudemode=simplekml.GxAltitudeMode.relativetoseafloor,latitude=j['latitude'], longitude=j['longitude'], range=1000, heading=0, tilt=0)
except Exception as e:
print(e)
print(kml.kml())
我的输出:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document id="635">
<Document id="636">
<name>PCI Data</name>
</Document>
<Folder id="637">
<name>MISCELNOUS</name>
<Placemark id="639">
<name>SOKA BAU GP-FK00</name>
<Point id="638">
<coordinates>32.77815,-96.7954,0.0</coordinates>
</Point>
</Placemark>
</Folder>
<Folder id="640">
<name>MISCELNOUS</name>
<Placemark id="642">
<name>Thematic Platform LP FA01</name>
<Point id="641">
<coordinates>32.77815,-96.7954,0.0</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</kml>
我还有更多的文件夹要创建,里面应该包含更多的地标。
目前,我正在尝试这样解决,但没有成功获得结果。任何指导都会有所帮助。
kml = simplekml.Kml()
doc = kml.newdocument(name="PCI Data")
for i, j in df_small.iterrows():
try:
if 'MISCELNOUS' or 'Office' in [x.name for x in kml.containers]:
fol.name = j['primary_property_type']
pnt = fol.newpoint(name=j['Name'], coords=[[j['latitude'], j['longitude']]])
pnt = fol.newpoint(name=j['Name'], coords=[[j['latitude'], j['longitude']]])
pnt.lookat = simplekml.LookAt(gxaltitudemode=simplekml.GxAltitudeMode.relativetoseafloor,latitude=j['latitude'], longitude=j['longitude'], range=1000, heading=0, tilt=0)
else:
fol = kml.newfolder()
fol.name = j['primary_property_type']
pnt = fol.newpoint(name=j['Name'], coords=[[j['latitude'], j['longitude']]])
pnt.lookat = simplekml.LookAt(gxaltitudemode=simplekml.GxAltitudeMode.relativetoseafloor,latitude=j['latitude'], longitude=j['longitude'], range=1000, heading=0, tilt=0)
except Exception as e:
print(e)
print(kml.kml())
示例数据集:
df_sample = pd.DataFrame({'Name': ['GLEACHER CENTER', 'TRANSUNION BUILDING', 'EAST NORTH ACADEMY'],
'description': ['Center', 'the post', 'My hero Academia'],
'longitude' : [-87.62224000000001, -87.641824, -82.38971100000001],
'latitude' : [41.889645, 41.878961, 34.852488],
'folder' : ['MISCELNOUS', 'MISCELNOUS', 'Office']})
只需在行循环外初始化 simpleKML Folder
。然后将 newpoint
s 添加到这个文件夹。要分离出文件夹标签,运行 一个 groupby
迭代并在 运行ning 数据框按行迭代之前分配文件夹名称。
kml = simplekml.Kml()
doc = kml.newdocument(name="PCI Data")
for grp, sub in df_sample.groupby("folder"):
fol = kml.newfolder(name=grp)
for row in sub.iterrows():
try:
fol.newpoint(
name=row['Name'],
description=row['description'],
coords=[[row['latitude'], row['longitude']]]
)
except Exception as e:
print(e)
print(kml.kml())
谢谢,@Parfait。对您的解决方案进行一些修改。我得到了我需要的东西。你救了我的命。
kml = simplekml.Kml()
doc = kml.newdocument(name="PCI Data")
for grp, sub in df_sample.groupby("folder"):
fol = kml.newfolder(name=grp)
for row in sub.iterrows():
try:
fol.newpoint(
name=row[1]['Name'],
description=row[1]['description'],
coords=[[row[1]['latitude'], row[1]['longitude']]]
)
except Exception as e:
print(e)
print(kml.kml())