Skip to content
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

Support for STAC 1.1 #220

Merged
merged 1 commit into from
Mar 10, 2025
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
11 changes: 11 additions & 0 deletions asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Asset struct {
Description string `json:"description,omitempty"`
Created string `json:"created,omitempty"`
Roles []string `json:"roles,omitempty"`
Bands []*Band `json:"bands,omitempty"`
Extensions []Extension `json:"-"`
}

Expand Down Expand Up @@ -47,6 +48,16 @@ func EncodeAssets(assets map[string]*Asset) (map[string]any, []string, error) {
return nil, nil, err
}
}

bandMaps, uris, err := EncodeBands(asset.Bands)
if err != nil {
return nil, nil, err
}
if len(bandMaps) > 0 {
extensionUris = append(extensionUris, uris...)
assetMap["bands"] = bandMaps
}

assetsMap[key] = assetMap
}
return assetsMap, extensionUris, nil
Expand Down
2 changes: 1 addition & 1 deletion asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/planetlabs/go-stac"
"github.com/planetlabs/go-stac/extensions/pl"
"github.com/planetlabs/go-stac/extensions/pl/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
72 changes: 72 additions & 0 deletions band.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package stac

import (
"regexp"

"github.com/go-viper/mapstructure/v2"
)

var bandExtensions = newExtensionRegistry()

func RegisterBandExtension(pattern *regexp.Regexp, provider ExtensionProvider) {
bandExtensions.register(pattern, provider)
}

func GetBandExtension(uri string) Extension {
return bandExtensions.get(uri)
}

type Band struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
NoData any `json:"nodata,omitempty"`
DataType string `json:"data_type,omitempty"`
Statistics *Statistics `json:"statistics,omitempty"`
Unit string `json:"unit,omitempty"`
Extensions []Extension `json:"-"`
}

type Statistics struct {
Mean *float64 `json:"mean,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
Stdev *float64 `json:"stdev,omitempty"`
ValidPercent *float64 `json:"valid_percent,omitempty"`
Count *int `json:"count,omitempty"`
}

func encodeBand(band *Band) (map[string]any, []string, error) {
extensionUris := []string{}
bandMap := map[string]any{}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Result: &bandMap,
})
if err != nil {
return nil, nil, err
}
if err := decoder.Decode(band); err != nil {
return nil, nil, err
}
for _, extension := range band.Extensions {
extensionUris = append(extensionUris, extension.URI())
if err := extension.Encode(bandMap); err != nil {
return nil, nil, err
}
}
return bandMap, extensionUris, nil
}

func EncodeBands(bands []*Band) ([]map[string]any, []string, error) {
extensionUris := []string{}
bandMaps := make([]map[string]any, len(bands))
for i, band := range bands {
bandMap, uris, err := encodeBand(band)
if err != nil {
return nil, nil, err
}
bandMaps[i] = bandMap
extensionUris = append(extensionUris, uris...)
}
return bandMaps, extensionUris, nil
}
4 changes: 4 additions & 0 deletions catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stac

import (
"encoding/json"
"errors"
"fmt"
"regexp"

Expand Down Expand Up @@ -116,6 +117,9 @@ func (catalog *Catalog) UnmarshalJSON(data []byte) error {
continue
}
if err := extension.Decode(catalogMap); err != nil {
if errors.Is(err, ErrExtensionDoesNotApply) {
continue
}
return fmt.Errorf("decoding error for %s: %w", uri, err)
}
catalog.Extensions = append(catalog.Extensions, extension)
Expand Down
55 changes: 55 additions & 0 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package stac

import (
"encoding/json"
"errors"
"fmt"
"regexp"

"github.com/Masterminds/semver/v3"
"github.com/go-viper/mapstructure/v2"
)

Expand All @@ -20,6 +22,7 @@ type Collection struct {
Summaries map[string]any `json:"summaries,omitempty"`
Links []*Link `json:"links"`
Assets map[string]*Asset `json:"assets,omitempty"`
ItemAssets map[string]*Asset `json:"item_assets,omitempty"`
Extensions []Extension `json:"-"`
}

Expand Down Expand Up @@ -78,6 +81,36 @@ func (collection Collection) MarshalJSON() ([]byte, error) {
extensionUris := []string{}
lookup := map[string]bool{}

assetsMap, assetExtensionUris, err := EncodeAssets(collection.Assets)
if err != nil {
return nil, err
}
if len(assetsMap) > 0 {
collectionMap["assets"] = assetsMap
}

for _, uri := range assetExtensionUris {
if !lookup[uri] {
extensionUris = append(extensionUris, uri)
lookup[uri] = true
}
}

itemAssetsMap, itemAssetExtensionUris, err := EncodeAssets(collection.ItemAssets)
if err != nil {
return nil, err
}
if len(itemAssetsMap) > 0 {
collectionMap["item_assets"] = itemAssetsMap
}

for _, uri := range itemAssetExtensionUris {
if !lookup[uri] {
extensionUris = append(extensionUris, uri)
lookup[uri] = true
}
}

for _, extension := range collection.Extensions {
if err := extension.Encode(collectionMap); err != nil {
return nil, err
Expand Down Expand Up @@ -140,11 +173,33 @@ func (collection *Collection) UnmarshalJSON(data []byte) error {
continue
}
if err := extension.Decode(collectionMap); err != nil {
if errors.Is(err, ErrExtensionDoesNotApply) {
continue
}
return fmt.Errorf("decoding error for %s: %w", uri, err)
}
collection.Extensions = append(collection.Extensions, extension)
}

if err := decodeExtendedAssets(collectionMap, "assets", collection.Assets, extensionUris); err != nil {
return err
}

// Item assets were added to collections in version 1.1.0
itemAssetsConstraint, err := semver.NewConstraint(">= 1.1.0")
if err != nil {
return fmt.Errorf("could not parse version constraint: %w", err)
}

if collectionVersion, err := semver.NewVersion(collection.Version); err == nil && itemAssetsConstraint.Check(collectionVersion) {
if err := decodeExtendedAssets(collectionMap, "item_assets", collection.ItemAssets, extensionUris); err != nil {
return err
}
} else {
// prior to 1.1.0, the item assets extension is used
collection.ItemAssets = nil
}

if err := decodeExtendedLinks(collectionMap, collection.Links, extensionUris); err != nil {
return err
}
Expand Down
Loading
Loading