Class WKTWriter

java.lang.Object
org.locationtech.jts.io.WKTWriter

public class WKTWriter extends Object
Writes the Well-Known Text representation of a Geometry. The Well-Known Text format is defined in the OGC Simple Features Specification for SQL. See WKTReader for a formal specification of the format syntax.

The WKTWriter outputs coordinates rounded to the precision model. Only the maximum number of decimal places necessary to represent the ordinates to the required precision will be output.

The SFS WKT spec does not define a special tag for LinearRings. Under the spec, rings are output as LINESTRINGs. In order to allow precisely specifying constructed geometries, JTS also supports a non-standard LINEARRING tag which is used to output LinearRings.

Version:
1.7
See Also:
  • Field Details

    • INDENT

      private static final int INDENT
      See Also:
    • outputDimension

      private int outputDimension
    • formatter

      private DecimalFormat formatter
    • isFormatted

      private boolean isFormatted
    • useFormatting

      private boolean useFormatting
    • level

      private int level
    • coordsPerLine

      private int coordsPerLine
    • indentTabStr

      private String indentTabStr
  • Constructor Details

    • WKTWriter

      public WKTWriter()
      Creates a new WKTWriter with default settings
    • WKTWriter

      public WKTWriter(int outputDimension)
      Creates a writer that writes Geometrys with the given output dimension (2 or 3). If the specified output dimension is 3, the Z value of coordinates will be written if it is present (i.e. if it is not Double.NaN).
      Parameters:
      outputDimension - the coordinate dimension to output (2 or 3)
  • Method Details

    • toPoint

      public static String toPoint(Coordinate p0)
      Generates the WKT for a POINT specified by a Coordinate.
      Parameters:
      p0 - the point coordinate
      Returns:
      the WKT
    • toLineString

      public static String toLineString(CoordinateSequence seq)
      Generates the WKT for a LINESTRING specified by a CoordinateSequence.
      Parameters:
      seq - the sequence to write
      Returns:
      the WKT string
    • toLineString

      public static String toLineString(Coordinate[] coord)
      Generates the WKT for a LINESTRING specified by a CoordinateSequence.
      Parameters:
      coord - the sequence to write
      Returns:
      the WKT string
    • toLineString

      public static String toLineString(Coordinate p0, Coordinate p1)
      Generates the WKT for a LINESTRING specified by two Coordinates.
      Parameters:
      p0 - the first coordinate
      p1 - the second coordinate
      Returns:
      the WKT
    • createFormatter

      private static DecimalFormat createFormatter(PrecisionModel precisionModel)
      Creates the DecimalFormat used to write doubles with a sufficient number of decimal places.
      Parameters:
      precisionModel - the PrecisionModel used to determine the number of decimal places to write.
      Returns:
      a DecimalFormat that write double s without scientific notation.
    • stringOfChar

      public static String stringOfChar(char ch, int count)
      Returns a String of repeated characters.
      Parameters:
      ch - the character to repeat
      count - the number of times to repeat the character
      Returns:
      a String of characters
    • setFormatted

      public void setFormatted(boolean isFormatted)
      Sets whether the output will be formatted.
      Parameters:
      isFormatted - true if the output is to be formatted
    • setMaxCoordinatesPerLine

      public void setMaxCoordinatesPerLine(int coordsPerLine)
      Sets the maximum number of coordinates per line written in formatted output. If the provided coordinate number is <= 0, coordinates will be written all on one line.
      Parameters:
      coordsPerLine - the number of coordinates per line to output.
    • setTab

      public void setTab(int size)
      Sets the tab size to use for indenting.
      Parameters:
      size - the number of spaces to use as the tab string
      Throws:
      IllegalArgumentException - if the size is non-positive
    • write

      public String write(Geometry geometry)
      Converts a Geometry to its Well-known Text representation.
      Parameters:
      geometry - a Geometry to process
      Returns:
      a <Geometry Tagged Text> string (see the OpenGIS Simple Features Specification)
    • write

      public void write(Geometry geometry, Writer writer) throws IOException
      Converts a Geometry to its Well-known Text representation.
      Parameters:
      geometry - a Geometry to process
      Throws:
      IOException
    • writeFormatted

      public String writeFormatted(Geometry geometry)
      Same as write, but with newlines and spaces to make the well-known text more readable.
      Parameters:
      geometry - a Geometry to process
      Returns:
      a <Geometry Tagged Text> string (see the OpenGIS Simple Features Specification), with newlines and spaces
    • writeFormatted

      public void writeFormatted(Geometry geometry, Writer writer) throws IOException
      Same as write, but with newlines and spaces to make the well-known text more readable.
      Parameters:
      geometry - a Geometry to process
      Throws:
      IOException
    • writeFormatted

      private void writeFormatted(Geometry geometry, boolean useFormatting, Writer writer) throws IOException
      Converts a Geometry to its Well-known Text representation.
      Parameters:
      geometry - a Geometry to process
      Throws:
      IOException
    • appendGeometryTaggedText

      private void appendGeometryTaggedText(Geometry geometry, int level, Writer writer) throws IOException
      Converts a Geometry to <Geometry Tagged Text> format, then appends it to the writer.
      Parameters:
      geometry - the Geometry to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendPointTaggedText

      private void appendPointTaggedText(Coordinate coordinate, int level, Writer writer, PrecisionModel precisionModel) throws IOException
      Converts a Coordinate to <Point Tagged Text> format, then appends it to the writer.
      Parameters:
      coordinate - the Coordinate to process
      writer - the output writer to append to
      precisionModel - the PrecisionModel to use to convert from a precise coordinate to an external coordinate
      Throws:
      IOException
    • appendLineStringTaggedText

      private void appendLineStringTaggedText(LineString lineString, int level, Writer writer) throws IOException
      Converts a LineString to <LineString Tagged Text> format, then appends it to the writer.
      Parameters:
      lineString - the LineString to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendLinearRingTaggedText

      private void appendLinearRingTaggedText(LinearRing linearRing, int level, Writer writer) throws IOException
      Converts a LinearRing to <LinearRing Tagged Text> format, then appends it to the writer.
      Parameters:
      linearRing - the LinearRing to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendPolygonTaggedText

      private void appendPolygonTaggedText(Polygon polygon, int level, Writer writer) throws IOException
      Converts a Polygon to <Polygon Tagged Text> format, then appends it to the writer.
      Parameters:
      polygon - the Polygon to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendMultiPointTaggedText

      private void appendMultiPointTaggedText(MultiPoint multipoint, int level, Writer writer) throws IOException
      Converts a MultiPoint to <MultiPoint Tagged Text> format, then appends it to the writer.
      Parameters:
      multipoint - the MultiPoint to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendMultiLineStringTaggedText

      private void appendMultiLineStringTaggedText(MultiLineString multiLineString, int level, Writer writer) throws IOException
      Converts a MultiLineString to <MultiLineString Tagged Text> format, then appends it to the writer.
      Parameters:
      multiLineString - the MultiLineString to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendMultiPolygonTaggedText

      private void appendMultiPolygonTaggedText(MultiPolygon multiPolygon, int level, Writer writer) throws IOException
      Converts a MultiPolygon to <MultiPolygon Tagged Text> format, then appends it to the writer.
      Parameters:
      multiPolygon - the MultiPolygon to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendGeometryCollectionTaggedText

      private void appendGeometryCollectionTaggedText(GeometryCollection geometryCollection, int level, Writer writer) throws IOException
      Converts a GeometryCollection to <GeometryCollection Tagged Text> format, then appends it to the writer.
      Parameters:
      geometryCollection - the GeometryCollection to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendPointText

      private void appendPointText(Coordinate coordinate, int level, Writer writer, PrecisionModel precisionModel) throws IOException
      Converts a Coordinate to <Point Text> format, then appends it to the writer.
      Parameters:
      coordinate - the Coordinate to process
      writer - the output writer to append to
      precisionModel - the PrecisionModel to use to convert from a precise coordinate to an external coordinate
      Throws:
      IOException
    • appendCoordinate

      private void appendCoordinate(CoordinateSequence seq, int i, Writer writer) throws IOException
      Appends the i'th coordinate from the sequence to the writer
      Parameters:
      seq - the CoordinateSequence to process
      i - the index of the coordinate to write
      writer - the output writer to append to
      Throws:
      IOException
    • appendCoordinate

      private void appendCoordinate(Coordinate coordinate, Writer writer) throws IOException
      Converts a Coordinate to <Point> format, then appends it to the writer.
      Parameters:
      coordinate - the Coordinate to process
      writer - the output writer to append to
      Throws:
      IOException
    • writeNumber

      private String writeNumber(double d)
      Converts a double to a String, not in scientific notation.
      Parameters:
      d - the double to convert
      Returns:
      the double as a String, not in scientific notation
    • appendSequenceText

      private void appendSequenceText(CoordinateSequence seq, int level, boolean doIndent, Writer writer) throws IOException
      Converts a LineString to <LineString Text> format, then appends it to the writer.
      Parameters:
      writer - the output writer to append to
      lineString - the LineString to process
      Throws:
      IOException
    • appendLineStringText

      private void appendLineStringText(LineString lineString, int level, boolean doIndent, Writer writer) throws IOException
      Converts a LineString to <LineString Text> format, then appends it to the writer.
      Parameters:
      lineString - the LineString to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendPolygonText

      private void appendPolygonText(Polygon polygon, int level, boolean indentFirst, Writer writer) throws IOException
      Converts a Polygon to <Polygon Text> format, then appends it to the writer.
      Parameters:
      polygon - the Polygon to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendMultiPointText

      private void appendMultiPointText(MultiPoint multiPoint, int level, Writer writer) throws IOException
      Converts a MultiPoint to <MultiPoint Text> format, then appends it to the writer.
      Parameters:
      multiPoint - the MultiPoint to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendMultiLineStringText

      private void appendMultiLineStringText(MultiLineString multiLineString, int level, boolean indentFirst, Writer writer) throws IOException
      Converts a MultiLineString to <MultiLineString Text> format, then appends it to the writer.
      Parameters:
      multiLineString - the MultiLineString to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendMultiPolygonText

      private void appendMultiPolygonText(MultiPolygon multiPolygon, int level, Writer writer) throws IOException
      Converts a MultiPolygon to <MultiPolygon Text> format, then appends it to the writer.
      Parameters:
      multiPolygon - the MultiPolygon to process
      writer - the output writer to append to
      Throws:
      IOException
    • appendGeometryCollectionText

      private void appendGeometryCollectionText(GeometryCollection geometryCollection, int level, Writer writer) throws IOException
      Converts a GeometryCollection to <GeometryCollectionText> format, then appends it to the writer.
      Parameters:
      geometryCollection - the GeometryCollection to process
      writer - the output writer to append to
      Throws:
      IOException
    • indentCoords

      private void indentCoords(int coordIndex, int level, Writer writer) throws IOException
      Throws:
      IOException
    • indent

      private void indent(int level, Writer writer) throws IOException
      Throws:
      IOException