public class WKTReader extends Object implements ShapeReader
parse___Shape methods further describe these
shapes, or you
Most users of this class will call just one method: parse(String), or
parseIfSupported(String) to not fail if it isn't parse-able.
To support more shapes, extend this class and override
parseShapeByType(WKTReader.State, String). It's also possible to delegate to a WKTParser
by also delegating newState(String).
Note, instances of this base class are threadsafe.| Modifier and Type | Class and Description |
|---|---|
class |
WKTReader.State
The parse state.
|
| Modifier and Type | Field and Description |
|---|---|
protected SpatialContext |
ctx |
| Constructor and Description |
|---|
WKTReader(SpatialContext ctx,
SpatialContextFactory factory)
This constructor is required by
com.spatial4j.core.context.SpatialContextFactory#makeWktShapeParser(com.spatial4j.core.context.SpatialContext)
. |
| Modifier and Type | Method and Description |
|---|---|
String |
getFormatName() |
protected WKTReader.State |
newState(String wktString)
(internal) Creates a new State with the given String.
|
protected double |
normDist(double v)
Called to normalize a value that isn't X or Y.
|
Shape |
parse(String wktString)
Parses the wktString, returning the defined Shape.
|
protected Shape |
parseBufferShape(WKTReader.State state)
Parses the BUFFER operation applied to a parsed shape.
|
protected Shape |
parseEnvelopeShape(WKTReader.State state)
Parses an ENVELOPE (aka Rectangle) shape from the raw string.
|
protected Shape |
parseGeometryCollectionShape(WKTReader.State state)
Parses a GEOMETRYCOLLECTION shape from the raw string.
|
Shape |
parseIfSupported(String wktString)
Parses the wktString, returning the defined Shape.
|
protected Shape |
parseLineStringShape(WKTReader.State state)
Parses a LINESTRING shape from the raw string -- an ordered sequence of points.
|
protected Shape |
parseMultiLineStringShape(WKTReader.State state)
Parses a MULTILINESTRING shape from the raw string -- a collection of line strings.
|
protected Shape |
parseMultiPointShape(WKTReader.State state)
Parses a MULTIPOINT shape from the raw string -- a collection of points.
|
protected Shape |
parsePointShape(WKTReader.State state)
Parses a POINT shape from the raw string.
|
protected Shape |
parseShapeByType(WKTReader.State state,
String shapeType)
(internal) Parses the remainder of a shape definition following the shape's name given as
shapeType already consumed via WKTReader.State.nextWord(). |
protected Point |
point(WKTReader.State state)
Reads a raw Point (AKA Coordinate) from the current position.
|
protected List<Point> |
pointList(WKTReader.State state)
Reads a list of Points (AKA CoordinateSequence) from the current position.
|
Shape |
read(Object value) |
Shape |
read(Reader reader)
Read a
Shape from the reader. |
Shape |
readIfSupported(Object value) |
protected Shape |
shape(WKTReader.State state)
Reads a shape from the current position, starting with the name of the shape.
|
protected final SpatialContext ctx
public WKTReader(SpatialContext ctx, SpatialContextFactory factory)
com.spatial4j.core.context.SpatialContextFactory#makeWktShapeParser(com.spatial4j.core.context.SpatialContext)
.public Shape parse(String wktString) throws ParseException, InvalidShapeException
ParseException - Thrown if there is an error in the Shape definitionInvalidShapeExceptionpublic Shape parseIfSupported(String wktString) throws ParseException, InvalidShapeException
ParseException.wktString - non-null, can be empty or have surrounding whitespaceParseException - Thrown if there is an error in the Shape definitionInvalidShapeExceptionprotected WKTReader.State newState(String wktString)
parseIfSupported(String). This is an extension point for subclassing.protected Shape parseShapeByType(WKTReader.State state, String shapeType) throws ParseException
shapeType already consumed via WKTReader.State.nextWord(). If it's able to parse the
shape, WKTReader.State.offset should be advanced beyond it (e.g. to the ',' or ')' or
EOF in general). The default implementation checks the name against some predefined names and
calls corresponding parse methods to handle the rest. Overriding this method is an excellent
extension point for additional shape types. Or, use this class by delegation to this method.
When writing a parse method that reacts to a specific shape type, remember to handle the
dimension and EMPTY token via
WKTReader.State.nextIfEmptyAndSkipZM().state - shapeType - Non-Null string; could have mixed case. The first character is a letter.ParseExceptionprotected Shape parseBufferShape(WKTReader.State state) throws ParseException
'(' shape ',' number ')'
Whereas 'number' is the distance to buffer the shape by.ParseExceptionprotected double normDist(double v)
SpatialContext.normX(double) & normY.protected Shape parsePointShape(WKTReader.State state) throws ParseException
'(' coordinate ')'
ParseExceptionpoint(WKTReader.State)protected Shape parseMultiPointShape(WKTReader.State state) throws ParseException
'(' coordinate (',' coordinate )* ')'
Furthermore, coordinate can optionally be wrapped in parenthesis.ParseExceptionpoint(WKTReader.State)protected Shape parseEnvelopeShape(WKTReader.State state) throws ParseException
'(' x1 ',' x2 ',' y2 ',' y1 ')'
ParseExceptionprotected Shape parseLineStringShape(WKTReader.State state) throws ParseException
coordinateSequence
ParseExceptionpointList(WKTReader.State)protected Shape parseMultiLineStringShape(WKTReader.State state) throws ParseException
'(' coordinateSequence (',' coordinateSequence )* ')'
ParseExceptionparseLineStringShape(com.spatial4j.core.io.WKTReader.State)protected Shape parseGeometryCollectionShape(WKTReader.State state) throws ParseException
'(' shape (',' shape )* ')'
ParseExceptionprotected Shape shape(WKTReader.State state) throws ParseException
parseShapeByType(com.spatial4j.core.io.WKTReader.State, String) and throws an
exception if the shape wasn't supported.ParseExceptionprotected List<Point> pointList(WKTReader.State state) throws ParseException
'(' coordinate (',' coordinate )* ')'
ParseExceptionpoint(WKTReader.State)protected Point point(WKTReader.State state) throws ParseException
number number number*
ParseExceptionpublic String getFormatName()
getFormatName in interface ShapeIOpublic Shape read(Reader reader) throws IOException, ParseException
ShapeReaderShape from the reader.read in interface ShapeReaderreader - -- the input. Note, it will not be closed by this functionIOExceptionParseExceptionpublic Shape read(Object value) throws IOException, ParseException, InvalidShapeException
read in interface ShapeReadervalue - -- the input value, could be a String or other objectIOExceptionParseExceptionInvalidShapeExceptionpublic Shape readIfSupported(Object value) throws InvalidShapeException
readIfSupported in interface ShapeReadervalue - -- the input value, could be a String or other objectInvalidShapeException when we could read a shape, but it was
invalidInvalidShapeExceptionCopyright © 2025. All rights reserved.