From 1bcdcfee9bc6048d9cc82b265135bab9a6ecd660 Mon Sep 17 00:00:00 2001 From: tomchavakis Date: Sun, 9 Jan 2022 16:22:18 +0100 Subject: [PATCH] GetGeometry Implementation --- invariant/invariant.go | 11 +++++++++ invariant/invariant_test.go | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/invariant/invariant.go b/invariant/invariant.go index 740e6cb..ab366b5 100644 --- a/invariant/invariant.go +++ b/invariant/invariant.go @@ -210,3 +210,14 @@ func GetType(geojson interface{}) string { } return "invalid" } + +// GetGeom returns the Geometry from Feature or Geometry Object +func GetGeom(geojson interface{}) *geometry.Geometry { + switch gtp := geojson.(type) { + case *feature.Feature: + return >p.Geometry + case *geometry.Geometry: + return gtp + } + return nil +} diff --git a/invariant/invariant_test.go b/invariant/invariant_test.go index ba32a73..9f41d55 100644 --- a/invariant/invariant_test.go +++ b/invariant/invariant_test.go @@ -767,3 +767,50 @@ func TestGetType(t *testing.T) { }) } } + +// func TestGetGeom(t *testing.T) { +// type args struct { +// geojson interface{} +// } +// fp, _ := feature.FromJSON("{ \"type\": \"Feature\", \"properties\": {}, \"geometry\": { \"type\": \"Point\", \"coordinates\": [102, 0.5] } }") +// ln, _ := geometry.FromJSON("{ \"type\": \"Feature\", \"properties\": {}, \"geometry\": { \"type\": \"LineString\", \"coordinates\": [ [-20.39062500000365, 33.72434000000235], [-3.5156249999990803, 47.51720099999992], [14.062499999996321, 16.97274100000141] ] } }") + +// tests := map[string]struct { +// args args +// want *geometry.Geometry +// }{ +// "error - required coords": { +// args: args{ +// geojson: "", +// }, +// want: nil, +// }, +// "feature - point": { +// args: args{ +// geojson: fp, +// }, +// want: &geometry.Geometry{ +// GeoJSONType: "Point", +// Coordinates: []interface{}{float64(102), float64(0.5)}, +// }, +// }, +// "geometry - lineString": { +// args: args{ +// geojson: ln, +// }, +// want: &geometry.Geometry{ +// GeoJSONType: "Point", +// Coordinates: []interface{}{float64(102), float64(0.5)}, +// }, +// }, +// } +// for name, tt := range tests { +// t.Run(name, func(t *testing.T) { +// geo := GetGeom(tt.args.geojson) + +// if got := geo; !reflect.DeepEqual(got, tt.want) { +// t.Errorf("TestGetCoord() = %v, want %v", got, tt.want) +// } +// }) +// } +// }