Skip to content

Commit

Permalink
TolerantEnumConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelmathot committed May 25, 2021
1 parent b62ff93 commit 82c77f6
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/DotNetStac.Test/Extensions/EoExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,15 @@ public void CloneEoItem()
JsonAssert.AreEqual(json, actualJson);
}

[Fact]
public void WrongCommonName()
{
var json = GetJson("Extensions");

var item = StacConvert.Deserialize<StacItem>(json);

Assert.Null(item.Assets["MS2"].EoExtension().Bands[0].CommonName);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
{
"stac_extensions": [
"https://stac-extensions.github.io/eo/v1.0.0/schema.json",
"https://stac-extensions.github.io/sat/v1.0.0/schema.json",
"https://stac-extensions.github.io/projection/v1.0.0/schema.json",
"https://stac-extensions.github.io/view/v1.0.0/schema.json"
],
"stac_version": "1.0.0-rc.4",
"links": [
{
"type": "application/json; profile=stac-item",
"rel": "derived_from",
"href": "786-KARI-KOMPSAT3-urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml.json"
},
{
"type": "application/json; profile=stac-item",
"rel": "self",
"href": "http://localhost:8082/catalog/acquisitions/call-786/786-KARI-KOMPSAT3-urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml/K3_20201112193439_45302_18521139_L1G.json"
},
{
"type": "application/json; profile=stac-catalog",
"rel": "root",
"href": "http://localhost:8082/catalog/catalog.json"
}
],
"assets": {
"metadata": {
"type": "text/xml",
"roles": [
"metadata"
],
"href": "http://localhost:8082/catalog/acquisitions/call-786/786-KARI-KOMPSAT3-urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml/urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml-product/K3_20201112193439_45302_18521139_L1G_Aux.xml"
},
"MS1": {
"type": "image/tiff",
"roles": [
"data"
],
"href": "http://localhost:8082/catalog/acquisitions/call-786/786-KARI-KOMPSAT3-urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml/urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml-product/K3_20201112193439_45302_18521139_L1G_B.tif",
"gsd": 2.8,
"eo:bands": [
{
"name": "MS1",
"common_name": "blue",
"center_wavelength": 0.07,
"scale": 36.36363636363637,
"offset": -28.4989,
"solar_illumination": 2001.0
}
]
},
"overview": {
"type": "image/jpeg",
"roles": [
"overview"
],
"href": "http://localhost:8082/catalog/acquisitions/call-786/786-KARI-KOMPSAT3-urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml/urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml-product/K3_20201112193439_45302_18521139_L1G_br.jpg"
},
"MS2": {
"type": "image/tiff",
"roles": [
"data"
],
"href": "http://localhost:8082/catalog/acquisitions/call-786/786-KARI-KOMPSAT3-urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml/urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml-product/K3_20201112193439_45302_18521139_L1G_G.tif",
"gsd": 2.8,
"eo:bands": [
{
"name": "MS2",
"common_name": "ndvi",
"center_wavelength": 0.08,
"scale": 26.17801047120419,
"offset": -27.3846,
"solar_illumination": 1875.0
}
]
},
"MS4": {
"type": "image/tiff",
"roles": [
"data"
],
"href": "http://localhost:8082/catalog/acquisitions/call-786/786-KARI-KOMPSAT3-urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml/urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml-product/K3_20201112193439_45302_18521139_L1G_N.tif",
"gsd": 2.8,
"eo:bands": [
{
"name": "MS4",
"common_name": "nir",
"center_wavelength": 0.14,
"scale": 52.083333333333336,
"offset": -13.591,
"solar_illumination": 1027.0
}
]
},
"PAN": {
"type": "image/tiff",
"roles": [
"data"
],
"href": "http://localhost:8082/catalog/acquisitions/call-786/786-KARI-KOMPSAT3-urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml/urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml-product/K3_20201112193439_45302_18521139_L1G_P.tif",
"gsd": 0.7,
"eo:bands": [
{
"name": "PAN",
"common_name": "pan",
"center_wavelength": 0.45
}
]
},
"MS3": {
"type": "image/tiff",
"roles": [
"data"
],
"href": "http://localhost:8082/catalog/acquisitions/call-786/786-KARI-KOMPSAT3-urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml/urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml-product/K3_20201112193439_45302_18521139_L1G_R.tif",
"gsd": 2.8,
"eo:bands": [
{
"name": "MS3",
"common_name": "red",
"center_wavelength": 0.06,
"scale": 34.013605442176875,
"offset": -21.6854,
"solar_illumination": 1525.0
}
]
},
"thumbnail": {
"type": "image/jpeg",
"roles": [
"thumbnail"
],
"href": "http://localhost:8082/catalog/acquisitions/call-786/786-KARI-KOMPSAT3-urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml/urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml-product/K3_20201112193439_45302_18521139_L1G_th.jpg"
}
},
"type": "Feature",
"id": "K3_20201112193439_45302_18521139_L1G",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-92.64663661,
18.576395368
],
[
-92.529930348,
18.57614059
],
[
-92.413226243,
18.575813599
],
[
-92.412722213,
18.722456862
],
[
-92.529526555,
18.722786621
],
[
-92.646333063,
18.723043555
],
[
-92.64663661,
18.576395368
]
]
]
},
"properties": {
"datetime": "2020-11-12T20:34:47.8741569+00:00",
"start_datetime": "2020-11-12T20:34:47.8741569+00:00",
"end_datetime": "2020-11-12T20:34:50.2062721+00:00",
"updated": "2020-12-13T19:01:45.790996Z",
"platform": "kompsat-3",
"constellation": "kompsat-3",
"mission": "kompsat-3",
"instruments": [
"aeiss"
],
"gsd": 0.7,
"title": "dummy",
"description": "<div style=\"float: left; width: 64px;\"><img width=\"64px\" src=\"https://d1nhio0ox7pgb.cloudfront.net/_img/g_collection_png/standard/128x128/satellite.png\" /></div>\n\n### dummy\n\n* Platform **kompsat-3**\n* Instruments **aeiss**\n* Sensing Time **[11/12/2020 20:34:47]**\n* Created **1/1/0001 12:00:00 AM**\n* Updated **12/13/2020 7:01:45 PM**\n",
"eo:cloud_cover": 0.0,
"sat:absolute_orbit": 45302,
"sat:relative_orbit": 45302,
"sat:orbit_state": "ascending",
"proj:epsg": 32615,
"proj:wkt2": "PROJCS[\"WGS 84 / UTM zone 15N\", GEOGCS[\"WGS 84\", DATUM[\"World Geodetic System 1984\", SPHEROID[\"WGS 84\", 6378137, 298.257223563, AUTHORITY[\"EPSG\", \"7030\"]], AUTHORITY[\"EPSG\", \"6326\"]], PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.017453292519943295, AUTHORITY[\"EPSG\", \"9102\"]], AUTHORITY[\"EPSG\", \"4326\"]], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], PROJECTION[\"UTM15N\", AUTHORITY[\"EPSG\", \"32615\"]], PARAMETER[\"latitude_of_origin\", 0], PARAMETER[\"central_meridian\", -93], PARAMETER[\"scale_factor\", 0.9996], PARAMETER[\"false_easting\", 500000], PARAMETER[\"false_northing\", 0], AUTHORITY[\"EPSG\", \"32615\"], AXIS[\"East\", EAST], AXIS[\"North\", NORTH]]",
"view:off_nadir": 30.864219939,
"view:incidence_angle": 34.605545581,
"view:azimuth": 318.194222473,
"view:sun_azimuth": 212.9437888826261,
"view:sun_elevation": 46.962222523232704,
"proc:level": "Level1G",
"product_type": "PAN_MS_L1G",
"eo:bands": [
{
"name": "MS1",
"common_name": "blue",
"center_wavelength": 0.07,
"scale": 36.36363636363637,
"offset": -28.4989,
"solar_illumination": 2001.0
},
{
"name": "MS2",
"common_name": "green",
"center_wavelength": 0.08,
"scale": 26.17801047120419,
"offset": -27.3846,
"solar_illumination": 1875.0
},
{
"name": "MS4",
"common_name": "nir",
"center_wavelength": 0.14,
"scale": 52.083333333333336,
"offset": -13.591,
"solar_illumination": 1027.0
},
{
"name": "PAN",
"common_name": "pan",
"center_wavelength": 0.45
},
{
"name": "MS3",
"common_name": "red",
"center_wavelength": 0.06,
"scale": 34.013605442176875,
"offset": -21.6854,
"solar_illumination": 1525.0
}
],
"disaster:class": "Acquisition",
"status": {
"stage": "dataset",
"message": null
},
"disaster:call_ids": [
786
],
"cos2:xml": null,
"cos2:id": "786-KARI-KOMPSAT3-urn_ogc_def_EOP_KARI_KOMPSAT3_K3_20201112193439_45302_18521139_L1G_Aux_xml"
},
"bbox": [
-92.64663661,
18.575813599,
-92.412722213,
18.723043555
]
}
2 changes: 1 addition & 1 deletion src/DotNetStac.Test/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class TestBase
.Assembly;
private static readonly string AssemblyName = ThisAssembly.GetName().Name;

