Skip to content

Commit 9514038

Browse files
authored
Merge pull request #160 from planetlabs/mm/add_eo_extension
Add eo extension
2 parents fc7fca3 + 30503af commit 9514038

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

extensions/eo/eo.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package eo
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/planetlabs/go-stac"
7+
)
8+
9+
const (
10+
extensionUri = "https://stac-extensions.github.io/eo/v1.1.0/schema.json"
11+
extensionPattern = `https://stac-extensions.github.io/eo/v1\..*/schema.json`
12+
)
13+
14+
func init() {
15+
stac.RegisterAssetExtension(
16+
regexp.MustCompile(extensionPattern),
17+
func() stac.Extension {
18+
return &Asset{}
19+
},
20+
)
21+
}
22+
23+
type Asset struct {
24+
CloudCover *float64 `json:"eo:cloud_cover,omitempty"`
25+
SnowCover *float64 `json:"eo:snow_cover,omitempty"`
26+
Bands []*Band `json:"eo:bands,omitempty"`
27+
}
28+
29+
type Band struct {
30+
Name string `json:"name,omitempty"`
31+
CommonName string `json:"common_name,omitempty"`
32+
Description string `json:"description,omitempty"`
33+
CenterWavelength *float64 `json:"center_wavelength,omitempty"`
34+
FullWidthHalfMax *float64 `json:"full_width_half_max,omitempty"`
35+
SolarIllumination *float64 `json:"solar_illumination,omitempty"`
36+
}
37+
38+
var _ stac.Extension = (*Asset)(nil)
39+
40+
func (*Asset) URI() string {
41+
return extensionUri
42+
}
43+
44+
func (e *Asset) Encode(assetMap map[string]any) error {
45+
return stac.EncodeExtendedAsset(e, assetMap)
46+
}
47+
48+
func (e *Asset) Decode(assetMap map[string]any) error {
49+
return stac.DecodeExtendedAsset(e, assetMap)
50+
}

extensions/eo/eo_test.go

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package eo_test
2+
3+
import (
4+
"encoding/json"
5+
"github.com/planetlabs/go-stac/extensions/eo"
6+
"testing"
7+
8+
"github.com/planetlabs/go-stac"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestItemExtendedMarshal(t *testing.T) {
14+
cloudCover := float64(25)
15+
snowCover := float64(10)
16+
centerWavelength := float64(0.85)
17+
item := &stac.Item{
18+
Version: "1.0.0",
19+
Id: "item-id",
20+
Geometry: map[string]any{
21+
"type": "Point",
22+
"coordinates": []float64{0, 0},
23+
},
24+
Properties: map[string]any{
25+
"test": "value",
26+
},
27+
Links: []*stac.Link{
28+
{Href: "https://example.com/stac/item-id", Rel: "self"},
29+
},
30+
Assets: map[string]*stac.Asset{
31+
"image": {
32+
Title: "Image",
33+
Href: "https://example.com/stac/item-id/image.tif",
34+
Type: "image/tif",
35+
Extensions: []stac.Extension{
36+
&eo.Asset{
37+
CloudCover: &cloudCover,
38+
SnowCover: &snowCover,
39+
Bands: []*eo.Band{
40+
{
41+
Name: "NIR",
42+
CenterWavelength: &centerWavelength,
43+
},
44+
},
45+
},
46+
},
47+
},
48+
},
49+
}
50+
51+
data, err := json.Marshal(item)
52+
require.NoError(t, err)
53+
54+
expected := `{
55+
"type": "Feature",
56+
"stac_version": "1.0.0",
57+
"id": "item-id",
58+
"geometry": {
59+
"type": "Point",
60+
"coordinates": [0, 0]
61+
},
62+
"properties": {
63+
"test": "value"
64+
},
65+
"links": [
66+
{
67+
"rel": "self",
68+
"href": "https://example.com/stac/item-id"
69+
}
70+
],
71+
"assets": {
72+
"image": {
73+
"title": "Image",
74+
"href": "https://example.com/stac/item-id/image.tif",
75+
"type": "image/tif",
76+
"eo:cloud_cover": 25,
77+
"eo:snow_cover": 10,
78+
"eo:bands": [
79+
{
80+
"name": "NIR",
81+
"center_wavelength": 0.85
82+
}
83+
]
84+
}
85+
},
86+
"stac_extensions": [
87+
"https://stac-extensions.github.io/eo/v1.1.0/schema.json"
88+
]
89+
}`
90+
91+
assert.JSONEq(t, expected, string(data))
92+
}
93+
94+
func TestItemExtendedUnmarshal(t *testing.T) {
95+
data := []byte(`{
96+
"type": "Feature",
97+
"stac_version": "1.0.0",
98+
"id": "item-id",
99+
"geometry": {
100+
"type": "Point",
101+
"coordinates": [0, 0]
102+
},
103+
"properties": {
104+
"test": "value"
105+
},
106+
"links": [
107+
{
108+
"rel": "self",
109+
"href": "https://example.com/stac/item-id"
110+
}
111+
],
112+
"assets": {
113+
"image": {
114+
"title": "Image",
115+
"href": "https://example.com/stac/item-id/image.tif",
116+
"type": "image/tif",
117+
"eo:cloud_cover": 25,
118+
"eo:snow_cover": 10,
119+
"eo:bands": [
120+
{
121+
"name": "NIR",
122+
"center_wavelength": 0.85
123+
}
124+
]
125+
}
126+
},
127+
"stac_extensions": [
128+
"https://stac-extensions.github.io/eo/v1.1.0/schema.json"
129+
]
130+
}`)
131+
132+
item := &stac.Item{}
133+
require.NoError(t, json.Unmarshal(data, item))
134+
135+
cloudCover := float64(25)
136+
snowCover := float64(10)
137+
centerWavelength := float64(0.85)
138+
expected := &stac.Item{
139+
Version: "1.0.0",
140+
Id: "item-id",
141+
Geometry: map[string]any{
142+
"type": "Point",
143+
"coordinates": []any{float64(0), float64(0)},
144+
},
145+
Properties: map[string]any{
146+
"test": "value",
147+
},
148+
Links: []*stac.Link{
149+
{Href: "https://example.com/stac/item-id", Rel: "self"},
150+
},
151+
Assets: map[string]*stac.Asset{
152+
"image": {
153+
Title: "Image",
154+
Href: "https://example.com/stac/item-id/image.tif",
155+
Type: "image/tif",
156+
Extensions: []stac.Extension{
157+
&eo.Asset{
158+
CloudCover: &cloudCover,
159+
SnowCover: &snowCover,
160+
Bands: []*eo.Band{
161+
{
162+
Name: "NIR",
163+
CenterWavelength: &centerWavelength,
164+
},
165+
},
166+
},
167+
},
168+
},
169+
},
170+
}
171+
172+
assert.Equal(t, expected, item)
173+
}

0 commit comments

Comments
 (0)