internal static void LoadWkt(this Polygon polygon, string wkt) { var json = wkt.ToJson("MULTIPOLYGON"); polygon.rings = json == null ? new double[][][] { } : json.Deserialize <double[][][][]>().SelectMany(p => p).ToArray(); }
/// <summary> /// Creates a new polygon from well-known text (WKT). /// </summary> /// <param name="wkt"></param> /// <returns></returns> public static Polygon FromWkt(string wkt) { if (wkt == null) return null; var polygon = new Polygon(); polygon.LoadWkt(wkt); return polygon; }
/// <summary> /// Determines if the polygon is inside the other polygon. /// </summary> /// <param name="polygon1"></param> /// <param name="polygon2"></param> /// <returns></returns> public static bool Within(this Polygon polygon1, Polygon polygon2) { if (Null(polygon1, polygon2)) return false; return polygon2.Contains(polygon1); }
/// <summary> /// Determines if the point is within the specified distance from the polygon. /// </summary> /// <param name="point"></param> /// <param name="polygon"></param> /// <param name="distance"></param> /// <returns></returns> public static bool WithinDistance(this Point point, Polygon polygon, double distance) { if (Null(point, polygon)) return false; return point.Extent().Buffer(distance).Intersects(polygon.Extent()) && point.Distance(polygon) < distance; }
/// <summary> /// Determines if the multipoint is inside the polygon. /// </summary> /// <param name="multipoint"></param> /// <param name="polygon"></param> /// <returns></returns> public static bool Within(this Multipoint multipoint, Polygon polygon) { if (Null(multipoint, polygon)) return false; return polygon.Contains(multipoint); }
/// <summary> /// Determines if the polyline is inside the polygon. /// </summary> /// <param name="polyline"></param> /// <param name="polygon"></param> /// <returns></returns> public static bool Within(this Polyline polyline, Polygon polygon) { if (Null(polyline, polygon)) return false; return polygon.Contains(polyline); }
/// <summary> /// Determines if the polygon intersects the other polygon. /// </summary> /// <param name="polygon1"></param> /// <param name="polygon2"></param> /// <returns></returns> public static bool Intersects(this Polygon polygon1, Polygon polygon2) { return new Polyline { paths = polygon1.rings }.Intersects(new Polyline { paths = polygon2.rings }); }
/// <summary> /// Determines if the point is inside the polygon. /// </summary> /// <param name="point"></param> /// <param name="polygon"></param> /// <returns></returns> public static bool Within(this Point point, Polygon polygon) { if (Null(point, polygon)) return false; return polygon.Contains(point); }
/// <summary> /// Calculates the distance to the other polygon. /// </summary> /// <param name="polygon1"></param> /// <param name="polygon2"></param> /// <returns></returns> public static double Distance(this Polygon polygon1, Polygon polygon2) { AssertNotNull(polygon1, polygon2); if (polygon1.Equals(polygon2)) return 0; if (polygon1.Intersects(polygon2)) return 0; if (polygon1.Within(polygon2)) return 0; if (polygon2.Within(polygon1)) return 0; return Math.Min(polygon1.rings.Distance(polygon2.rings), polygon2.rings.Distance(polygon1.rings)); }
/// <summary> /// Determines if the polyline intersects the polygon. /// </summary> /// <param name="polyline"></param> /// <param name="polygon"></param> /// <returns></returns> public static bool Intersects(this Polyline polyline, Polygon polygon) { return polyline.Intersects(new Polyline { paths = polygon.rings }); }
/// <summary> /// Calculates the shortest distance to the polygon. /// </summary> /// <param name="polyline"></param> /// <param name="polygon"></param> /// <returns></returns> public static double Distance(this Polyline polyline, Polygon polygon) { AssertNotNull(polyline, polygon); if (polyline.Intersects(polygon)) return 0; if (polyline.Within(polygon)) return 0; return Math.Min(polyline.paths.Distance(polygon.rings), polygon.rings.Distance(polyline.paths)); }
/// <summary> /// Calculates the shortest distance to the polygon. /// </summary> /// <param name="multipoint"></param> /// <param name="polygon"></param> /// <returns></returns> public static double Distance(this Multipoint multipoint, Polygon polygon) { AssertNotNull(multipoint, polygon); return multipoint.points.Min(p => new Point(p[0], p[1]).Distance(polygon)); }
/// <summary> /// Calculates the shortest distance to the polygon. /// </summary> /// <param name="point"></param> /// <param name="polygon"></param> /// <returns></returns> public static double Distance(this Point point, Polygon polygon) { AssertNotNull(point, polygon); if (polygon.Contains(point)) return 0; return point.Distance(polygon.rings); }
/// <summary> /// Determines if the polygon completely contains the other polygon. /// </summary> /// <param name="polygon1"></param> /// <param name="polygon2"></param> /// <returns></returns> public static bool Contains(this Polygon polygon1, Polygon polygon2) { if (Null(polygon1, polygon2)) return false; return !polygon1.Intersects(polygon2) && polygon2.rings.All(polygon1.Contains); }
/// <summary> /// Determines if the polygon is within the specified distance from the other polygon. /// </summary> /// <param name="polygon1"></param> /// <param name="polygon2"></param> /// <param name="distance"></param> /// <returns></returns> public static bool WithinDistance(this Polygon polygon1, Polygon polygon2, double distance) { if (Null(polygon1, polygon2)) return false; return polygon1.Extent().Buffer(distance).Intersects(polygon2.Extent()) && polygon1.Distance(polygon2) < distance; }