我有一个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)正如您所看到的,我已经访问了placemark并设法获得了它的计数,..now,获得每个placemark的ID的正确方法是什么。
发布于 2022-03-23 10:25:29
答案很简单:
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'):
print(item.get('id')) #This line have the way to access attributes
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)https://stackoverflow.com/questions/71585032
复制相似问题