/// <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="multiPolygon"/> to a corresponding instance of <seealso cref="IMultiPolygon"/>. /// </summary> /// <param name="multiPolygon">The multi polygon to be converted.</param> /// <returns>An instance of <see cref="IMultiPolygon"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="multiPolygon"/> is <c>null</c>.</exception> public static IMultiPolygon ToMultiPolygon(WktMultiPolygon multiPolygon) { if (multiPolygon == null) { throw new ArgumentNullException(nameof(multiPolygon)); } return(new MultiPolygon(multiPolygon.Select(ToPolygon))); }
/// <summary> /// Returns the area of the specified <paramref name="multiPolygon"/> shape measured in sqaure metres. /// </summary> /// <param name="multiPolygon">The collection of polygons.</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="multiPolygon"/> is <c>null</c>.</exception> public static double GetArea(WktMultiPolygon multiPolygon) { if (multiPolygon == null) { throw new ArgumentNullException(nameof(multiPolygon)); } return(multiPolygon.Count == 0 ? 0 : multiPolygon.Sum(GetArea)); }