// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk) // // This file is part of SharpMap. // SharpMap is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // SharpMap is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // You should have received a copy of the GNU Lesser General Public License // along with SharpMap; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // SOURCECODE IS MODIFIED FROM ANOTHER WORK AND IS ORIGINALLY BASED ON GeoTools.NET: /* * Copyright (C) 2002 Urban Science Applications, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ using GeoAPI.Geometries; using GisSharpBlog.NetTopologySuite.IO; namespace SharpMap.Converters.WellKnownText { /// /// Outputs the textual representation of a instance. /// /// /// The Well-Known Text (WKT) representation of Geometry is designed to exchange geometry data in ASCII form. /// Examples of WKT representations of geometry objects are: /// /// Geometry WKT Representation /// A Point /// POINT(15 20)
Note that point coordinates are specified with no separating comma.
/// A LineString with four points: /// LINESTRING(0 0, 10 10, 20 25, 50 60) /// A Polygon with one exterior ring and one interior ring: /// POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5)) /// A MultiPoint with three Point values: /// MULTIPOINT(0 0, 20 20, 60 60) /// A MultiLineString with two LineString values: /// MULTILINESTRING((10 10, 20 20), (15 15, 30 15)) /// A MultiPolygon with two Polygon values: /// MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5))) /// A GeometryCollection consisting of two Point values and one LineString: /// GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20)) ///
///
public class GeometryToWKT { // #region Methods /// /// Converts a Geometry to its Well-known Text representation. /// /// A Geometry to write. /// A <Geometry Tagged Text> string (see the OpenGIS Simple /// Features Specification) public static string Write(IGeometry geometry) { return new WKTWriter().Write(geometry); // StringWriter sw = new StringWriter(); // Write(geometry, sw); // return sw.ToString(); } // /// // /// Converts a Geometry to its Well-known Text representation. // /// // /// A geometry to process. // /// Stream to write out the geometry's text representation. // /// // /// Geometry is written to the output stream as <Gemoetry Tagged Text> string (see the OpenGIS // /// Simple Features Specification). // /// // public static void Write(IGeometry geometry, StringWriter writer) // { // AppendGeometryTaggedText(geometry, writer); // } // // /// // /// Converts a Geometry to <Geometry Tagged Text > format, then Appends it to the writer. // /// // /// The Geometry to process. // /// The output stream to Append to. // private static void AppendGeometryTaggedText(IGeometry geometry, StringWriter writer) // { // if (geometry == null) // throw new NullReferenceException("Cannot write Well-Known Text: geometry was null"); ; // if (geometry is Point) // { // Point point = geometry as Point; // AppendPointTaggedText(point, writer); // } // else if (geometry is LineString) // AppendLineStringTaggedText(geometry as LineString, writer); // else if (geometry is Polygon) // AppendPolygonTaggedText(geometry as Polygon, writer); // else if (geometry is MultiPoint) // AppendMultiPointTaggedText(geometry as MultiPoint, writer); // else if (geometry is MultiLineString) // AppendMultiLineStringTaggedText(geometry as MultiLineString, writer); // else if (geometry is MultiPolygon) // AppendMultiPolygonTaggedText(geometry as MultiPolygon, writer); // else if (geometry is GeometryCollection) // AppendGeometryCollectionTaggedText(geometry as GeometryCollection, writer); // else // throw new NotSupportedException("Unsupported Geometry implementation:"+ geometry.GetType().Name); // } // // /// // /// Converts a Coordinate to <Point Tagged Text> format, // /// then Appends it to the writer. // /// // /// the Coordinate to process // /// the output writer to Append to // private static void AppendPointTaggedText(Point coordinate, StringWriter writer) // { // writer.Write("POINT "); // AppendPointText(coordinate, writer); // } // // /// // /// Converts a LineString to LineString tagged text format, // /// // /// The LineString to process. // /// The output stream writer to Append to. // private static void AppendLineStringTaggedText(LineString lineString, StringWriter writer) // { // writer.Write("LINESTRING "); // AppendLineStringText(lineString, writer); // } // // /// // /// Converts a Polygon to <Polygon Tagged Text> format, // /// then Appends it to the writer. // /// // /// Th Polygon to process. // /// The stream writer to Append to. // private static void AppendPolygonTaggedText(Polygon polygon, StringWriter writer) // { // writer.Write("POLYGON "); // AppendPolygonText(polygon, writer); // } // // /// // /// Converts a MultiPoint to <MultiPoint Tagged Text> // /// format, then Appends it to the writer. // /// // /// The MultiPoint to process. // /// The output writer to Append to. // private static void AppendMultiPointTaggedText(MultiPoint multipoint, StringWriter writer) // { // writer.Write("MULTIPOINT "); // AppendMultiPointText(multipoint, writer); // } // // /// // /// Converts a MultiLineString to <MultiLineString Tagged // /// Text> format, then Appends it to the writer. // /// // /// The MultiLineString to process // /// The output stream writer to Append to. // private static void AppendMultiLineStringTaggedText(MultiLineString multiLineString, StringWriter writer) // { // writer.Write("MULTILINESTRING "); // AppendMultiLineStringText(multiLineString, writer); // } // // /// // /// Converts a MultiPolygon to <MultiPolygon Tagged // /// Text> format, then Appends it to the writer. // /// // /// The MultiPolygon to process // /// The output stream writer to Append to. // private static void AppendMultiPolygonTaggedText(MultiPolygon multiPolygon, StringWriter writer) // { // // writer.Write("MULTIPOLYGON "); // AppendMultiPolygonText(multiPolygon, writer); // // } // // /// // /// Converts a GeometryCollection to <GeometryCollection Tagged // /// Text> format, then Appends it to the writer. // /// // /// The GeometryCollection to process // /// The output stream writer to Append to. // private static void AppendGeometryCollectionTaggedText(GeometryCollection geometryCollection, StringWriter writer) // { // writer.Write("GEOMETRYCOLLECTION "); // AppendGeometryCollectionText(geometryCollection, writer); // } // // // /// // /// Converts a Coordinate to Point Text format then Appends it to the writer. // /// // /// The Coordinate to process. // /// The output stream writer to Append to. // private static void AppendPointText(Point coordinate, StringWriter writer) // { // if (coordinate == null || coordinate.IsEmpty()) // writer.Write("EMPTY"); // else // { // writer.Write("("); // AppendCoordinate(coordinate, writer); // writer.Write(")"); // } // } // // /// // /// Converts a Coordinate to <Point> format, then Appends // /// it to the writer. // /// // /// The Coordinate to process. // /// The output writer to Append to. // private static void AppendCoordinate(Point coordinate, StringWriter writer) // { // for(uint i=0;i // /// Converts a double to a string, not in scientific notation. // /// // /// The double to convert. // /// The double as a string, not in scientific notation. // private static string WriteNumber(double d) // { // return d.ToString(SharpMap.Map.numberFormat_EnUS); // } // // /// // /// Converts a LineString to <LineString Text> format, then // /// Appends it to the writer. // /// // /// The LineString to process. // /// The output stream to Append to. // private static void AppendLineStringText(LineString lineString, StringWriter writer) // { // // if (lineString==null || lineString.IsEmpty() ) // writer.Write("EMPTY"); // else // { // writer.Write("("); // for (int i = 0; i < lineString.NumPoints; i++) // { // if (i > 0) // writer.Write(", "); // AppendCoordinate( lineString.Vertices[i], writer); // } // writer.Write(")"); // } // } // // /// // /// Converts a Polygon to <Polygon Text> format, then // /// Appends it to the writer. // /// // /// The Polygon to process. // /// // private static void AppendPolygonText(Polygon polygon, StringWriter writer) // { // if (polygon == null || polygon.IsEmpty() ) // writer.Write("EMPTY"); // else // { // writer.Write("("); // AppendLineStringText(polygon.ExteriorRing, writer); // for (int i = 0; i < polygon.InteriorRings.Count; i++) // { // writer.Write(", "); // AppendLineStringText(polygon.InteriorRings[i], writer); // } // writer.Write(")"); // } // } // // /// // /// Converts a MultiPoint to <MultiPoint Text> format, then // /// Appends it to the writer. // /// // /// The MultiPoint to process. // /// The output stream writer to Append to. // private static void AppendMultiPointText(MultiPoint multiPoint, StringWriter writer) // { // // if(multiPoint == null || multiPoint.IsEmpty() ) // writer.Write("EMPTY"); // else // { // writer.Write("("); // for (int i = 0; i < multiPoint.Points.Count; i++) // { // if (i > 0) // writer.Write(", "); // AppendCoordinate(multiPoint[i], writer); // } // writer.Write(")"); // } // } // // /// // /// Converts a MultiLineString to <MultiLineString Text> // /// format, then Appends it to the writer. // /// // /// The MultiLineString to process. // /// The output stream writer to Append to. // private static void AppendMultiLineStringText(MultiLineString multiLineString, StringWriter writer) // { // // if (multiLineString == null || multiLineString.IsEmpty() ) // writer.Write("EMPTY"); // else // { // writer.Write("("); // for (int i = 0; i < multiLineString.LineStrings.Count; i++) // { // if (i > 0) // writer.Write(", "); // AppendLineStringText(multiLineString[i], writer); // } // writer.Write(")"); // } // } // // /// // /// Converts a MultiPolygon to <MultiPolygon Text> format, then Appends to it to the writer. // /// // /// The MultiPolygon to process. // /// The output stream to Append to. // private static void AppendMultiPolygonText(MultiPolygon multiPolygon, StringWriter writer) // { // // if (multiPolygon==null || multiPolygon.IsEmpty() ) // writer.Write("EMPTY"); // else // { // writer.Write("("); // for (int i = 0; i < multiPolygon.Polygons.Count; i++) // { // if (i > 0) // writer.Write(", "); // AppendPolygonText(multiPolygon[i], writer); // } // writer.Write(")"); // } // } // // /// // /// Converts a GeometryCollection to <GeometryCollection Text > format, then Appends it to the writer. // /// // /// The GeometryCollection to process. // /// The output stream writer to Append to. // private static void AppendGeometryCollectionText(GeometryCollection geometryCollection, StringWriter writer) // { // if (geometryCollection==null || geometryCollection.IsEmpty() ) // writer.Write("EMPTY"); // else // { // writer.Write("("); // for (int i = 0; i < geometryCollection.Collection.Count; i++) // { // if (i > 0) // writer.Write(", "); // AppendGeometryTaggedText(geometryCollection[i], writer); // } // writer.Write(")"); // } // } // #endregion } }