This repository was archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
179 lines (162 loc) · 4.07 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"log"
"os"
"strconv"
"github.com/donomii/goof"
)
//{ "type": "Feature", "id": "2292591621", "geometry": { "type": "Point", "coordinates": [ 121.534116, 25.0146649 ] }, "properties": { "name": "NET", "shop": "clothes", "wheelchair": "limited", "addr:street": "羅斯福路四段", "addr:housenumber": "64", "toilets:wheelchair": "no", "wheelchair:description": "門口有一個階梯,每層樓上下只有樓梯,沒有電梯
var strict bool
type Geometry struct {
Type string `json:"type"`
Coordinates []float64 `json:"coordinates"`
}
type GeoJSON struct {
Type string `json:"type"`
Id string `json:"id"`
Geometry Geometry `json:"geometry"`
Properties map[string]string `json:"properties"`
}
func checkErr(err error) {
if err != nil {
log.Println(fmt.Sprintf("Error: %v", err))
}
}
func main() {
//defer profile.Start(profile.TraceProfile).Stop()
var xmlFile *bufio.Reader
var inFile string
var outFile string
var compression string
flag.StringVar(&compression, "compression", "", "Input is compressed with bz2 or gz")
flag.BoolVar(&strict, "strict", false, "Emit correct geojson format. By default, emit grep-friendly geojson.")
flag.Parse()
args := flag.Args()
///log.Println(args)
if len(args) > 0 {
inFile = args[0]
log.Println("Reading from", inFile)
if inFile == "-" {
inFile = ""
}
xmlFile = goof.OpenBufferedInput(inFile, compression)
//defer xmlFile.Close()
} else {
log.Println("Reading from stdin")
xmlFile = goof.OpenBufferedInput("", compression)
}
outBuff := bufio.NewWriter(os.Stdout)
if len(args) > 1 {
outFile = args[1]
log.Println("Writing to", outFile)
if outFile == "-" {
} else {
f, err := os.Create(outFile)
checkErr(err)
outBuff = bufio.NewWriter(f)
}
//defer xmlFile.Close()
}
if strict {
fmt.Fprintf(outBuff, "[")
}
//out := json.NewEncoder(outBuff)
firstItem := true
decoder := xml.NewDecoder(xmlFile)
var current_element *xml.StartElement
var tags map[string]string
tags = map[string]string{}
a := map[string]string{}
for {
gen_token, err := decoder.Token()
if gen_token == nil {
log.Println("Error while decoding: ", err)
break
}
//log.Printf("%V\n", gen_token)
switch token := gen_token.(type) {
case xml.StartElement:
if token.Name.Local == "node" {
a = map[string]string{}
tags = map[string]string{}
if se, ok := gen_token.(xml.StartElement); ok {
for _, v := range se.Attr {
a[fmt.Sprintf("%v", v.Name)] = v.Value
}
}
current_element = &token
//log.Println(gen_token.Attr.());
} else if token.Name.Local == "tag" {
if current_element != nil {
var key string
var value string
for _, attr := range token.Attr {
switch attr.Name.Local {
case "k":
key = attr.Value
case "v":
value = attr.Value
}
}
// log.Printf("%s = %s\n", key, value)
if key != "" && value != "" {
tags[key] = value
}
}
}
case xml.EndElement:
if token.Name.Local == "node" {
lat, ok := a["lat"]
if !ok {
lat = a["{ lat}"]
}
lon, ok := a["lon"]
if !ok {
lon = a["{ lon}"]
}
id, _ := a["{ id}"]
flat, _ := strconv.ParseFloat(lon, 64)
flon, _ := strconv.ParseFloat(lat, 64)
geom := Geometry{
Type: "Point",
Coordinates: []float64{flat, flon},
}
g := GeoJSON{
Type: "Feature",
Id: id,
Geometry: geom,
Properties: tags,
}
byt, err := json.Marshal(g)
if err != nil {
panic(fmt.Sprintf("Could not encode output data because: %v", err))
}
//out.Encode(g)
if firstItem {
firstItem = false
} else {
if strict {
fmt.Fprintf(outBuff, ",")
}
}
fmt.Fprintf(outBuff, "%s", string(byt))
if !strict {
fmt.Fprintf(outBuff, "\n")
}
outBuff.Flush()
current_element = nil
tags = nil
}
}
}
if strict {
fmt.Fprintf(outBuff, "]")
}
outBuff.Flush()
log.Println("Job's a good'un, boss!")
}