-
Notifications
You must be signed in to change notification settings - Fork 20
/
routeGeojson.py
129 lines (106 loc) · 4.18 KB
/
routeGeojson.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
#!/usr/bin/python
# -*- mode: python; indent-tabs-mode: nil; tab-width: 2 -*-
#----------------------------------------------------------------
# routeGeojson - routes from GeoJSON with OSM data, and generates a
# GeoJSON file containing the results. Input file must be in WGS84 CRS.
#
#------------------------------------------------------
# Usage:
# routeGeojson.py [input_file] -o [output_file]
#------------------------------------------------------
# Copyright 2007-2008, Oliver White
# Copyright 2016, Michael Farrell
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------
import argparse
import geojson
from route import Router
from loadOsm import LoadOsm
def pythagoras(x1, y1, x2, y2):
# Not really correct for world geometry, but good enough for what we're doing
# with it.
x_d = abs(x1 - x2)
y_d = abs(y1 - y2)
return ((x_d ** 2) + (y_d ** 2)) ** 0.5
def route_geojson(input_f, output_f, mode='foot', local_planet=None):
osmdata = LoadOsm(mode)
if local_planet != None:
osmdata.getArea = lambda lat, lon: None
osmdata.api = None
print('loading osm data (this may take a while)...')
osmdata.loadOsm(local_planet)
print('starting router...')
router = Router(osmdata)
print('processing shapes...')
# First load up the shapes
layer = geojson.load(input_f)
non_linestring = 0
not_two_points = 0
unsuccessful = 0
successful = 0
very_long = 0
first = True
output_f.write('{"crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:OGC:1.3:CRS84"}}, "type": "FeatureCollection", "features": [\n')
for feature in layer.features:
if feature.geometry.type != 'LineString':
# Not a LineString, skip!
non_linestring += 1
continue
geometry = list(feature.geometry.coordinates)
if len(geometry) != 2:
# LineString with other than two points, skip!
not_two_points += 1
continue
if pythagoras(*geometry[0] + geometry[1]) > 1.0:
very_long += 1
continue
# Now find a route. Data has x,y coordinates, but function is y,x, so
# reverse the parameters.
start = osmdata.findNode(*geometry[0][::-1])
end = osmdata.findNode(*geometry[1][::-1])
result, route = router.doRoute(start, end)
if result != 'success':
unsuccessful += 1
continue
routed_geometry = []
for node_id in route:
node = osmdata.rnodes[node_id]
routed_geometry.append((node[1], node[0]))
new_feature = geojson.Feature(
geometry=geojson.LineString(coordinates=routed_geometry),
properties=feature.properties,
id=feature.id,
)
if not first:
output_f.write(',\n')
first = False
geojson.dump(new_feature, output_f)
output_f.flush()
successful += 1
output_f.write('\n]}\n')
output_f.close()
print('%d LineStrings routed. Errors: %d non-linestring(s), %d linestring(s) with !=2 points, %d very long, %d unsuccessful routings' % (successful, non_linestring, not_two_points, very_long, unsuccessful))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('input', nargs=1, type=argparse.FileType('rb'))
parser.add_argument('-o', '--output', required=True, type=argparse.FileType('wb'))
parser.add_argument('-m', '--mode', default='foot',
help='Mode of transportation to route on [default: %(default)s]')
parser.add_argument('-l', '--local-planet',
help='Use a local OSM XML file. Make sure to crop it for good performance!')
options = parser.parse_args()
route_geojson(options.input[0], options.output, options.mode, options.local_planet)
if __name__ == "__main__":
main()