From c7c900af0ec158c6f91f666d93328d92ba76f947 Mon Sep 17 00:00:00 2001 From: Emmanuel Mathot Date: Mon, 9 Nov 2020 22:36:09 +0100 Subject: [PATCH] More helpers for collections --- .../Extensions/AssignableStacExtension.cs | 5 ++++- src/DotNetStac/Item/StacItem.Helper.cs | 2 +- src/DotNetStac/Item/StacItem.Model.cs | 1 + src/DotNetStac/StacExtent.cs | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/DotNetStac/Extensions/AssignableStacExtension.cs b/src/DotNetStac/Extensions/AssignableStacExtension.cs index ba6d8026..55ba4a76 100644 --- a/src/DotNetStac/Extensions/AssignableStacExtension.cs +++ b/src/DotNetStac/Extensions/AssignableStacExtension.cs @@ -1,4 +1,5 @@ using System; +using System.Reflection; using Newtonsoft.Json.Linq; using Stac.Item; @@ -54,7 +55,9 @@ protected T GetField(string fieldName) if (@object == null) return default(T); if (@object is JToken) return (@object as JToken).ToObject(); - return (T)@object; + if (typeof(T).GetTypeInfo().IsEnum) + return (T)Enum.Parse(typeof(T), @object.ToString()); + return (T)Convert.ChangeType(@object, typeof(T)); } } diff --git a/src/DotNetStac/Item/StacItem.Helper.cs b/src/DotNetStac/Item/StacItem.Helper.cs index 40a4788a..62604b0b 100644 --- a/src/DotNetStac/Item/StacItem.Helper.cs +++ b/src/DotNetStac/Item/StacItem.Helper.cs @@ -117,7 +117,7 @@ public Itenso.TimePeriod.ITimePeriod DateTime } } } - return null; + return Itenso.TimePeriod.TimeInterval.Anytime; } set { diff --git a/src/DotNetStac/Item/StacItem.Model.cs b/src/DotNetStac/Item/StacItem.Model.cs index 427d2cef..5d592aa7 100644 --- a/src/DotNetStac/Item/StacItem.Model.cs +++ b/src/DotNetStac/Item/StacItem.Model.cs @@ -44,6 +44,7 @@ public StacItem(StacItem stacItem) : this(stacItem.Geometry, stacItem.Properties this.assets = stacItem.assets; this.collection = stacItem.collection; this.sourceUri = stacItem.sourceUri; + this.extensions = stacItem.extensions; } [JsonProperty("stac_extensions")] diff --git a/src/DotNetStac/StacExtent.cs b/src/DotNetStac/StacExtent.cs index 2fa9d90c..02e9db92 100644 --- a/src/DotNetStac/StacExtent.cs +++ b/src/DotNetStac/StacExtent.cs @@ -1,4 +1,8 @@ +using System; +using System.Collections.Generic; +using System.Linq; using Newtonsoft.Json; +using Stac.Item; namespace Stac { @@ -10,5 +14,17 @@ public class StacExtent [JsonProperty("temporal")] public StacTemporalExtent Temporal { get; set; } + + public static StacExtent Create(IEnumerable items) + { + return new StacExtent() + { + Spatial = new StacSpatialExtent(items.Min(i => i.GetBoundingBoxFromGeometryExtent()[0]), + items.Min(i => i.GetBoundingBoxFromGeometryExtent()[1]), + items.Max(i => i.GetBoundingBoxFromGeometryExtent()[2]), + items.Max(i => i.GetBoundingBoxFromGeometryExtent()[3])), + Temporal = new StacTemporalExtent(items.Min(i => i.DateTime.Start), items.Max(i => i.DateTime.End)) + }; + } } } \ No newline at end of file