Skip to content

Commit f1a9cf0

Browse files
committed
Serialize BoundingBox to number array
1 parent 6a0463e commit f1a9cf0

7 files changed

Lines changed: 91 additions & 50 deletions

File tree

samples/AzureMapsControl.Sample/Pages/WithCameraOptions.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
@using AzureMapsControl.Components.Map
44
<AzureMap Id="map"
5-
CameraOptions="new CameraOptions { Center= new AzureMapsControl.Components.Atlas.Position(11.581990, 48.143534), Zoom= 8.5, MinZoom = 3, MaxZoom = 12.5 }"
5+
CameraOptions="new CameraOptions { Bounds = new Components.Atlas.BoundingBox(-9, 38, 38, -9), Zoom= 8.5, MinZoom = 3, MaxZoom = 12.5 }"
66
StyleOptions="StyleOptions"
77
EventActivationFlags="MapEventActivationFlags
88
.None()

src/AzureMapsControl.Components/Atlas/BoundingBox.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
namespace AzureMapsControl.Components.Atlas
22
{
3+
using System;
34
using System.Diagnostics.CodeAnalysis;
5+
using System.Text.Json;
6+
using System.Text.Json.Serialization;
47

58
[ExcludeFromCodeCoverage]
9+
[JsonConverter(typeof(BoundingBoxJsonConverter))]
610
public sealed class BoundingBox
711
{
812
/// <summary>
@@ -40,4 +44,44 @@ public BoundingBox(double west, double south, double east, double north)
4044
North = north;
4145
}
4246
}
47+
48+
internal sealed class BoundingBoxJsonConverter : JsonConverter<BoundingBox>
49+
{
50+
public override BoundingBox Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
51+
{
52+
if (reader.TokenType == JsonTokenType.None)
53+
{
54+
reader.Read();
55+
}
56+
57+
if (reader.TokenType == JsonTokenType.StartArray)
58+
{
59+
reader.Read();
60+
var west = reader.GetDouble();
61+
reader.Read();
62+
var south = reader.GetDouble();
63+
reader.Read();
64+
var east = reader.GetDouble();
65+
reader.Read();
66+
var north = reader.GetDouble();
67+
reader.Read();
68+
return new BoundingBox(west, south, east, north);
69+
}
70+
71+
return null;
72+
}
73+
74+
public override void Write(Utf8JsonWriter writer, BoundingBox value, JsonSerializerOptions options)
75+
{
76+
if (value is not null)
77+
{
78+
writer.WriteStartArray();
79+
writer.WriteNumberValue(value.West);
80+
writer.WriteNumberValue(value.South);
81+
writer.WriteNumberValue(value.East);
82+
writer.WriteNumberValue(value.North);
83+
writer.WriteEndArray();
84+
}
85+
}
86+
}
4387
}

