Splitting KML file per placemark

720 views Asked by At

I want to split kml file per placemark and save it as separate kml file, the code creates separate files but includes all previous placemarks in the file eg: Grid3 holds the data for Grid 1 & 2 , Grid 5 hold all previous and so on. Can someone please help? Thanks in advance.

Source kml grid.kml

    <?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Folder>
<name>xyz company aug.20 18:06 UTC</name>
<open>1</open>
<Placemark>
 <name>GRID1</name>
 <visibility>1</visibility>
 <open>1</open>
 <LookAt>
  <latitude>57.44000000</latitude>
  <longitude>10.55000000</longitude>
  <range>5000</range>
 </LookAt>
 <description>20-08-2020 18:05 UTC
Speed 0 knots
</description>
 <Style>
  <IconStyle>
   <Icon>
    <href>http://10.0.1.100/map/arrow_n.gif</href>
    <x>0</x>
    <y>0</y>
    <w>25</w>
    <h>25</h>
   </Icon>
  </IconStyle>
 </Style>
 <Point>
  <coordinates>10.55000000,57.44000000,0</coordinates>
 </Point>
</Placemark>
<Placemark>
 <name>GRID2</name>
 <visibility>1</visibility>
 <open>1</open>
 <LookAt>
  <latitude>59.49555556</latitude>
  <longitude>23.60916667</longitude>
  <range>5000</range>
 </LookAt>
 <description>20-08-2020 18:04 UTC
Speed 0 knots
</description>
 <Style>
  <IconStyle>
   <Icon>
    <href>http://10.0.1.100/map/still.gif</href>
    <x>0</x>
    <y>0</y>
    <w>25</w>
    <h>25</h>
   </Icon>
  </IconStyle>
 </Style>
 <Point>
  <coordinates>23.60916667,59.49555556,0</coordinates>
 </Point>
</Placemark>
<Placemark>
 <name>GRID3</name>
 <visibility>1</visibility>
 <open>1</open>
 <LookAt>
  <latitude>28.06166667</latitude>
  <longitude>-15.37583333</longitude>
  <range>5000</range>
 </LookAt>
 <description>20-08-2020 18:04 UTC
Speed 0 knots
</description>
 <Style>
  <IconStyle>
   <Icon>
    <href>http://10.0.1.100/map/still.gif</href>
    <x>0</x>
    <y>0</y>
    <w>25</w>
    <h>25</h>
   </Icon>
  </IconStyle>
 </Style>
 <Point>
  <coordinates>-15.37583333,28.06166667,0</coordinates>
 </Point>
</Placemark>
<Placemark>
 <name>GRID4</name>
 <visibility>1</visibility>
 <open>1</open>
 <LookAt>
  <latitude>57.61361111</latitude>
  <longitude>10.60000000</longitude>
  <range>5000</range>
 </LookAt>
 <description>20-08-2020 18:04 UTC
Speed 0 knots
</description>
 <Style>
  <IconStyle>
   <Icon>
    <href>http://10.0.1.100/map/still.gif</href>
    <x>0</x>
    <y>0</y>
    <w>25</w>
    <h>25</h>
   </Icon>
  </IconStyle>
 </Style>
 <Point>
  <coordinates>10.60000000,57.61361111,0</coordinates>
 </Point>
</Placemark>
<Placemark>
 <name>GRID5</name>
 <visibility>1</visibility>
 <open>1</open>
 <LookAt>
  <latitude>59.44805556</latitude>
  <longitude>24.76861111</longitude>
  <range>5000</range>
 </LookAt>
 <description>20-08-2020 18:04 UTC
Speed 0 knots
</description>
 <Style>
  <IconStyle>
   <Icon>
    <href>http://10.0.1.100/map/still.gif</href>
    <x>0</x>
    <y>0</y>
    <w>25</w>
    <h>25</h>
   </Icon>
  </IconStyle>
 </Style>
 <Point>
  <coordinates>24.76861111,59.44805556,0</coordinates>
 </Point>
</Placemark>
<Placemark>
 <name>GRID6</name>
 <visibility>1</visibility>
 <open>1</open>
 <LookAt>
  <latitude>57.65888889</latitude>
  <longitude>10.63972222</longitude>
  <range>5000</range>
 </LookAt>
 <description>20-08-2020 18:04 UTC
