-
Notifications
You must be signed in to change notification settings - Fork 0
/
KMLSplit
36 lines (28 loc) · 1.26 KB
/
KMLSplit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from xml.dom import minidom
import simplekml
# Prompt user to enter the file name
file_name = input("Enter the KML file name (including path): ")
# Parse the original KML file
xmldoc = minidom.parse(file_name)
placemarks = xmldoc.getElementsByTagName("Placemark")
# Iterate over each Placemark and create a separate KML file
for placemark in placemarks:
kml = simplekml.Kml()
# Extract the name from <SimpleData name="Name">
simpleData_elements = placemark.getElementsByTagName("SimpleData")
gridname = None
for element in simpleData_elements:
if element.getAttribute("name") == "Name":
gridname = element.firstChild.data
break
if not gridname:
continue # Skip this placemark if there is no name
# Extracting coordinates from the <Polygon> tag
polygon = placemark.getElementsByTagName("Polygon")[0]
coordinates = polygon.getElementsByTagName("coordinates")[0].firstChild.data
coords_list = coordinates.strip().split(" ")
# Create a polygon in the new KML object
pol = kml.newpolygon(name=gridname)
pol.outerboundaryis = [tuple(map(float, coord.split(','))) for coord in coords_list]
# Save the KML file with the name of the placemark
kml.save(f'C:\\convert\\kmlsplit\\{gridname}.kml')