-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamping_selector.py
144 lines (114 loc) · 4.39 KB
/
camping_selector.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import sys
import math
import xml.etree.ElementTree as ET
from html import escape
from geopy import Point, distance
def main():
# get input parameters.
try:
waypoints_files = sys.argv[1].split(',')
except (IndexError, IOError):
print('Give a valid input waypoint file')
exit(1)
try:
route_files = sys.argv[2].split(',')
except (IndexError, IOError):
print('Give a valid input route file')
exit(1)
try:
km_limit = abs(float(sys.argv[3]))
skip = math.ceil(km_limit) * 15
km_limit *= 1.1
except (IndexError, IOError):
print('Give a valid distance in km')
exit(1)
try:
output_file = sys.argv[4]
except (IndexError, IOError):
print('Give a valid output file')
exit(1)
# fetch routepoints
route_points = []
smallest_lat = 1000000
smallest_lon = 1000000
greatest_lat = -1000000
greatest_lon = -1000000
for route_file in route_files:
print('Parsing', route_file)
tree = ET.parse(route_file)
root = tree.getroot()
trk = root.find('{http://www.topografix.com/GPX/1/1}trk')
trksegs = trk.findall('{http://www.topografix.com/GPX/1/1}trkseg')
trkpts = []
for trkseg in trksegs:
trkpts = trkpts + trkseg.findall('{http://www.topografix.com/GPX/1/1}trkpt')
for trkpt in trkpts:
lat = float(trkpt.get('lat'))
lon = float(trkpt.get('lon'))
route_points.append({
'lat': lat,
'lon': lon
})
if lat < smallest_lat:
smallest_lat = lat
if lat > greatest_lat:
greatest_lat = lat
if lon < smallest_lon:
smallest_lon = lon
if lon > greatest_lon:
greatest_lon = lon
# we store in which rectange (+ a margin) all route points are located
SW = Point(smallest_lat, smallest_lon)
NE = Point(greatest_lat, greatest_lon)
d = distance.distance(kilometers=km_limit)
NE = d.destination(point=NE, bearing=45)
SW = d.destination(point=SW, bearing=225)
print('Searching in set of', len(route_points),'routepoints.')
# fetch campings
waypoints = []
for waypoints_file in waypoints_files:
print('Parsing', waypoints_file)
tree = ET.parse(waypoints_file)
root = tree.getroot()
for child in root:
lat = float(child.get('lat'))
lon = float(child.get('lon'))
name = child.find('{http://www.topografix.com/GPX/1/1}name').text
# use a camping only if it is in our rectangle
if SW[0] <= lat <= NE[0] and SW[1] <= lon <= NE[1]:
waypoints.append({
'lat': lat,
'lon': lon,
'name': name
})
print('Searching in set of', len(waypoints),'campings')
output = '<?xml version="1.0" encoding="ISO-8859-1"?>\n'
output += '<gpx creator="waypoint_selector" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1">\n'
found = 0
for waypoint in waypoints:
counter = 0
for route_point in route_points:
# only look for waypoints every 'skip' route point.
if counter % skip == 0:
km = distance.distance(
Point(
waypoint['lat'],
waypoint['lon']
),
Point(
route_point['lat'],
route_point['lon']
)
).km
if km < km_limit:
output += '<wpt lat="' + str(waypoint['lat']) + '" lon="' + str(waypoint['lon']) + '"><name>' + escape(waypoint['name']) + '</name></wpt>\n'
print(waypoint['name'], waypoint['lat'], waypoint['lon'], km)
found += 1
break
counter += 1
output += '</gpx>'
with open(output_file, 'w') as f:
f.write(output)
print(found, 'campings found.')
if __name__ == '__main__':
main()