public void CreateGeographyPolygon()
        {
            GeographyPolygon polygon = GeographyFactory
                                       .Polygon()
                                       .Ring(0.0, 0.0)
                                       .LineTo(1.0, 0.0)
                                       .LineTo(1.0, 1.0)
                                       .LineTo(0.0, 1.0)
                                       .LineTo(0.0, 0.0);
            GeographyPolygonProxy proxy = new GeographyPolygonProxy(polygon);

            Assert.AreSame(polygon, proxy.Value);
            Assert.AreEqual(1, proxy.Rings.Count);

            GeographyLineString      line0      = polygon.Rings[0];
            GeographyLineStringProxy proxyLine0 = proxy.Rings[0];

            Assert.AreSame(line0, proxyLine0.Value);
            Assert.AreEqual(5, line0.Points.Count);
            Assert.AreEqual(line0.Points.Count, proxyLine0.Points.Count);

            for (int i = 0; i < line0.Points.Count; i++)
            {
                Assert.AreEqual(line0.Points[i].Latitude, proxyLine0.Points[i].Latitude);
                Assert.AreEqual(line0.Points[i].Longitude, proxyLine0.Points[i].Longitude);
            }
        }
        private static string CreatePolygon()
        {
            /*
             * GeographyPolygon polygonValue =
             *  GeographyFactory.Polygon()
             *      .Ring(33.1, -110.0)
             *      .LineTo(35.97, -110.15)
             *      .LineTo(11.45, 87.75)
             *      .Ring(35.97, -110)
             *      .LineTo(36.97, -110.15)
             *      .LineTo(45.23, 23.18)
             *      .Build();*/

            GeographyPolygon polygonValue =
                GeographyFactory.Polygon()
                .Ring(0, 0)
                .LineTo(5, 0)
                .LineTo(5, 5)
                .LineTo(0, 5)
                .Build();

            string polygonString = ODataUriUtils.ConvertToUriLiteral(polygonValue, ODataVersion.V4);

            Console.WriteLine(polygonString);

            return(polygonString);
        }
        private static void WritePolygonCoordinates(JsonWriter writer, GeographyPolygon polygon)
        {
            writer.WriteStartArray();

            foreach (var ring in polygon.Rings)
            {
                WriteLineStringCoordinates(writer, ring);
            }

            writer.WriteEndArray();
        }
 public static bool GetIsIntersects(GeographyPoint point, GeographyPolygon polygon)
 {
     if (point == null)
     {
         throw new ArgumentNullException("point");
     }
     if (polygon == null)
     {
         throw new ArgumentNullException("polygon");
     }
     return(point.Intersects(polygon).Value);
 }
Exemple #5
0
        private IEnumerable <Customer> BindGeoIntersections(SingleValueFunctionCallNode node, IEnumerable <Customer> customers)
        {
            Debug.Assert(node.Name == "geo.intersects");
            Debug.Assert(2 == node.Parameters.Count());

            SingleValuePropertyAccessNode first = node.Parameters.First() as SingleValuePropertyAccessNode;
            ConstantNode second = node.Parameters.Last() as ConstantNode;

            Debug.Assert(first != null && second != null);

            IEdmProperty     property = first.Property;
            GeographyPolygon polygon  = second.Value as GeographyPolygon;

            Debug.Assert(property != null && polygon != null);

            return(DoGeoIntersections(customers, property, polygon));
        }
        public void BuildPolygon()
        {
            GeographyPolygon poly = GeographyFactory.Polygon(NonDefaultGeographicCoords)
                                    .Ring(10, 20).LineTo(20, 30).LineTo(30, 40)
                                    .Ring(15, 25).LineTo(25, 35).LineTo(35, 45).LineTo(15, 25)
                                    .Ring(10, 20).LineTo(20, 30).LineTo(30, 40).LineTo(10, 20).LineTo(40, 50);

            Assert.AreEqual(NonDefaultGeographicCoords, poly.CoordinateSystem);

            poly.VerifyAsPolygon(new PositionData[] {
                new PositionData(10, 20),
                new PositionData(20, 30),
                new PositionData(30, 40),
                new PositionData(10, 20)
            },
                                 new PositionData[] {
                new PositionData(15, 25),
                new PositionData(25, 35),
                new PositionData(35, 45),
                new PositionData(15, 25)
            },
                                 new PositionData[] {
                new PositionData(10, 20),
                new PositionData(20, 30),
                new PositionData(30, 40),
                new PositionData(10, 20),
                new PositionData(40, 50),
                new PositionData(10, 20)
            });

            poly = GeographyFactory.Polygon()
                   .Ring(10, 20).LineTo(20, 30).LineTo(30, 40);

            Assert.AreEqual(CoordinateSystem.DefaultGeography, poly.CoordinateSystem);

            poly.VerifyAsPolygon(new PositionData[] {
                new PositionData(10, 20),
                new PositionData(20, 30),
                new PositionData(30, 40),
                new PositionData(10, 20)
            });

            poly = GeographyFactory.Polygon();
            poly.VerifyAsPolygon(null);
        }
