Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
Events = AzureMapsControl.Components.Drawing.DrawingToolbarEventActivationFlags.All()
}"
OnDrawingModeChanged="OnDrawingModeChanged"
OnDrawingComplete="OnDrawingComplete"
/>

@code {
public async Task OnDrawingModeChanged(AzureMapsControl.Components.Drawing.DrawingToolbarModeEventArgs eventArgs)
{
Console.WriteLine(eventArgs.NewMode);
}

public async Task OnDrawingComplete(AzureMapsControl.Components.Drawing.DrawingToolbarEventArgs eventArgs)
{
Console.WriteLine(eventArgs.Data);
}
}
98 changes: 98 additions & 0 deletions src/AzureMapsControl.Components/Atlas/Feature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text.Json.Serialization;

[ExcludeFromCodeCoverage]
[JsonConverter(typeof(FeatureJsonConverter))]
public abstract class Feature
{
[JsonConverter(typeof(FeatureIdConverter))]
Expand Down Expand Up @@ -55,6 +56,44 @@ public Feature(TGeometry geometry, IDictionary<string, object> properties) : thi
public Feature(string id, TGeometry geometry, IDictionary<string, object> properties) : base(id, properties) => Geometry = geometry;
}

internal class FeatureJsonConverter : JsonConverter<Feature>
{
public override Feature Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var originalDepth = reader.CurrentDepth;

string Id = null;
IDictionary<string, object> Properties = null;
Geometry geometry = null;

while (reader.TokenType != JsonTokenType.EndObject || originalDepth != reader.CurrentDepth)
{
reader.Read();

if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "id")
{
reader.Read();
var converter = new FeatureIdConverter();
Id = converter.Read(ref reader, typeof(string), options);
}

if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "properties")
{
var converter = new FeaturePropertiesConverter();
Properties = converter.Read(ref reader, typeof(IDictionary<string, object>), options);
}

}


return new Feature<Geometry>(Id, geometry, Properties);
}

public override void Write(Utf8JsonWriter writer, Feature value, JsonSerializerOptions options) => throw new NotSupportedException();


}

internal sealed class FeatureIdConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Expand Down Expand Up @@ -93,4 +132,63 @@ public override void Write(Utf8JsonWriter writer, IDictionary<string, object> va
writer.WriteEndObject();
}
}

internal class FeatureJsonConverter<TGeometry> : JsonConverter<Feature<TGeometry>> where TGeometry : Geometry
{
public FeatureJsonConverter() { }

public override Feature<TGeometry> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var originalDepth = reader.CurrentDepth;

string Id = null;
IDictionary<string, object> Properties = null;
TGeometry geometry = null;

while (reader.TokenType != JsonTokenType.EndObject || originalDepth != reader.CurrentDepth)
{
reader.Read();

if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "id")
{
reader.Read();
var converter = new FeatureIdConverter();
Id = converter.Read(ref reader, typeof(string), options);
}

if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "geometry")
{
var converter = new GeometryJsonConverter<TGeometry>();
geometry = converter.Read(ref reader, typeof(TGeometry), options);
}

if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "properties")
{
var converter = new FeaturePropertiesConverter();
Properties = converter.Read(ref reader, typeof(IDictionary<string, object>), options);
}

}


return new Feature<TGeometry>(Id, geometry, Properties);
}

public override void Write(Utf8JsonWriter writer, Feature<TGeometry> value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("geometry");
JsonSerializer.Serialize(writer, value.Geometry);
writer.WriteString("id", value.Id);
writer.WritePropertyName("properties");
JsonSerializer.Serialize(writer, value.Properties);
writer.WritePropertyName("bbox");
JsonSerializer.Serialize(writer, value.BBox);
writer.WriteEndObject();
}

}



}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[ExcludeFromCodeCoverage]
public sealed class DrawingToolbarEventArgs : MapEventArgs
{
public Feature Data { get; }
public Feature<Geometry> Data { get; }

internal DrawingToolbarEventArgs(Map map, DrawingToolbarJsEventArgs eventArgs) : base(map, eventArgs.Type) => Data = eventArgs.Data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ internal class DrawingToolbarJsEventArgs
{
public string Type { get; set; }
public string NewMode { get; set; }
public Feature Data { get; set; }
public Feature<Geometry> Data { get; set; }
}
}
97 changes: 97 additions & 0 deletions tests/AzureMapsControl.Components.Tests/Atlas/Feature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
namespace AzureMapsControl.Components.Tests.Atlas
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

