Ejemplo n.º 1
0
        public static PolygonGeoShape GetPolygon(Geometry geom, ICoordinateTransformation transformer = null)
        {
            try
            {
                var listofpoints = new List <List <GeoCoordinate> >();

                // only take the first (outer) polygon
                var subgeom   = geom.GetGeometryN(0);
                var numpoints = subgeom.NumPoints;
                var points    = new List <GeoCoordinate>();

                for (var i = 0; i < numpoints; i++)
                {
                    var coord = subgeom.Coordinates[i];
                    if (transformer != null)
                    {
                        coord = coord.Transform(transformer);
                    }
                    points.Add(new GeoCoordinate(coord.Y, coord.X));
                }

                var close = points[0].Longitude != points[numpoints - 1].Longitude ||
                            points[0].Latitude != points[numpoints - 1].Latitude
                            ? 1 : 0;

                // add 1st point as last to complete the polygon
                if (close == 1)
                {
                    points.Add(new GeoCoordinate(points[0].Latitude, points[0].Longitude));
                }

                listofpoints.Add(points);

                PolygonGeoShape result = new PolygonGeoShape(listofpoints);
                return(result);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Error", ex);
            }
        }
		public void GeoShapePolygon()
		{
			var polygon = new PolygonGeoShape
				{
					Coordinates = new[] { 
							new[] { new[] { 100.0, 0.0 }, new[] { 101.0, 0.0 }, new[] { 101.0, 1.0 }, new[] { 100.0, 1.0 }, new [] { 100.0, 0.0 } },
							new[] { new[] { 100.2, 0.2}, new[] { 100.8, 0.2 }, new[] { 100.8, 0.8}, new[] { 100.2, 0.8 }, new [] { 100.2, 0.2} }
						}
				};

			var s = new SearchDescriptor<ElasticsearchProject>()
				.From(0)
				.Size(10)
				.Query(q => q
					.GeoShapePolygon(qs => qs
						.OnField(p => p.MyGeoShape)
						.Coordinates(polygon.Coordinates)
					)
				);

			this.JsonEquals(s, MethodInfo.GetCurrentMethod());
		}
Ejemplo n.º 3
0
        public void GeoShapePolygon()
        {
            var polygon = new PolygonGeoShape
            {
                Coordinates = new[] {
                    new[] { new[] { 100.0, 0.0 }, new[] { 101.0, 0.0 }, new[] { 101.0, 1.0 }, new[] { 100.0, 1.0 }, new [] { 100.0, 0.0 } },
                    new[] { new[] { 100.2, 0.2 }, new[] { 100.8, 0.2 }, new[] { 100.8, 0.8 }, new[] { 100.2, 0.8 }, new [] { 100.2, 0.2 } }
                }
            };

            var s = new SearchDescriptor <ElasticsearchProject>()
                    .From(0)
                    .Size(10)
                    .Query(q => q
                           .GeoShapePolygon(qs => qs
                                            .OnField(p => p.MyGeoShape)
                                            .Coordinates(polygon.Coordinates)
                                            )
                           );

            this.JsonEquals(s, MethodInfo.GetCurrentMethod());
        }
Ejemplo n.º 4
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))");
        }