This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoJsonParser.go
62 lines (54 loc) · 1.69 KB
/
geoJsonParser.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
package turfgo
import (
"encoding/json"
"github.com/kpawlik/geojson"
)
func EncodePoint(point *Point) *geojson.Point {
var c geojson.Coordinate
c[0] = geojson.CoordType(point.Lng)
c[1] = geojson.CoordType(point.Lat)
return geojson.NewPoint(c)
}
func DecodePoint(coord geojson.Coordinate) *Point {
return &Point{float64(coord[1]), float64(coord[0])}
}
func EncodeMultiPointsIntoFeature(points []*Point) *geojson.Feature {
var coordinates = make(geojson.Coordinates, len(points))
for i := range points {
var c geojson.Coordinate
c[0] = geojson.CoordType(points[i].Lng)
c[1] = geojson.CoordType(points[i].Lat)
coordinates[i] = c
}
multiPoint := geojson.NewMultiPoint(coordinates)
return geojson.NewFeature(multiPoint, nil, nil)
}
func EncodeMultiPointsIntoLineString(points []*Point) *geojson.Feature {
var coordinates = make(geojson.Coordinates, len(points))
for i := range points {
var c geojson.Coordinate
c[0] = geojson.CoordType(points[i].Lng)
c[1] = geojson.CoordType(points[i].Lat)
coordinates[i] = c
}
lineString := geojson.NewLineString(coordinates)
return geojson.NewFeature(lineString, nil, nil)
}
func EncodeFeatureCollection(features []*geojson.Feature) *geojson.FeatureCollection {
return geojson.NewFeatureCollection(features)
}
// DecodeLineStringFromFeatureJSON decode geojson feature type lineString into *LineString
func DecodeLineStringFromFeatureJSON(gj []byte) (*LineString, error) {
var f *geojson.Feature
json.Unmarshal(gj, &f)
g, err := f.GetGeometry()
if err != nil {
return nil, err
}
ls, _ := g.(*geojson.LineString)
points := []*Point{}
for _, c := range ls.Coordinates {
points = append(points, DecodePoint(c))
}
return NewLineString(points), nil
}