diff --git a/src/DotNetStac.Test/Item/ItemTests.cs b/src/DotNetStac.Test/Item/ItemTests.cs index b3bc1973..13bc4a8b 100644 --- a/src/DotNetStac.Test/Item/ItemTests.cs +++ b/src/DotNetStac.Test/Item/ItemTests.cs @@ -71,11 +71,12 @@ public void CanSerializeMinimalSample() var properties = new Dictionary(); - properties.Add("datetime", DateTime.Parse("2016-05-03T13:21:30.040Z").ToUniversalTime()); properties.Add("collection", "CS3"); StacItem item = new StacItem("CS3-20160503_132130_04", geometry, properties); + item.CommonMetadata().DateTime = new Itenso.TimePeriod.TimeInterval(DateTime.Parse("2016-05-03T13:21:30.040Z")); + item.Links.Add(StacLink.CreateSelfLink(new Uri("http://cool-sat.com/catalog/CS3-20160503_132130_04/CS3-20160503_132130_04.json"))); item.SetCollection("cool-sat", new Uri("http://cool-sat.com/catalog.json")); @@ -99,6 +100,77 @@ public void CanSerializeMinimalSample() Assert.Null(item.Collection); } + [Fact] + public void CanSerializeExtendedSample() + { + var coordinates = new[] + { + new List + { + new Position(37.488035566,-122.308150179), + new Position(37.538869539,-122.597502109), + new Position(37.613537207,-122.576687533), + new Position(37.562818007,-122.288048600), + new Position(37.488035566,-122.308150179) + } + }; + + var geometry = new Polygon(new LineString[] { new LineString(coordinates[0]) }); + + var properties = new Dictionary(); + + properties.Add("collection", "CS3"); + + StacItem item = new StacItem("CS3-20160503_132130_04", geometry, properties); + + item.CommonMetadata().DateTime = new Itenso.TimePeriod.TimeInterval(DateTime.MinValue, DateTime.MaxValue); + item.CommonMetadata().DateTime = new Itenso.TimePeriod.TimeInterval(DateTime.Parse("2016-05-03T13:21:30.040Z"), DateTime.Parse("2016-05-03T14:21:30.040Z")); + + item.Links.Add(StacLink.CreateSelfLink(new Uri("http://cool-sat.com/catalog/CS3-20160503_132130_04/CS3-20160503_132130_04.json"))); + item.SetCollection("cool-sat", new Uri("http://cool-sat.com/catalog.json")); + + item.Assets.Add("analytic", new StacAsset(item, new Uri("relative-path/to/analytic.tif", UriKind.Relative), null, "4-Band Analytic", null)); + item.Assets.Add("thumbnail", StacAsset.CreateThumbnailAsset(item, new Uri("http://cool-sat.com/catalog/CS3-20160503_132130_04/thumbnail.png"), null, "Thumbnail")); + + item.CommonMetadata().Created = new DateTime(2018, 1, 1); + item.CommonMetadata().Updated = new DateTime(2018, 1, 1); + + Assert.Equal(new DateTime(2018, 1, 1), item.CommonMetadata().Created); + Assert.Equal(new DateTime(2018, 1, 1), item.CommonMetadata().Updated); + + item.CommonMetadata().Gsd = 0; + item.CommonMetadata().Gsd = 1; + Assert.Equal(1, item.CommonMetadata().Gsd); + + item.CommonMetadata().Title = "CS3-20160503_132130_04"; + Assert.Equal("CS3-20160503_132130_04", item.CommonMetadata().Title); + + item.CommonMetadata().Platform = "coolsat-3"; + Assert.Equal("coolsat-3", item.CommonMetadata().Platform); + + item.CommonMetadata().Mission = "coolsat-3"; + Assert.Equal("coolsat-3", item.CommonMetadata().Mission); + + item.CommonMetadata().Constellation = "coolsat"; + Assert.Equal("coolsat", item.CommonMetadata().Constellation); + + // item.BoundingBoxes = new double[4] { -122.59750209, 37.48803556, -122.2880486, 37.613537207 }; + item.BoundingBoxes = item.GetBoundingBoxFromGeometryExtent(); + + var actualJson = StacConvert.Serialize(item); + + ValidateJson(actualJson); + + var expectedJson = GetJson("Item"); + + ValidateJson(expectedJson); + + JsonAssert.AreEqual(expectedJson, actualJson); + + item.Links.Remove(item.Links.First(l => l.RelationshipType == "collection")); + Assert.Null(item.Collection); + } + [Fact] public void CanManageDates() { diff --git a/src/DotNetStac.Test/Resources/Item/ItemTests_CanSerializeExtendedSample.json b/src/DotNetStac.Test/Resources/Item/ItemTests_CanSerializeExtendedSample.json new file mode 100644 index 00000000..11e89d51 --- /dev/null +++ b/src/DotNetStac.Test/Resources/Item/ItemTests_CanSerializeExtendedSample.json @@ -0,0 +1,75 @@ +{ + "stac_version": "1.0.0-rc.4", + "type": "Feature", + "id": "CS3-20160503_132130_04", + "bbox": [ + -122.597502109, + 37.488035566, + -122.2880486, + 37.613537207 + ], + "collection": "cool-sat", + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + -122.308150179, + 37.488035566 + ], + [ + -122.597502109, + 37.538869539 + ], + [ + -122.576687533, + 37.613537207 + ], + [ + -122.288048600, + 37.562818007 + ], + [ + -122.308150179, + 37.488035566 + ] + ] + ] + }, + "properties": { + "title": "CS3-20160503_132130_04", + "platform": "coolsat-3", + "mission": "coolsat-3", + "constellation": "coolsat", + "datetime": "2016-05-03T13:21:30.040Z", + "start_datetime": "2016-05-03T13:21:30.040Z", + "end_datetime": "2016-05-03T14:21:30.040Z", + "collection": "CS3", + "created": "2018-01-01T00:00:00Z", + "updated": "2018-01-01T00:00:00Z", + "gsd": 1.0 + }, + "links": [ + { + "rel": "self", + "href": "http://cool-sat.com/catalog/CS3-20160503_132130_04/CS3-20160503_132130_04.json" + }, + { + "rel": "collection", + "href": "http://cool-sat.com/catalog.json" + } + ], + "assets": { + "analytic": { + "href": "relative-path/to/analytic.tif", + "title": "4-Band Analytic" + }, + "thumbnail": { + "href": "http://cool-sat.com/catalog/CS3-20160503_132130_04/thumbnail.png", + "title": "Thumbnail", + "roles": [ + "thumbnail" + ] + } + } +} \ No newline at end of file diff --git a/src/DotNetStac/StacAsset.cs b/src/DotNetStac/StacAsset.cs index 1e4b75f7..9f9a4b47 100644 --- a/src/DotNetStac/StacAsset.cs +++ b/src/DotNetStac/StacAsset.cs @@ -7,6 +7,7 @@ using Newtonsoft.Json; using System.Net.Mime; using System.Runtime.Serialization; +using System.Diagnostics.CodeAnalysis; namespace Stac { @@ -229,6 +230,7 @@ public IDictionary Properties public IStacObject ParentStacObject { get => parentStacObject; internal set => parentStacObject = value; } #pragma warning disable 1591 + [ExcludeFromCodeCoverage] public bool ShouldSerializeStacExtensions() { // don't serialize the Manager property if an employee is their own manager diff --git a/src/DotNetStac/StacCommonMetadata.cs b/src/DotNetStac/StacCommonMetadata.cs index 0140f353..0c0b3f1c 100644 --- a/src/DotNetStac/StacCommonMetadata.cs +++ b/src/DotNetStac/StacCommonMetadata.cs @@ -159,26 +159,25 @@ public Itenso.TimePeriod.ITimePeriod DateTime } set { - // remove previous values - stacPropertiesContainer.Properties.Remove("datetime"); - stacPropertiesContainer.Properties.Remove("start_datetime"); - stacPropertiesContainer.Properties.Remove("end_datetime"); - // datetime, start_datetime, end_datetime if (value.IsAnytime) { - stacPropertiesContainer.Properties.Add("datetime", null); + stacPropertiesContainer.RemoveProperty("start_datetime"); + stacPropertiesContainer.RemoveProperty("end_datetime"); + stacPropertiesContainer.SetProperty("datetime", null); } if (value.IsMoment) { - stacPropertiesContainer.Properties.Add("datetime", value.Start); + stacPropertiesContainer.RemoveProperty("start_datetime"); + stacPropertiesContainer.RemoveProperty("end_datetime"); + stacPropertiesContainer.SetProperty("datetime", value.Start); } else { - stacPropertiesContainer.Properties.Add("datetime", value.Start); - stacPropertiesContainer.Properties.Add("start_datetime", value.Start); - stacPropertiesContainer.Properties.Add("end_datetime", value.End); + stacPropertiesContainer.SetProperty("datetime", value.Start); + stacPropertiesContainer.SetProperty("start_datetime", value.Start); + stacPropertiesContainer.SetProperty("end_datetime", value.End); } } }