-
Notifications
You must be signed in to change notification settings - Fork 1
/
geosfromorb.go
79 lines (72 loc) · 2.79 KB
/
geosfromorb.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
package geobabel
import (
"fmt"
"github.com/paulmach/orb"
"github.com/twpayne/go-geos"
)
func NewGEOSGeomFromOrbGeometry(geosContext *geos.Context, orbGeometry orb.Geometry) *geos.Geom {
switch orbGeometry := orbGeometry.(type) {
case orb.Point:
return geosContext.NewPoint(geosCoordsFromOrbPoint(orbGeometry))
case orb.LineString:
return geosContext.NewLineString(geosCoordsFromOrbLineString(orbGeometry))
case orb.Ring:
return geosContext.NewLinearRing(geosCoordsFromOrbRing(orbGeometry))
case orb.Polygon:
return geosContext.NewPolygon(geosCoordsFromOrbPolygon(orbGeometry))
case orb.MultiPoint:
geosPoints := make([]*geos.Geom, 0, len(orbGeometry))
for _, orbPoint := range orbGeometry {
geosPoint := NewGEOSGeomFromOrbGeometry(geosContext, orbPoint)
geosPoints = append(geosPoints, geosPoint)
}
return geosContext.NewCollection(geos.TypeIDMultiPoint, geosPoints)
case orb.MultiLineString:
geosLineStrings := make([]*geos.Geom, 0, len(orbGeometry))
for _, orbLineString := range orbGeometry {
geosLineString := NewGEOSGeomFromOrbGeometry(geosContext, orbLineString)
geosLineStrings = append(geosLineStrings, geosLineString)
}
return geosContext.NewCollection(geos.TypeIDMultiLineString, geosLineStrings)
case orb.MultiPolygon:
geosPolygons := make([]*geos.Geom, 0, len(orbGeometry))
for _, orbPolygon := range orbGeometry {
geosPolygon := NewGEOSGeomFromOrbGeometry(geosContext, orbPolygon)
geosPolygons = append(geosPolygons, geosPolygon)
}
return geosContext.NewCollection(geos.TypeIDMultiPolygon, geosPolygons)
case orb.Collection:
geosGeometries := make([]*geos.Geom, 0, len(orbGeometry))
for _, orbGeometry := range orbGeometry {
geosGeom := NewGEOSGeomFromOrbGeometry(geosContext, orbGeometry)
geosGeometries = append(geosGeometries, geosGeom)
}
return geosContext.NewCollection(geos.TypeIDGeometryCollection, geosGeometries)
default:
panic(fmt.Sprintf("%T: unsupported type", orbGeometry))
}
}
func geosCoordsFromOrbPoint(orbPoint orb.Point) []float64 {
return []float64{orbPoint[0], orbPoint[1]}
}
func geosCoordsFromOrbLineString(orbLineString orb.LineString) [][]float64 {
geosCoords := make([][]float64, 0, len(orbLineString))
for _, orbPoint := range orbLineString {
geosCoords = append(geosCoords, geosCoordsFromOrbPoint(orbPoint))
}
return geosCoords
}
func geosCoordsFromOrbRing(orbRing orb.Ring) [][]float64 {
geosCoords := make([][]float64, 0, len(orbRing))
for _, orbPoint := range orbRing {
geosCoords = append(geosCoords, geosCoordsFromOrbPoint(orbPoint))
}
return geosCoords
}
func geosCoordsFromOrbPolygon(orbPolygon orb.Polygon) [][][]float64 {
geosCoords := make([][][]float64, 0, len(orbPolygon))
for _, orbRing := range orbPolygon {
geosCoords = append(geosCoords, geosCoordsFromOrbRing(orbRing))
}
return geosCoords
}