forked from kercos/PickMeUp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparseKml.py
99 lines (86 loc) · 3.67 KB
/
parseKml.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# coding=utf-8
import requests
import xml.etree.ElementTree as ET
ZONE_LAYER_NAME = 'Luoghi'
FARMATE_LAYER_NAME = 'Fermate'
tagPrefix = '{http://www.opengis.net/kml/2.2}'
docTag = tagPrefix + 'Document'
folderTag = tagPrefix + 'Folder'
nameTag = tagPrefix + 'name'
placemarkTag = tagPrefix + 'Placemark'
pointTag = tagPrefix + 'Point'
coordinatesTag = tagPrefix + 'coordinates'
polygonTag = tagPrefix + 'Polygon'
outerBoundaryIsTag = tagPrefix + 'outerBoundaryIs'
linearRingTag = tagPrefix + 'LinearRing'
lineStringTag = tagPrefix + 'LineString'
def mean(numbers):
return float(sum(numbers)) / max(len(numbers), 1)
def getPolygonCentroid(poly):
return mean([x[0] for x in poly]),mean([x[1] for x in poly])
def getZonaConainingPoint(point, zone):
from geoUtils import point_inside_polygon
for n, v in zone.iteritems():
polycoordinateList = v['polygon']
if point_inside_polygon(point[0], point[1], polycoordinateList):
return n
return None
# needs to be duplicated from routing_util to prevent cycle imports from parseMap()
def encodeFermataKey(zona, fermata):
return '{} ({})'.format(zona, fermata)
def parseMap():
import key
#r = requests.get(key.map_url)
#kml_xml = r.content
#root = ET.fromstring(kml_xml)
root = ET.parse('data/PickMeUp.kml') #getroot()
document = root.find(docTag)
folders = document.findall(folderTag)
nameFolders = {}
for fold in folders:
name = fold.find(nameTag).text # Fermate, ZoneFlags, Zone, Lines
nameFolders[name] = fold
# ZONE
# IMPORTANT - ZONE CANNOT SHARE THE SAME PREFIX
zone = {} # {zona: {'loc': (<lat>,<lon>), 'stops': [stop1, stop2, ...]}, 'polygon': <list polygon coords>}
zone_folder = nameFolders[ZONE_LAYER_NAME]
placemarks = zone_folder.findall(placemarkTag)
for p in placemarks:
name = p.find(nameTag).text.strip() # zona name
name = name.encode('utf-8')
polygon = p.find(polygonTag)
outerBoundaryIs = polygon.find(outerBoundaryIsTag)
linearRing = outerBoundaryIs.find(linearRingTag)
coordinatesStringList = [x.strip() for x in linearRing.find(coordinatesTag).text.strip().split(' ')]
coordinateList = []
for coordinatesString in coordinatesStringList:
lon, lat = [float(x) for x in coordinatesString.split(',')[:2]]
coordinateList.append((lat, lon))
centroid_lat, centroid_lon = getPolygonCentroid(coordinateList)
zone[name] = {
'loc': (centroid_lat, centroid_lon), # centroid
'polygon': coordinateList,
'stops': []
}
# FERMATE
fermate = {} # {zona_stop: {'zona': refZona, 'stop': <fermata_name>, 'loc': (<lat>,<lon>)}}
fermate_folder = nameFolders[FARMATE_LAYER_NAME]
placemarks = fermate_folder.findall(placemarkTag)
for p in placemarks:
stop = p.find(nameTag).text.strip() # fermata name
stop = stop.encode('utf-8')
point = p.find(pointTag)
coordinatesString = point.find(coordinatesTag).text.strip().split(',')
lon, lat = [float(x) for x in coordinatesString[:2]]
#point = Point(lat, lon)
zona = getZonaConainingPoint((lat, lon), zone)
zona_stop = encodeFermataKey(zona, stop)
fermate[zona_stop] = {'zona': zona, 'stop': stop, 'loc': (lat, lon)}
zone[zona]['stops'].append(stop)
return zone, fermate
def checkMap():
zone, fermate = parseMap()
checkZone = all(len(v['stops'])>0 for v in zone.values())
checkFermate = all(fv['zona'] is not None for fv in fermate.values())
print "Zone: {} check: {}".format(len(zone), checkZone)
print "Fermate: {} check: {}".format(len(fermate), checkFermate)