From 30503af926199ab2c115130ba01d245f68d7f939 Mon Sep 17 00:00:00 2001 From: maya Date: Thu, 4 Jan 2024 21:47:29 -0800 Subject: [PATCH] add eo extension --- extensions/eo/eo.go | 50 +++++++++++ extensions/eo/eo_test.go | 173 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 extensions/eo/eo.go create mode 100644 extensions/eo/eo_test.go diff --git a/extensions/eo/eo.go b/extensions/eo/eo.go new file mode 100644 index 0000000..b3ee41a --- /dev/null +++ b/extensions/eo/eo.go @@ -0,0 +1,50 @@ +package eo + +import ( + "regexp" + + "github.com/planetlabs/go-stac" +) + +const ( + extensionUri = "https://stac-extensions.github.io/eo/v1.1.0/schema.json" + extensionPattern = `https://stac-extensions.github.io/eo/v1\..*/schema.json` +) + +func init() { + stac.RegisterAssetExtension( + regexp.MustCompile(extensionPattern), + func() stac.Extension { + return &Asset{} + }, + ) +} + +type Asset struct { + CloudCover *float64 `json:"eo:cloud_cover,omitempty"` + SnowCover *float64 `json:"eo:snow_cover,omitempty"` + Bands []*Band `json:"eo:bands,omitempty"` +} + +type Band struct { + Name string `json:"name,omitempty"` + CommonName string `json:"common_name,omitempty"` + Description string `json:"description,omitempty"` + CenterWavelength *float64 `json:"center_wavelength,omitempty"` + FullWidthHalfMax *float64 `json:"full_width_half_max,omitempty"` + SolarIllumination *float64 `json:"solar_illumination,omitempty"` +} + +var _ stac.Extension = (*Asset)(nil) + +func (*Asset) URI() string { + return extensionUri +} + +func (e *Asset) Encode(assetMap map[string]any) error { + return stac.EncodeExtendedAsset(e, assetMap) +} + +func (e *Asset) Decode(assetMap map[string]any) error { + return stac.DecodeExtendedAsset(e, assetMap) +} diff --git a/extensions/eo/eo_test.go b/extensions/eo/eo_test.go new file mode 100644 index 0000000..e0fa48b --- /dev/null +++ b/extensions/eo/eo_test.go @@ -0,0 +1,173 @@ +package eo_test + +import ( + "encoding/json" + "github.com/planetlabs/go-stac/extensions/eo" + "testing" + + "github.com/planetlabs/go-stac" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestItemExtendedMarshal(t *testing.T) { + cloudCover := float64(25) + snowCover := float64(10) + centerWavelength := float64(0.85) + item := &stac.Item{ + Version: "1.0.0", + Id: "item-id", + Geometry: map[string]any{ + "type": "Point", + "coordinates": []float64{0, 0}, + }, + Properties: map[string]any{ + "test": "value", + }, + Links: []*stac.Link{ + {Href: "https://example.com/stac/item-id", Rel: "self"}, + }, + Assets: map[string]*stac.Asset{ + "image": { + Title: "Image", + Href: "https://example.com/stac/item-id/image.tif", + Type: "image/tif", + Extensions: []stac.Extension{ + &eo.Asset{ + CloudCover: &cloudCover, + SnowCover: &snowCover, + Bands: []*eo.Band{ + { + Name: "NIR", + CenterWavelength: ¢erWavelength, + }, + }, + }, + }, + }, + }, + } + + data, err := json.Marshal(item) + require.NoError(t, err) + + expected := `{ + "type": "Feature", + "stac_version": "1.0.0", + "id": "item-id", + "geometry": { + "type": "Point", + "coordinates": [0, 0] + }, + "properties": { + "test": "value" + }, + "links": [ + { + "rel": "self", + "href": "https://example.com/stac/item-id" + } + ], + "assets": { + "image": { + "title": "Image", + "href": "https://example.com/stac/item-id/image.tif", + "type": "image/tif", + "eo:cloud_cover": 25, + "eo:snow_cover": 10, + "eo:bands": [ + { + "name": "NIR", + "center_wavelength": 0.85 + } + ] + } + }, + "stac_extensions": [ + "https://stac-extensions.github.io/eo/v1.1.0/schema.json" + ] + }` + + assert.JSONEq(t, expected, string(data)) +} + +func TestItemExtendedUnmarshal(t *testing.T) { + data := []byte(`{ + "type": "Feature", + "stac_version": "1.0.0", + "id": "item-id", + "geometry": { + "type": "Point", + "coordinates": [0, 0] + }, + "properties": { + "test": "value" + }, + "links": [ + { + "rel": "self", + "href": "https://example.com/stac/item-id" + } + ], + "assets": { + "image": { + "title": "Image", + "href": "https://example.com/stac/item-id/image.tif", + "type": "image/tif", + "eo:cloud_cover": 25, + "eo:snow_cover": 10, + "eo:bands": [ + { + "name": "NIR", + "center_wavelength": 0.85 + } + ] + } + }, + "stac_extensions": [ + "https://stac-extensions.github.io/eo/v1.1.0/schema.json" + ] + }`) + + item := &stac.Item{} + require.NoError(t, json.Unmarshal(data, item)) + + cloudCover := float64(25) + snowCover := float64(10) + centerWavelength := float64(0.85) + expected := &stac.Item{ + Version: "1.0.0", + Id: "item-id", + Geometry: map[string]any{ + "type": "Point", + "coordinates": []any{float64(0), float64(0)}, + }, + Properties: map[string]any{ + "test": "value", + }, + Links: []*stac.Link{ + {Href: "https://example.com/stac/item-id", Rel: "self"}, + }, + Assets: map[string]*stac.Asset{ + "image": { + Title: "Image", + Href: "https://example.com/stac/item-id/image.tif", + Type: "image/tif", + Extensions: []stac.Extension{ + &eo.Asset{ + CloudCover: &cloudCover, + SnowCover: &snowCover, + Bands: []*eo.Band{ + { + Name: "NIR", + CenterWavelength: ¢erWavelength, + }, + }, + }, + }, + }, + }, + } + + assert.Equal(t, expected, item) +}