-1

我想拆分每个地标的 kml 文件并将其保存为单独的 kml 文件,代码创建单独的文件,但在文件中包含所有以前的地标,例如:Grid3 保存 Grid 1 和 2 的数据,Grid 5 保存所有以前的等等。有人可以帮忙吗?提前致谢。

源 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代码”

    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')
4

1 回答 1

0

您需要将以下行移动到 for 循环中,以便为每个地标创建一个新对象:

kml = simplekml.Kml()
于 2020-09-02T22:19:10.397 回答