Skip to content

Commit

Permalink
Fix UnmarshalFeature
Browse files Browse the repository at this point in the history
- GeometryCollection cannot unmarshal
  • Loading branch information
mapyo authored and paulmach committed Apr 5, 2020
1 parent f23a705 commit 616fcf9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions geojson/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (f *Feature) UnmarshalJSON(data []byte) error {
return fmt.Errorf("geojson: not a feature: type=%s", jf.Type)
}

if jf.Geometry == nil || jf.Geometry.Coordinates == nil {
if jf.Geometry == nil || (jf.Geometry.Coordinates == nil && jf.Geometry.Geometries == nil) {
return ErrInvalidGeometry
}

Expand All @@ -87,7 +87,7 @@ func (f *Feature) UnmarshalJSON(data []byte) error {
Type: jf.Type,
Properties: jf.Properties,
BBox: jf.BBox,
Geometry: jf.Geometry.Coordinates,
Geometry: jf.Geometry.Geometry(),
}

return nil
Expand Down
17 changes: 17 additions & 0 deletions geojson/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ func TestUnmarshalFeature(t *testing.T) {
}
}

func TestUnmarshalFeature_GeometryCollection(t *testing.T) {
rawJSON := `
{ "type": "Feature",
"geometry": {"type":"GeometryCollection","geometries":[{"type": "Point", "coordinates": [102.0, 0.5]}]}
}`

f, err := UnmarshalFeature([]byte(rawJSON))
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}

wantType := orb.Collection{}.GeoJSONType()
if f.Geometry.GeoJSONType() != wantType {
t.Fatalf("invalid GeoJSONType: %v", f.Geometry.GeoJSONType())
}
}

func TestUnmarshalFeature_missingGeometry(t *testing.T) {
t.Run("empty geometry", func(t *testing.T) {
rawJSON := `{ "type": "Feature", "geometry": {} }`
Expand Down

0 comments on commit 616fcf9

Please sign in to comment.