src/AzureMapsControl.Components/typescript/controls/geolocation-control.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ export class GeolocationControl {
3636
code: args.code,
3737
message: args.message,
3838
feature: {
39-
bbox: args.bbox ? {
40-
west: args.bbox[0],
41-
south: args.bbox[1],
42-
east: args.bbox[2],
43-
north: args.bbox[3]
44-
} : null,
39+
bbox: args.bbox,
4540
geometry: args.geometry,
4641
properties: Core.formatProperties(args.properties)
4742
},

src/AzureMapsControl.Components/typescript/core/core.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,7 @@ export class Core {
582582

583583
private static _getSerializableFeature(feature: azmaps.data.Feature<azmaps.data.Geometry, any>): Feature {
584584
return {
585-
bbox: feature.bbox ? {
586-
west: feature.bbox[0],
587-
south: feature.bbox[1],
588-
east: feature.bbox[2],
589-
north: feature.bbox[3]
590-
} : null,
585+
bbox: feature.bbox,
591586
geometry: feature.geometry,
592587
id: feature.id,
593588
properties: feature.properties

src/AzureMapsControl.Components/typescript/geometries/geometry-builder.ts

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ export class GeometryBuilder {
77
public static buildFeature(feature: Feature): azmaps.data.Feature<azmaps.data.Geometry, any> {
88
const geometry = this.buildGeometry(feature.geometry);
99
return new azmaps.data.Feature(geometry, Core.formatProperties(feature.properties), feature.id,
10-
feature.bbox ?
11-
new azmaps.data.BoundingBox(
12-
new azmaps.data.Position(feature.bbox.south, feature.bbox.west)
13-
, new azmaps.data.Position(feature.bbox.north, feature.bbox.east)
14-
) : null
10+
feature.bbox ? new azmaps.data.BoundingBox(feature.bbox) : null
1511
);
1612
}
1713

@@ -48,50 +44,35 @@ export class GeometryBuilder {
4844
public static buildLineString(geometry: Geometry): azmaps.data.LineString {
4945
return new azmaps.data.LineString(
5046
geometry.coordinates,
51-
geometry.bbox ? new azmaps.data.BoundingBox(
52-
new azmaps.data.Position(geometry.bbox.south, geometry.bbox.west)
53-
, new azmaps.data.Position(geometry.bbox.north, geometry.bbox.east)
54-
) : null
47+
geometry.bbox ? new azmaps.data.BoundingBox(geometry.bbox) : null
5548
);
5649
}
5750

5851
public static buildPolygon(geometry: Geometry): azmaps.data.Polygon {
5952
return new azmaps.data.Polygon(
6053
geometry.coordinates,
61-
geometry.bbox ? new azmaps.data.BoundingBox(
62-
new azmaps.data.Position(geometry.bbox.south, geometry.bbox.west)
63-
, new azmaps.data.Position(geometry.bbox.north, geometry.bbox.east)
64-
) : null
54+
geometry.bbox ? new azmaps.data.BoundingBox(geometry.bbox) : null
6555
);
6656
}
6757

6858
public static buildMultiPoint(geometry: Geometry): azmaps.data.MultiPoint {
6959
return new azmaps.data.MultiPoint(
7060
geometry.coordinates,
71-
geometry.bbox ? new azmaps.data.BoundingBox(
72-
new azmaps.data.Position(geometry.bbox.south, geometry.bbox.west)
73-
, new azmaps.data.Position(geometry.bbox.north, geometry.bbox.east)
74-
) : null
61+
geometry.bbox ? new azmaps.data.BoundingBox(geometry.bbox) : null
7562
);
7663
}
7764

7865
public static buildMultiLineString(geometry: Geometry): azmaps.data.MultiLineString {
7966
return new azmaps.data.MultiLineString(
8067
geometry.coordinates,
81-
geometry.bbox ? new azmaps.data.BoundingBox(
82-
new azmaps.data.Position(geometry.bbox.south, geometry.bbox.west)
83-
, new azmaps.data.Position(geometry.bbox.north, geometry.bbox.east)
84-
) : null
68+
geometry.bbox ? new azmaps.data.BoundingBox(geometry.bbox) : null
8569
);
8670
}
8771

8872
public static buildMultiPolygon(geometry: Geometry): azmaps.data.MultiPolygon {
8973
return new azmaps.data.MultiPolygon(
9074
geometry.coordinates,
91-
geometry.bbox ? new azmaps.data.BoundingBox(
92-
new azmaps.data.Position(geometry.bbox.south, geometry.bbox.west)
93-
, new azmaps.data.Position(geometry.bbox.north, geometry.bbox.east)
94-
) : null
75+
geometry.bbox ? new azmaps.data.BoundingBox(geometry.bbox) : null
9576
);
9677
}
9778
}

src/AzureMapsControl.Components/typescript/geometries/geometry.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
export interface Geometry {
22
coordinates: any;
3-
bbox: {
4-
east: number;
5-
north: number;
6-
south: number;
7-
west: number;
8-
},
3+
bbox: number[],
94
type: 'LineString' | 'MultiLineString' | 'MultiPoint' | 'MultiPolygon' | 'Point' | 'Polygon';
105
}
116

127
export interface Feature {
138
id?: string;
14-
bbox: {
15-
east: number;
16-
north: number;
17-
south: number;
18-
west: number;
19-
},
9+
bbox: number[],
2010
geometry: Geometry,
2111
properties: { [key: string]: any }
2212
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace AzureMapsControl.Components.Tests.Atlas
2+
{
3+
4+
using AzureMapsControl.Components.Atlas;
5+
using AzureMapsControl.Components.Tests.Json;
6+
7+
using Xunit;
8+
9+
public class BoundingBoxJsonConverterTests : JsonConverterTests<BoundingBox>
10+
{
11+
public BoundingBoxJsonConverterTests() : base(new BoundingBoxJsonConverter())
12+
{
13+
}
14+
15+
[Fact]
16+
public void Should_Write()
17+
{
18+
var boundingBox = new BoundingBox(1, 2, 3, 4);
19+
var expectedJson = "[1,2,3,4]";
20+
21+
TestAndAssertWrite(boundingBox, expectedJson);
22+
}
23+
24+
[Fact]
25+
public void Should_Read()
26+
{
27+
var json = "[1,2,3,4]";
28+
29+
var boundingBox = Read(json);
30+
Assert.Equal(1, boundingBox.West);
31+
Assert.Equal(2, boundingBox.South);
32+
Assert.Equal(3, boundingBox.East);
33+
Assert.Equal(4, boundingBox.North);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)