如何在 KML 文件中使用 pykml 访问对象参数
我有一个 python 脚本,可以打开 kml 文件并对其进行访问以访问其中的特定元素,我已经轻松地访问了每个文件中的数据,但我仍然需要访问其中的 id 属性每个标签..,这是我的 kml 文件的示例:
<Placemark id="ID_00000">
<name>وصلة الدبه</name>
<Snippet maxLines="0"></Snippet>
<description><![CDATA[<html xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<head>
<META http-equiv="Content-Type" content="text/html">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body style="margin:0px 0px 0px 0px;overflow:auto;background:#FFFFFF;">
<table style="font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px">
<tr style="text-align:center;font-weight:bold;background:#9CBCE2">
<td>وصلة الدبه</td>
…
</body>
</html>]]></description>
<styleUrl>#LineStyle00</styleUrl>
<MultiGeometry>
<LineString>
<coordinates>
31.23880592746422,30.48828642589049,0 31.2388088420489,30.48828001644751,0 31.23889998866499,30.48807953905985,0 31.23899477580304,30.48773579409284,0 31.2391819138694,30.48729967038745,0 31.23937226808513,30.48683884102257,0 31.23956912620324,30.48648937112743,0 31.23979826580608,30.48615191271868,0 31.24014029281084,30.48584735139158,0 31.24054211140007,30.48560073426295,0 31.24108232959133,30.48538316344337,0 31.24135710345509,30.4852853601617,0 31.24165000915282,30.48518514932555,0 31.24240540500461,30.48497683095443,0 31.24303541295797,30.48477745955918,0 31.24368664271374,30.48459329312845,0 31.24402029659368,30.48448464182598,0
</coordinates>
</LineString>
</MultiGeometry>
</Placemark>
这是我正在使用的 python 代码的示例:
the_dir = os.path.join(
settings.BASE_DIR,f"temp_kml_file/{instance.pk}/doc.kml")
file_path = Path(the_dir)
kml = open(file_path,encoding='utf-8')
doc = parser.parse(kml).getroot()
items_count = 0
for item in doc.findall('.//{http://www.opengis.net/kml/2.2}Placemark'):
items_count += 1
new_water_element = WaterElement.objects.create(
element_name=str(item.name), map_layer=instance)
string_of_lat = str(item.MultiGeometry.LineString.coordinates).split(",")[0]
the_lat = re.sub('[^\d\.]', '', string_of_lat)
string_of_lng = str(item.MultiGeometry.LineString.coordinates).split(",")[1].split(",")[0]
the_lng = re.sub('[^\d\.]', '', string_of_lng)
final_lat_lng = the_lat+","+the_lng
new_water_element.first_cord=final_lat_lng
new_water_element.save()
MapLayers.objects.filter(pk=instance.pk).update(element_count=items_count)
如您所见,我已经访问了地标并设法获取了它的计数..现在正确的方法是什么获取每个人的ID地标。
I have a python script that opens kml file and prase it to access specific elements inside of it, I have easily managed to access the data that lies inside every ,but I still need to access the id
attribute inside every tag.. , here is an example of my kml file :
<Placemark id="ID_00000">
<name>وصلة الدبه</name>
<Snippet maxLines="0"></Snippet>
<description><![CDATA[<html xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<head>
<META http-equiv="Content-Type" content="text/html">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body style="margin:0px 0px 0px 0px;overflow:auto;background:#FFFFFF;">
<table style="font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px">
<tr style="text-align:center;font-weight:bold;background:#9CBCE2">
<td>وصلة الدبه</td>
…
</body>
</html>]]></description>
<styleUrl>#LineStyle00</styleUrl>
<MultiGeometry>
<LineString>
<coordinates>
31.23880592746422,30.48828642589049,0 31.2388088420489,30.48828001644751,0 31.23889998866499,30.48807953905985,0 31.23899477580304,30.48773579409284,0 31.2391819138694,30.48729967038745,0 31.23937226808513,30.48683884102257,0 31.23956912620324,30.48648937112743,0 31.23979826580608,30.48615191271868,0 31.24014029281084,30.48584735139158,0 31.24054211140007,30.48560073426295,0 31.24108232959133,30.48538316344337,0 31.24135710345509,30.4852853601617,0 31.24165000915282,30.48518514932555,0 31.24240540500461,30.48497683095443,0 31.24303541295797,30.48477745955918,0 31.24368664271374,30.48459329312845,0 31.24402029659368,30.48448464182598,0
</coordinates>
</LineString>
</MultiGeometry>
</Placemark>
And here Is an example of the python code am using :
the_dir = os.path.join(
settings.BASE_DIR,f"temp_kml_file/{instance.pk}/doc.kml")
file_path = Path(the_dir)
kml = open(file_path,encoding='utf-8')
doc = parser.parse(kml).getroot()
items_count = 0
for item in doc.findall('.//{http://www.opengis.net/kml/2.2}Placemark'):
items_count += 1
new_water_element = WaterElement.objects.create(
element_name=str(item.name), map_layer=instance)
string_of_lat = str(item.MultiGeometry.LineString.coordinates).split(",")[0]
the_lat = re.sub('[^\d\.]', '', string_of_lat)
string_of_lng = str(item.MultiGeometry.LineString.coordinates).split(",")[1].split(",")[0]
the_lng = re.sub('[^\d\.]', '', string_of_lng)
final_lat_lng = the_lat+","+the_lng
new_water_element.first_cord=final_lat_lng
new_water_element.save()
MapLayers.objects.filter(pk=instance.pk).update(element_count=items_count)
as you can see I have accessed placemarks and managed to get the count of it ..now what is the proper way to get the ID of every placemark.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案很简单:
the answer was as simple as :