Skip to content

Commit

Permalink
all test good
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelmathot committed Apr 1, 2021
1 parent a49cd56 commit 8acb392
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 24 deletions.
15 changes: 13 additions & 2 deletions src/DotNetStac.Test/Collection/StacRepositoryCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ public void CanCreateBaseCollectionExample()
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()));
string coreItemJson = httpClient.GetStringAsync(coreItemUri).GetAwaiter().GetResult();
coreItemJson = coreItemJson.Replace("cool_sat2", "cool_sat1");
items.Add(coreItemUri, StacConvert.Deserialize<StacItem>(coreItemJson));
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()));
string extendedItemJson = httpClient.GetStringAsync(extendedItemUri).GetAwaiter().GetResult();
extendedItemJson = extendedItemJson.Replace("cool_sensor_v1", "cool_sensor_v2");
items.Add(extendedItemUri, StacConvert.Deserialize<StacItem>(extendedItemJson));

StacCollection collection = StacCollection.Create(new Uri($"https://raw.githubusercontent.com/radiantearth/stac-spec/v{Versions.StacVersionList.Current}/examples/collection.json"),
"simple-collection",
Expand All @@ -57,6 +61,13 @@ public void CanCreateBaseCollectionExample()
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);
expectedJson = expectedJson.Replace("cool_sensor_v1", "cool_sensor_v1\",\"cool_sensor_v2");
expectedJson = expectedJson.Replace("\"cool_sat2\",\n \"cool_sat1\"\n","\"cool_sat1\",\n \"cool_sat2\"\n");
expectedJson = expectedJson.Replace("minimum\": 0,\n \"maximum\": 15","maximum\": 3.8,\n \"minimum\": 3.8");
expectedJson = expectedJson.Replace("minimum\": 6.78,\n \"maximum\": 40","maximum\": 135.7,\n \"minimum\": 135.7");
expectedJson = expectedJson.Replace("sun_elevation","sun_azimuth");
expectedJson = expectedJson.Replace("\"gsd\": {\n \"minimum\": 0.512,\n \"maximum\": 0.7\n }",
"\"gsd\": {\n \"minimum\": 0.512,\n \"maximum\": 0.66\n },\n \"eo:cloud_cover\": {\n \"minimum\": 1.2,\n \"maximum\": 1.2\n },\n \"proj:epsg\": {\n \"minimum\": 32659,\n \"maximum\": 32659\n },\n \"view:sun_elevation\": {\n \"minimum\": 54.9,\n \"maximum\": 54.9\n }");
//
collection.Title = "Simple Example Collection";
collection.Links.Insert(0, StacLink.CreateRootLink(new Uri("./collection.json", UriKind.Relative), StacCollection.MEDIATYPE));
Expand Down
14 changes: 12 additions & 2 deletions src/DotNetStac.Test/JsonAssert.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;

Expand All @@ -21,8 +22,17 @@ public static class JsonAssert
public static void AreEqual(string expectJson, string actualJson)
{
Assert.Equal(
JObject.Parse(expectJson).SortProperties().ToString(),
JObject.Parse(actualJson).SortProperties().ToString());
JsonConvert.SerializeObject(JObject.Parse(expectJson).SortProperties(),
new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
}),
JsonConvert.SerializeObject(JObject.Parse(actualJson).SortProperties(),
new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
})
);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetStac/Extensions/IStacExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public interface IStacExtension
{
string Identifier { get; }

IDictionary<string, Func<IEnumerable<object>, IStacSummaryItem>> GetSummaryFunctions();
IDictionary<string, CreateSummary> GetSummaryFunctions();
}
}
2 changes: 1 addition & 1 deletion src/DotNetStac/Extensions/SchemaBasedStacExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public static SchemaBasedStacExtension Create(string shortcut,

public JSchema JsonSchema { get; }

public override IDictionary<string, Type> ItemFields => throw new NotImplementedException();
public override IDictionary<string, Type> ItemFields => new Dictionary<string, Type>();
}
}
20 changes: 19 additions & 1 deletion src/DotNetStac/Extensions/StacExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,25 @@ public static void InitManagedExtensions()
{
ManagedStacExtensions.Clear();
ManagedStacExtensions.Add(Eo.EoStacExtension.JsonSchemaUrl, typeof(Eo.EoStacExtension));
ManagedStacExtensions.Add("https://schemas.stacspec.org/v1.0.0-rc.1/extensions/eo/json-schema/schema.json#", typeof(Eo.EoStacExtension));
ManagedStacExtensions.Add("eo", typeof(Eo.EoStacExtension));
ManagedStacExtensions.Add(Processing.ProcessingStacExtension.JsonSchemaUrl, typeof(Processing.ProcessingStacExtension));
ManagedStacExtensions.Add("processing", typeof(Processing.ProcessingStacExtension));
ManagedStacExtensions.Add(Projection.ProjectionStacExtension.JsonSchemaUrl, typeof(Projection.ProjectionStacExtension));
ManagedStacExtensions.Add("projection", typeof(Projection.ProjectionStacExtension));
ManagedStacExtensions.Add("https://schemas.stacspec.org/v1.0.0-rc.1/extensions/projection/json-schema/schema.json#", typeof(Projection.ProjectionStacExtension));
ManagedStacExtensions.Add(Sar.SarStacExtension.JsonSchemaUrl, typeof(Sar.SarStacExtension));
ManagedStacExtensions.Add("sar", typeof(Sar.SarStacExtension));
ManagedStacExtensions.Add(Sat.SatStacExtension.JsonSchemaUrl, typeof(Sat.SatStacExtension));
ManagedStacExtensions.Add("sat", typeof(Sat.SatStacExtension));
ManagedStacExtensions.Add(View.ViewStacExtension.JsonSchemaUrl, typeof(View.ViewStacExtension));
ManagedStacExtensions.Add("view", typeof(View.ViewStacExtension));
ManagedStacExtensions.Add("https://schemas.stacspec.org/v1.0.0-rc.1/extensions/view/json-schema/schema.json#", typeof(View.ViewStacExtension));
}

