Skip to content

Add eo extension #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}