Exemple #1
0
        protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
        {
            if (client.Indices.Exists(Index).Exists)
            {
                return;
            }

            var createIndexResponse = client.Indices.Create(Index, c => c
                                                            .Settings(s => s
                                                                      .NumberOfShards(1)
                                                                      .NumberOfReplicas(0)
                                                                      )
                                                            .Map <Domain.GeoShape>(mm => mm
                                                                                   .AutoMap()
                                                                                   .Properties(p => p
                                                                                               .GeoShape(g => g
                                                                                                         .Name(n => n.GeometryCollection)
                                                                                                         )
                                                                                               .GeoShape(g => g
                                                                                                         .Name(n => n.Envelope)
                                                                                                         )
                                                                                               .GeoShape(g => g
                                                                                                         .Name(n => n.Circle)
                                                                                                         .Strategy(GeoStrategy.Recursive)
                                                                                                         )
                                                                                               )
                                                                                   )
                                                            );

            if (!createIndexResponse.IsValid)
            {
                throw new Exception($"Error creating index for integration test: {createIndexResponse.DebugInformation}");
            }

            var bulk = new List <object>();

            foreach (var shape in Domain.GeoShape.Shapes)
            {
                bulk.Add(new { index = new { _index = Index, _id = shape.Id } });
                bulk.Add(new
                {
                    id = shape.Id,
                    geometryCollection = GeoWKTWriter.Write(shape.GeometryCollection),
                    envelope           = GeoWKTWriter.Write(shape.Envelope),
                    circle             = shape.Circle
                });
            }

            var bulkResponse = Client.LowLevel.Bulk <BulkResponse>(
                PostData.MultiJson(bulk),
                new BulkRequestParameters {
                Refresh = Refresh.WaitFor
            }
                );

            if (!bulkResponse.IsValid)
            {
                throw new Exception($"Error indexing shapes for integration test: {bulkResponse.DebugInformation}");
            }
        }
        public void WriteWKTCoordinatesWithInvariantCulture()
        {
            var wkt = "POINT (45.1 42.25)";

            var shape  = GeoWKTReader.Read(wkt);
            var actual = GeoWKTWriter.Write(shape);

            actual.Should().Be(wkt);
        }
Exemple #3
0
        public void WriteEnvelopeIgnoresZValues()
        {
            var envelope = new EnvelopeGeoShape(new[]
            {
                new GeoCoordinate(40.73, -74.1, 3),
                new GeoCoordinate(40.01, -71.12, 2)
            });

            GeoWKTWriter.Write(envelope).Should().Be("BBOX (-74.1, -71.12, 40.73, 40.01)");
        }
Exemple #4
0
        public void ReadAndWriteGeometryCollection()
        {
            var wkt   = "GEOMETRYCOLLECTION (POINT (100 0), LINESTRING (101 0, 102 1))";
            var shape = GeoWKTReader.Read(wkt);
            var geometryCollection = shape as IGeometryCollection;

            geometryCollection.Should().NotBeNull();
            geometryCollection.Geometries.Should().HaveCount(2);
            geometryCollection.Geometries.First().Should().BeOfType <PointGeoShape>();
            geometryCollection.Geometries.Last().Should().BeOfType <LineStringGeoShape>();

            GeoWKTWriter.Write(geometryCollection).Should().Be(wkt);
        }
Exemple #5
0
        public void ReadAndWritePoint()
        {
            var wkt   = "POINT (-77.03653 38.897676)";
            var shape = GeoWKTReader.Read(wkt);

            shape.Should().BeOfType <PointGeoShape>();
            var point = (PointGeoShape)shape;

            point.Coordinates.Latitude.Should().Be(38.897676);
            point.Coordinates.Longitude.Should().Be(-77.03653);

            GeoWKTWriter.Write(point).Should().Be(wkt);
        }
Exemple #6
0
        public void ReadAndWriteEnvelope()
        {
            var wkt      = "BBOX (-74.1, -71.12, 40.73, 40.01)";
            var shape    = GeoWKTReader.Read(wkt);
            var envelope = shape as EnvelopeGeoShape;

            envelope.Should().NotBeNull();
            envelope.Coordinates.First().Latitude.Should().Be(40.73);
            envelope.Coordinates.First().Longitude.Should().Be(-74.1);
            envelope.Coordinates.Last().Latitude.Should().Be(40.01);
            envelope.Coordinates.Last().Longitude.Should().Be(-71.12);

            GeoWKTWriter.Write(shape).Should().Be(wkt);
        }
        public void ReadAndWritePointWithExponent()
        {
            var wkt   = "POINT (1.2E2 -2.5E-05)";
            var shape = GeoWKTReader.Read(wkt);

            shape.Should().BeOfType <PointGeoShape>();
            var point = (PointGeoShape)shape;

            point.Coordinates.Latitude.Should().Be(-0.000025);
            point.Coordinates.Longitude.Should().Be(120);

            // 1.2E2 will be expanded
            GeoWKTWriter.Write(point).Should().Be("POINT (120 -2.5E-05)");
        }
Exemple #8
0
        public void ReadAndWriteLineString()
        {
            var wkt = "LINESTRING (-77.03653 38.897676, -77.009051 38.889939)";

            var shape = GeoWKTReader.Read(wkt);

            var lineString = shape as LineStringGeoShape;

            lineString.Should().NotBeNull();
            lineString.Coordinates.First().Latitude.Should().Be(38.897676);
            lineString.Coordinates.First().Longitude.Should().Be(-77.03653);

            lineString.Coordinates.Last().Latitude.Should().Be(38.889939);
            lineString.Coordinates.Last().Longitude.Should().Be(-77.009051);

            GeoWKTWriter.Write(lineString).Should().Be(wkt);
        }
