/// <summary> /// Generates a WKT string for the polygons in a Placemark <see cref="Placemark"/>. /// Currently only supports placemarks with MultipleGeometry, Polygon and LineString Geometries. /// </summary> /// <param name="placemark">The placemark instance.</param> /// <param name="convertLineStringToPolygon">If linestring should be converted to polygon</param> /// <returns> /// A <c>string</c> containing the well known text string for the geometry in the /// placemark. /// </returns> /// <exception cref="ArgumentNullException">placemark is null.</exception> /// <exception cref="ArgumentException">placemark geometry is not a MultipleGeometry, Polygon or LineString.</exception> public static string AsWKT(this Placemark placemark, bool convertLineStringToPolygon = false) { if (placemark == null) { throw new ArgumentNullException(); } if (!(placemark.Geometry is MultipleGeometry) && !(placemark.Geometry is Polygon) && !(placemark.Geometry is LineString)) { throw new NotImplementedException("Only implemented types are Polygon, MultiplePolygon and LineString"); } List <Vector[][]> coordinates = placemark.ConvertToCoordinates(); if (placemark.Geometry is MultipleGeometry) { return(GenerateMultiplePolygonWKT(coordinates)); } if (placemark.Geometry is LineString && !convertLineStringToPolygon) { return(GenerateLineStringWKT(coordinates.FirstOrDefault())); } return(GeneratePolygonWKT(coordinates.FirstOrDefault())); }
/// <summary> /// Generates a WKT string for the polygons in a Placemark <see cref="Placemark"/>. /// Currently only supports placemarks with MultipleGeometry or Polygon Geometries. /// </summary> /// <param name="placemark">The placemark instance.</param> /// <returns> /// A <c>string</c> containing the well known text string for the geometry in the /// placemark. /// </returns> /// <exception cref="ArgumentNullException">placemark is null.</exception> /// <exception cref="ArgumentException">placemark geometry is not a MultipleGeometry or Polygon.</exception> public static string AsWKT(this Placemark placemark) { if (placemark == null) { throw new ArgumentNullException(); } if (!(placemark.Geometry is MultipleGeometry) && !(placemark.Geometry is SharpKml.Dom.Polygon)) { throw new NotImplementedException("Only implemented types are Polygon and MultiplePolygon"); } List <Vector[][]> coordinates = placemark.ConvertToCoordinates(); if (placemark.Geometry is MultipleGeometry) { return(GenerateMultiplePolygonWKT(coordinates)); } return(GeneratePolygonWKT(coordinates.FirstOrDefault())); }
/// <summary> /// Generates a WKT string for the polygons,LineString And Points in a Placemark <see cref="Placemark"/>. /// Currently only supports placemarks with MultipleGeometry or Polygon Geometries. /// </summary> /// <param name="placemark">The placemark instance.</param> /// <returns> /// A <c>string</c> containing the well known text string for the geometry in the /// placemark. /// </returns> /// <exception cref="ArgumentNullException">placemark is null.</exception> /// <exception cref="ArgumentException">placemark geometry is not a MultipleGeometry or Polygon.</exception> public static string AsWKT(this Placemark placemark) { if (placemark == null) { throw new ArgumentNullException(); } if ((placemark.Geometry is MultipleGeometry) || (placemark.Geometry is Polygon)) { List <Vector[][]> coordinates = placemark.ConvertToCoordinates(); if (placemark.Geometry is MultipleGeometry) { return(GenerateMultiplePolygonWKT(coordinates)); } return(GeneratePolygonWKT(coordinates.FirstOrDefault())); } else if (placemark.Geometry is LineString) { var lineStringCoordinates = ((LineString)placemark.Geometry).Coordinates .Select(x => $"{x.Longitude} {x.Latitude}"); string wkt = $"LINESTRING({string.Join(", ", lineStringCoordinates)})"; return(wkt); } else if (placemark.Geometry is Point) { Point point = ((Point)placemark.Geometry); return($"POINT({point.Coordinate.Longitude} {point.Coordinate.Latitude})"); } throw new NotImplementedException("Only implemented types are Polygon - MultiplePolygon - LineString - Point"); }