Exemple #7
0
        private static void WriteGeography(
            this Utf8JsonWriter writer,
            GeographyPolygon geography)
        {
            writer.WriteStartObject();

            writer.WriteString(s_TypePropertyNameBytes, GeoJsonConstants.PolygonTypeName);

            writer.WriteStartArray(s_CoordinatesPropertyNameBytes);

            foreach (GeographyLineString ring in geography.Rings)
            {
                WritePointArray(writer, ring.Points);
            }

            writer.WriteEndArray();

            writer.WriteEndObject();
        }
        private static IEnumerable GetMicrosoftSpatialPolygonsData()
        {
            GeographyLineString line = GeographyFactory
                .LineString(0, 0)
                .LineTo(1, 0)
                .LineTo(1, 1)
                .LineTo(0, 0);

            yield return new TestCaseData(line).Returns("geo.intersects(Foo, geography'POLYGON((0 0,0 1,1 1,0 0))')");

            GeographyPolygon polygon = GeographyFactory
                .Polygon()
                .Ring(0, 0)
                .LineTo(1, 0)
                .LineTo(1, 1)
                .LineTo(0, 0);

            yield return new TestCaseData(polygon).Returns("geo.intersects(Foo, geography'POLYGON((0 0,0 1,1 1,0 0))')");
        }
Exemple #9
0
        // just for simplicity, assume:
        // it's x-y plane
        // polygon hasn't inner rings, it is a rectangle
        private bool IsIntersection(GeographyPoint point, GeographyPolygon polygon)
        {
            var outRing = polygon.Rings.First();
            //int pointNum = outRing.Points.Count;

            double maxLat = double.MinValue;
            double maxLon = double.MinValue;
            double minLat = double.MaxValue;
            double minLon = double.MaxValue;

            foreach (var pt in outRing.Points)
            {
                if (maxLat < pt.Latitude)
                {
                    maxLat = pt.Latitude;
                }

                if (maxLon < pt.Longitude)
                {
                    maxLon = pt.Longitude;
                }

                if (minLat > pt.Latitude)
                {
                    minLat = pt.Latitude;
                }

                if (minLon > pt.Longitude)
                {
                    minLon = pt.Longitude;
                }
            }

            if (point.Latitude < minLat || point.Latitude > maxLat ||
                point.Longitude < minLon || point.Longitude > maxLon)
            {
                return(false);
            }

            return(true);
        }
        static ODataSpatialTypeUtil()
        {
            // Geometry type values.
            GeometryValue                = GeometryFactory.Point(32.0, -10.0).Build();
            GeometryPointValue           = GeometryFactory.Point(33.1, -11.0).Build();
            GeometryLineStringValue      = GeometryFactory.LineString(33.1, -11.5).LineTo(35.97, -11).Build();
            GeometryPolygonValue         = GeometryFactory.Polygon().Ring(33.1, -13.6).LineTo(35.97, -11.15).LineTo(11.45, 87.75).Ring(35.97, -11).LineTo(36.97, -11.15).LineTo(45.23, 23.18).Build();
            GeometryCollectionValue      = GeometryFactory.Collection().Point(-19.99, -12.0).Build();
            GeometryMultiPointValue      = GeometryFactory.MultiPoint().Point(10.2, 11.2).Point(11.9, 11.6).Build();
            GeometryMultiLineStringValue = GeometryFactory.MultiLineString().LineString(10.2, 11.2).LineTo(11.9, 11.6).LineString(16.2, 17.2).LineTo(18.9, 19.6).Build();
            GeometryMultiPolygonValue    = GeometryFactory.MultiPolygon().Polygon().Ring(10.2, 11.2).LineTo(11.9, 11.6).LineTo(11.45, 87.75).Ring(16.2, 17.2).LineTo(18.9, 19.6).LineTo(11.45, 87.75).Build();

            // Geography type values.
            GeographyValue                = GeographyFactory.Point(32.0, -100.0).Build();
            GeographyPointValue           = GeographyFactory.Point(33.1, -110.0).Build();
            GeographyLineStringValue      = GeographyFactory.LineString(33.1, -110.0).LineTo(35.97, -110).Build();
            GeographyPolygonValue         = GeographyFactory.Polygon().Ring(33.1, -110.0).LineTo(35.97, -110.15).LineTo(11.45, 87.75).Ring(35.97, -110).LineTo(36.97, -110.15).LineTo(45.23, 23.18).Build();
            GeographyCollectionValue      = GeographyFactory.Collection().Point(-19.99, -12.0).Build();
            GeographyMultiPointValue      = GeographyFactory.MultiPoint().Point(10.2, 11.2).Point(11.9, 11.6).Build();
            GeographyMultiLineStringValue = GeographyFactory.MultiLineString().LineString(10.2, 11.2).LineTo(11.9, 11.6).LineString(16.2, 17.2).LineTo(18.9, 19.6).Build();
            GeographyMultiPolygonValue    = GeographyFactory.MultiPolygon().Polygon().Ring(10.2, 11.2).LineTo(11.9, 11.6).LineTo(11.45, 87.75).Ring(16.2, 17.2).LineTo(18.9, 19.6).LineTo(11.45, 87.75).Build();
        }
        private static IEnumerable GetMicrosoftSpatialPolygonsThrowsData()
        {
            // Require >= 4 points.
            GeographyLineString line = GeographyFactory
                .LineString(0, 0)
                .LineTo(1, 1);

            yield return new TestCaseData(
                line,
                "A GeographyLineString must have at least four Points to form a searchable polygon.");

            // Requires that first and last points are the same.
            line = GeographyFactory
                .LineString(0, 0)
                .LineTo(0, 0)
                .LineTo(0, 0)
                .LineTo(1, 1);

            yield return new TestCaseData(
                line,
                "A GeographyLineString must have matching first and last Points to form a searchable polygon.");

            // Require that polygons define exactly 1 ring.
           GeographyPolygon polygon = GeographyFactory
                .Polygon()
                .Ring(0, 0)
                .LineTo(0, 1)
                .LineTo(1, 1)
                .LineTo(0, 0)
                .Ring(2, 2)
                .LineTo(2, 3)
                .LineTo(3, 3)
                .LineTo(2, 2);

            yield return new TestCaseData(
                polygon,
                "A GeographyPolygon must have exactly one Rings to form a searchable polygon.");
        }
