Skip to content

Commit

Permalink
Fixed Polygon and Multi Polygon (ChilliCream#4394)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Nov 11, 2021
1 parent e27df75 commit ba51ef9
Show file tree
Hide file tree
Showing 57 changed files with 1,667 additions and 1,011 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ input PublisherInput {
directive @defer("If this argument label has a value other than null, it will be passed on to the result of this defer directive. This label is intended to give client applications a way to identify to which fragment a deferred result belongs to." label: String "Deferred when true." if: Boolean) on FRAGMENT_SPREAD | INLINE_FRAGMENT

"The `@stream` directive may be provided for a field of `List` type so that the backend can leverage technology such as asynchronous iterators to provide a partial list in the initial response, and additional list items in subsequent responses. `@include` and `@skip` take precedence over `@stream`."
directive @stream("If this argument label has a value other than null, it will be passed on to the result of this stream directive. This label is intended to give client applications a way to identify to which fragment a streamed result belongs to." label: String "The initial elements that shall be send down to the consumer." initialCount: Int! "Streamed when true." if: Boolean!) on FIELD
directive @stream("If this argument label has a value other than null, it will be passed on to the result of this stream directive. This label is intended to give client applications a way to identify to which fragment a streamed result belongs to." label: String "The initial elements that shall be send down to the consumer." initialCount: Int! = 0 "Streamed when true." if: Boolean) on FIELD
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ input PublisherInput {
directive @defer("If this argument label has a value other than null, it will be passed on to the result of this defer directive. This label is intended to give client applications a way to identify to which fragment a deferred result belongs to." label: String "Deferred when true." if: Boolean) on FRAGMENT_SPREAD | INLINE_FRAGMENT

"The `@stream` directive may be provided for a field of `List` type so that the backend can leverage technology such as asynchronous iterators to provide a partial list in the initial response, and additional list items in subsequent responses. `@include` and `@skip` take precedence over `@stream`."
directive @stream("If this argument label has a value other than null, it will be passed on to the result of this stream directive. This label is intended to give client applications a way to identify to which fragment a streamed result belongs to." label: String "The initial elements that shall be send down to the consumer." initialCount: Int! "Streamed when true." if: Boolean!) on FIELD
directive @stream("If this argument label has a value other than null, it will be passed on to the result of this stream directive. This label is intended to give client applications a way to identify to which fragment a streamed result belongs to." label: String "The initial elements that shall be send down to the consumer." initialCount: Int! = 0 "Streamed when true." if: Boolean) on FIELD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections;
using NetTopologySuite.Geometries;

namespace HotChocolate.Types.Spatial.Serialization
{
internal static class GeoJsonConverterExtensions
{
public static bool TryConvertToCoordinates(
this IList coordinatesList,
out Coordinate[] coordinates)
{
if (coordinatesList.Count == 0)
{
coordinates = Array.Empty<Coordinate>();
return true;
}

coordinates = new Coordinate[coordinatesList.Count];
for (var i = 0; i < coordinates.Length; i++)
{
if (coordinatesList[i] is Coordinate c)
{
coordinates[i] = c;
}
else
{
return false;
}
}

return true;
}
}
}
37 changes: 37 additions & 0 deletions src/HotChocolate/Spatial/src/Types/GeoJsonCoordinatesType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using HotChocolate.Language;
using HotChocolate.Types.Spatial.Properties;

namespace HotChocolate.Types.Spatial
{
/// <summary>
/// <para>
/// The Coordinates scalar type represents a list of arbitrary depth of positions
/// </para>
/// <para>https://tools.ietf.org/html/rfc7946#section-3.1.1</para>
/// </summary>
public sealed class GeoJsonCoordinatesType : ScalarType<object[]>
{
public GeoJsonCoordinatesType() : base(WellKnownTypeNames.CoordinatesTypeName)
{
Description = Resources.GeoJsonCoordinatesScalar_Description;
}

public override bool IsInstanceOfType(IValueNode valueSyntax)
=> GeoJsonCoordinatesSerializer.Default.IsInstanceOfType(this, valueSyntax);

public override object? ParseLiteral(IValueNode valueSyntax)
=> GeoJsonCoordinatesSerializer.Default.ParseLiteral(this, valueSyntax);

public override IValueNode ParseValue(object? value)
=> GeoJsonCoordinatesSerializer.Default.ParseValue(this, value);

public override IValueNode ParseResult(object? resultValue)
=> GeoJsonCoordinatesSerializer.Default.ParseResult(this, resultValue);

public override bool TryDeserialize(object? serialized, out object? value)
=> GeoJsonCoordinatesSerializer.Default.TryDeserialize(this, serialized, out value);

public override bool TrySerialize(object? value, out object? serialized)
=> GeoJsonCoordinatesSerializer.Default.TrySerialize(this, value, out serialized);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override void Configure(IInputObjectTypeDescriptor<MultiPolygon> descr

descriptor
.Field(CoordinatesFieldName)
.Type<ListType<ListType<GeoJsonPositionType>>>()
.Type<GeoJsonCoordinatesType>()
.Description(GeoJson_Field_Coordinates_Description_MultiPolygon);

descriptor
Expand Down
7 changes: 5 additions & 2 deletions src/HotChocolate/Spatial/src/Types/GeoJsonMultiPolygonType.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NetTopologySuite.Geometries;
using static HotChocolate.Types.Spatial.Properties.Resources;
using static HotChocolate.Types.Spatial.WellKnownFields;
using static HotChocolate.Types.Spatial.WellKnownTypeNames;

namespace HotChocolate.Types.Spatial
Expand All @@ -16,8 +17,10 @@ protected override void Configure(IObjectTypeDescriptor<MultiPolygon> descriptor
.BindFieldsExplicitly();

descriptor
.Field(x => x.Coordinates)
.Description(GeoJson_Field_Coordinates_Description_MultiPolygon);
.Field<GeoJsonResolvers>(x => x.GetGeometryCollectionCoordinates(default!))
.Name(CoordinatesFieldName)
.Description(GeoJson_Field_Coordinates_Description_MultiPolygon)
.Type<GeoJsonCoordinatesType>();

descriptor
.Field<GeoJsonResolvers>(x => x.GetType(default!))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override void Configure(IInputObjectTypeDescriptor<Polygon> descriptor

descriptor
.Field(CoordinatesFieldName)
.Type<ListType<GeoJsonPositionType>>()
.Type<ListType<ListType<GeoJsonPositionType>>>()
.Description(GeoJson_Field_Coordinates_Description_Polygon);

descriptor
Expand Down
22 changes: 20 additions & 2 deletions src/HotChocolate/Spatial/src/Types/GeoJsonPolygonType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ protected override void Configure(IObjectTypeDescriptor<Polygon> descriptor)
.BindFieldsExplicitly();

descriptor
.Field(x => x.Coordinates)
.Description(GeoJson_Field_Coordinates_Description_Polygon);
.Field<Resolvers>(x => x.GetCoordinates(default!))
.Name(WellKnownFields.CoordinatesFieldName)
.Description(GeoJson_Field_Coordinates_Description_Polygon)
.Type<ListType<ListType<GeoJsonPositionType>>>();

descriptor
.Field<GeoJsonResolvers>(x => x.GetType(default!))
Expand All @@ -31,5 +33,21 @@ protected override void Configure(IObjectTypeDescriptor<Polygon> descriptor)
.Field<GeoJsonResolvers>(x => x.GetCrs(default!))
.Description(GeoJson_Field_Crs_Description);
}

public class Resolvers
{
public Coordinate[][] GetCoordinates([Parent] Polygon polygon)
{
Coordinate[][] coordinates = new Coordinate[polygon.NumInteriorRings + 1][];
coordinates[0] = polygon.ExteriorRing.Coordinates;

for (var i = 0; i < polygon.InteriorRings.Length; i++)
{
coordinates[i] = polygon.InteriorRings[i].Coordinates;
}

return coordinates;
}
}
}
}
12 changes: 12 additions & 0 deletions src/HotChocolate/Spatial/src/Types/GeoJsonResolvers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ namespace HotChocolate.Types.Spatial
{
internal class GeoJsonResolvers
{
public Coordinate[][] GetGeometryCollectionCoordinates(
[Parent] GeometryCollection collection)
{
Coordinate[][] coordinates = new Coordinate[collection.Count][];
for (var i = 0; i < collection.Count; i++)
{
coordinates[i] = collection[i].Coordinates;
}

return coordinates;
}

public GeoJsonGeometryType GetType([Parent] Geometry geometry) =>
geometry.OgcGeometryType switch
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/HotChocolate/Spatial/src/Types/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@
<data name="GeoJsonPositionScalar_Description" xml:space="preserve">
<value>A position is an array of numbers. There MUST be two or more elements. The first two elements are longitude and latitude, or easting and northing, precisely in that order and using decimal numbers. Altitude or elevation MAY be included as an optional third element.</value>
</data>
<data name="GeoJsonCoordinatesScalar_Description" xml:space="preserve">
<value>A coordinate is an array of positions.</value>
</data>
<data name="InvalidInputObjectStructure_IsOfWrongGeometryType" xml:space="preserve">
<value>`type` is of type {0} but should be {1} </value>
</data>
Expand Down Expand Up @@ -237,4 +240,13 @@
<data name="GeoJsonGeometrySerializer_CreateInstance_NotSupported" xml:space="preserve">
<value>CreateInstance is only supported on concreted serializers.</value>
</data>
<data name="CoordinatesScalar_InvalidCoordinatesObject" xml:space="preserve">
<value>A valid coordinates object must be a list of depth N of two or three int or float literals representing a position. e.g. [1,1] or [2,2,0]</value>
</data>
<data name="CoordinatesScalar_CoordinatesCannotBeNull" xml:space="preserve">
<value>The specified value has to be a list of depth N of coordinates</value>
</data>
<data name="Serializer_OperationIsNotSupported" xml:space="preserve">
<value>Serializer {0} does not support operation {1}!</value>
</data>
</root>
11 changes: 11 additions & 0 deletions src/HotChocolate/Spatial/src/Types/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
HotChocolate.Types.Spatial.GeoJsonCoordinatesType
HotChocolate.Types.Spatial.GeoJsonCoordinatesType.GeoJsonCoordinatesType() -> void
HotChocolate.Types.Spatial.GeoJsonPolygonType.Resolvers
HotChocolate.Types.Spatial.GeoJsonPolygonType.Resolvers.GetCoordinates(NetTopologySuite.Geometries.Polygon! polygon) -> NetTopologySuite.Geometries.Coordinate![]![]!
HotChocolate.Types.Spatial.GeoJsonPolygonType.Resolvers.Resolvers() -> void
override HotChocolate.Types.Spatial.GeoJsonCoordinatesType.IsInstanceOfType(HotChocolate.Language.IValueNode! valueSyntax) -> bool
override HotChocolate.Types.Spatial.GeoJsonCoordinatesType.ParseLiteral(HotChocolate.Language.IValueNode! valueSyntax) -> object?
override HotChocolate.Types.Spatial.GeoJsonCoordinatesType.ParseResult(object? resultValue) -> HotChocolate.Language.IValueNode!
override HotChocolate.Types.Spatial.GeoJsonCoordinatesType.ParseValue(object? value) -> HotChocolate.Language.IValueNode!
override HotChocolate.Types.Spatial.GeoJsonCoordinatesType.TryDeserialize(object? serialized, out object? value) -> bool
override HotChocolate.Types.Spatial.GeoJsonCoordinatesType.TrySerialize(object? value, out object? serialized) -> bool
Loading

0 comments on commit ba51ef9

Please sign in to comment.