-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwrite_feature.go
351 lines (317 loc) · 10.2 KB
/
write_feature.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package vt
import (
"github.com/paulmach/go.geojson"
"reflect"
//"github.com/murphy214/geobuf/geobuf_raw"
p "github.com/murphy214/pbf"
"fmt"
)
// adding a geojson feature to a given layer
func (layer *LayerWrite) AddFeature(feature *geojson.Feature) {
// creating total bytes that holds the bytes for a given layer
var array1, array2, array3, array4, array5, array6, array7, array8, array9 []byte
// refreshing cursor
layer.RefreshCursor()
if feature.ID != nil {
// do the id shit
vv := reflect.ValueOf(feature.ID)
kd := vv.Kind()
switch kd {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
array3 = []byte{8}
array4 = p.EncodeVarint(uint64(vv.Int()))
}
}
if len(feature.Properties) > 0 {
// do the tag shit here
array5 = []byte{18} // the key val
array6 = WritePackedUint32(layer.GetTags(feature.Properties))
}
if feature.Geometry != nil {
// do the geometry type shit here
var geomtype byte
switch feature.Geometry.Type {
case "Point", "MultiPoint":
geomtype = 1
case "LineString", "MultiLineString":
geomtype = 2
case "Polygon", "MultiPolygon":
geomtype = 3
}
array7 = []byte{24, geomtype}
}
var abort_bool bool
if feature.Geometry != nil {
switch feature.Geometry.Type {
case "Point":
array8 = []byte{34}
layer.Cursor.MakePointFloat(feature.Geometry.Point)
array9 = WritePackedUint32(layer.Cursor.Geometry)
case "LineString":
array8 = []byte{34}
layer.Cursor.MakeLineFloat(feature.Geometry.LineString)
if layer.Cursor.Count == 0 {
abort_bool = true
}
array9 = WritePackedUint32(layer.Cursor.Geometry)
case "Polygon":
array8 = []byte{34}
layer.Cursor.MakePolygonFloat(feature.Geometry.Polygon)
array9 = WritePackedUint32(layer.Cursor.Geometry)
case "MultiPoint":
array8 = []byte{34}
layer.Cursor.MakeMultiPointFloat(feature.Geometry.MultiPoint)
array9 = WritePackedUint32(layer.Cursor.Geometry)
case "MultiLineString":
array8 = []byte{34}
layer.Cursor.MakeMultiLineFloat(feature.Geometry.MultiLineString)
array9 = WritePackedUint32(layer.Cursor.Geometry)
case "MultiPolygon":
array8 = []byte{34}
layer.Cursor.MakeMultiPolygonFloat(feature.Geometry.MultiPolygon)
array9 = WritePackedUint32(layer.Cursor.Geometry)
}
}
// on the off chane one of my lines contains one point
if !abort_bool {
array1 = []byte{18}
array2 = p.EncodeVarint(uint64(len(array3) + len(array4) + len(array5) + len(array6) + len(array7) + len(array8) + len(array9)))
layer.Features = append(layer.Features, AppendAll(array1, array2, array3, array4, array5, array6, array7, array8, array9)...)
} else {
}
}
// adds a lazy feature to the layer write as effeciently as possible
func (layer *LayerWrite) AddFeatureLazy(feature *Feature) {
// creating total bytes that holds the bytes for a given layer
var array1, array2, array3, array4, array5, array6, array7, array8, array9 []byte
// refreshing cursor
layer.RefreshCursor()
if feature.ID != 0 {
// do the id shit
array3 = []byte{8}
array4 = p.EncodeVarint(uint64(feature.ID))
// vv := reflect.ValueOf(feature.ID)
// kd := vv.Kind()
// switch kd {
// case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// array3 = []byte{8}
// array4 = p.EncodeVarint(uint64(vv.Int()))
// }
}
if len(feature.Properties) > 0 {
// do the tag shit here
array5 = []byte{18} // the key val
array6 = WritePackedUint32(layer.GetTags(feature.Properties))
}
array7 = []byte{24, byte(feature.Geom_int)}
var abort_bool bool
array8 = []byte{34}
array9 = feature.GeomBytes()
// case "Point":
// array8 = []byte{34}
// layer.Cursor.MakePointFloat(feature.Geometry.Point)
// array9 = WritePackedUint32(layer.Cursor.Geometry)
// case "LineString":
// array8 = []byte{34}
// layer.Cursor.MakeLineFloat(feature.Geometry.LineString)
// if layer.Cursor.Count == 0 {
// abort_bool = true
// }
// array9 = WritePackedUint32(layer.Cursor.Geometry)
// case "Polygon":
// array8 = []byte{34}
// layer.Cursor.MakePolygonFloat(feature.Geometry.Polygon)
// array9 = WritePackedUint32(layer.Cursor.Geometry)
// case "MultiPoint":
// array8 = []byte{34}
// layer.Cursor.MakeMultiPointFloat(feature.Geometry.MultiPoint)
// array9 = WritePackedUint32(layer.Cursor.Geometry)
// case "MultiLineString":
// array8 = []byte{34}
// layer.Cursor.MakeMultiLineFloat(feature.Geometry.MultiLineString)
// array9 = WritePackedUint32(layer.Cursor.Geometry)
// case "MultiPolygon":
// array8 = []byte{34}
// layer.Cursor.MakeMultiPolygonFloat(feature.Geometry.MultiPolygon)
// array9 = WritePackedUint32(layer.Cursor.Geometry)
// }
// on the off chane one of my lines contains one point
if !abort_bool {
array1 = []byte{18}
array2 = p.EncodeVarint(uint64(len(array3) + len(array4) + len(array5) + len(array6) + len(array7) + len(array8) + len(array9)))
layer.Features = append(layer.Features, AppendAll(array1, array2, array3, array4, array5, array6, array7, array8, array9)...)
} else {
}
}
// adds a list of lazy features to write to the layerwrite this will be useful for filtering
func (laywrite *LayerWrite) AddFeaturesLazy(positions []int,srclayer *Layer) {
for _,pos := range positions {
feat,err := srclayer.FeaturePos(pos)
if err != nil {
fmt.Println(err)
} else {
laywrite.AddFeatureLazy(feat)
}
}
}
// function for adding the feature for a raw implementation
func (layer *LayerWrite) AddFeatureRaw(id int, geomtype int, geometry []uint32, properties map[string]interface{}) {
var array1, array2, array3, array4, array5, array6, array7, array8, array9 []byte
// refreshing cursor
layer.RefreshCursor()
if id > 0 {
// do the id shit
array3 = []byte{8}
array4 = p.EncodeVarint(uint64(id))
}
if len(properties) > 0 {
// do the tag shit here
array5 = []byte{18} // the key val
array6 = WritePackedUint32(layer.GetTags(properties))
}
if geomtype != 0 {
// do the geometry type shit here
array7 = []byte{24, byte(geomtype)}
}
// adding geometry
if len(geometry) > 0 {
array8 = []byte{34}
array9 = WritePackedUint32(geometry)
}
// on the off chane one of my lines contains one point
array1 = []byte{18}
array2 = p.EncodeVarint(uint64(len(array3) + len(array4) + len(array5) + len(array6) + len(array7) + len(array8) + len(array9)))
layer.Features = append(layer.Features, AppendAll(array1, array2, array3, array4, array5, array6, array7, array8, array9)...)
}
// adding a geobuf byte array to a given layer
// this function house's both the ingestion and output to vector tiles
// hopefully to reduce allocations
func (layer *LayerWrite) AddFeatureGeobuf(bytevals []byte) {
// the pbf representing a feauture
pbf := p.PBF{Pbf: bytevals, Length: len(bytevals)}
// creating total bytes that holds the bytes for a given layer
var array1, array2, array3, array4, array5, array6, array7, array8, array9 []byte
// refreshing cursor
layer.RefreshCursor()
key, val := pbf.ReadKey()
if key == 1 && val == 0 {
array3 = []byte{8}
startpos := pbf.Pos
pbf.ReadVarint()
array4 = pbf.Pbf[startpos:pbf.Pos]
}
tags := []uint32{}
for key == 2 && val == 2 {
// starting properties shit here
size := pbf.ReadVarint()
endpos := pbf.Pos + size
pbf.Pos += 1
keyvalue := pbf.ReadString()
keytag, keybool := layer.Keys_Map[keyvalue]
if keybool == false {
keytag = layer.AddKey(keyvalue)
}
tags = append(tags, keytag)
pbf.Pos += 1
pbf.ReadVarint()
newkey, _ := pbf.ReadKey()
var value interface{}
switch newkey {
case 1:
value = pbf.ReadString()
case 2:
value = pbf.ReadFloat()
case 3:
value = pbf.ReadDouble()
case 4:
value = pbf.ReadInt64()
case 5:
value = pbf.ReadUInt64()
case 6:
value = pbf.ReadUInt64()
case 7:
value = pbf.ReadBool()
}
valuetag, valuebool := layer.Values_Map[value]
if valuebool == false {
valuetag = layer.AddValue(value)
}
tags = append(tags, valuetag)
pbf.Pos = endpos
key, val = pbf.ReadKey()
}
array5 = []byte{18} // the key val
array6 = WritePackedUint32(tags)
var geomtype string
if key == 3 && val == 0 {
switch int(pbf.Pbf[pbf.Pos]) {
case 1:
geomtype = "Point"
case 2:
geomtype = "LineString"
case 3:
geomtype = "Polygon"
case 4:
geomtype = "MultiPoint"
case 5:
geomtype = "MultiLineString"
case 6:
geomtype = "MultiPolygon"
}
pbf.Pos += 1
key, val = pbf.ReadKey()
}
if len(geomtype) > 0 {
// do the geometry type shit here
var geomtypeb byte
switch geomtype {
case "Point", "MultiPoint":
geomtypeb = 1
case "LineString", "MultiLineString":
geomtypeb = 2
case "Polygon", "MultiPolygon":
geomtypeb = 3
}
array7 = []byte{24, geomtypeb}
}
var abort_bool bool
if key == 4 && val == 2 {
size := pbf.ReadVarint()
endpos := pbf.Pos + size
switch geomtype {
case "Point":
array8 = []byte{34}
layer.Cursor.MakePointFloat(pbf.ReadPoint(endpos))
array9 = WritePackedUint32(layer.Cursor.Geometry)
case "LineString":
array8 = []byte{34}
layer.Cursor.MakeLineFloat(pbf.ReadLine(0, endpos))
if layer.Cursor.Count == 0 {
abort_bool = true
}
array9 = WritePackedUint32(layer.Cursor.Geometry)
case "Polygon":
array8 = []byte{34}
layer.Cursor.MakePolygonFloat(pbf.ReadPolygon(endpos))
array9 = WritePackedUint32(layer.Cursor.Geometry)
case "MultiPoint":
array8 = []byte{34}
layer.Cursor.MakeMultiPointFloat(pbf.ReadLine(0, endpos))
array9 = WritePackedUint32(layer.Cursor.Geometry)
case "MultiLineString":
array8 = []byte{34}
layer.Cursor.MakeMultiLineFloat(pbf.ReadPolygon(endpos))
array9 = WritePackedUint32(layer.Cursor.Geometry)
case "MultiPolygon":
array8 = []byte{34}
layer.Cursor.MakeMultiPolygonFloat(pbf.ReadMultiPolygon(endpos))
array9 = WritePackedUint32(layer.Cursor.Geometry)
}
key, val = pbf.ReadKey()
}
if !abort_bool {
array1 = []byte{18}
array2 = p.EncodeVarint(uint64(len(array3) + len(array4) + len(array5) + len(array6) + len(array7) + len(array8) + len(array9)))
layer.Features = append(layer.Features, AppendAll(array1, array2, array3, array4, array5, array6, array7, array8, array9)...)
}
}