Exemple #12
0
        /// <summary>
        ///     Converts a Microsoft.Spatial GeographyPolygon to a Polygon.
        /// </summary>
        /// <param name="geographyPolygon">The Microsoft.Spatial GeographyPolygon.</param>
        /// <returns></returns>
        public static Polygon ToNtsPolygon(this GeographyPolygon geographyPolygon)
        {
            if (geographyPolygon == null)
            {
                return(null);
            }

            var coords = new List <Coordinate>();

            foreach (var ring in geographyPolygon.Rings)
            {
                foreach (var coord in ring.Points)
                {
                    coords.Add(new Coordinate(coord.Longitude, coord.Latitude));
                }
            }

            var geomFactory = new GeometryFactory(new PrecisionModel(), 4326);

            coords.RemoveAt(coords.Count - 1);
            //coords.Sort(new CoordinateComparer(CalculateCentre(coords)));
            var first = coords.First();

            coords.Add(new Coordinate(first.X, first.Y));
            var sequence = new CoordinateArraySequence(coords.ToArray());
            var poly     = new Polygon(
                new LinearRing(sequence, geomFactory),
                geomFactory);

            // Reverse if this is counter clock-wise
            if (poly.Shell.IsCCW)
            {
                poly.Shell.Reverse();
            }
            return(poly);
        }
Exemple #13
0
        private static GeographyFactory <GeographyCollection> Add(this GeographyFactory <GeographyCollection> factory, GeographyPolygon polygon)
        {
            factory = factory.Polygon();

            foreach (GeographyLineString ring in polygon.Rings)
            {
                factory = factory.Ring(ring.Points[0].Latitude, ring.Points[0].Longitude);

                for (int i = 1; i < ring.Points.Count; i++)
                {
                    factory = factory.LineTo(ring.Points[i].Latitude, ring.Points[i].Longitude);
                }
            }

            return(factory);
        }
Exemple #14
0
        private IEnumerable <Customer> DoGeoIntersections(IEnumerable <Customer> customers, IEdmProperty property, GeographyPolygon polygon)
        {
            PropertyInfo propertyInfo = typeof(Customer).GetProperty(property.Name);

            foreach (var customer in customers)
            {
                GeographyPoint point = propertyInfo.GetValue(customer) as GeographyPoint;

                if (point == null)
                {
                    continue;
                }

                bool isIntersection = IsIntersection(point, polygon);
                if (isIntersection)
                {
                    yield return(customer);
                }
            }
        }