/// <summary> /// Parses the specified <paramref name="input"/> string into an instance of <see cref="WktGeometry"/>. /// </summary> /// <param name="input">The input string to parse.</param> /// <returns>An instance of <see cref="WktGeometry"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="input"/> is <c>null</c>.</exception> /// <exception cref="WktInvalidFormatException"><paramref name="input"/> is not in a known format.</exception> /// <exception cref="WktUnsupportedTypeException">type of <paramref name="input"/> is not supported.</exception> public static WktGeometry Parse(string input) { if (string.IsNullOrWhiteSpace(input)) { throw new ArgumentNullException(nameof(input)); } string type = input.Split('(')[0].ToUpper().Trim(); if (string.IsNullOrWhiteSpace(type)) { throw new WktInvalidFormatException(input); } switch (type) { case "POINT": return(WktPoint.Parse(input)); case "POLYGON": return(WktPolygon.Parse(input)); case "LINESTRING": return(WktLineString.Parse(input)); case "MULTIPOINT": return(WktMultiPoint.Parse(input)); case "MULTILINESTRING": return(WktMultiLineString.Parse(input)); case "MULTIPOLYGON": return(WktMultiPolygon.Parse(input)); default: throw new WktUnsupportedTypeException(type); } }
/// <summary> /// Converts the specified <paramref name="polygon"/> to a corresponding instance of <seealso cref="IPolygon"/>. /// </summary> /// <param name="polygon">The polygon to be converted.</param> /// <returns>An instance of <see cref="IPolygon"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="polygon"/> is <c>null</c>.</exception> public static IPolygon ToPolygon(WktPolygon polygon) { if (polygon == null) { throw new ArgumentNullException(nameof(polygon)); } var outer = polygon.Outer.Select(ToPoint); var inner = polygon.Inner.Select(x => x.Select(ToPoint)).ToArray(); return(new Polygon(outer, inner)); }
/// <summary> /// Returns the area of the specified <paramref name="polygon"/> measured in sqaure metres. /// </summary> /// <param name="polygon">The polygon.</param> /// <returns>A <see cref="double"/> representing the area in square metres.</returns> /// <remarks>For this method to work, it is assumed that coordinates are specified using the /// <strong>WGS 84</strong> coordinate system (eg. used by the Global Positioning System).</remarks> /// <see> /// <cref>https://en.wikipedia.org/wiki/World_Geodetic_System#WGS84</cref> /// </see> /// <see> /// <cref>https://en.wikipedia.org/wiki/Global_Positioning_System</cref> /// </see> /// <exception cref="ArgumentNullException"><paramref name="polygon"/> is <c>null</c>.</exception> public static double GetArea(WktPolygon polygon) { if (polygon == null) { throw new ArgumentNullException(nameof(polygon)); } // Get the overall area from the outer points double area = MapsUtils.GetArea(polygon.Outer.Select(x => new Point(x.Y, x.X))); // Substract the area of the inner points foreach (WktPoint[] inner in polygon.Inner) { area -= MapsUtils.GetArea(inner.Select(x => new Point(x.Y, x.X))); } return(area); }
/// <summary> /// Returns the <strong>Well Known Text</strong> string representation of the specified <paramref name="polygon"/>. /// </summary> /// <param name="polygon">The polygon to be converted.</param> /// <param name="formatting">The formatting to be used.</param> /// <param name="indentation">The indendatation to be used.</param> /// <returns>The polygon formatted as a <strong>Well Known Text</strong> string.</returns> protected string ToString(WktPolygon polygon, WktFormatting formatting, int indentation) { if (formatting == WktFormatting.Indented) { throw new NotImplementedException("Indented formatting is currently not supported"); } List <string> temp = new List <string>(); temp.Add(ToString(polygon.Outer, formatting, indentation)); foreach (WktPoint[] array in polygon.Inner) { temp.Add(ToString(array, formatting, indentation)); } return("(" + string.Join(formatting == WktFormatting.Minified ? "," : ", ", temp) + ")"); }