Skip to content

Commit 616fcf9

Browse files
mapyopaulmach
authored andcommitted
Fix UnmarshalFeature
- GeometryCollection cannot unmarshal
1 parent f23a705 commit 616fcf9

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

geojson/feature.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (f *Feature) UnmarshalJSON(data []byte) error {
7878
return fmt.Errorf("geojson: not a feature: type=%s", jf.Type)
7979
}
8080

81-
if jf.Geometry == nil || jf.Geometry.Coordinates == nil {
81+
if jf.Geometry == nil || (jf.Geometry.Coordinates == nil && jf.Geometry.Geometries == nil) {
8282
return ErrInvalidGeometry
8383
}
8484

@@ -87,7 +87,7 @@ func (f *Feature) UnmarshalJSON(data []byte) error {
8787
Type: jf.Type,
8888
Properties: jf.Properties,
8989
BBox: jf.BBox,
90-
Geometry: jf.Geometry.Coordinates,
90+
Geometry: jf.Geometry.Geometry(),
9191
}
9292

9393
return nil

geojson/feature_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ func TestUnmarshalFeature(t *testing.T) {
146146
}
147147
}
148148

149+
func TestUnmarshalFeature_GeometryCollection(t *testing.T) {
150+
rawJSON := `
151+
{ "type": "Feature",
152+
"geometry": {"type":"GeometryCollection","geometries":[{"type": "Point", "coordinates": [102.0, 0.5]}]}
153+
}`
154+
155+
f, err := UnmarshalFeature([]byte(rawJSON))
156+
if err != nil {
157+
t.Fatalf("unmarshal error: %v", err)
158+
}
159+
160+
wantType := orb.Collection{}.GeoJSONType()
161+
if f.Geometry.GeoJSONType() != wantType {
162+
t.Fatalf("invalid GeoJSONType: %v", f.Geometry.GeoJSONType())
163+
}
164+
}
165+
149166
func TestUnmarshalFeature_missingGeometry(t *testing.T) {
150167
t.Run("empty geometry", func(t *testing.T) {
151168
rawJSON := `{ "type": "Feature", "geometry": {} }`

0 commit comments

Comments
 (0)