using AzureMapsControl.Components.Atlas;
using AzureMapsControl.Components.Tests.Json;

using Xunit;
public class FeaturePolygonJsonConverterTests : JsonConverterTests<Feature<Polygon>>
{
JsonSerializerOptions _jsonSerializerOptions;
public FeaturePolygonJsonConverterTests() : base(new FeatureJsonConverter<Polygon>())
{
_jsonSerializerOptions = new JsonSerializerOptions {
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
}

[Fact]
public void Should_ReadFeature()
{
var feature = new Feature<Polygon>("b9d9f4b7-e5cc-42da-8b22-7bb36fe9fa23", new Polygon() {
GeometryType = "Polygon",
Coordinates = new[] {
new [] {
new Position(0, 1),
new Position(2, 3)
}
},
Id = "abcd-abcd-abcd-abcd-abcd"
}, new Dictionary<string, object> { { "_azureMapsShapeId", "efgh-efgh-efgh-efgh-efgh" } });
var json = JsonSerializer.Serialize(feature, _jsonSerializerOptions);
var result = Read(json);
Assert.IsType<Feature<Polygon>>(result);
Assert.Equal(json, JsonSerializer.Serialize(result, _jsonSerializerOptions));
}

[Fact]
public void Should_Write()
{
var geometry = new Feature<Polygon>("b9d9f4b7-e5cc-42da-8b22-7bb36fe9fa23", new Polygon() {
GeometryType = "Polygon",
Coordinates = new[] {
new [] {
new Position(0, 1),
new Position(2, 3)
}
},
Id = "abcd-abcd-abcd-abcd-abcd"
}, new Dictionary<string, object> { { "_azureMapsShapeId", "efgh-efgh-efgh-efgh-efgh" } });
TestAndAssertWrite(geometry, JsonSerializer.Serialize(geometry, _jsonSerializerOptions));
}

}

public class FeaturePointJsonConverterTests : JsonConverterTests<Feature<Point>>
{
JsonSerializerOptions _jsonSerializerOptions;
public FeaturePointJsonConverterTests() : base(new FeatureJsonConverter<Point>())
{
_jsonSerializerOptions = new JsonSerializerOptions {
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
}

[Fact]
public void Should_ReadFeature()
{
var feature = new Feature<Point>("b9d9f4b7-e5cc-42da-8b22-7bb36fe9fa23", new Point() {
GeometryType = "Point",
Coordinates = new Position(0, 1),
Id = "abcd-abcd-abcd-abcd-abcd"
}, new Dictionary<string, object> { { "_azureMapsShapeId", "efgh-efgh-efgh-efgh-efgh" } });
var json = JsonSerializer.Serialize(feature, _jsonSerializerOptions);
var result = Read(json);
Assert.IsType<Feature<Point>>(result);
Assert.Equal(json, JsonSerializer.Serialize(result, _jsonSerializerOptions));
}

[Fact]
public void Should_Write()
{
var feature = new Feature<Point>("b9d9f4b7-e5cc-42da-8b22-7bb36fe9fa23", new Point() {
GeometryType = "Point",
Coordinates = new Position(0, 1),
Id = "abcd-abcd-abcd-abcd-abcd"
}, new Dictionary<string, object> { { "_azureMapsShapeId", "efgh-efgh-efgh-efgh-efgh" } });
TestAndAssertWrite(feature, JsonSerializer.Serialize(feature, _jsonSerializerOptions));
}

}
}