private StacValidator stacValidator = new StacValidator(new JSchemaUrlResolver());
private static StacValidator stacValidator = new StacValidator(new JSchemaUrlResolver());

protected HttpClient httpClient = new HttpClient();

Expand Down
76 changes: 76 additions & 0 deletions src/DotNetStac/Common/TolerantEnumConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

using System;
using System.Linq;
using Newtonsoft.Json;

namespace Stac.Common
{
class TolerantEnumConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
Type type = IsNullableType(objectType) ? Nullable.GetUnderlyingType(objectType) : objectType;
return type.IsEnum;
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
bool isNullable = IsNullableType(objectType);
Type enumType = isNullable ? Nullable.GetUnderlyingType(objectType) : objectType;

string[] names = Enum.GetNames(enumType);

if (reader.TokenType == JsonToken.String)
{
string enumText = reader.Value.ToString();

if (!string.IsNullOrEmpty(enumText))
{
string match = names
.Where(n => string.Equals(n, enumText, StringComparison.OrdinalIgnoreCase))
.FirstOrDefault();

if (match != null)
{
return Enum.Parse(enumType, match);
}
}
}
else if (reader.TokenType == JsonToken.Integer)
{
int enumVal = Convert.ToInt32(reader.Value);
int[] values = (int[])Enum.GetValues(enumType);
if (values.Contains(enumVal))
{
return Enum.Parse(enumType, enumVal.ToString());
}
}

if (!isNullable)
{
string defaultName = names
.Where(n => string.Equals(n, "Unknown", StringComparison.OrdinalIgnoreCase))
.FirstOrDefault();

if (defaultName == null)
{
defaultName = names.First();
}

return Enum.Parse(enumType, defaultName);
}

return null;
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString());
}

private bool IsNullableType(Type t)
{
return (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
}
}
}
5 changes: 5 additions & 0 deletions src/DotNetStac/Extensions/Eo/EoBandCommonName.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Stac.Common;

namespace Stac.Extensions.Eo
{
/// <summary>
/// Enumeration of the allowed band common name is the name that is commonly used to refer to that band's spectral properties.
/// </summary>
[JsonConverter(typeof(TolerantEnumConverter))]
public enum EoBandCommonName
{
coastal,
Expand Down
Loading

0 comments on commit 82c77f6

Please sign in to comment.