forked from RolandJunior/KML_Coordinates_Extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KML_coord.py
50 lines (42 loc) · 1.75 KB
/
KML_coord.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import re
def main():
# KML parse
tree = ET.parse('input.kml')
root = tree.getroot()
# Identify default namespace
namespace = re.match('\{(.*?)\}kml', root.tag).group(1)
ns = {'def': namespace}
# Define coordinates RegEx
coord_ex = '(-?\d+\.\d+),'
heig_ex = '(\d+)'
regex = coord_ex + coord_ex + heig_ex
# Create output files (overwrite if already exist)
with open('output_pins.txt','w') as out_pin, \
open('output_paths.txt','w') as out_pat, \
open('output_polygons.txt','w') as out_pol:
# Add headers
out_pin.write('Pin Name,Latitude,Longitude,Height\n')
out_pat.write('Pin Name,Pin_#,Latitude,Longitude,Height\n')
out_pol.write('Pin Name,Pin_#,Latitude,Longitude,Height\n')
# Find coordinates
for i in root.findall('.//def:Placemark', ns):
name = i.find('def:name', ns).text
coord = i.find('.//def:coordinates', ns)
# Check for placeless placemark
if not coord is None:
coord = coord.text.strip()
coord = re.findall(regex, coord)
# Save data
pin = 0
for (long, lat, heig) in coord:
pin += 1
if i.find('.//def:Point', ns):
out_pin.write(f'{name},{lat},{long},{heig}\n')
elif i.find('.//def:LineString', ns):
out_pat.write(f'{name},pin_{pin},{lat},{long},{heig}\n')
elif i.find('.//def:Polygon', ns):
out_pol.write(f'{name},pin_{pin},{lat},{long},{heig}\n')
if __name__ == '__main__':
main()