public class WktShapeParser extends Object
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(WktShapeParser.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 |
WktShapeParser.State
The parse state.
|
Modifier and Type | Field and Description |
---|---|
protected SpatialContext |
ctx |
Constructor and Description |
---|
WktShapeParser(SpatialContext ctx,
SpatialContextFactory factory)
This constructor is required by
SpatialContextFactory.makeWktShapeParser(com.spatial4j.core.context.SpatialContext) . |
Modifier and Type | Method and Description |
---|---|
SpatialContext |
getCtx() |
protected WktShapeParser.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(WktShapeParser.State state)
Parses the BUFFER operation applied to a parsed shape.
|
protected Shape |
parseEnvelopeShape(WktShapeParser.State state)
Parses an ENVELOPE (aka Rectangle) shape from the raw string.
|
protected Shape |
parseGeometryCollectionShape(WktShapeParser.State state)
Parses a GEOMETRYCOLLECTION shape from the raw string.
|
Shape |
parseIfSupported(String wktString)
Parses the wktString, returning the defined Shape.
|
protected Shape |
parseLineStringShape(WktShapeParser.State state)
Parses a LINESTRING shape from the raw string -- an ordered sequence of points.
|
protected Shape |
parseMultiLineStringShape(WktShapeParser.State state)
Parses a MULTILINESTRING shape from the raw string -- a collection of line strings.
|
protected Shape |
parseMultiPointShape(WktShapeParser.State state)
Parses a MULTIPOINT shape from the raw string -- a collection of points.
|
protected Shape |
parsePointShape(WktShapeParser.State state)
Parses a POINT shape from the raw string.
|
protected Shape |
parseShapeByType(WktShapeParser.State state,
String shapeType)
(internal) Parses the remainder of a shape definition following the shape's name
given as
shapeType already consumed via
WktShapeParser.State.nextWord() . |
protected Point |
point(WktShapeParser.State state)
Reads a raw Point (AKA Coordinate) from the current position.
|
protected List<Point> |
pointList(WktShapeParser.State state)
Reads a list of Points (AKA CoordinateSequence) from the current position.
|
protected Shape |
shape(WktShapeParser.State state)
Reads a shape from the current position, starting with the name of the shape.
|
protected final SpatialContext ctx
public WktShapeParser(SpatialContext ctx, SpatialContextFactory factory)
SpatialContextFactory.makeWktShapeParser(com.spatial4j.core.context.SpatialContext)
.public SpatialContext getCtx()
public Shape parse(String wktString) throws ParseException
ParseException
- Thrown if there is an error in the Shape definitionpublic Shape parseIfSupported(String wktString) throws ParseException
ParseException
.wktString
- non-null, can be empty or have surrounding whitespaceParseException
- Thrown if there is an error in the Shape definitionprotected WktShapeParser.State newState(String wktString)
parseIfSupported(String)
. This is an extension point for subclassing.protected Shape parseShapeByType(WktShapeParser.State state, String shapeType) throws ParseException
shapeType
already consumed via
WktShapeParser.State.nextWord()
. If
it's able to parse the shape, WktShapeParser.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
WktShapeParser.State.nextIfEmptyAndSkipZM()
.state
- shapeType
- Non-Null string; could have mixed case. The first character is a letter.ParseException
protected Shape parseBufferShape(WktShapeParser.State state) throws ParseException
'(' shape ',' number ')'Whereas 'number' is the distance to buffer the shape by.
ParseException
protected double normDist(double v)
SpatialContext.normX(double)
& normY.protected Shape parsePointShape(WktShapeParser.State state) throws ParseException
'(' coordinate ')'
ParseException
point(WktShapeParser.State)
protected Shape parseMultiPointShape(WktShapeParser.State state) throws ParseException
'(' coordinate (',' coordinate )* ')'Furthermore, coordinate can optionally be wrapped in parenthesis.
ParseException
point(WktShapeParser.State)
protected Shape parseEnvelopeShape(WktShapeParser.State state) throws ParseException
'(' x1 ',' x2 ',' y2 ',' y1 ')'
ParseException
protected Shape parseLineStringShape(WktShapeParser.State state) throws ParseException
coordinateSequence
ParseException
pointList(WktShapeParser.State)
protected Shape parseMultiLineStringShape(WktShapeParser.State state) throws ParseException
'(' coordinateSequence (',' coordinateSequence )* ')'
protected Shape parseGeometryCollectionShape(WktShapeParser.State state) throws ParseException
'(' shape (',' shape )* ')'
ParseException
protected Shape shape(WktShapeParser.State state) throws ParseException
parseShapeByType(com.spatial4j.core.io.WktShapeParser.State, String)
and throws an exception if the shape wasn't supported.ParseException
protected List<Point> pointList(WktShapeParser.State state) throws ParseException
'(' coordinate (',' coordinate )* ')'
ParseException
point(WktShapeParser.State)
protected Point point(WktShapeParser.State state) throws ParseException
number number number*
ParseException
Copyright © 2016 LocationTech. All rights reserved.