Skip to content

Commit

Permalink
WIP #6
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelmathot committed Apr 1, 2021
1 parent 2153d04 commit a49cd56
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 75 deletions.
12 changes: 2 additions & 10 deletions src/DotNetStac.Test/Catalog/StacRepositoryCatalogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,16 @@ namespace Stac.Test.Catalog
{
public class StacRepositoryCatalogTests : TestBase
{
private readonly string currentVersion;

public StacRepositoryCatalogTests(){
var packageJson = httpClient.GetStringAsync("https://raw.githubusercontent.com/radiantearth/stac-spec/master/package.json").GetAwaiter().GetResult();
JObject package = JsonConvert.DeserializeObject<JObject>(packageJson);
currentVersion = package.Value<string>("version");
}

[Fact]
public void CanDeserializeBaseCatalogExample()
{
var json = httpClient.GetStringAsync("https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/catalog.json").GetAwaiter().GetResult();
var json = httpClient.GetStringAsync($"https://raw.githubusercontent.com/radiantearth/stac-spec/v{Versions.StacVersionList.Current}/examples/catalog.json").GetAwaiter().GetResult();

StacCatalog catalog = StacConvert.Deserialize<StacCatalog>(json);

Assert.NotNull(catalog);

Assert.Equal(currentVersion, catalog.StacVersion.ToString());
Assert.Equal(Versions.StacVersionList.Current.ToString(), catalog.StacVersion.ToString());

string catalog2json = StacConvert.Serialize(catalog);

Expand Down
7 changes: 4 additions & 3 deletions src/DotNetStac.Test/Collection/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ public void CanSerializeSentinel2Sample()
collection.Keywords.Add("radiance");
collection.Keywords.Add("sentinel");

collection.Providers.Add(new StacProvider("European Union/ESA/Copernicus"){
Roles = new List<StacProviderRole>() { StacProviderRole.producer, StacProviderRole.licensor},
Uri = new Uri("https://sentinel.esa.int/web/sentinel/user-guides/sentinel-2-msi")
collection.Providers.Add(new StacProvider("European Union/ESA/Copernicus",
new List<StacProviderRole>() { StacProviderRole.producer, StacProviderRole.licensor })
{
Uri = new Uri("https://sentinel.esa.int/web/sentinel/user-guides/sentinel-2-msi")
});

collection.Summaries.Add("datetime",
Expand Down
60 changes: 51 additions & 9 deletions src/DotNetStac.Test/Collection/StacRepositoryCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
Expand All @@ -7,26 +10,19 @@ namespace Stac.Test.Catalog
{
public class StacRepositoryCollectionTests : TestBase
{
private readonly string currentVersion;

public StacRepositoryCollectionTests(){
var packageJson = httpClient.GetStringAsync("https://raw.githubusercontent.com/radiantearth/stac-spec/master/package.json").GetAwaiter().GetResult();
JObject package = JsonConvert.DeserializeObject<JObject>(packageJson);
currentVersion = package.Value<string>("version");
}

[Fact]
public void CanDeserializeBaseCollectionExample()
{
var json = httpClient.GetStringAsync("https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/collection.json").GetAwaiter().GetResult();
var json = httpClient.GetStringAsync($"https://raw.githubusercontent.com/radiantearth/stac-spec/v{Versions.StacVersionList.Current}/examples/collection.json").GetAwaiter().GetResult();

ValidateJson(json);

StacCollection collection = StacConvert.Deserialize<StacCollection>(json);

Assert.NotNull(collection);

Assert.Equal(currentVersion, collection.StacVersion.ToString());
Assert.Equal(Versions.StacVersionList.Current.ToString(), collection.StacVersion.ToString());

string collection2json = StacConvert.Serialize(collection);

Expand All @@ -35,5 +31,51 @@ public void CanDeserializeBaseCollectionExample()
JsonAssert.AreEqual(json, collection2json);
}

[Fact]
public void CanCreateBaseCollectionExample()
{
var expectedJson = httpClient.GetStringAsync($"https://raw.githubusercontent.com/radiantearth/stac-spec/v{Versions.StacVersionList.Current}/examples/collection.json").GetAwaiter().GetResult();

ValidateJson(expectedJson);

Dictionary<Uri, StacItem> items = new Dictionary<Uri, StacItem>();
Uri simpleItemUri = new Uri($"https://raw.githubusercontent.com/radiantearth/stac-spec/v{Versions.StacVersionList.Current}/examples/simple-item.json");
items.Add(simpleItemUri, StacConvert.Deserialize<StacItem>(httpClient.GetStringAsync(simpleItemUri).GetAwaiter().GetResult()));
Uri coreItemUri = new Uri($"https://raw.githubusercontent.com/radiantearth/stac-spec/v{Versions.StacVersionList.Current}/examples/core-item.json");
items.Add(coreItemUri, StacConvert.Deserialize<StacItem>(httpClient.GetStringAsync(coreItemUri).GetAwaiter().GetResult()));
Uri extendedItemUri = new Uri($"https://raw.githubusercontent.com/radiantearth/stac-spec/v{Versions.StacVersionList.Current}/examples/extended-item.json");
items.Add(extendedItemUri, StacConvert.Deserialize<StacItem>(httpClient.GetStringAsync(extendedItemUri).GetAwaiter().GetResult()));

StacCollection collection = StacCollection.Create(new Uri($"https://raw.githubusercontent.com/radiantearth/stac-spec/v{Versions.StacVersionList.Current}/examples/collection.json"),
"simple-collection",
"A simple collection demonstrating core catalog fields with links to a couple of items",
items,
null,
"CC-BY-4.0");
// TEMPORARY overrides until #1080 is fixed
collection.Extent.Spatial.BoundingBoxes[0] = new double[4] { 172.911, 1.343, 172.955, 1.3691 };
collection.Extent.Temporal.Interval[0] = new DateTime?[2] { DateTime.Parse("2020-12-11T09:06:43.312000Z"), DateTime.Parse("2020-12-14T18:02:31.437000Z") };
expectedJson = expectedJson.Replace(",\n \"title\": \"Simple Item\"", "");
expectedJson = expectedJson.Replace("RC.1", "rc.1", false, CultureInfo.CurrentCulture);
//
collection.Title = "Simple Example Collection";
collection.Links.Insert(0, StacLink.CreateRootLink(new Uri("./collection.json", UriKind.Relative), StacCollection.MEDIATYPE));
collection.Links.Add(StacLink.CreateSelfLink(new Uri($"https://raw.githubusercontent.com/radiantearth/stac-spec/v{Versions.StacVersionList.Current}/examples/collection.json"), StacCollection.MEDIATYPE));
collection.Providers.Add(new Stac.Collection.StacProvider("Remote Data, Inc"){
Description = "Producers of awesome spatiotemporal assets",
Uri = new Uri("http://remotedata.io")
});
collection.Providers[0].Roles.Add(Stac.Collection.StacProviderRole.producer);
collection.Providers[0].Roles.Add(Stac.Collection.StacProviderRole.processor);

Assert.Equal(Versions.StacVersionList.Current.ToString(), collection.StacVersion.ToString());

string actualJson = StacConvert.Serialize(collection);

ValidateJson(actualJson);

JsonAssert.AreEqual(expectedJson, actualJson);
}

}
}
3 changes: 1 addition & 2 deletions src/DotNetStac.Test/Examples/Example1Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public void CanSerializeSentinel2Sample()
collection.Keywords.Add("sentinel");

collection.Providers.Add(
new StacProvider("European Union/ESA/Copernicus"){
Roles = new List<StacProviderRole>() { StacProviderRole.producer, StacProviderRole.licensor},
new StacProvider("European Union/ESA/Copernicus", new List<StacProviderRole>() { StacProviderRole.producer, StacProviderRole.licensor}){
Uri = new Uri("https://sentinel.esa.int/web/sentinel/user-guides/sentinel-2-msi")
}
);
Expand Down
32 changes: 19 additions & 13 deletions src/DotNetStac/Collection/StacProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

Expand All @@ -8,28 +10,32 @@ namespace Stac.Collection
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
public class StacProvider
{
private string name;

private string description;

private List<StacProviderRole> roles;
private Uri uri;

public StacProvider(string name)
public StacProvider(string name, IEnumerable<StacProviderRole> providerRoles = null)
{
this.name = name;
this.Name = name;
if (providerRoles != null)
Roles = new Collection<StacProviderRole>(providerRoles.ToList());
else
Roles = new Collection<StacProviderRole>();
}

[JsonProperty("name")]
public string Name { get => name; set => name = value; }
public string Name { get; set; }

[JsonProperty("description")]
public string Description { get => description; set => description = value; }
public string Description { get; set; }

[JsonProperty("roles")]
public List<StacProviderRole> Roles { get => roles; set => roles = value; }
public Collection<StacProviderRole> Roles { get; private set; }

[JsonProperty("url")]
public Uri Uri { get => uri; set => uri = value; }
public Uri Uri { get; set; }

#pragma warning disable 1591
public bool ShouldSerializeRoles()
{
// don't serialize the Manager property if an employee is their own manager
return Roles.Count > 0;
}
}
}
7 changes: 3 additions & 4 deletions src/DotNetStac/DotNetStac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
<Title>DotNetStac</Title>
<Description>Terradue .Net library for working with any SpatioTemporal Asset Catalog</Description>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>0.6.2</Version>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>rc.2</VersionSuffix>
<Authors>Emmanuel Mathot</Authors>
<Authors>emmanuelmathot</Authors>
<Company>Terradue</Company>
<Company>Terradue</Company>
<RepositoryUrl>https://github.com/Terradue/DotNetStac</RepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<PackageTags>STAC;Terradue;Geo;Json</PackageTags>
<PackageIcon>stac_logo.png</PackageIcon>
<Copyright>Copyright (c) 2010-2020 by Terradue</Copyright>
<Copyright>Copyright (c) 2010-2021 by Terradue</Copyright>
<CodeAnalysisRuleSet>$(SolutionDir)StyleCop.ruleset</CodeAnalysisRuleSet>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DebugType>full</DebugType>
Expand All @@ -31,7 +31,6 @@
<PackageReference Include="System.Reflection" Version="4.3.0" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
<PackageReference Include="OpenCover" Version="4.7.922" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\LICENSE" Pack="true" PackagePath="" />
Expand Down
2 changes: 2 additions & 0 deletions src/DotNetStac/Extensions/IStacExtension.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using Stac.Collection;

namespace Stac.Extensions
{
public interface IStacExtension
{
string Identifier { get; }

IDictionary<string, Func<IEnumerable<object>, IStacSummaryItem>> GetSummaryFunctions();
}
}
25 changes: 0 additions & 25 deletions src/DotNetStac/Extensions/StacAssetExtension.cs

This file was deleted.

43 changes: 43 additions & 0 deletions src/DotNetStac/Extensions/StacExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Schema;

namespace Stac.Extensions
{
public static class StacExtensions
{
public static Dictionary<string, Type> ManagedStacExtensions = new Dictionary<string, Type>();

public static void InitManagedExtensions()
{
ManagedStacExtensions.Clear();
ManagedStacExtensions.Add(Eo.EoStacExtension.JsonSchemaUrl, typeof(Eo.EoStacExtension));
ManagedStacExtensions.Add(Processing.ProcessingStacExtension.JsonSchemaUrl, typeof(Processing.ProcessingStacExtension));
ManagedStacExtensions.Add(Projection.ProjectionStacExtension.JsonSchemaUrl, typeof(Projection.ProjectionStacExtension));
ManagedStacExtensions.Add(Sar.SarStacExtension.JsonSchemaUrl, typeof(Sar.SarStacExtension));
ManagedStacExtensions.Add(Sat.SatStacExtension.JsonSchemaUrl, typeof(Sat.SatStacExtension));
ManagedStacExtensions.Add(View.ViewStacExtension.JsonSchemaUrl, typeof(View.ViewStacExtension));
}

public static IEnumerable<IStacExtension> GetDeclaredExtensions(this IStacPropertiesContainer stacPropertiesContainer)
{
return stacPropertiesContainer.StacObjectContainer.StacExtensions
.Select(stacExtension => LoadStacExtension(stacExtension, stacPropertiesContainer.StacObjectContainer));
}

private static IStacExtension LoadStacExtension(string stacExtension, IStacObject stacObject)
{
if (ManagedStacExtensions.ContainsKey(stacExtension))
{
try
{
return Activator.CreateInstance(ManagedStacExtensions[stacExtension], new object[1] { stacObject }) as IStacExtension;
}
catch { }
}

return new SchemaBasedStacExtension(new StacSchemaResolver(new JSchemaUrlResolver()).LoadSchema(stacExtension), stacObject);
}
}
}
10 changes: 10 additions & 0 deletions src/DotNetStac/Extensions/StacPropertiesContainerExtension.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Stac.Collection;
using Stac.Exceptions;

namespace Stac.Extensions
Expand All @@ -23,6 +25,14 @@ public StacPropertiesContainerExtension(string identifier, IStacPropertiesContai

public abstract IDictionary<string, Type> ItemFields { get; }

public virtual IDictionary<string, Func<IEnumerable<object>, IStacSummaryItem>> GetSummaryFunctions(){
return ItemFields.ToDictionary(k => k.Key,
k => {
if ( k.Value == typeof(bool) || k.Value == typeof(short) || k.Value == typeof(int) || k.Value == typeof(long) ||
k.Value == typeof(float) || k.Value == typeof(double) || k.Value == typeof(DateTime))
});
}

protected void DeclareStacExtension()
{
if (!StacPropertiesContainer.StacObjectContainer.StacExtensions.Contains(Identifier))
Expand Down
Loading

0 comments on commit a49cd56

Please sign in to comment.