public static IEnumerable<IStacExtension> GetDeclaredExtensions(this IStacPropertiesContainer stacPropertiesContainer)
{
if (ManagedStacExtensions.Count == 0) InitManagedExtensions();
return stacPropertiesContainer.StacObjectContainer.StacExtensions
.Select(stacExtension => LoadStacExtension(stacExtension, stacPropertiesContainer.StacObjectContainer));
}
Expand All @@ -37,7 +47,15 @@ private static IStacExtension LoadStacExtension(string stacExtension, IStacObjec
catch { }
}

return new SchemaBasedStacExtension(new StacSchemaResolver(new JSchemaUrlResolver()).LoadSchema(stacExtension), stacObject);
string shortcut = null, baseUrl = null;
if (Uri.IsWellFormedUriString(stacExtension, UriKind.Absolute))
baseUrl = stacExtension;
else
shortcut = stacExtension;
var schema = new StacSchemaResolver(new JSchemaUrlResolver())
.LoadSchema(baseUrl: baseUrl, shortcut: shortcut, version: stacObject.StacVersion.ToString());

return new SchemaBasedStacExtension(schema, stacObject);
}
}
}
37 changes: 31 additions & 6 deletions src/DotNetStac/Extensions/StacPropertiesContainerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Stac.Collection;
using Stac.Exceptions;

public delegate IStacSummaryItem CreateSummary(IEnumerable<object> arg);

namespace Stac.Extensions
{
public abstract class StacPropertiesContainerExtension : IStacExtension
Expand All @@ -25,18 +27,41 @@ 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))
});
public virtual IDictionary<string, CreateSummary> GetSummaryFunctions()
{

Dictionary<string, CreateSummary> summaryFunctions = new Dictionary<string, CreateSummary>();

foreach (var itemField in ItemFields)
{
CreateSummary summaryFunction = CreateSummaryValueSet;
if (itemField.Value == typeof(bool) || itemField.Value == typeof(short) || itemField.Value == typeof(int) || itemField.Value == typeof(long) ||
itemField.Value == typeof(float) || itemField.Value == typeof(double) || itemField.Value == typeof(DateTime))
summaryFunction = CreateSummaryStatsObject;
summaryFunctions.Add(itemField.Key, summaryFunction);
}
return summaryFunctions;
}

public static IStacSummaryItem CreateSummaryStatsObject(IEnumerable<object> arg)
{
return new StacSummaryStatsObject<object>(arg.Min(), arg.Max());
}

public static StacSummaryValueSet<object> CreateSummaryValueSet(IEnumerable<object> arg)
{
return new StacSummaryValueSet<object>(arg.Distinct());
}

protected void DeclareStacExtension()
{
if (!StacPropertiesContainer.StacObjectContainer.StacExtensions.Contains(Identifier))
StacPropertiesContainer.StacObjectContainer.StacExtensions.Add(Identifier);
}

