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 @@ -17,7 +17,7 @@ protected override void Configure(IObjectTypeDescriptor<MultiPolygon> descriptor
.BindFieldsExplicitly();

descriptor
.Field<GeoJsonResolvers>(x => x.GetGeometryCollectionCoordinates(null!))
.Field<GeoJsonResolvers>(x => x.GetMultiPolygonCoordinates(null!))
.Name(CoordinatesFieldName)
.Description(GeoJson_Field_Coordinates_Description_MultiPolygon)
.Type<GeoJsonCoordinatesType>();
Expand Down
25 changes: 25 additions & 0 deletions src/HotChocolate/Spatial/src/Types/GeoJsonResolvers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ public Coordinate[][] GetGeometryCollectionCoordinates(
return coordinates;
}

public Coordinate[][][] GetMultiPolygonCoordinates([Parent] MultiPolygon multiPolygon)
{
var coordinates = new Coordinate[multiPolygon.NumGeometries][][];

for (var i = 0; i < multiPolygon.NumGeometries; i++)
{
coordinates[i] = GetPolygonRings((Polygon)multiPolygon.GetGeometryN(i));
}

return coordinates;
}

public GeoJsonGeometryType GetType([Parent] Geometry geometry) =>
geometry.OgcGeometryType switch
{
Expand All @@ -39,4 +51,17 @@ public IReadOnlyCollection<double> GetBbox([Parent] Geometry geometry)

public int GetCrs([Parent] Geometry geometry) =>
geometry.SRID is 0 or -1 ? 4326 : geometry.SRID;

private static Coordinate[][] GetPolygonRings(Polygon polygon)
{
var rings = new Coordinate[polygon.NumInteriorRings + 1][];
rings[0] = polygon.ExteriorRing.Coordinates;

for (var i = 0; i < polygon.NumInteriorRings; i++)
{
rings[i + 1] = polygon.GetInteriorRingN(i).Coordinates;
}

return rings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,64 @@ public class GeoJsonMultiPolygonTypeTests
]))
]);

[Fact]
public async Task GetMultiPolygonCoordinates_Should_ReturnRingsPerPolygon_When_PolygonHasHoles()
{
// arrange
var multiPolygon = new MultiPolygon(
[
new Polygon(
new LinearRing(
[
new Coordinate(0, 0),
new Coordinate(40, 0),
new Coordinate(40, 40),
new Coordinate(0, 40),
new Coordinate(0, 0)
]),
[
new LinearRing(
[
new Coordinate(10, 10),
new Coordinate(20, 10),
new Coordinate(20, 20),
new Coordinate(10, 20),
new Coordinate(10, 10)
])
]),
new Polygon(
new LinearRing(
[
new Coordinate(50, 50),
new Coordinate(60, 50),
new Coordinate(60, 60),
new Coordinate(50, 60),
new Coordinate(50, 50)
]))
]);

var schema = SchemaBuilder.New()
.AddConvention<INamingConventions, MockNamingConvention>()
.BindRuntimeType<Coordinate, GeoJsonPositionType>()
.AddType<GeoJsonMultiPolygonType>()
.AddQueryType(
d => d
.Name("Query")
.Field("test")
.Resolve(multiPolygon))
.Create();

var executor = schema.MakeExecutable();

// act
var result = await executor.ExecuteAsync(
"{ test { type coordinates bbox crs }}",
TestContext.Current.CancellationToken);

// assert
result.MatchSnapshot();
}

[Fact]
public async Task MultiPolygon_Execution_Output()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"data": {
"test": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
0,
0
],
[
40,
0
],
[
40,
40
],
[
0,
40
],
[
0,
0
]
],
[
[
10,
10
],
[
20,
10
],
[
20,
20
],
[
10,
20
],
[
10,
10
]
]
],
[
[
[
50,
50
],
[
60,
50
],
[
60,
60
],
[
50,
60
],
[
50,
50
]
]
]
],
"bbox": [
0,
0,
60,
60
],
"crs": 4326
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,46 @@
"coordinates": [
[
[
30,
20
],
[
45,
40
],
[
10,
40
],
[
30,
20
[
30,
20
],
[
45,
40
],
[
10,
40
],
[
30,
20
]
]
],
[
[
15,
5
],
[
40,
10
],
[
10,
20
],
[
5,
15
],
[
15,
5
[
15,
5
],
[
40,
10
],
[
10,
20
],
[
5,
15
],
[
15,
5
]
]
]
],
Expand Down
Loading