/
osm.go
294 lines (249 loc) · 6.62 KB
/
osm.go
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// 48.66,1.90,49.040,2.85
// 48.8077,2.2467,48.9059,2.4245
package main
import (
"xml"
"os"
"log"
// "time"
"flag"
"math"
"strings"
"strconv"
"runtime"
"archive/zip"
"path/filepath"
"compress/bzip2"
)
var (
filenameptr = flag.String("f", "", "You must specify an osm file - Required")
boundsptr = flag.String("b", "", "Bounds to limit OSM import. Format minlat,minlon,maxlat,maxlon")
)
func main() {
flag.Parse()
filename := *filenameptr
var bounds Bounds
if *boundsptr != "" {
comp := strings.Split(*boundsptr, ",",-1)
minlat, _ := strconv.Atof64(comp[0])
minlon, _ := strconv.Atof64(comp[1])
maxlat, _ := strconv.Atof64(comp[2])
maxlon, _ := strconv.Atof64(comp[3])
bounds = Bounds{minlat,minlon,maxlat,maxlon}
log.Println("loadingWithBounds", bounds)
} else {
bounds = Bounds{-90,-180,90,180}
}
if filename == "" {
flag.Usage()
os.Exit(1)
}
result := OSMFile{Ways:make([]Way,0)}
parser := getParser(filename)
// log.Println("parser", parser)
token, err := parser.Token()
var currentWay *Way
shouldConserveCurrentWay := false
skippedNodeCount := 0
nodeCount := 0
wayCount := 0
usedNodesCount := 0
nodes := make(map[int]Node)
for err == nil {
switch token.(type) {
case nil:
log.Println("nil Token ?")
break
case xml.StartElement:
startElement, ok := token.(xml.StartElement)
if !ok { break }
if startElement.Name.Local == "way" {
currentWay = new(Way)
for _, attr := range startElement.Attr {
if attr.Name.Local == "id" {
v, _ := strconv.Atoi(attr.Value)
currentWay.Id = v
}
}
} else if startElement.Name.Local == "nd" {
if currentWay == nil { break }
for _, attr := range startElement.Attr {
if attr.Name.Local == "ref" {
v, _ := strconv.Atoi(attr.Value)
if foundNode, ok := nodes[v]; ok {
currentWay.Nodes = append(currentWay.Nodes, foundNode)
usedNodesCount = usedNodesCount + 1
}
}
}
} else if startElement.Name.Local == "tag" {
if currentWay == nil { break }
var key, value string
for _, attr := range startElement.Attr {
if attr.Name.Local == "k" {
key = attr.Value
} else if attr.Name.Local == "v" {
value = attr.Value
}
}
if key != "" && value != "" {
switch key {
case "highway":
switch value {
case "motorway", "motorway_link", "trunk", "trunk_link", "primary", "primary_link", "secondary", "secondary_link", "tertiary":
shouldConserveCurrentWay = true
}
}
}
} else if startElement.Name.Local == "node" {
node := new(Node)
for _, attr := range startElement.Attr {
if attr.Name.Local == "id" {
v, _ := strconv.Atoi(attr.Value)
node.Id = v
} else if attr.Name.Local == "lat" {
v, _ := strconv.Atof64(attr.Value)
node.Lat = v
} else if attr.Name.Local == "lon" {
v, _ := strconv.Atof64(attr.Value)
node.Lon = v
}
}
// log.Println("Node", node, node.Id, node.Lat, node.Lon)
if node.Within(bounds) {
nodes[node.Id] = *node
nodeCount = nodeCount + 1
if nodeCount % 100000 == 0 {
log.Println("Node count:", nodeCount)
}
} else {
skippedNodeCount = skippedNodeCount + 1
if skippedNodeCount % 100000 == 0 {
log.Println("Skipped node count:", skippedNodeCount)
}
}
}
break
case xml.EndElement:
endElement, ok := token.(xml.EndElement)
if !ok { break }
if endElement.Name.Local == "way" {
if shouldConserveCurrentWay && len(currentWay.Nodes) > 5 {
wayCount = wayCount + 1
if wayCount % 100000 == 0 {
log.Println("Way count:", wayCount)
}
result.Ways = append(result.Ways, *currentWay)
log.Println("new accepted way, node count:", len(currentWay.Nodes))
}
currentWay = nil
shouldConserveCurrentWay = false
}
break
case xml.CharData:
case xml.Comment:
case xml.ProcInst:
case xml.Directive:
break
}
token, err = parser.Token()
}
log.Println("Node count:", nodeCount)
log.Println("Skipped node count:", skippedNodeCount)
log.Println("Way count:", wayCount)
reallyUsedNodesCount := 0
for _, way := range result.Ways {
reallyUsedNodesCount = reallyUsedNodesCount + len(way.Nodes)
}
log.Println("Used nodes in ways count", usedNodesCount)
log.Println("Used nodes in ways count", reallyUsedNodesCount)
log.Printf("Before GC - bytes = %d - footprint = %d", runtime.MemStats.HeapAlloc, runtime.MemStats.Sys)
nodes = make(map[int]Node)
log.Println("Running GC")
runtime.GC()
log.Printf("After GC - bytes = %d - footprint = %d", runtime.MemStats.HeapAlloc, runtime.MemStats.Sys)
if err != nil && err != os.EOF {
panic(err)
}
}
func getParser(filename string) *xml.Parser {
var parser *xml.Parser
if filepath.Ext(filename) == ".zip" {
zipcontainer, err := zip.OpenReader(filename)
if err != nil {
panic(err)
}
zippedfile := zipcontainer.File[0]
reader, err := zippedfile.Open()
if err != nil {
panic(err)
}
if filepath.Ext(zippedfile.FileHeader.Name) == ".bz2" {
log.Println("Uncompressing and unmarshaling XML of zip file")
parser = xml.NewParser(bzip2.NewReader(reader))
} else {
log.Println("Unmarshaling XML of zip file")
parser = xml.NewParser(reader)
}
reader.Close()
if err != nil {
panic(err)
}
} else {
openfile, err := os.Open(filename)
if err != nil {
panic(err)
}
if filepath.Ext(filename) == ".bz2" {
log.Println("Uncompressing and unmarshaling XML")
parser = xml.NewParser(bzip2.NewReader(openfile))
} else {
log.Println("Unmarshaling XML")
parser = xml.NewParser(openfile)
}
if err != nil {
panic(err)
}
}
return parser
}
type OSMFile struct {
Bounds Bounds
Ways []Way
}
type Bounds struct {
Minlat float64
Minlon float64
Maxlat float64
Maxlon float64
}
type Node struct {
Id int
Lat float64
Lon float64
}
func (n *Node)Within(bounds Bounds) bool {
if n.Lat >= bounds.Minlat && n.Lon >= bounds.Minlon && n.Lat <= bounds.Maxlat && n.Lon <= bounds.Maxlon {
return true
}
return false
}
type Way struct {
Id int
Nodes []Node
}
type Point struct {
X, Y float64
}
func (p Point)DistanceTo(other Point) float64 {
return math.Sqrt(math.Pow(math.Fabs(p.X - other.X), 2) + math.Pow(math.Fabs(p.Y - other.Y), 2))
}
func TriangleAltitude(A, B, C Point) float64 {
a := A.DistanceTo(B)
b := A.DistanceTo(C)
return a*b/2*TriangleCircumradius(a, b, B.DistanceTo(C))
}
// TriangleCircumradius return the circumradius for the given triangle with edge lengths a, b and c
func TriangleCircumradius(a, b, c float64) float64 {
return (a*b*c)/math.Sqrt((a+b+c)*(b+c-a)*(c+a-b)*(a+b-c))
}