internal static IStacSummaryItem CreateSummaryValueSetFromArrays(IEnumerable<object> arg)
{
return new StacSummaryValueSet<object>(arg.SelectMany(a => a as IEnumerable<object>).Distinct());
}
}
}
2 changes: 1 addition & 1 deletion src/DotNetStac/Extensions/View/ViewStacExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Stac.Extensions.View
public class ViewStacExtension : StacPropertiesContainerExtension, IStacExtension
{

public const string JsonSchemaUrl = "https://stac-extensions.github.io/sat/v1.0.0/schema.json";
public const string JsonSchemaUrl = "https://stac-extensions.github.io/view/v1.0.0/schema.json";
private readonly Dictionary<string, Type> itemFields;

public static string OffNadirField => "view:off_nadir";
Expand Down
11 changes: 4 additions & 7 deletions src/DotNetStac/Schemas/StacSchemaResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Stac.Extensions
{
public class StacSchemaResolver
public class StacSchemaResolver
{
private readonly JSchemaResolver jSchemaResolver;
private IDictionary<string, JSchema> schemaCompiled;
Expand All @@ -28,10 +28,7 @@ public JSchema LoadSchema(string baseUrl = null, string version = null, string s
Uri baseUri = null;
if (string.IsNullOrEmpty(baseUrl))
{
if (CoreTypes.Contains(shortcut))
baseUri = new Uri($"https://schemas.stacspec.org/{vversion}/");
else
baseUri = new Uri($"https://schemas.stacspec.org/");
baseUri = new Uri($"https://schemas.stacspec.org/{vversion}/");
}
else
baseUri = new Uri(baseUrl);
Expand All @@ -47,7 +44,7 @@ public JSchema LoadSchema(string baseUrl = null, string version = null, string s
// Capture a very common mistake and give a better explanation (see #4)
throw new Exception("'stac_extensions' must contain 'projection instead of 'proj'.");
}
schemaUri = new Uri(baseUri, $"extensions/{shortcut}/json-schema/schema.json`");
schemaUri = new Uri(baseUri, $"extensions/{shortcut}/json-schema/schema.json");
isExtension = true;
}
else
Expand All @@ -66,7 +63,7 @@ public JSchema LoadSchema(string baseUrl = null, string version = null, string s
}
else
{
var stream = jSchemaResolver.GetSchemaResource(null, new SchemaReference() { BaseUri = schemaUri } );
var stream = jSchemaResolver.GetSchemaResource(null, new SchemaReference() { BaseUri = schemaUri });
var sr = new StreamReader(stream);
schemaCompiled[schemaUri.ToString()] = JSchema.Parse(sr.ReadToEnd(), jSchemaResolver);
return schemaCompiled[schemaUri.ToString()];
Expand Down
13 changes: 10 additions & 3 deletions src/DotNetStac/StacCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,19 @@ public static StacCollection Create(Uri collectionUri, string id, string descrip
}),
license);

items.SelectMany(item => item.Value.GetDeclaredExtensions().Select(ext => ext.GetSummaryFunctions()));
var summaryFunctions = items.SelectMany(item => item.Value.GetDeclaredExtensions().SelectMany(ext => ext.GetSummaryFunctions()))
.GroupBy(prop => prop.Key)
.ToDictionary(key => key.Key, value => value.First().Value);

summaryFunctions.Add("gsd", StacPropertiesContainerExtension.CreateSummaryStatsObject);
summaryFunctions.Add("platform", StacPropertiesContainerExtension.CreateSummaryValueSet);
summaryFunctions.Add("constellation", StacPropertiesContainerExtension.CreateSummaryValueSet);
summaryFunctions.Add("instruments", StacPropertiesContainerExtension.CreateSummaryValueSetFromArrays);

collection.Summaries =
items.Values.SelectMany(item => item.Properties)
items.Values.SelectMany(item => item.Properties.Where(k => summaryFunctions.Keys.Contains(k.Key)))
.GroupBy(prop => prop.Key)
.ToDictionary(key => key.Key, value => new StacSummaryValueSet<object>(value.Select(i => i.Value).Distinct()) as IStacSummaryItem);
.ToDictionary(key => key.Key, value => summaryFunctions[value.Key](value.Select(i => i.Value)));

return collection;
}
Expand Down
2 changes: 2 additions & 0 deletions src/DotNetStac/Versions/StacVersionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public static class StacVersionList
{
public static SemVersion Current => V100rc1;

public static SemVersion V100rc2 => new SemVersion(1, 0, 0, "rc.2");

public static SemVersion V100rc1 => new SemVersion(1, 0, 0, "rc.1");

public static SemVersion V100beta2 => new SemVersion(1, 0, 0, "beta.2");
Expand Down

0 comments on commit 8acb392

Please sign in to comment.