Package org.locationtech.jts.io
Class WKTWriter
- java.lang.Object
-
- org.locationtech.jts.io.WKTWriter
-
public class WKTWriter extends java.lang.Object
Writes the Well-Known Text representation of aGeometry
. The Well-Known Text format is defined in the OGC Simple Features Specification for SQL. SeeWKTReader
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
LinearRing
s. Under the spec, rings are output asLINESTRING
s. In order to allow precisely specifying constructed geometries, JTS also supports a non-standardLINEARRING
tag which is used to output LinearRings.- Version:
- 1.7
- See Also:
WKTReader
-
-
Field Summary
Fields Modifier and Type Field Description private int
coordsPerLine
private java.text.DecimalFormat
formatter
private static int
INDENT
private java.lang.String
indentTabStr
private boolean
isFormatted
private int
level
private int
outputDimension
private boolean
useFormatting
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
appendCoordinate(Coordinate coordinate, java.io.Writer writer)
Converts aCoordinate
to<Point>
format, then appends it to the writer.private void
appendCoordinate(CoordinateSequence seq, int i, java.io.Writer writer)
Appends the i'th coordinate from the sequence to the writerprivate void
appendGeometryCollectionTaggedText(GeometryCollection geometryCollection, int level, java.io.Writer writer)
Converts aGeometryCollection
to <GeometryCollection Tagged Text> format, then appends it to the writer.private void
appendGeometryCollectionText(GeometryCollection geometryCollection, int level, java.io.Writer writer)
Converts aGeometryCollection
to <GeometryCollectionText> format, then appends it to the writer.private void
appendGeometryTaggedText(Geometry geometry, int level, java.io.Writer writer)
Converts aGeometry
to <Geometry Tagged Text> format, then appends it to the writer.private void
appendLinearRingTaggedText(LinearRing linearRing, int level, java.io.Writer writer)
Converts aLinearRing
to <LinearRing Tagged Text> format, then appends it to the writer.private void
appendLineStringTaggedText(LineString lineString, int level, java.io.Writer writer)
Converts aLineString
to <LineString Tagged Text> format, then appends it to the writer.private void
appendLineStringText(LineString lineString, int level, boolean doIndent, java.io.Writer writer)
Converts aLineString
to <LineString Text> format, then appends it to the writer.private void
appendMultiLineStringTaggedText(MultiLineString multiLineString, int level, java.io.Writer writer)
Converts aMultiLineString
to <MultiLineString Tagged Text> format, then appends it to the writer.private void
appendMultiLineStringText(MultiLineString multiLineString, int level, boolean indentFirst, java.io.Writer writer)
Converts aMultiLineString
to <MultiLineString Text> format, then appends it to the writer.private void
appendMultiPointTaggedText(MultiPoint multipoint, int level, java.io.Writer writer)
Converts aMultiPoint
to <MultiPoint Tagged Text> format, then appends it to the writer.private void
appendMultiPointText(MultiPoint multiPoint, int level, java.io.Writer writer)
Converts aMultiPoint
to <MultiPoint Text> format, then appends it to the writer.private void
appendMultiPolygonTaggedText(MultiPolygon multiPolygon, int level, java.io.Writer writer)
Converts aMultiPolygon
to <MultiPolygon Tagged Text> format, then appends it to the writer.private void
appendMultiPolygonText(MultiPolygon multiPolygon, int level, java.io.Writer writer)
Converts aMultiPolygon
to <MultiPolygon Text> format, then appends it to the writer.private void
appendPointTaggedText(Coordinate coordinate, int level, java.io.Writer writer, PrecisionModel precisionModel)
Converts aCoordinate
to <Point Tagged Text> format, then appends it to the writer.private void
appendPointText(Coordinate coordinate, int level, java.io.Writer writer, PrecisionModel precisionModel)
Converts aCoordinate
to <Point Text> format, then appends it to the writer.private void
appendPolygonTaggedText(Polygon polygon, int level, java.io.Writer writer)
Converts aPolygon
to <Polygon Tagged Text> format, then appends it to the writer.private void
appendPolygonText(Polygon polygon, int level, boolean indentFirst, java.io.Writer writer)
Converts aPolygon
to <Polygon Text> format, then appends it to the writer.private void
appendSequenceText(CoordinateSequence seq, int level, boolean doIndent, java.io.Writer writer)
Converts aLineString
to <LineString Text> format, then appends it to the writer.private static java.text.DecimalFormat
createFormatter(PrecisionModel precisionModel)
Creates theDecimalFormat
used to writedouble
s with a sufficient number of decimal places.private void
indent(int level, java.io.Writer writer)
private void
indentCoords(int coordIndex, int level, java.io.Writer writer)
void
setFormatted(boolean isFormatted)
Sets whether the output will be formatted.void
setMaxCoordinatesPerLine(int coordsPerLine)
Sets the maximum number of coordinates per line written in formatted output.void
setTab(int size)
Sets the tab size to use for indenting.static java.lang.String
stringOfChar(char ch, int count)
Returns aString
of repeated characters.static java.lang.String
toLineString(Coordinate[] coord)
Generates the WKT for a LINESTRING specified by aCoordinateSequence
.static java.lang.String
toLineString(Coordinate p0, Coordinate p1)
Generates the WKT for a LINESTRING specified by twoCoordinate
s.static java.lang.String
toLineString(CoordinateSequence seq)
Generates the WKT for a LINESTRING specified by aCoordinateSequence
.static java.lang.String
toPoint(Coordinate p0)
Generates the WKT for a POINT specified by aCoordinate
.java.lang.String
write(Geometry geometry)
Converts aGeometry
to its Well-known Text representation.void
write(Geometry geometry, java.io.Writer writer)
Converts aGeometry
to its Well-known Text representation.java.lang.String
writeFormatted(Geometry geometry)
Same aswrite
, but with newlines and spaces to make the well-known text more readable.private void
writeFormatted(Geometry geometry, boolean useFormatting, java.io.Writer writer)
Converts aGeometry
to its Well-known Text representation.void
writeFormatted(Geometry geometry, java.io.Writer writer)
Same aswrite
, but with newlines and spaces to make the well-known text more readable.private java.lang.String
writeNumber(double d)
Converts adouble
to aString
, not in scientific notation.
-
-
-
Field Detail
-
INDENT
private static final int INDENT
- See Also:
- Constant Field Values
-
outputDimension
private int outputDimension
-
formatter
private java.text.DecimalFormat formatter
-
isFormatted
private boolean isFormatted
-
useFormatting
private boolean useFormatting
-
level
private int level
-
coordsPerLine
private int coordsPerLine
-
indentTabStr
private java.lang.String indentTabStr
-
-
Constructor Detail
-
WKTWriter
public WKTWriter()
Creates a new WKTWriter with default settings
-
WKTWriter
public WKTWriter(int outputDimension)
Creates a writer that writesGeometry
s 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 notDouble.NaN
).- Parameters:
outputDimension
- the coordinate dimension to output (2 or 3)
-
-
Method Detail
-
toPoint
public static java.lang.String toPoint(Coordinate p0)
Generates the WKT for a POINT specified by aCoordinate
.- Parameters:
p0
- the point coordinate- Returns:
- the WKT
-
toLineString
public static java.lang.String toLineString(CoordinateSequence seq)
Generates the WKT for a LINESTRING specified by aCoordinateSequence
.- Parameters:
seq
- the sequence to write- Returns:
- the WKT string
-
toLineString
public static java.lang.String toLineString(Coordinate[] coord)
Generates the WKT for a LINESTRING specified by aCoordinateSequence
.- Parameters:
coord
- the sequence to write- Returns:
- the WKT string
-
toLineString
public static java.lang.String toLineString(Coordinate p0, Coordinate p1)
Generates the WKT for a LINESTRING specified by twoCoordinate
s.- Parameters:
p0
- the first coordinatep1
- the second coordinate- Returns:
- the WKT
-
createFormatter
private static java.text.DecimalFormat createFormatter(PrecisionModel precisionModel)
Creates theDecimalFormat
used to writedouble
s with a sufficient number of decimal places.- Parameters:
precisionModel
- thePrecisionModel
used to determine the number of decimal places to write.- Returns:
- a
DecimalFormat
that writedouble
s without scientific notation.
-
stringOfChar
public static java.lang.String stringOfChar(char ch, int count)
Returns aString
of repeated characters.- Parameters:
ch
- the character to repeatcount
- 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:
java.lang.IllegalArgumentException
- if the size is non-positive
-
write
public java.lang.String write(Geometry geometry)
Converts aGeometry
to its Well-known Text representation.- Parameters:
geometry
- aGeometry
to process- Returns:
- a <Geometry Tagged Text> string (see the OpenGIS Simple Features Specification)
-
write
public void write(Geometry geometry, java.io.Writer writer) throws java.io.IOException
Converts aGeometry
to its Well-known Text representation.- Parameters:
geometry
- aGeometry
to process- Throws:
java.io.IOException
-
writeFormatted
public java.lang.String writeFormatted(Geometry geometry)
Same aswrite
, but with newlines and spaces to make the well-known text more readable.- Parameters:
geometry
- aGeometry
to process- Returns:
- a <Geometry Tagged Text> string (see the OpenGIS Simple Features Specification), with newlines and spaces
-
writeFormatted
public void writeFormatted(Geometry geometry, java.io.Writer writer) throws java.io.IOException
Same aswrite
, but with newlines and spaces to make the well-known text more readable.- Parameters:
geometry
- aGeometry
to process- Throws:
java.io.IOException
-
writeFormatted
private void writeFormatted(Geometry geometry, boolean useFormatting, java.io.Writer writer) throws java.io.IOException
Converts aGeometry
to its Well-known Text representation.- Parameters:
geometry
- aGeometry
to process- Throws:
java.io.IOException
-
appendGeometryTaggedText
private void appendGeometryTaggedText(Geometry geometry, int level, java.io.Writer writer) throws java.io.IOException
Converts aGeometry
to <Geometry Tagged Text> format, then appends it to the writer.- Parameters:
geometry
- theGeometry
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendPointTaggedText
private void appendPointTaggedText(Coordinate coordinate, int level, java.io.Writer writer, PrecisionModel precisionModel) throws java.io.IOException
Converts aCoordinate
to <Point Tagged Text> format, then appends it to the writer.- Parameters:
coordinate
- theCoordinate
to processwriter
- the output writer to append toprecisionModel
- thePrecisionModel
to use to convert from a precise coordinate to an external coordinate- Throws:
java.io.IOException
-
appendLineStringTaggedText
private void appendLineStringTaggedText(LineString lineString, int level, java.io.Writer writer) throws java.io.IOException
Converts aLineString
to <LineString Tagged Text> format, then appends it to the writer.- Parameters:
lineString
- theLineString
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendLinearRingTaggedText
private void appendLinearRingTaggedText(LinearRing linearRing, int level, java.io.Writer writer) throws java.io.IOException
Converts aLinearRing
to <LinearRing Tagged Text> format, then appends it to the writer.- Parameters:
linearRing
- theLinearRing
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendPolygonTaggedText
private void appendPolygonTaggedText(Polygon polygon, int level, java.io.Writer writer) throws java.io.IOException
Converts aPolygon
to <Polygon Tagged Text> format, then appends it to the writer.- Parameters:
polygon
- thePolygon
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendMultiPointTaggedText
private void appendMultiPointTaggedText(MultiPoint multipoint, int level, java.io.Writer writer) throws java.io.IOException
Converts aMultiPoint
to <MultiPoint Tagged Text> format, then appends it to the writer.- Parameters:
multipoint
- theMultiPoint
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendMultiLineStringTaggedText
private void appendMultiLineStringTaggedText(MultiLineString multiLineString, int level, java.io.Writer writer) throws java.io.IOException
Converts aMultiLineString
to <MultiLineString Tagged Text> format, then appends it to the writer.- Parameters:
multiLineString
- theMultiLineString
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendMultiPolygonTaggedText
private void appendMultiPolygonTaggedText(MultiPolygon multiPolygon, int level, java.io.Writer writer) throws java.io.IOException
Converts aMultiPolygon
to <MultiPolygon Tagged Text> format, then appends it to the writer.- Parameters:
multiPolygon
- theMultiPolygon
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendGeometryCollectionTaggedText
private void appendGeometryCollectionTaggedText(GeometryCollection geometryCollection, int level, java.io.Writer writer) throws java.io.IOException
Converts aGeometryCollection
to <GeometryCollection Tagged Text> format, then appends it to the writer.- Parameters:
geometryCollection
- theGeometryCollection
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendPointText
private void appendPointText(Coordinate coordinate, int level, java.io.Writer writer, PrecisionModel precisionModel) throws java.io.IOException
Converts aCoordinate
to <Point Text> format, then appends it to the writer.- Parameters:
coordinate
- theCoordinate
to processwriter
- the output writer to append toprecisionModel
- thePrecisionModel
to use to convert from a precise coordinate to an external coordinate- Throws:
java.io.IOException
-
appendCoordinate
private void appendCoordinate(CoordinateSequence seq, int i, java.io.Writer writer) throws java.io.IOException
Appends the i'th coordinate from the sequence to the writer- Parameters:
seq
- theCoordinateSequence
to processi
- the index of the coordinate to writewriter
- the output writer to append to- Throws:
java.io.IOException
-
appendCoordinate
private void appendCoordinate(Coordinate coordinate, java.io.Writer writer) throws java.io.IOException
Converts aCoordinate
to<Point>
format, then appends it to the writer.- Parameters:
coordinate
- theCoordinate
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
writeNumber
private java.lang.String writeNumber(double d)
Converts adouble
to aString
, not in scientific notation.- Parameters:
d
- thedouble
to convert- Returns:
- the
double
as aString
, not in scientific notation
-
appendSequenceText
private void appendSequenceText(CoordinateSequence seq, int level, boolean doIndent, java.io.Writer writer) throws java.io.IOException
Converts aLineString
to <LineString Text> format, then appends it to the writer.- Parameters:
lineString
- theLineString
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendLineStringText
private void appendLineStringText(LineString lineString, int level, boolean doIndent, java.io.Writer writer) throws java.io.IOException
Converts aLineString
to <LineString Text> format, then appends it to the writer.- Parameters:
lineString
- theLineString
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendPolygonText
private void appendPolygonText(Polygon polygon, int level, boolean indentFirst, java.io.Writer writer) throws java.io.IOException
Converts aPolygon
to <Polygon Text> format, then appends it to the writer.- Parameters:
polygon
- thePolygon
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendMultiPointText
private void appendMultiPointText(MultiPoint multiPoint, int level, java.io.Writer writer) throws java.io.IOException
Converts aMultiPoint
to <MultiPoint Text> format, then appends it to the writer.- Parameters:
multiPoint
- theMultiPoint
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendMultiLineStringText
private void appendMultiLineStringText(MultiLineString multiLineString, int level, boolean indentFirst, java.io.Writer writer) throws java.io.IOException
Converts aMultiLineString
to <MultiLineString Text> format, then appends it to the writer.- Parameters:
multiLineString
- theMultiLineString
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendMultiPolygonText
private void appendMultiPolygonText(MultiPolygon multiPolygon, int level, java.io.Writer writer) throws java.io.IOException
Converts aMultiPolygon
to <MultiPolygon Text> format, then appends it to the writer.- Parameters:
multiPolygon
- theMultiPolygon
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
appendGeometryCollectionText
private void appendGeometryCollectionText(GeometryCollection geometryCollection, int level, java.io.Writer writer) throws java.io.IOException
Converts aGeometryCollection
to <GeometryCollectionText> format, then appends it to the writer.- Parameters:
geometryCollection
- theGeometryCollection
to processwriter
- the output writer to append to- Throws:
java.io.IOException
-
indentCoords
private void indentCoords(int coordIndex, int level, java.io.Writer writer) throws java.io.IOException
- Throws:
java.io.IOException
-
indent
private void indent(int level, java.io.Writer writer) throws java.io.IOException
- Throws:
java.io.IOException
-
-