Skip to content

Commit

Permalink
getGeom implementation (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchavakis authored Dec 1, 2022
1 parent 9033ffb commit 51f4aca
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
Empty file added .fleet/settings.json
Empty file.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions invariant/invariant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 &gtp.Geometry, nil
case *geometry.Geometry:
return gtp, nil
}
return nil, errors.New("invalid type")
}
74 changes: 74 additions & 0 deletions invariant/invariant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down

0 comments on commit 51f4aca

Please sign in to comment.