Skip to content

Commit

Permalink
New Accessor for common metadata of StacItem
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelmathot committed Oct 15, 2020
1 parent d979da4 commit 07eb788
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 118 deletions.
6 changes: 3 additions & 3 deletions src/DotNetStac/Extensions/AssignableStacExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -48,7 +48,7 @@ protected object GetField(string fieldName)
return StacObject.Properties[key];
}

internal T GetField<T>(string fieldName)
protected T GetField<T>(string fieldName)
{
var @object = GetField(fieldName);
if (@object == null) return default(T);
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetStac/Extensions/StacExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public StacExtensions(IEnumerable<IStacExtension> stacExtensions = null)
}
}

internal void InitStacObject(IStacObject stacObject)
public void InitStacObject(IStacObject stacObject)
{
foreach (var stacExtension in Values.OfType<AssignableStacExtension>())
{
Expand Down
148 changes: 139 additions & 9 deletions src/DotNetStac/Item/StacItem.Helper.cs
Original file line number Diff line number Diff line change
@@ -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<IStacItem> LoadUri(Uri uri)
{
var catalog = await StacFactory.LoadUriAsync(uri);
Expand Down Expand Up @@ -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<string>("title");
set => this.SetProperty("title", value);
}

[JsonIgnore]
public string Description
{
get => this.GetProperty<string>("description");
set => this.SetProperty("description", value);
}

[JsonIgnore]
public DateTime Created
{
get => this.GetProperty<DateTime>("created");
set => this.SetProperty("created", value);
}

[JsonIgnore]
public DateTime Updated
{
get => this.GetProperty<DateTime>("updated");
set => this.SetProperty("updated", value);
}

}
}
80 changes: 2 additions & 78 deletions src/DotNetStac/Item/StacItem.Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
{
Expand Down Expand Up @@ -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)
Expand All @@ -189,7 +114,6 @@ internal void OnSerializingMethod(StreamingContext context)
StacExtensionsStrings = StacExtensionsStrings.Concat(StacExtensions.Keys).Distinct().ToArray();
}

[JsonIgnore]
public bool IsCatalog => false;

}
}
35 changes: 35 additions & 0 deletions src/DotNetStac/StacAccessorsHelpers.cs
Original file line number Diff line number Diff line change
@@ -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<T>(this IStacObject stacObject, string key)
{
if (!stacObject.Properties.ContainsKey(key))
return default(T);
return (T)stacObject.Properties[key];
}

}
}
27 changes: 0 additions & 27 deletions src/DotNetStac/StacCommonMetadataHelpers.cs

This file was deleted.

0 comments on commit 07eb788

Please sign in to comment.