diff --git a/src/DotNetStac/Extensions/AssignableStacExtension.cs b/src/DotNetStac/Extensions/AssignableStacExtension.cs index 4847902e..ba6d8026 100644 --- a/src/DotNetStac/Extensions/AssignableStacExtension.cs +++ b/src/DotNetStac/Extensions/AssignableStacExtension.cs @@ -29,12 +29,12 @@ public IStacObject StacObject } } - internal void InitStacObject(IStacObject stacObject) + public void InitStacObject(IStacObject stacObject) { this.stacObject = stacObject; } - internal void SetField(string key, object value) + protected void SetField(string key, object value) { StacObject.Properties.Remove(prefix + ":" + key); StacObject.Properties.Add(prefix + ":" + key, value); @@ -48,7 +48,7 @@ protected object GetField(string fieldName) return StacObject.Properties[key]; } - internal T GetField(string fieldName) + protected T GetField(string fieldName) { var @object = GetField(fieldName); if (@object == null) return default(T); diff --git a/src/DotNetStac/Extensions/StacExtensions.cs b/src/DotNetStac/Extensions/StacExtensions.cs index da5c91ec..c045bb4a 100644 --- a/src/DotNetStac/Extensions/StacExtensions.cs +++ b/src/DotNetStac/Extensions/StacExtensions.cs @@ -20,7 +20,7 @@ public StacExtensions(IEnumerable stacExtensions = null) } } - internal void InitStacObject(IStacObject stacObject) + public void InitStacObject(IStacObject stacObject) { foreach (var stacExtension in Values.OfType()) { diff --git a/src/DotNetStac/Item/StacItem.Helper.cs b/src/DotNetStac/Item/StacItem.Helper.cs index 259f70b5..40a4788a 100644 --- a/src/DotNetStac/Item/StacItem.Helper.cs +++ b/src/DotNetStac/Item/StacItem.Helper.cs @@ -1,26 +1,17 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.IO; -using System.Linq; -using System.Net; using System.Threading.Tasks; using Stac; -using Stac.Converters; -using GeoJSON.Net.Geometry; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using Stac.Catalog; using Stac.Extensions; -using Stac.Model; namespace Stac.Item { public partial class StacItem : IStacItem, IStacObject { - - public static async Task LoadUri(Uri uri) { var catalog = await StacFactory.LoadUriAsync(uri); @@ -64,5 +55,144 @@ private static IStacItem LoadStacItem(JToken jsonRoot) return (IStacItem)jsonRoot.ToObject(itemType); } + + [JsonIgnore] + public bool IsCatalog => false; + + [JsonIgnore] + public Uri Uri + { + get + { + if (sourceUri == null) + { + return new Uri(Id + ".json", UriKind.Relative); + } + return sourceUri; + } + set { sourceUri = value; } + } + + public IStacObject Upgrade() + { + return this; + } + + [JsonIgnore] + public Itenso.TimePeriod.ITimePeriod DateTime + { + get + { + if (Properties.ContainsKey("datetime")) + { + if (Properties["datetime"] is DateTime) + return new Itenso.TimePeriod.TimeInterval((DateTime)Properties["datetime"]); + else + { + try + { + return new Itenso.TimePeriod.TimeInterval(System.DateTime.Parse(Properties["datetime"].ToString())); + } + catch (Exception e) + { + if (Properties.ContainsKey("start_datetime") && Properties.ContainsKey("end_datetime")) + { + if (Properties["start_datetime"] is DateTime && Properties["end_datetime"] is DateTime) + return new Itenso.TimePeriod.TimeInterval((DateTime)Properties["start_datetime"], + (DateTime)Properties["end_datetime"]); + else + { + try + { + return new Itenso.TimePeriod.TimeInterval(System.DateTime.Parse(Properties["start_datetime"].ToString()), + System.DateTime.Parse(Properties["end_datetime"].ToString())); + } + catch (Exception e1) + { + throw new FormatException(string.Format("start_datetime or end_datetime {0} is not a valid"), e1); + } + } + } + throw new FormatException(string.Format("datetime {0} is not a valid"), e); + } + } + } + return null; + } + set + { + // remove previous values + Properties.Remove("datetime"); + Properties.Remove("start_datetime"); + Properties.Remove("end_datetime"); + + // datetime, start_datetime, end_datetime + if (value.IsAnytime) + { + Properties.Add("datetime", null); + } + + if (value.IsMoment) + { + Properties.Add("datetime", value.Start); + } + else + { + Properties.Add("datetime", value.Start); + Properties.Add("start_datetime", value.Start); + Properties.Add("end_datetime", value.Start); + } + } + } + + private StacExtensions extensions; + + [JsonIgnore] + public StacExtensions StacExtensions + { + get + { + if (extensions == null) + { + extensions = new StacExtensions(); + extensions.InitStacObject(this); + } + return extensions; + } + set + { + extensions = value; + extensions.InitStacObject(this); + } + } + + [JsonIgnore] + public string Title + { + get => this.GetProperty("title"); + set => this.SetProperty("title", value); + } + + [JsonIgnore] + public string Description + { + get => this.GetProperty("description"); + set => this.SetProperty("description", value); + } + + [JsonIgnore] + public DateTime Created + { + get => this.GetProperty("created"); + set => this.SetProperty("created", value); + } + + [JsonIgnore] + public DateTime Updated + { + get => this.GetProperty("updated"); + set => this.SetProperty("updated", value); + } + } } diff --git a/src/DotNetStac/Item/StacItem.Model.cs b/src/DotNetStac/Item/StacItem.Model.cs index e7c019b8..410c6bf7 100644 --- a/src/DotNetStac/Item/StacItem.Model.cs +++ b/src/DotNetStac/Item/StacItem.Model.cs @@ -21,10 +21,10 @@ public partial class StacItem : GeoJSON.Net.Feature.Feature, IStacObject, IInter private string stacVersion = StacVersionList.Current; - private StacExtensions extensions; private string collection; private Uri sourceUri; + private string[] stacExtensionsStrings = new string[0]; [JsonConstructor] @@ -40,30 +40,6 @@ public StacItem(IGeometryObject geometry, object properties, string id = null) : public string[] StacExtensionsStrings { get => stacExtensionsStrings; set => stacExtensionsStrings = value; } - [JsonIgnore] - public StacExtensions StacExtensions - { - get - { - if (extensions == null) - { - extensions = new StacExtensions(); - extensions.InitStacObject(this); - } - return extensions; - } - set - { - extensions = value; - extensions.InitStacObject(this); - } - } - - private object[] GetStactExtensionConverterParameters() - { - return new object[1] { this }; - } - [JsonProperty("stac_version")] public string StacVersion { @@ -118,58 +94,7 @@ public string Collection } } - [JsonIgnore] - public Itenso.TimePeriod.ITimePeriod DateTime - { - get - { - if (Properties.ContainsKey("datetime")) - { - if (Properties["datetime"] is DateTime) - return new Itenso.TimePeriod.TimeInterval((DateTime)Properties["datetime"]); - else - { - try - { - return new Itenso.TimePeriod.TimeInterval(System.DateTime.Parse(Properties["datetime"].ToString())); - } - catch (Exception e) - { - if (Properties.ContainsKey("start_datetime") && Properties.ContainsKey("end_datetime")) - { - if (Properties["start_datetime"] is DateTime && Properties["end_datetime"] is DateTime) - return new Itenso.TimePeriod.TimeInterval((DateTime)Properties["start_datetime"], - (DateTime)Properties["end_datetime"]); - else - { - try - { - return new Itenso.TimePeriod.TimeInterval(System.DateTime.Parse(Properties["start_datetime"].ToString()), - System.DateTime.Parse(Properties["end_datetime"].ToString())); - } - catch (Exception e1) - { - throw new FormatException(string.Format("start_datetime or end_datetime {0} is not a valid"), e1); - } - } - } - throw new FormatException(string.Format("datetime {0} is not a valid"), e); - } - } - } - - - return null; - } - } - - [JsonIgnore] - public Uri Uri { get => sourceUri; set => sourceUri = value; } - public IStacObject Upgrade() - { - return this; - } [OnDeserialized] internal void OnDeserializedMethod(StreamingContext context) @@ -189,7 +114,6 @@ internal void OnSerializingMethod(StreamingContext context) StacExtensionsStrings = StacExtensionsStrings.Concat(StacExtensions.Keys).Distinct().ToArray(); } - [JsonIgnore] - public bool IsCatalog => false; + } } diff --git a/src/DotNetStac/StacAccessorsHelpers.cs b/src/DotNetStac/StacAccessorsHelpers.cs new file mode 100644 index 00000000..eaea70d5 --- /dev/null +++ b/src/DotNetStac/StacAccessorsHelpers.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Runtime.Serialization; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Stac.Catalog; +using Stac.Collection; +using Stac.Extensions; +using Stac.Item; +using Stac.Model; + +namespace Stac +{ + public static class StacAccessorsHelpers + { + + public static void SetProperty(this IStacObject stacObject, string key, object value) + { + stacObject.Properties.Remove(key); + stacObject.Properties.Add(key, value); + } + + public static T GetProperty(this IStacObject stacObject, string key) + { + if (!stacObject.Properties.ContainsKey(key)) + return default(T); + return (T)stacObject.Properties[key]; + } + + } +} diff --git a/src/DotNetStac/StacCommonMetadataHelpers.cs b/src/DotNetStac/StacCommonMetadataHelpers.cs deleted file mode 100644 index 199602b7..00000000 --- a/src/DotNetStac/StacCommonMetadataHelpers.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Runtime.Serialization; -using System.Threading.Tasks; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using Stac.Catalog; -using Stac.Collection; -using Stac.Extensions; -using Stac.Item; -using Stac.Model; - -namespace Stac -{ - public static class StacCommonMetadataHelpers - { - - public static string GetTitle(this IStacObject stacObject) - { - return stacObject.Properties.ContainsKey("title") ? (string)stacObject.Properties["title"] : null; - } - - } -}