Speed 0 knots
</description>
 <Style>
  <IconStyle>
   <Icon>
    <href>http://10.0.1.100/map/still.gif</href>
    <x>0</x>
    <y>0</y>
    <w>25</w>
    <h>25</h>
   </Icon>
  </IconStyle>
 </Style>
 <Point>
  <coordinates>10.63972222,57.65888889,0</coordinates>
 </Point>
</Placemark>
<Placemark>
 <name>GRID7</name>
 <visibility>1</visibility>
 <open>1</open>
 <LookAt>
  <latitude>17.47694444</latitude>
  <longitude>-63.01138889</longitude>
  <range>5000</range>
 </LookAt>
 <description>20-08-2020 18:04 UTC
Speed 0 knots
</description>
 <Style>
  <IconStyle>
   <Icon>
    <href>http://10.0.1.100/map/still.gif</href>
    <x>0</x>
    <y>0</y>
    <w>25</w>
    <h>25</h>
   </Icon>
  </IconStyle>
 </Style>
 <Point>
  <coordinates>-63.01138889,17.47694444,0</coordinates>
 </Point>
</Placemark>
<Placemark>
 <name>GRID8</name>
 <visibility>1</visibility>
 <open>1</open>
 <LookAt>
  <latitude>57.65111111</latitude>
  <longitude>10.59500000</longitude>
  <range>5000</range>
 </LookAt>
 <description>20-08-2020 18:04 UTC
Speed 0 knots
</description>
 <Style>
  <IconStyle>
   <Icon>
    <href>http://10.0.1.100/map/still.gif</href>
    <x>0</x>
    <y>0</y>
    <w>25</w>
    <h>25</h>
   </Icon>
  </IconStyle>
 </Style>
 <Point>
  <coordinates>10.59500000,57.65111111,0</coordinates>
 </Point>
</Placemark>
<Placemark>
 <name>GRID9</name>
 <visibility>1</visibility>
 <open>1</open>
 <LookAt>
  <latitude>28.51111111</latitude>
  <longitude>-16.16416667</longitude>
  <range>5000</range>
 </LookAt>
 <description>20-08-2020 18:04 UTC
Speed 0 knots
</description>
 <Style>
  <IconStyle>
   <Icon>
    <href>http://10.0.1.100/map/still.gif</href>
    <x>0</x>
    <y>0</y>
    <w>25</w>
    <h>25</h>
   </Icon>
  </IconStyle>
 </Style>
 <Point>
  <coordinates>-16.16416667,28.51111111,0</coordinates>
 </Point>
</Placemark>
<Placemark>
 <name>GRID10</name>
 <visibility>1</visibility>
 <open>1</open>
 <LookAt>
  <latitude>28.51250000</latitude>
  <longitude>-16.15083333</longitude>
  <range>5000</range>
 </LookAt>
 <description>20-08-2020 18:04 UTC
Speed 0 knots
</description>
 <Style>
  <IconStyle>
   <Icon>
    <href>http://10.0.1.100/map/still.gif</href>
    <x>0</x>
    <y>0</y>
    <w>25</w>
    <h>25</h>
   </Icon>
  </IconStyle>
 </Style>
 <Point>
  <coordinates>-16.15083333,28.51250000,0</coordinates>
 </Point>
</Placemark>
</Folder>
</kml>

"Python code"

    import simplekml
kml = simplekml.Kml()
from xml.dom import minidom

xmldoc = minidom.parse("grids.kml")

kmltag = xmldoc.getElementsByTagName("kml")[0]

folder = kmltag.getElementsByTagName("Folder")[0]

placemarks = folder.getElementsByTagName("Placemark")

for placemark in placemarks:
    gridname = placemark.getElementsByTagName("name")[0].firstChild.data
    lats = placemark.getElementsByTagName("latitude")[0].firstChild.data
    longs = placemark.getElementsByTagName("longitude")[0].firstChild.data
    desc = placemark.getElementsByTagName("description")[0].firstChild.data
    kml.newpoint(name=gridname, coords=[(longs,lats)], description=desc)
    kml.save('C:\\Users\\Gigabyte\\Desktop\\python stuff\\ships\\' + gridname + '.kml')
1

There are 1 answers

0
muratgu On BEST ANSWER

You need to move the following line into the for-loop so that a new object is created for every placemark:

kml = simplekml.Kml()