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

alternate-assets extension + tests #13

Merged
merged 3 commits into from
Apr 7, 2022
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

<h3 align="center">

![Build Status](https://github.com/Terradue/DotNetStac/actions/workflows/build.yaml/badge.svg?branch=develop)
![Build Status](https://github.com/Terradue/DotNetStac/actions/workflows/build.yaml/badge.svg?branch=feature/alternate-ext)
[![NuGet](https://img.shields.io/nuget/vpre/DotNetStac)](https://www.nuget.org/packages/DotNetStac/)
[![codecov](https://codecov.io/gh/Terradue/DotNetStac/branch/develop/graph/badge.svg)](https://codecov.io/gh/Terradue/DotNetStac)
[![codecov](https://codecov.io/gh/Terradue/DotNetStac/branch/feature/alternate-ext/graph/badge.svg)](https://codecov.io/gh/Terradue/DotNetStac)
[![Gitter](https://img.shields.io/gitter/room/SpatioTemporal-Asset-Catalog/Lobby?color=yellow)](https://gitter.im/SpatioTemporal-Asset-Catalog/Lobby)
[![License](https://img.shields.io/badge/license-AGPL3-blue.svg)](LICENSE)
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/Terradue/DotNetStac/master?filepath=example.ipynb)
Expand Down
38 changes: 38 additions & 0 deletions src/DotNetStac.Test/Extensions/AlternateExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.IO;
using Newtonsoft.Json;
using Stac.Extensions.Alternate;
using Xunit;

namespace Stac.Test.Extensions
{
public class AlternateExtensionTests : TestBase
{
[Fact]
public async System.Threading.Tasks.Task SetAlternateAsset()
{
var simpleJson = GetJson("Extensions", "MinimalSample");
ValidateJson(simpleJson);

StacItem simpleitem = StacConvert.Deserialize<StacItem>(simpleJson);

StacAsset stacAsset = StacAsset.CreateDataAsset(simpleitem,
new Uri("file:///srid.csv"),
new System.Net.Mime.ContentType("text/csv"),
"System reference Ids");
stacAsset.AlternateExtension().AddAlternate("s3", new Uri("s3://bucket/key/srid.csv"));
simpleitem.Assets.Add("srid", stacAsset);

string actualJson = JsonConvert.SerializeObject(simpleitem);

ValidateJson(actualJson);

string expectedJson = GetJson("Extensions");

JsonAssert.AreEqual(expectedJson, actualJson);

Assert.Equal("s3://bucket/key/srid.csv", simpleitem.Assets["srid"].AlternateExtension().AlternateAssets["s3"].Uri.ToString());
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"stac_version": "1.0.0",
"type": "Feature",
"id": "CS3-20160503_132130_04",
"collection": "CS3",
"bbox": [
-122.59750209,
37.48803556,
-122.2880486,
37.613537207
],
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-122.308150179,
37.488035566
],
[
-122.597502109,
37.538869539
],
[
-122.576687533,
37.613537207
],
[
-122.288048600,
37.562818007
],
[
-122.308150179,
37.488035566
]
]
]
},
"properties": {
"datetime": "2016-05-03T13:21:30.040Z",
},
"links": [
{
"rel": "self",
"href": "http://cool-sat.com/catalog/CS3-20160503_132130_04/CS3-20160503_132130_04.json"
},
{
"rel": "collection",
"href": "http://cool-sat.com/catalog.json"
}
],
"assets": {
"analytic": {
"href": "relative-path/to/analytic.tif",
"title": "4-Band Analytic"
},
"thumbnail": {
"href": "http://cool-sat.com/catalog/CS3-20160503_132130_04/thumbnail.png",
"title": "Thumbnail",
"roles": [
"thumbnail"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"stac_version": "1.0.0",
"stac_extensions": [
"https://stac-extensions.github.io/alternate-assets/v1.1.0/schema.json"
],
"type": "Feature",
"id": "CS3-20160503_132130_04",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-122.308150179,
37.488035566
],
[
-122.597502109,
37.538869539
],
[
-122.576687533,
37.613537207
],
[
-122.2880486,
37.562818007
],
[
-122.308150179,
37.488035566
]
]
]
},
"properties": {
"datetime": "2016-05-03T13:21:30.04Z"
},
"bbox": [
-122.59750209,
37.48803556,
-122.2880486,
37.613537207
],
"assets": {
"analytic": {
"roles": [],
"title": "4-Band Analytic",
"href": "relative-path/to/analytic.tif"
},
"thumbnail": {
"roles": [
"thumbnail"
],
"title": "Thumbnail",
"href": "http://cool-sat.com/catalog/CS3-20160503_132130_04/thumbnail.png"
},
"srid": {
"type": "text/csv",
"roles": [
"data"
],
"title": "System reference Ids",
"href": "file:///srid.csv",
"alternate": {
"s3" : {
"href": "s3://bucket/key/srid.csv"
}
}
}
},
"links": [
{
"rel": "self",
"href": "http://cool-sat.com/catalog/CS3-20160503_132130_04/CS3-20160503_132130_04.json"
},
{
"rel": "collection",
"href": "http://cool-sat.com/catalog.json"
}
],
"collection": "CS3"
}
71 changes: 71 additions & 0 deletions src/DotNetStac/Extensions/Alternate/AlternateAssetObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Stac.Extensions.Alternate
{
/// <summary>
/// Represents the <seealso href="https://github.com/stac-extensions/alternate-assets#alternate-asset-object">Alternate Asset Object</seealso>
/// of the Alternate extension
/// </summary>
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
public class AlternateAssetObject : IStacPropertiesContainer
{
private Uri uri;
private readonly IStacObject _parent;
private string title;

private string description;

IDictionary<string, object> properties;

/// <summary>
/// Initialize a new Band Object
/// </summary>
/// <param name="uri">URI to the asset object</param>
/// <param name="parent">Parent object</param>
/// <param name="title">The displayed title for clients and users.</param>
/// <param name="description">A description of the Asset providing additional details, such as how it was processed or created. CommonMark 0.29 syntax MAY be used for rich text representation.</param>
public AlternateAssetObject(Uri uri, IStacObject parent = null, string title = null, string description = null)
{
this.uri = uri;
_parent = parent;
this.title = title;
this.description = description;
properties = new Dictionary<string, object>();
}

/// <summary>
/// REQUIRED. URI to the asset object. Relative and absolute URI are both allowed.
/// </summary>
[JsonProperty("href")]
[JsonRequired]
public string Uri { get => uri.ToString(); set => uri = new Uri(value); }

/// <summary>
/// The displayed title for clients and users.
/// </summary>
[JsonProperty("title")]
public string Title { get => title; set => title = value; }

/// <summary>
/// A description of the Asset providing additional details, such as how it was processed or created. CommonMark 0.29 syntax MAY be used for rich text representation.
/// </summary>
[JsonProperty("description")]
public string Description { get => description; set => description = value; }

/// <summary>
/// Additional fields
/// </summary>
/// <value></value>
[JsonExtensionData]
public IDictionary<string, object> Properties { get => properties; set => properties = value; }

/// <summary>
/// Parent Stac Object
/// </summary>
/// <returns></returns>
[JsonIgnore]
public IStacObject StacObjectContainer => _parent;
}
}
80 changes: 80 additions & 0 deletions src/DotNetStac/Extensions/Alternate/AlternateStacExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Stac.Extensions.Alternate
{
/// <summary>
/// Helper class to access the fields defined by the <seealso href="https://github.com/stac-extensions/alternate">Alternate extension</seealso>
/// </summary>
public class AlternateStacExtension : StacPropertiesContainerExtension, IStacAssetExtension, IStacExtension
{
/// Extension identifier and schema url
public const string JsonSchemaUrl = "https://stac-extensions.github.io/alternate-assets/v1.1.0/schema.json";

private const string AlternateField = "alternate";

private static IDictionary<string, Type> assetFields;

internal AlternateStacExtension(StacAsset stacAsset) : base(JsonSchemaUrl, stacAsset)
{
assetFields = new Dictionary<string, Type>();
assetFields.Add(AlternateField, typeof(AlternateAssetObject[]));
}

/// <summary>
/// A dictionary of alternate location information for an asset.
/// </summary>
public IDictionary<string, AlternateAssetObject> AlternateAssets
{
get { return StacPropertiesContainer.GetProperty<Dictionary<string, AlternateAssetObject>>(AlternateField); }
set
{
if (value == null || value.Count() == 0)
StacPropertiesContainer.RemoveProperty(AlternateField);
else
{
StacPropertiesContainer.SetProperty(AlternateField, value);
DeclareStacExtension();
}
}
}

/// <summary>
/// Potential fields and their types
/// </summary>
public override IDictionary<string, Type> ItemFields => assetFields;

public StacAsset StacAsset => StacPropertiesContainer as StacAsset;

public override IDictionary<string, ISummaryFunction> GetSummaryFunctions()
{
Dictionary<string, ISummaryFunction> summaryFunctions = new Dictionary<string, ISummaryFunction>();
return summaryFunctions;
}

public AlternateAssetObject AddAlternate(string key, Uri uri, string title = null, string description = null)
{
AlternateAssetObject alternateAssetObject = new AlternateAssetObject(uri, StacAsset.ParentStacObject, title, description);
var alternateAssets = AlternateAssets ?? new Dictionary<string, AlternateAssetObject>();
alternateAssets.Add(key, alternateAssetObject);
AlternateAssets = alternateAssets;
return alternateAssetObject;
}
}

/// <summary>
/// Extension methods for accessing Alternate extension
/// </summary>
public static class AlternateStacExtensionExtensions
{

/// <summary>
/// Initilize a EoStacExtension class from a STAC asset
/// </summary>
public static AlternateStacExtension AlternateExtension(this StacAsset stacAsset)
{
return new AlternateStacExtension(stacAsset);
}
}
}
11 changes: 11 additions & 0 deletions src/DotNetStac/Extensions/IStacAssetExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using Stac.Collection;

namespace Stac.Extensions
{
public interface IStacAssetExtension
{
StacAsset StacAsset { get; }
}
}