func geometryToS2Polygon(geom geojson.GeoJSON) (*s2.Polygon, error) { var poly *s2.Polygon builder := s2.NewPolygonBuilder(s2.DIRECTED_XOR()) switch geom := geom.(type) { case geojson.Polygon: for _, ring := range geom.Coordinates { var points []s2.Point for _, v := range ring { points = append(points, s2.PointFromLatLng(s2.LatLngFromDegrees(v[1], v[0]))) } builder.AddLoop(s2.NewLoopFromPath(points)) } poly = new(s2.Polygon) builder.AssemblePolygon(poly, nil) } return poly, nil }
package geo_test import ( "fmt" "testing" "github.com/TomiHiltunen/geohash-golang" "github.com/davidreynolds/gos2/s2" "github.com/nick11roberts/geozip" "github.com/stretchr/testify/assert" "github.com/taik/geo-benchmark/basic" ) var topLeft = s2.LatLngFromDegrees(40.755534, -74.001743) var bottomRight = s2.LatLngFromDegrees(40.732351, -73.984963) var center = s2.LatLngFromDegrees(40.743700, -73.991615) var points = []s2.LatLng{ topLeft, bottomRight, } func TestRectFromLatLong(t *testing.T) { rect := geo.RectFromLatLong(points) assert.True(t, rect.IsValid()) assert.True(t, rect.Contains(center)) } func BenchmarkRectContains(b *testing.B) { rect := geo.RectFromLatLong(points)