Exemple #9
0
        public void WriteMultiPolygon()
        {
            var multiPolygon = new MultiPolygonGeoShape(new[]
            {
                new[]
                {
                    new[]
                    {
                        new GeoCoordinate(2, 102),
                        new GeoCoordinate(2, 103),
                        new GeoCoordinate(3, 103),
                        new GeoCoordinate(3, 102),
                        new GeoCoordinate(2, 102),
                    }
                },
                new[]
                {
                    new[]
                    {
                        new GeoCoordinate(0, 100),
                        new GeoCoordinate(0, 101),
                        new GeoCoordinate(1, 101),
                        new GeoCoordinate(1, 100),
                        new GeoCoordinate(0, 100),
                    },
                    new[]
                    {
                        new GeoCoordinate(0.2, 100.2),
                        new GeoCoordinate(0.2, 100.8),
                        new GeoCoordinate(0.8, 100.8),
                        new GeoCoordinate(0.8, 100.2),
                        new GeoCoordinate(0.2, 100.2),
                    }
                }
            });

            GeoWKTWriter.Write(multiPolygon)
            .Should()
            .Be(
                "MULTIPOLYGON (((102 2, 103 2, 103 3, 102 3, 102 2)), ((100 0, 101 0, 101 1, 100 1, 100 0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2)))");
        }
Exemple #10
0
        public void ReadAndWriteMultiPoint()
        {
            var wkt   = "MULTIPOINT (102.0 2.0, 103.0 2.0)";
            var shape = GeoWKTReader.Read(wkt);

            var multiPoint = shape as MultiPointGeoShape;

            multiPoint.Should().NotBeNull();

            var firstPoint = multiPoint.Coordinates.First();

            firstPoint.Latitude.Should().Be(2);
            firstPoint.Longitude.Should().Be(102);

            var lastPoint = multiPoint.Coordinates.Last();

            lastPoint.Latitude.Should().Be(2);
            lastPoint.Longitude.Should().Be(103);

            GeoWKTWriter.Write(multiPoint).Should().Be("MULTIPOINT (102 2, 103 2)");
        }
Exemple #11
0
        public void WriteMultiLineString()
        {
            var multLineString = new MultiLineStringGeoShape(new[]
            {
                new[]
                {
                    new GeoCoordinate(2, 102),
                    new GeoCoordinate(2, 103),
                    new GeoCoordinate(3, 103),
                    new GeoCoordinate(3, 102),
                },
                new[]
                {
                    new GeoCoordinate(0, 100),
                    new GeoCoordinate(0, 101),
                    new GeoCoordinate(1, 101),
                    new GeoCoordinate(1, 100),
                }
            });

            var wkt = GeoWKTWriter.Write(multLineString);

            wkt.Should().Be("MULTILINESTRING ((102 2, 103 2, 103 3, 102 3), (100 0, 101 0, 101 1, 100 1))");
        }
Exemple #12
0
        public void WritePolygon()
        {
            var polygon = new PolygonGeoShape(new[]
            {
                new[]
                {
                    new GeoCoordinate(2, 102),
                    new GeoCoordinate(2, 103),
                    new GeoCoordinate(3, 103),
                    new GeoCoordinate(3, 102),
                },
                new[]
                {
                    new GeoCoordinate(0, 100),
                    new GeoCoordinate(0, 101),
                    new GeoCoordinate(1, 101),
                    new GeoCoordinate(1, 100),
                }
            });

            var wkt = GeoWKTWriter.Write(polygon);

            wkt.Should().Be("POLYGON ((102 2, 103 2, 103 3, 102 3), (100 0, 101 0, 101 1, 100 1))");
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            if (value is IGeoShape shape)
            {
                if (value is GeoShapeBase shapeBase && shapeBase.Format == GeoShapeFormat.WellKnownText)
                {
                    writer.WriteValue(GeoWKTWriter.Write(shapeBase));
                    return;
                }

                writer.WriteStartObject();
                writer.WritePropertyName("type");
                writer.WriteValue(shape.Type);

                switch (shape)
                {
                case IPointGeoShape point:
                    writer.WritePropertyName("coordinates");
                    serializer.Serialize(writer, point.Coordinates);
                    break;

                case IMultiPointGeoShape multiPoint:
                    writer.WritePropertyName("coordinates");
                    serializer.Serialize(writer, multiPoint.Coordinates);
                    break;

                case ILineStringGeoShape lineString:
                    writer.WritePropertyName("coordinates");
                    serializer.Serialize(writer, lineString.Coordinates);
                    break;

                case IMultiLineStringGeoShape multiLineString:
                    writer.WritePropertyName("coordinates");
                    serializer.Serialize(writer, multiLineString.Coordinates);
                    break;

                case IPolygonGeoShape polygon:
                    writer.WritePropertyName("coordinates");
                    serializer.Serialize(writer, polygon.Coordinates);
                    break;

                case IMultiPolygonGeoShape multiPolygon:
                    writer.WritePropertyName("coordinates");
                    serializer.Serialize(writer, multiPolygon.Coordinates);
                    break;

                case IEnvelopeGeoShape envelope:
                    writer.WritePropertyName("coordinates");
                    serializer.Serialize(writer, envelope.Coordinates);
                    break;

                case ICircleGeoShape circle:
                    writer.WritePropertyName("coordinates");
                    serializer.Serialize(writer, circle.Coordinates);
                    writer.WritePropertyName("radius");
                    writer.WriteValue(circle.Radius);
                    break;

                case IGeometryCollection collection:
                    writer.WritePropertyName("geometries");
                    serializer.Serialize(writer, collection.Geometries);
                    break;
                }

                writer.WriteEndObject();
            }
            else
            {
                throw new NotSupportedException($"{value.GetType()} is not a supported {nameof(IGeoShape)}");
            }
        }