-
Notifications
You must be signed in to change notification settings - Fork 0
/
way.go
236 lines (185 loc) · 4.93 KB
/
way.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
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
"sync"
pb "github.com/Timahawk/osm_file_decoder/proto"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
)
type MyWayFeature struct {
Feature geojson.Feature
LineString orb.LineString
Polygon orb.Polygon
Refs []int64
}
// type MyPolygonFeature struct {
// Feature geojson.Feature
// LineString orb.LineString
// }
func DecodeWay(way *pb.Way, strtable [][]byte) (string, MyWayFeature) {
feature := MyWayFeature{
Feature: geojson.Feature{
ID: way.GetId(),
Properties: map[string]interface{}{}}}
keys := way.GetKeys()
values := way.GetVals()
for i, k := range keys {
feature.Feature.Properties[string(strtable[k])] = strtable[values[i]]
}
refs := way.GetRefs()
delta_ref := int64(0)
ls := orb.LineString{}
ring := orb.Ring{}
for _, k := range refs {
delta_ref = k + delta_ref
feature.Refs = append(feature.Refs, delta_ref)
point, ok := largeMapNode.Get(fmt.Sprint(delta_ref))
if ok {
ls = append(ls, point.Point)
ring = append(ring, point.Point)
}
}
if feature.Refs[0] == feature.Refs[len(feature.Refs)-1] {
polygon := orb.Polygon{ring}
feature.Feature.BBox = geojson.NewBBox(polygon.Bound())
feature.Feature.Type = polygon.GeoJSONType()
feature.Feature.Geometry = polygon
return fmt.Sprint(feature.Feature.ID), feature
} else {
feature.Feature.BBox = geojson.NewBBox(ls.Bound())
feature.Feature.Type = ls.GeoJSONType()
feature.Feature.Geometry = ls
return fmt.Sprint(feature.Feature.ID), feature
}
}
/*
func decodePolygon(way *pb.Way, strtable [][]byte) (string, MyPolygon) {
mlp := MyPolygon{way.GetId(), map[string]string{}, []int64{}, *geom.NewPolygon(geom.XY)}
keys := way.GetKeys()
values := way.GetVals()
for i, k := range keys {
mlp.Tags[string(strtable[k])] = string(strtable[values[i]])
}
refs := way.GetRefs()
delta_ref := int64(0)
liste_of_coords := []geom.Coord{}
for _, k := range refs {
delta_ref = k + delta_ref
mlp.Refs = append(mlp.Refs, delta_ref)
coord, ok := largeMapNode.Get(fmt.Sprint(delta_ref))
if ok {
liste_of_coords = append(liste_of_coords, coord.Coords.Coords())
}
}
mlp.Coords = *mlp.Coords.MustSetCoords([][]geom.Coord{liste_of_coords})
return fmt.Sprint(way.GetId()), mlp
}
*/
func DecodeWays(pg *pb.PrimitiveGroup, strtable [][]byte, wg *sync.WaitGroup) {
defer wg.Done()
ways := pg.GetWays()
for _, way := range ways {
id, feature := DecodeWay(way, strtable)
largeMapWays.Set(id, feature)
}
}
func (f *MyWayFeature) SQLString() string {
// Schreibe zum String wenn es einen Tag hat.
if len(f.Feature.Properties) == 0 {
return ""
}
var sb strings.Builder
sb.WriteString("(" + fmt.Sprint(f.Feature.ID) + ", ('")
j := 0
len := len(f.Feature.Properties)
for key, value := range f.Feature.Properties {
value2 := fmt.Sprintf("%s", value)
value2 = strings.Replace(value2, "'", "''", -1)
// Muss wegen dem Komma am Ende.
if j+1 == len {
sb.WriteString(fmt.Sprintf("%q=>%q", key, value2))
} else {
sb.WriteString(fmt.Sprintf("%q=>%q, ", key, value2))
}
j += 1
}
sb.WriteString("'), ST_GeomFromGeoJSON('{\"type\":\"" + f.Feature.Geometry.GeoJSONType() + "\", \"coordinates\":")
str, err := json.Marshal(f.Feature.Geometry)
if err != nil {
log.Fatal(err)
}
sb.WriteString(string(str) + "}'))")
return sb.String()
}
/*
func (f *MyLineString) SQLString() string {
// Schreibe zum String wenn es einen Tag hat.
if len(ls.Tags) == 0 {
return ""
}
var sb strings.Builder
sb.WriteString("(" + fmt.Sprint(ls.Id) + ", ('")
j := 0
len := len(ls.Tags)
for key, value := range ls.Tags {
// Das weil das sonst Escaped.
// TODO nicht weglasssen sondern Umformen
if strings.ContainsRune(value, '\'') {
j += 1
continue
}
// Muss wegen dem Komma am Ende.
if j+1 == len {
sb.WriteString(fmt.Sprintf("%q=>%q", key, value))
} else {
sb.WriteString(fmt.Sprintf("%q=>%q, ", key, value))
}
j += 1
}
sb.WriteString("'), ST_GeomFromText('")
encoder := wkt.NewEncoder()
str, err := encoder.Encode(&ls.Coords)
if err != nil {
log.Fatal(err)
}
sb.WriteString(str + "'))")
return sb.String()
}
/*
func (pg *MyPolygon) SQLString() string {
// Schreibe zum String wenn es einen Tag hat.
if len(pg.Tags) == 0 {
return ""
}
var sb strings.Builder
sb.WriteString("(" + fmt.Sprint(pg.Id) + ", ('")
j := 0
len := len(pg.Tags)
for key, value := range pg.Tags {
// Das weil das sonst Escaped.
// TODO nicht weglasssen sondern Umformen
if strings.ContainsRune(value, '\'') {
j += 1
continue
}
// Muss wegen dem Komma am Ende.
if j+1 == len {
sb.WriteString(fmt.Sprintf("%q=>%q", key, value))
} else {
sb.WriteString(fmt.Sprintf("%q=>%q, ", key, value))
}
j += 1
}
sb.WriteString("'), ST_GeomFromText('")
encoder := wkt.NewEncoder()
str, err := encoder.Encode(&pg.Coords)
if err != nil {
log.Fatal(err)
}
sb.WriteString(str + "'))")
return sb.String()
}
*/