Skip to content

Commit

Permalink
Merge pull request #160 from planetlabs/mm/add_eo_extension
Browse files Browse the repository at this point in the history
Add eo extension
  • Loading branch information
mmidzik authored Jan 5, 2024
2 parents fc7fca3 + 30503af commit 9514038
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 0 deletions.
50 changes: 50 additions & 0 deletions extensions/eo/eo.go
Original file line number Diff line number Diff line change
@@ -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)
}
173 changes: 173 additions & 0 deletions extensions/eo/eo_test.go
Original file line number Diff line number Diff line change
@@ -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: &centerWavelength,
},
},
},
},
},
},
}

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: &centerWavelength,
},
},
},
},
},
},
}

assert.Equal(t, expected, item)
}

0 comments on commit 9514038

Please sign in to comment.