1

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.

Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116

1 Answers1

0

the answer was as simple as :

    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)

Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116