public class WKTReader
extends java.lang.Object
Geometry
.
WKTReader
supports
extracting Geometry
objects from either Reader
s or
String
s. This allows it to function as a parser to read Geometry
objects from text blocks embedded in other data formats (e.g. XML).
A WKTReader
is parameterized by a GeometryFactory
,
to allow it to create Geometry
objects of the appropriate
implementation. In particular, the GeometryFactory
determines the PrecisionModel
and SRID
that is
used.
The WKTReader
converts all input numbers to the precise
internal representation.
As of version 1.15, JTS can read (but not write) WKT Strings including Z, M or ZM in the name of the geometry type (ex. POINT Z, LINESTRINGZM). Note that it only makes the reader more flexible, but JTS could already read 3D coordinates from WKT String and still can't read 4D coordinates.
WKTGeometry: one of WKTPoint WKTLineString WKTLinearRing WKTPolygon WKTMultiPoint WKTMultiLineString WKTMultiPolygon WKTGeometryCollection WKTPoint: POINT[Dimension] ( Coordinate ) WKTLineString: LINESTRING[Dimension] CoordinateSequence WKTLinearRing: LINEARRING[Dimension] CoordinateSequence WKTPolygon: POLYGON[Dimension] CoordinateSequenceList WKTMultiPoint: MULTIPOINT[Dimension] CoordinateSingletonList WKTMultiLineString: MULTILINESTRING[Dimension] CoordinateSequenceList WKTMultiPolygon: MULTIPOLYGON[Dimension] ( CoordinateSequenceList { , CoordinateSequenceList } ) WKTGeometryCollection: GEOMETRYCOLLECTION[Dimension] ( WKTGeometry { , WKTGeometry } ) CoordinateSingletonList: ( CoordinateSingleton { , CoordinateSingleton } ) | EMPTY CoordinateSingleton: ( Coordinate ) | EMPTY CoordinateSequenceList: ( CoordinateSequence { , CoordinateSequence } ) | EMPTY CoordinateSequence: ( Coordinate { , Coordinate } ) | EMPTY Coordinate: Number Number Numberopt Number: A Java-style floating-point number (including NaN, with arbitrary case) Dimension: Z| Z|M| M|ZM| ZM
WKTWriter
Modifier and Type | Field and Description |
---|---|
private static boolean |
ALLOW_OLD_JTS_MULTIPOINT_SYNTAX |
private static java.lang.String |
COMMA |
private static java.lang.String |
EMPTY |
private GeometryFactory |
geometryFactory |
private static java.lang.String |
L_PAREN |
private boolean |
m |
private static java.lang.String |
NAN_SYMBOL |
private PrecisionModel |
precisionModel |
private static java.lang.String |
R_PAREN |
private java.io.StreamTokenizer |
tokenizer |
private boolean |
z |
Constructor and Description |
---|
WKTReader()
Creates a reader that creates objects using the default
GeometryFactory . |
WKTReader(GeometryFactory geometryFactory)
Creates a reader that creates objects using the given
GeometryFactory . |
Modifier and Type | Method and Description |
---|---|
private Coordinate[] |
getCoordinates()
Returns the next array of
Coordinate s in the stream. |
private Coordinate[] |
getCoordinatesNoLeftParen() |
private java.lang.String |
getNextCloser()
Returns the next R_PAREN in the stream.
|
private java.lang.String |
getNextCloserOrComma()
Returns the next R_PAREN or COMMA in the stream.
|
private java.lang.String |
getNextEmptyOrOpener()
Returns the next EMPTY or L_PAREN in the stream as uppercase text.
|
private double |
getNextNumber()
Parses the next number in the stream.
|
private java.lang.String |
getNextWord()
Returns the next word in the stream.
|
private Coordinate |
getPreciseCoordinate() |
private boolean |
isNumberNext() |
private java.lang.String |
lookaheadWord()
Returns the next word in the stream.
|
private void |
parseErrorExpected(java.lang.String expected)
Throws a formatted ParseException reporting that the current token
was unexpected.
|
private void |
parseErrorWithLine(java.lang.String msg) |
Geometry |
read(java.io.Reader reader)
Reads a Well-Known Text representation of a
Geometry
from a Reader . |
Geometry |
read(java.lang.String wellKnownText)
Reads a Well-Known Text representation of a
Geometry
from a String . |
private GeometryCollection |
readGeometryCollectionText()
Creates a
GeometryCollection using the next token in the
stream. |
private Geometry |
readGeometryTaggedText()
Creates a
Geometry using the next token in the stream. |
private LinearRing |
readLinearRingText()
Creates a
LinearRing using the next token in the stream. |
private LineString |
readLineStringText()
Creates a
LineString using the next token in the stream. |
private MultiLineString |
readMultiLineStringText()
Creates a
MultiLineString using the next token in the stream. |
private MultiPoint |
readMultiPointText()
Creates a
MultiPoint using the next tokens in the stream. |
private MultiPolygon |
readMultiPolygonText()
Creates a
MultiPolygon using the next token in the stream. |
private Point |
readPointText()
Creates a
Point using the next token in the stream. |
private Polygon |
readPolygonText()
Creates a
Polygon using the next token in the stream. |
private java.lang.String |
tokenString()
Gets a description of the current token
|
private Point[] |
toPoints(Coordinate[] coordinates)
Creates an array of
Point s having the given Coordinate
s. |
private static final java.lang.String EMPTY
private static final java.lang.String COMMA
private static final java.lang.String L_PAREN
private static final java.lang.String R_PAREN
private static final java.lang.String NAN_SYMBOL
private GeometryFactory geometryFactory
private PrecisionModel precisionModel
private java.io.StreamTokenizer tokenizer
private boolean z
private boolean m
private static final boolean ALLOW_OLD_JTS_MULTIPOINT_SYNTAX
public WKTReader()
GeometryFactory
.public WKTReader(GeometryFactory geometryFactory)
GeometryFactory
.geometryFactory
- the factory used to create Geometry
s.public Geometry read(java.lang.String wellKnownText) throws ParseException
Geometry
from a String
.wellKnownText
- one or more <Geometry Tagged Text> strings (see the OpenGIS
Simple Features Specification) separated by whitespaceGeometry
specified by wellKnownText
ParseException
- if a parsing problem occurspublic Geometry read(java.io.Reader reader) throws ParseException
Geometry
from a Reader
.reader
- a Reader which will return a <Geometry Tagged Text>
string (see the OpenGIS Simple Features Specification)Geometry
read from reader
ParseException
- if a parsing problem occursprivate Coordinate[] getCoordinates() throws java.io.IOException, ParseException
Coordinate
s in the stream.tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next element returned by the stream should be L_PAREN (the
beginning of "(x1 y1, x2 y2, ..., xn yn)") or EMPTY.Coordinate
s in the
stream, or an empty array if EMPTY is the next element returned by
the stream.java.io.IOException
- if an I/O error occursParseException
- if an unexpected token was encounteredprivate Coordinate[] getCoordinatesNoLeftParen() throws java.io.IOException, ParseException
java.io.IOException
ParseException
private Coordinate getPreciseCoordinate() throws java.io.IOException, ParseException
java.io.IOException
ParseException
private boolean isNumberNext() throws java.io.IOException
java.io.IOException
private double getNextNumber() throws java.io.IOException, ParseException
tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next token must be a number.ParseException
- if the next token is not a valid numberjava.io.IOException
- if an I/O error occursprivate java.lang.String getNextEmptyOrOpener() throws java.io.IOException, ParseException
tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next token must be EMPTY or L_PAREN.ParseException
- if the next token is not EMPTY or L_PARENjava.io.IOException
- if an I/O error occursprivate java.lang.String getNextCloserOrComma() throws java.io.IOException, ParseException
tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next token must be R_PAREN or COMMA.ParseException
- if the next token is not R_PAREN or COMMAjava.io.IOException
- if an I/O error occursprivate java.lang.String getNextCloser() throws java.io.IOException, ParseException
tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next token must be R_PAREN.ParseException
- if the next token is not R_PARENjava.io.IOException
- if an I/O error occursprivate java.lang.String getNextWord() throws java.io.IOException, ParseException
tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next token must be a word.ParseException
- if the next token is not a wordjava.io.IOException
- if an I/O error occursprivate java.lang.String lookaheadWord() throws java.io.IOException, ParseException
tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next token must be a word.ParseException
- if the next token is not a wordjava.io.IOException
- if an I/O error occursprivate void parseErrorExpected(java.lang.String expected) throws ParseException
expected
- a description of what was expectedParseException
AssertionFailedException
- if an invalid token is encounteredprivate void parseErrorWithLine(java.lang.String msg) throws ParseException
ParseException
private java.lang.String tokenString()
private Geometry readGeometryTaggedText() throws java.io.IOException, ParseException
Geometry
using the next token in the stream.tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next tokens must form a <Geometry Tagged Text>.Geometry
specified by the next token
in the streamParseException
- if the coordinates used to create a Polygon
shell and holes do not form closed linestrings, or if an unexpected
token was encounteredjava.io.IOException
- if an I/O error occursprivate Point readPointText() throws java.io.IOException, ParseException
Point
using the next token in the stream.tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next tokens must form a <Point Text>.Point
specified by the next token in
the streamjava.io.IOException
- if an I/O error occursParseException
- if an unexpected token was encounteredprivate LineString readLineStringText() throws java.io.IOException, ParseException
LineString
using the next token in the stream.tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next tokens must form a <LineString Text>.LineString
specified by the next
token in the streamjava.io.IOException
- if an I/O error occursParseException
- if an unexpected token was encounteredprivate LinearRing readLinearRingText() throws java.io.IOException, ParseException
LinearRing
using the next token in the stream.tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next tokens must form a <LineString Text>.LinearRing
specified by the next
token in the streamjava.io.IOException
- if an I/O error occursParseException
- if the coordinates used to create the LinearRing
do not form a closed linestring, or if an unexpected token was
encounteredprivate MultiPoint readMultiPointText() throws java.io.IOException, ParseException
MultiPoint
using the next tokens in the stream.tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next tokens must form a <MultiPoint Text>.MultiPoint
specified by the next
token in the streamjava.io.IOException
- if an I/O error occursParseException
- if an unexpected token was encounteredprivate Point[] toPoints(Coordinate[] coordinates)
Point
s having the given Coordinate
s.coordinates
- the Coordinate
s with which to create the
Point
sPoint
s created using this WKTReader
s GeometryFactory
private Polygon readPolygonText() throws java.io.IOException, ParseException
Polygon
using the next token in the stream.tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next tokens must form a <Polygon Text>.Polygon
specified by the next token
in the streamParseException
- if the coordinates used to create the Polygon
shell and holes do not form closed linestrings, or if an unexpected
token was encountered.java.io.IOException
- if an I/O error occursprivate MultiLineString readMultiLineStringText() throws java.io.IOException, ParseException
MultiLineString
using the next token in the stream.tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next tokens must form a <MultiLineString Text>.MultiLineString
specified by the
next token in the streamjava.io.IOException
- if an I/O error occursParseException
- if an unexpected token was encounteredprivate MultiPolygon readMultiPolygonText() throws java.io.IOException, ParseException
MultiPolygon
using the next token in the stream.tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next tokens must form a <MultiPolygon Text>.MultiPolygon
specified by the next
token in the stream, or if if the coordinates used to create the
Polygon
shells and holes do not form closed linestrings.java.io.IOException
- if an I/O error occursParseException
- if an unexpected token was encounteredprivate GeometryCollection readGeometryCollectionText() throws java.io.IOException, ParseException
GeometryCollection
using the next token in the
stream.tokenizer
- tokenizer over a stream of text in Well-known Text
format. The next tokens must form a <GeometryCollection Text>.GeometryCollection
specified by the
next token in the streamParseException
- if the coordinates used to create a Polygon
shell and holes do not form closed linestrings, or if an unexpected
token was encounteredjava.io.IOException
- if an I/O error occurs