-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeojsoncoords.go
56 lines (49 loc) · 1.49 KB
/
geojsoncoords.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
// Package geojsoncoords extract coordinates from GeoJSON objects
package geojsoncoords
import (
"reflect"
)
// ExtractCoordinates return a single slice of coordinates that
// the GeoJSON object contains
func ExtractCoordinates(geojson []byte) ([][]float64, error) {
fc, err := UnmarshalFeatureCollection(geojson)
if err != nil {
return nil, err
}
result := [][]float64{}
for _, s := range fc.Features {
if s.Coordinates != nil {
coordsSlice := FlattenCoordsTo2DSlice(s.Coordinates)
result = append(result, coordsSlice...)
}
if s.Geometry != nil {
coordsSlice := FlattenCoordsTo2DSlice(s.Geometry.Coordinates)
result = append(result, coordsSlice...)
}
}
return result, nil
}
// FlattenCoordsTo2DSlice return a single slice of coordinates from a slice of slices
func FlattenCoordsTo2DSlice(coordinates ...interface{}) (coordsSlice [][]float64) {
flattenCoords := FlattenSlice(coordinates)
for i := 0; i < len(flattenCoords); i = i + 2 {
coords := []float64{}
coords = append(coords, flattenCoords[i].(float64))
coords = append(coords, flattenCoords[i+1].(float64))
coordsSlice = append(coordsSlice, coords)
}
return coordsSlice
}
// FlattenSlice recursively flattens a nested interface
func FlattenSlice(args ...interface{}) (list []interface{}) {
for _, v := range args {
if reflect.TypeOf(v).Kind() == reflect.Slice {
for _, z := range FlattenSlice((v.([]interface{}))...) {
list = append(list, z)
}
} else {
list = append(list, v)
}
}
return list
}