-
Notifications
You must be signed in to change notification settings - Fork 2
/
osmToPaths.groovy
executable file
·170 lines (140 loc) · 2.96 KB
/
osmToPaths.groovy
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import groovy.json.*
inputFile = args[0]
opendata = new XmlSlurper().parse(inputFile)
nodeMap = [:]
class Node{
Double lat
Double lon
}
class Street{
String name
List points = []
}
opendata.node.each
{
def n = new Node()
n.lat = Double.valueOf( [email protected]() )
n.lon = Double.valueOf( [email protected]() )
k = "${it.@id}"
nodeMap[k] = n;
}
streetMap = [:]
opendata.way.each
{
way = it
tags = [:]
it.tag.each
{
tags["${it.@k}"] = "${it.@v}"
}
streetName = tags["name"]
//building = tags["building"]
if(streetName != null) // && ( streetName.startsWith("Via") || streetName.startsWith("Piaz")))
{
def sn = [email protected]()
def street = streetMap[sn]
if(street == null)
{
street = new Street()
street.name = streetName
streetMap.put(sn,street)
}
way.nd.each
{
k = "${it.@ref}"
node = nodeMap[k]
street.points.add(node)
}
}
}
class RoutePath{
String path_id
List streets = []
}
def pathList = []
opendata.relation.each
{
rel = it
tags = [:]
it.tag.each
{
tags["${it.@k}"] = "${it.@v}"
}
type = tags["type"]
if(type == "route")
{
def path = new RoutePath()
path.path_id = rel.@id
it.member.each
{
if(it.@type == "way")
{
def ref = streetMap[ "${it.@ref}"]
if(ref != null)
{
path.streets.add(ref)
}
}
}
if(! path.streets.isEmpty())
pathList.add(path)
}
}
// add fine-grained points
def calculateDistance = { lat1,lon1,lat2,lon2 ->
earthRadius = 6371000 // raggio della terra
dLat = Math.toRadians(lat2 - lat1)
dLng = Math.toRadians(lon2 - lon1)
a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2)
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
dist = (earthRadius * c)
return dist
}
def carLen = 4 //meter
def routes = []
pathList.each
{
def route = new RoutePath()
routes.add(route)
route.path_id = it.path_id
it.streets.each
{
def street = new Street()
street.name = it.name
route.streets.add(street)
def finePoints = []
def prevPoint = it.points[0]
it.points.each
{
finePoints.add(prevPoint)
Double lat1 = prevPoint.lat
Double lon1 = prevPoint.lon
Double lat2 = it.lat
Double lon2 = it.lon
def lenMeter = calculateDistance(lat1,lon1,lat2,lon2)
def lenDegree = Math.sqrt((lat1-lat2)**2+(lon1-lon2)**2)
int carNum = lenMeter / carLen
if(carNum > 0)
{
Double carDegree = lenDegree/carNum
Double fi = Math.atan2(lon1 - lon2, lat1 - lat2 )
Double x = carDegree * Math.cos(fi)
Double y = carDegree * Math.sin(fi)
carNum.times
{
Double lat = lat2 + x*it
Double lon = lon2 + y*it
def point = new Node()
point.lat = lat
point.lon = lon
finePoints.add(point)
}
}
prevPoint = it
}//each point
finePoints.add(prevPoint)
street.points = finePoints
}//streets
}
def json = JsonOutput.toJson(routes)
println JsonOutput.prettyPrint(json)