Fix a bug in the GeoJSON handling when there are no active polygons
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
This commit is contained in:
@@ -33,6 +33,11 @@ type GeoJSONProperties struct {
|
||||
DN *int `json:"DN"`
|
||||
}
|
||||
|
||||
type geometryMetadata struct {
|
||||
Type string `json:"type"`
|
||||
Geometries []json.RawMessage `json:"geometries"`
|
||||
}
|
||||
|
||||
// DecodeGeoJSON decodes an SPC GeoJSON outlook product and compacts feature
|
||||
// geometry JSON for stable downstream storage.
|
||||
func DecodeGeoJSON(raw []byte) (GeoJSONFeatureCollection, error) {
|
||||
@@ -50,6 +55,16 @@ func DecodeGeoJSON(raw []byte) (GeoJSONFeatureCollection, error) {
|
||||
return collection, nil
|
||||
}
|
||||
|
||||
// IsEmptyGeometryCollection reports whether raw is SPC's no-polygon placeholder
|
||||
// geometry shape: a GeometryCollection with no child geometries.
|
||||
func IsEmptyGeometryCollection(raw json.RawMessage) bool {
|
||||
var meta geometryMetadata
|
||||
if err := json.Unmarshal(raw, &meta); err != nil {
|
||||
return false
|
||||
}
|
||||
return meta.Type == "GeometryCollection" && len(meta.Geometries) == 0
|
||||
}
|
||||
|
||||
func (p *GeoJSONProperties) UnmarshalJSON(raw []byte) error {
|
||||
type alias GeoJSONProperties
|
||||
var aux struct {
|
||||
|
||||
@@ -66,6 +66,43 @@ func TestDecodeGeoJSONParsesSeverityRankString(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsEmptyGeometryCollection(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "empty geometry collection",
|
||||
raw: `{"type":"GeometryCollection","geometries":[]}`,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "non-empty geometry collection",
|
||||
raw: `{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[]} ]}`,
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "polygon",
|
||||
raw: `{"type":"Polygon","coordinates":[]}`,
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "invalid json",
|
||||
raw: `{`,
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsEmptyGeometryCollection([]byte(tt.raw)); got != tt.want {
|
||||
t.Fatalf("IsEmptyGeometryCollection() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseISOTimestampTrimsAndReturnsUTC(t *testing.T) {
|
||||
got, err := ParseISOTimestamp(" 2026-06-11T12:34:56Z ")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user