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.As of version 1.15, JTS can read (but not write) WKT syntax which specifies coordinate dimension Z, M or ZM as modifiers (e.g. POINT Z) or in the name of the geometry type (e.g. LINESTRINGZM). If the coordinate dimension is specified it will be set in the created geometry. If the coordinate dimension is not specified, the default behaviour is to create XYZ geometry (this is backwards compatible with older JTS versions). This can be altered to create XY geometry by calling
setIsOldJtsCoordinateSyntaxAllowed(boolean)
.A reader can be set to ensure the input is structurally valid by calling
setFixStructure(boolean)
. This ensures that geometry can be constructed without errors due to missing coordinates. The created geometry may still be topologically invalid.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.)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 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
-
-
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 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
.void
setFixStructure(boolean isFixStructure)
Sets a flag indicating that the structure of input geometry should be fixed so that the geometry can be constructed without error.void
setIsOldJtsCoordinateSyntaxAllowed(boolean value)
Sets a flag indicating, that coordinates may have 3 ordinate values even though no Z or M ordinate indicator is present.void
setIsOldJtsMultiPointSyntaxAllowed(boolean value)
Sets a flag indicating, that point coordinates in a MultiPoint geometry must not be enclosed in paren.
-
-
-
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
-
setIsOldJtsCoordinateSyntaxAllowed
public void setIsOldJtsCoordinateSyntaxAllowed(boolean value)
Sets a flag indicating, that coordinates may have 3 ordinate values even though no Z or M ordinate indicator is present. The default value isALLOW_OLD_JTS_COORDINATE_SYNTAX
.- Parameters:
value
- a boolean value
-
setIsOldJtsMultiPointSyntaxAllowed
public void setIsOldJtsMultiPointSyntaxAllowed(boolean value)
Sets a flag indicating, that point coordinates in a MultiPoint geometry must not be enclosed in paren. The default value isALLOW_OLD_JTS_MULTIPOINT_SYNTAX
- Parameters:
value
- a boolean value
-
setFixStructure
public void setFixStructure(boolean isFixStructure)
Sets a flag indicating that the structure of input geometry should be fixed so that the geometry can be constructed without error. This involves adding coordinates if the input coordinate sequence is shorter than required.- Parameters:
isFixStructure
- true if the input structure should be fixed- See Also:
LinearRing.MINIMUM_VALID_SIZE
-
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
-
-