diff --git a/.fleet/settings.json b/.fleet/settings.json new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md index e034162..3e11eb8 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ This version also include the clustering module that doesn't exist in the offici - [ ] flattenReduce - [x] getCoord - [x] getCoords -- [ ] getGeom +- [x] getGeom - [x] getType - [ ] geomEach - [ ] geomReduce diff --git a/invariant/invariant.go b/invariant/invariant.go index 27e696e..9f50097 100644 --- a/invariant/invariant.go +++ b/invariant/invariant.go @@ -207,3 +207,21 @@ func GetType(geojson interface{}) string { } return "invalid" } + + +// GetGeom gets the geometry from Feature or Geometry Object +// +// Examples: +// +// fp, err := feature.FromJSON("{ \"type\": \"Feature\", \"properties\": {}, \"geometry\": { \"type\": \"Point\", \"coordinates\": [102, 0.5] } }") +// result := GetGeom(fp) +// = {"geometry\": { \"type\": \"Point\", \"coordinates\": [102, 0.5] } +func GetGeom(geojson interface{}) (*geometry.Geometry, error) { + switch gtp := geojson.(type) { + case *feature.Feature: + return >p.Geometry, nil + case *geometry.Geometry: + return gtp, nil + } + return nil, errors.New("invalid type") +} \ No newline at end of file diff --git a/invariant/invariant_test.go b/invariant/invariant_test.go index 607a191..ab59213 100644 --- a/invariant/invariant_test.go +++ b/invariant/invariant_test.go @@ -10,6 +10,80 @@ import ( "github.com/tomchavakis/geojson/geometry" ) +func TestGetGeom(t *testing.T){ + type args struct { + coords interface{} + } + tests := map[string]struct { + args args + want *geometry.Geometry + wantErr bool + err error + }{ + "error - point": { + args: args{ + coords: &geometry.Point{ + Lat: 23.52, + Lng: 44.34, + }, + }, + wantErr: true, + err: errors.New("invalid type"), + }, + "geometry - point": { + args: args{ + coords: &geometry.Geometry{ + GeoJSONType: geojson.Point, + Coordinates: []float64{44.34, 23.52}, + }, + }, + wantErr: false, + want: &geometry.Geometry{ + GeoJSONType: geojson.Point, + Coordinates: []float64{44.34, 23.52}, + }, + }, + "feature - point": { + args: args{ + coords: &feature.Feature{ + ID: "", + Type: geojson.Feature, + Properties: map[string]interface{}{}, + Bbox: []float64{}, + Geometry: geometry.Geometry{ + GeoJSONType: geojson.Polygon, + Coordinates: [][][]float64{ + {{2, 1}, {4, 3}}, {{6, 5}, {8, 7}}, + }, + }, + }, + }, + wantErr: false, + want: &geometry.Geometry{ + GeoJSONType: geojson.Polygon, + Coordinates: [][][]float64{ + {{2, 1}, {4, 3}}, {{6, 5}, {8, 7}}, + }, + }, + }, + } + for name, tt := range tests { + t.Run(name, func(t *testing.T){ + geom,err := GetGeom(tt.args.coords) + if (err != nil) && tt.wantErr { + if err.Error() != tt.err.Error() { + t.Errorf("TestGetGeom() error = %v, wantErr %v", err.Error(), tt.err.Error()) + return + } + } + + if got := geom; !reflect.DeepEqual(got, tt.want) { + t.Errorf("TestGetGeom() = %v, want %v", got, tt.want) + } + }) + } +} + func TestGetCoord(t *testing.T) { type args struct { coords interface{}