-
Notifications
You must be signed in to change notification settings - Fork 0
/
osm.go
71 lines (57 loc) · 1.21 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
package osm
import (
"compress/bzip2"
"encoding/json"
"encoding/xml"
"log"
"os"
)
type Osm struct {
XMLName xml.Name `xml:"osm"`
Node []*Node `xml:"node"`
Way []*Way `xml:"way"`
Relation []*Relation `xml:"relation"`
}
// Loads packaged version of Melbourne OSM ( perhaps somewhat outdated )
func LoadPackagedMelbourne() *Osm {
return LoadFromBz2(packageDir() + "/melbourne.osm.bz2")
}
// Loads bzip'ed xml
func LoadFromBz2(filename string) *Osm {
bzip_file, err := os.Open(filename)
defer bzip_file.Close()
if err != nil {
log.Panic(err)
}
osm_file := bzip2.NewReader(bzip_file)
decoder := xml.NewDecoder(osm_file)
osm := Osm{}
err = decoder.Decode(&osm)
if err != nil {
panic(err)
}
return &osm
}
func LoadFromJson(filename string) *Osm {
json_file, err := os.Open(filename)
defer json_file.Close()
if err != nil {
log.Panic(err)
}
decoder := json.NewDecoder(json_file)
osm := Osm{}
err = decoder.Decode(&osm)
if err != nil {
panic(err)
}
return &osm
}
func (osm *Osm) SaveToJson(filename string) {
json_file, err := os.Create(filename)
if err != nil {
panic(err)
}
defer json_file.Close()
json_encoder := json.NewEncoder(json_file)
json_encoder.Encode(osm)
}