public static bool InPolygon(MultiPolygon polygon, double latitude, double longitude)
        {
            var numOfPoints          = polygon.Count;
            var lats                 = polygon.Select(x => x[0]).ToList();
            var lngs                 = polygon.Select(x => x[1]).ToList();
            var polygonContainsPoint = false;

            for (int node = 0, altNode = (numOfPoints - 1); node < numOfPoints; altNode = node++)
            {
                if ((lngs[node] > longitude != (lngs[altNode] > longitude)) &&
                    (latitude < ((lats[altNode] - lats[node])
                                 * (longitude - lngs[node])
                                 / (lngs[altNode] - lngs[node]))
                     + lats[node]
                    )
                    )
                {
                    polygonContainsPoint = !polygonContainsPoint;
                }
            }
            lats.Clear();
            lngs.Clear();
            return(polygonContainsPoint);
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="geometries"></param>
        /// <param name="wkid"></param>
        /// <returns></returns>
        private async Task <MultiPolygon> Transform(MultiPolygon geometries, int wkid)
        {
            var transformation = await GetTransformation(geometries.SRID, wkid);

            var polygons = geometries.Select(x => x.Coordinates);

            var polygonsTransforme = polygons.Select((polygon) =>
            {
                var cordenadasPre = transformation.MathTransform.TransformList(polygon.Select(x => new[] { x.X, x.Y }).ToList());
                var pt            = transformation.MathTransform.TransformList(geometries.Coordinates.Select(x => new[] { x.X, x.Y }).ToList());
                var cordenadas    = pt.Select(x => new Coordinate(x[0], x[1])).ToArray();
                var lineRing      = new LinearRing(cordenadas);
                return(new Polygon(lineRing));
            });

            return(new MultiPolygon(polygonsTransforme.Distinct().ToArray()));
        }