Class WKTReader
- java.lang.Object
-
- org.locationtech.jts.io.WKTReader
-
public class WKTReader extends java.lang.Object
Converts a geometry in Well-Known Text format to aGeometry
.WKTReader
supports extractingGeometry
objects from eitherReader
s orString
s. This allows it to function as a parser to readGeometry
objects from text blocks embedded in other data formats (e.g. XML).A
WKTReader
is parameterized by aGeometryFactory
, to allow it to createGeometry
objects of the appropriate implementation. In particular, theGeometryFactory
determines thePrecisionModel
andSRID
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:
WKTWriter
-
-
Field Summary
Fields Modifier and Type Field 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 Summary
Constructors Constructor Description WKTReader()
Creates a reader that creates objects using the defaultGeometryFactory
.WKTReader(GeometryFactory geometryFactory)
Creates a reader that creates objects using the givenGeometryFactory
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private Coordinate[]
getCoordinates()
Returns the next array ofCoordinate
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 aGeometry
from aReader
.Geometry
read(java.lang.String wellKnownText)
Reads a Well-Known Text representation of aGeometry
from aString
.private GeometryCollection
readGeometryCollectionText()
Creates aGeometryCollection
using the next token in the stream.private Geometry
readGeometryTaggedText()
Creates aGeometry
using the next token in the stream.private LinearRing
readLinearRingText()
Creates aLinearRing
using the next token in the stream.private LineString
readLineStringText()
Creates aLineString
using the next token in the stream.private MultiLineString
readMultiLineStringText()
Creates aMultiLineString
using the next token in the stream.private MultiPoint
readMultiPointText()
Creates aMultiPoint
using the next tokens in the stream.private MultiPolygon
readMultiPolygonText()
Creates aMultiPolygon
using the next token in the stream.private Point
readPointText()
Creates aPoint
using the next token in the stream.private Polygon
readPolygonText()
Creates aPolygon
using the next token in the stream.private java.lang.String
tokenString()
Gets a description of the current tokenprivate Point[]
toPoints(Coordinate[] coordinates)
Creates an array ofPoint
s having the givenCoordinate
s.
-
-
-
Field Detail
-
EMPTY
private static final java.lang.String EMPTY
- See Also:
- Constant Field Values
-
COMMA
private static final java.lang.String COMMA
- See Also:
- Constant Field Values
-
L_PAREN
private static final java.lang.String L_PAREN
- See Also:
- Constant Field Values
-
R_PAREN
private static final java.lang.String R_PAREN
- See Also:
- Constant Field Values
-
NAN_SYMBOL
private static final java.lang.String NAN_SYMBOL
- See Also:
- Constant Field Values
-
geometryFactory
private GeometryFactory geometryFactory
-
precisionModel
private PrecisionModel precisionModel
-
tokenizer
private java.io.StreamTokenizer 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:
- Constant Field Values
-
-
Constructor Detail
-
WKTReader
public WKTReader()
Creates a reader that creates objects using the defaultGeometryFactory
.
-
WKTReader
public WKTReader(GeometryFactory geometryFactory)
Creates a reader that creates objects using the givenGeometryFactory
.- Parameters:
geometryFactory
- the factory used to createGeometry
s.
-
-
Method Detail
-
read
public Geometry read(java.lang.String wellKnownText) throws ParseException
Reads a Well-Known Text representation of aGeometry
from aString
.- Parameters:
wellKnownText
- one or more <Geometry Tagged Text> strings (see the OpenGIS Simple Features Specification) separated by whitespace- Returns:
- a
Geometry
specified bywellKnownText
- Throws:
ParseException
- if a parsing problem occurs
-
read
public Geometry read(java.io.Reader reader) throws ParseException
Reads a Well-Known Text representation of aGeometry
from aReader
.- Parameters:
reader
- a Reader which will return a <Geometry Tagged Text> string (see the OpenGIS Simple Features Specification)- Returns:
- a
Geometry
read fromreader
- Throws:
ParseException
- if a parsing problem occurs
-
getCoordinates
private Coordinate[] getCoordinates() throws java.io.IOException, ParseException
Returns the next array ofCoordinate
s 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
Coordinate
s in the stream, or an empty array if EMPTY is the next element returned by the stream. - Throws:
java.io.IOException
- if an I/O error occursParseException
- if an unexpected token was encountered
-
getCoordinatesNoLeftParen
private Coordinate[] getCoordinatesNoLeftParen() throws java.io.IOException, ParseException
- Throws:
java.io.IOException
ParseException
-
getPreciseCoordinate
private Coordinate getPreciseCoordinate() throws java.io.IOException, ParseException
- Throws:
java.io.IOException
ParseException
-
isNumberNext
private boolean isNumberNext() throws java.io.IOException
- Throws:
java.io.IOException
-
getNextNumber
private double getNextNumber() throws java.io.IOException, ParseException
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 numberjava.io.IOException
- if an I/O error occurs
-
getNextEmptyOrOpener
private java.lang.String getNextEmptyOrOpener() throws java.io.IOException, ParseException
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_PARENjava.io.IOException
- if an I/O error occurs
-
getNextCloserOrComma
private java.lang.String getNextCloserOrComma() throws java.io.IOException, ParseException
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 COMMAjava.io.IOException
- if an I/O error occurs
-
getNextCloser
private java.lang.String getNextCloser() throws java.io.IOException, ParseException
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_PARENjava.io.IOException
- if an I/O error occurs
-
getNextWord
private java.lang.String getNextWord() throws java.io.IOException, ParseException
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 wordjava.io.IOException
- if an I/O error occurs
-
lookaheadWord
private java.lang.String lookaheadWord() throws java.io.IOException, ParseException
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 wordjava.io.IOException
- if an I/O error occurs
-
parseErrorExpected
private void parseErrorExpected(java.lang.String expected) throws ParseException
Throws a formatted ParseException reporting that the current token was unexpected.- Parameters:
expected
- a description of what was expected- Throws:
ParseException
AssertionFailedException
- if an invalid token is encountered
-
parseErrorWithLine
private void parseErrorWithLine(java.lang.String msg) throws ParseException
- Throws:
ParseException
-
tokenString
private java.lang.String tokenString()
Gets a description of the current token- Returns:
- a description of the current token
-
readGeometryTaggedText
private Geometry readGeometryTaggedText() throws java.io.IOException, ParseException
Creates aGeometry
using 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
Geometry
specified by the next token in the stream - Throws:
ParseException
- if the coordinates used to create aPolygon
shell and holes do not form closed linestrings, or if an unexpected token was encounteredjava.io.IOException
- if an I/O error occurs
-
readPointText
private Point readPointText() throws java.io.IOException, ParseException
Creates aPoint
using 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
Point
specified by the next token in the stream - Throws:
java.io.IOException
- if an I/O error occursParseException
- if an unexpected token was encountered
-
readLineStringText
private LineString readLineStringText() throws java.io.IOException, ParseException
Creates aLineString
using 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
LineString
specified by the next token in the stream - Throws:
java.io.IOException
- if an I/O error occursParseException
- if an unexpected token was encountered
-
readLinearRingText
private LinearRing readLinearRingText() throws java.io.IOException, ParseException
Creates aLinearRing
using 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
LinearRing
specified by the next token in the stream - Throws:
java.io.IOException
- if an I/O error occursParseException
- if the coordinates used to create theLinearRing
do not form a closed linestring, or if an unexpected token was encountered
-
readMultiPointText
private MultiPoint readMultiPointText() throws java.io.IOException, ParseException
Creates aMultiPoint
using 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
MultiPoint
specified by the next token in the stream - Throws:
java.io.IOException
- if an I/O error occursParseException
- if an unexpected token was encountered
-
toPoints
private Point[] toPoints(Coordinate[] coordinates)
Creates an array ofPoint
s having the givenCoordinate
s.- Parameters:
coordinates
- theCoordinate
s with which to create thePoint
s- Returns:
Point
s created using thisWKTReader
sGeometryFactory
-
readPolygonText
private Polygon readPolygonText() throws java.io.IOException, ParseException
Creates aPolygon
using 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
Polygon
specified by the next token in the stream - Throws:
ParseException
- if the coordinates used to create thePolygon
shell and holes do not form closed linestrings, or if an unexpected token was encountered.java.io.IOException
- if an I/O error occurs
-
readMultiLineStringText
private MultiLineString readMultiLineStringText() throws java.io.IOException, ParseException
Creates aMultiLineString
using 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
MultiLineString
specified by the next token in the stream - Throws:
java.io.IOException
- if an I/O error occursParseException
- if an unexpected token was encountered
-
readMultiPolygonText
private MultiPolygon readMultiPolygonText() throws java.io.IOException, ParseException
Creates aMultiPolygon
using 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
MultiPolygon
specified by the next token in the stream, or if if the coordinates used to create thePolygon
shells and holes do not form closed linestrings. - Throws:
java.io.IOException
- if an I/O error occursParseException
- if an unexpected token was encountered
-
readGeometryCollectionText
private GeometryCollection readGeometryCollectionText() throws java.io.IOException, ParseException
Creates aGeometryCollection
using 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
GeometryCollection
specified by the next token in the stream - Throws:
ParseException
- if the coordinates used to create aPolygon
shell and holes do not form closed linestrings, or if an unexpected token was encounteredjava.io.IOException
- if an I/O error occurs
-
-