0

I have question related to SimpleKML. How to create(Append) a placemark inside a folder having the same name tag? Meaning inside one folder 2 data points are seen. Below is the output I am looking for. Thanks in advance.

Output looking for.

  <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>

My code:

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())

My output:

<?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>

Also I have more folder to be created and and it should contain more placemark inside them.

Currently, I'm trying to work it out like this, But not successful in getting the result. Any guidance would be helpful.

 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())

Sample Dataset:

 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']})

2 Answers2

0

Simply initialize the simpleKML Folder once outside the rowwise loop. Then add newpoints to this single Folder. To separate out the folder tags, run a groupby iteration and assign folder name before running data frame rowwise iteration.

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
  • 104,375
  • 17
  • 94
  • 125
  • Thanks, @Parfait for your reply, My bad, Here I have only given one folder. But my requirement is to create more folders and they should have their own data in them. Please Look into my updated output requirement above. Thanks! – Eventually_there May 21 '22 at 05:56
  • Can you post a sample of data frame for [reproducible example](https://stackoverflow.com/q/20109391/1422451)? – Parfait May 21 '22 at 15:57
  • Please refer to the below code. – Eventually_there May 23 '22 at 00:26
  • 1
    Do not post sample as an answer! [Edit](https://stackoverflow.com/posts/72317430/edit) your original post with such information. – Parfait May 23 '22 at 00:58
  • Nevertheless, I have edited to fit your folder grouping needs. – Parfait May 23 '22 at 01:04
0

Thanks, @Parfait. Some modifications to your solution. I got what I required. You saved my day.

 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())