Class WKTReader
Geometry.
WKTReader supports
extracting Geometry objects from either Readers or
Strings. 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.
Notes:
- Keywords are case-insensitive.
- The reader supports non-standard "LINEARRING" tags.
- The reader uses Double.parseDouble to perform the conversion of ASCII numbers to floating point. This means it supports the Java syntax for floating point literals (including scientific notation).
Syntax
The following syntax specification describes the version of Well-Known Text supported by JTS. (The specification uses a syntax language similar to that used in the C and Java language specifications.)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
- Version:
- 1.7
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final booleanprivate static final Stringprivate static final Stringprivate GeometryFactoryprivate static final Stringprivate booleanprivate static final Stringprivate PrecisionModelprivate static final Stringprivate StreamTokenizerprivate boolean -
Constructor Summary
ConstructorsConstructorDescriptionCreates a reader that creates objects using the defaultGeometryFactory.WKTReader(GeometryFactory geometryFactory) Creates a reader that creates objects using the givenGeometryFactory. -
Method Summary
Modifier and TypeMethodDescriptionprivate Coordinate[]Returns the next array ofCoordinates in the stream.private Coordinate[]private StringReturns the next R_PAREN in the stream.private StringReturns the next R_PAREN or COMMA in the stream.private StringReturns the next EMPTY or L_PAREN in the stream as uppercase text.private doubleParses the next number in the stream.private StringReturns the next word in the stream.private Coordinateprivate booleanprivate StringReturns the next word in the stream.private voidparseErrorExpected(String expected) Throws a formatted ParseException reporting that the current token was unexpected.private voidparseErrorWithLine(String msg) private GeometryCollectionCreates aGeometryCollectionusing the next token in the stream.private GeometryCreates aGeometryusing the next token in the stream.private LinearRingCreates aLinearRingusing the next token in the stream.private LineStringCreates aLineStringusing the next token in the stream.private MultiLineStringCreates aMultiLineStringusing the next token in the stream.private MultiPointCreates aMultiPointusing the next tokens in the stream.private MultiPolygonCreates aMultiPolygonusing the next token in the stream.private PointCreates aPointusing the next token in the stream.private PolygonCreates aPolygonusing the next token in the stream.private StringGets a description of the current tokenprivate Point[]toPoints(Coordinate[] coordinates) Creates an array ofPoints having the givenCoordinates.
-
Field Details
-
EMPTY
- See Also:
-
COMMA
- See Also:
-
L_PAREN
- See Also:
-
R_PAREN
- See Also:
-
NAN_SYMBOL
- See Also:
-
geometryFactory
-
precisionModel
-
tokenizer
-
z
private boolean z -
m
private boolean m -
ALLOW_OLD_JTS_MULTIPOINT_SYNTAX
private static final boolean ALLOW_OLD_JTS_MULTIPOINT_SYNTAX- See Also:
-
-
Constructor Details
-
WKTReader
public WKTReader()Creates a reader that creates objects using the defaultGeometryFactory. -
WKTReader
Creates a reader that creates objects using the givenGeometryFactory.- Parameters:
geometryFactory- the factory used to createGeometrys.
-
-
Method Details
-
read
- Parameters:
wellKnownText- one or more <Geometry Tagged Text> strings (see the OpenGIS Simple Features Specification) separated by whitespace- Returns:
- a
Geometryspecified bywellKnownText - Throws:
ParseException- if a parsing problem occurs
-
read
- Parameters:
reader- a Reader which will return a <Geometry Tagged Text> string (see the OpenGIS Simple Features Specification)- Returns:
- a
Geometryread fromreader - Throws:
ParseException- if a parsing problem occurs
-
getCoordinates
Returns the next array ofCoordinates in the stream.- Parameters:
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.- Returns:
- the next array of
Coordinates in the stream, or an empty array if EMPTY is the next element returned by the stream. - Throws:
IOException- if an I/O error occursParseException- if an unexpected token was encountered
-
getCoordinatesNoLeftParen
- Throws:
IOExceptionParseException
-
getPreciseCoordinate
- Throws:
IOExceptionParseException
-
isNumberNext
- Throws:
IOException
-
getNextNumber
Parses the next number in the stream. Numbers with exponents are handled. NaN values are handled correctly, and the case of the "NaN" symbol is not significant.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next token must be a number.- Returns:
- the next number in the stream
- Throws:
ParseException- if the next token is not a valid numberIOException- if an I/O error occurs
-
getNextEmptyOrOpener
Returns the next EMPTY or L_PAREN in the stream as uppercase text.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next token must be EMPTY or L_PAREN.- Returns:
- the next EMPTY or L_PAREN in the stream as uppercase text.
- Throws:
ParseException- if the next token is not EMPTY or L_PARENIOException- if an I/O error occurs
-
getNextCloserOrComma
Returns the next R_PAREN or COMMA in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next token must be R_PAREN or COMMA.- Returns:
- the next R_PAREN or COMMA in the stream
- Throws:
ParseException- if the next token is not R_PAREN or COMMAIOException- if an I/O error occurs
-
getNextCloser
Returns the next R_PAREN in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next token must be R_PAREN.- Returns:
- the next R_PAREN in the stream
- Throws:
ParseException- if the next token is not R_PARENIOException- if an I/O error occurs
-
getNextWord
Returns the next word in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next token must be a word.- Returns:
- the next word in the stream as uppercase text
- Throws:
ParseException- if the next token is not a wordIOException- if an I/O error occurs
-
lookaheadWord
Returns the next word in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next token must be a word.- Returns:
- the next word in the stream as uppercase text
- Throws:
ParseException- if the next token is not a wordIOException- if an I/O error occurs
-
parseErrorExpected
Throws a formatted ParseException reporting that the current token was unexpected.- Parameters:
expected- a description of what was expected- Throws:
ParseExceptionAssertionFailedException- if an invalid token is encountered
-
parseErrorWithLine
- Throws:
ParseException
-
tokenString
Gets a description of the current token- Returns:
- a description of the current token
-
readGeometryTaggedText
Creates aGeometryusing the next token in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next tokens must form a <Geometry Tagged Text>.- Returns:
- a
Geometryspecified by the next token in the stream - Throws:
ParseException- if the coordinates used to create aPolygonshell and holes do not form closed linestrings, or if an unexpected token was encounteredIOException- if an I/O error occurs
-
readPointText
Creates aPointusing the next token in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next tokens must form a <Point Text>.- Returns:
- a
Pointspecified by the next token in the stream - Throws:
IOException- if an I/O error occursParseException- if an unexpected token was encountered
-
readLineStringText
Creates aLineStringusing the next token in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next tokens must form a <LineString Text>.- Returns:
- a
LineStringspecified by the next token in the stream - Throws:
IOException- if an I/O error occursParseException- if an unexpected token was encountered
-
readLinearRingText
Creates aLinearRingusing the next token in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next tokens must form a <LineString Text>.- Returns:
- a
LinearRingspecified by the next token in the stream - Throws:
IOException- if an I/O error occursParseException- if the coordinates used to create theLinearRingdo not form a closed linestring, or if an unexpected token was encountered
-
readMultiPointText
Creates aMultiPointusing the next tokens in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next tokens must form a <MultiPoint Text>.- Returns:
- a
MultiPointspecified by the next token in the stream - Throws:
IOException- if an I/O error occursParseException- if an unexpected token was encountered
-
toPoints
Creates an array ofPoints having the givenCoordinates.- Parameters:
coordinates- theCoordinates with which to create thePoints- Returns:
Points created using thisWKTReadersGeometryFactory
-
readPolygonText
Creates aPolygonusing the next token in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next tokens must form a <Polygon Text>.- Returns:
- a
Polygonspecified by the next token in the stream - Throws:
ParseException- if the coordinates used to create thePolygonshell and holes do not form closed linestrings, or if an unexpected token was encountered.IOException- if an I/O error occurs
-
readMultiLineStringText
Creates aMultiLineStringusing the next token in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next tokens must form a <MultiLineString Text>.- Returns:
- a
MultiLineStringspecified by the next token in the stream - Throws:
IOException- if an I/O error occursParseException- if an unexpected token was encountered
-
readMultiPolygonText
Creates aMultiPolygonusing the next token in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next tokens must form a <MultiPolygon Text>.- Returns:
- a
MultiPolygonspecified by the next token in the stream, or if if the coordinates used to create thePolygonshells and holes do not form closed linestrings. - Throws:
IOException- if an I/O error occursParseException- if an unexpected token was encountered
-
readGeometryCollectionText
Creates aGeometryCollectionusing the next token in the stream.- Parameters:
tokenizer- tokenizer over a stream of text in Well-known Text format. The next tokens must form a <GeometryCollection Text>.- Returns:
- a
GeometryCollectionspecified by the next token in the stream - Throws:
ParseException- if the coordinates used to create aPolygonshell and holes do not form closed linestrings, or if an unexpected token was encounteredIOException- if an I/O error occurs
-