package org.apache.lucene.geo;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class SimpleWKTShapeParser {
public static final String EMPTY = "EMPTY";
public static final String SPACE = " ";
public static final String LPAREN = "(";
public static final String RPAREN = ")";
public static final String COMMA = ",";
public static final String NAN = "NaN";
private static final String NUMBER = "<NUMBER>";
private static final String EOF = "END-OF-STREAM";
private static final String EOL = "END-OF-LINE";
private SimpleWKTShapeParser() {}
public static Object parse(String wkt) throws IOException, ParseException {
return parseExpectedType(wkt, null);
}
public static Object parseExpectedType(String wkt, final ShapeType shapeType) throws IOException, ParseException {
try (StringReader reader = new StringReader(wkt)) {
StreamTokenizer tokenizer = new StreamTokenizer(reader);
tokenizer.resetSyntax();
tokenizer.wordChars('a', 'z');
tokenizer.wordChars('A', 'Z');
tokenizer.wordChars(128 + 32, 255);
tokenizer.wordChars('0', '9');
tokenizer.wordChars('-', '-');
tokenizer.wordChars('+', '+');
tokenizer.wordChars('.', '.');
tokenizer.whitespaceChars(0, ' ');
tokenizer.commentChar('#');
Object geometry = parseGeometry(tokenizer, shapeType);
checkEOF(tokenizer);
return geometry;
}
}
private static Object parseGeometry(StreamTokenizer stream, ShapeType shapeType) throws IOException, ParseException {
final ShapeType type = ShapeType.forName(nextWord(stream));
if (shapeType != null && shapeType != ShapeType.GEOMETRYCOLLECTION) {
if (type.wktName().equals(shapeType.wktName()) == false) {
throw new ParseException("Expected geometry type: [" + shapeType + "], but found: [" + type + "]", stream.lineno());
}
}
switch (type) {
case POINT:
return parsePoint(stream);
case MULTIPOINT:
return parseMultiPoint(stream);
case LINESTRING:
return parseLine(stream);
case MULTILINESTRING:
return parseMultiLine(stream);
case POLYGON:
return parsePolygon(stream);
case MULTIPOLYGON:
return parseMultiPolygon(stream);
case ENVELOPE:
return parseBBox(stream);
case GEOMETRYCOLLECTION:
return parseGeometryCollection(stream);
default:
throw new IllegalArgumentException("Unknown geometry type: " + type);
}
}
private static double[] parsePoint(StreamTokenizer stream) throws IOException, ParseException {
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
return null;
}
double[] pt = new double[]{nextNumber(stream), nextNumber(stream)};
if (isNumberNext(stream) == true) {
nextNumber(stream);
}
nextCloser(stream);
return pt;
}
private static void parseCoordinates(StreamTokenizer stream, ArrayList lats, ArrayList lons)
throws IOException, ParseException {
boolean isOpenParen = false;
if (isNumberNext(stream) || (isOpenParen = nextWord(stream).equals(LPAREN))) {
parseCoordinate(stream, lats, lons);
}
while (nextCloserOrComma(stream).equals(COMMA)) {
isOpenParen = false;
if (isNumberNext(stream) || (isOpenParen = nextWord(stream).equals(LPAREN))) {
parseCoordinate(stream, lats, lons);
}
if (isOpenParen && nextCloser(stream).equals(RPAREN) == false) {
throw new ParseException("expected: [" + RPAREN + "] but found: [" + tokenString(stream) + "]", stream.lineno());
}
}
if (isOpenParen && nextCloser(stream).equals(RPAREN) == false) {
throw new ParseException("expected: [" + RPAREN + "] but found: [" + tokenString(stream) + "]", stream.lineno());
}
}
private static void parseCoordinate(StreamTokenizer stream, ArrayList lats, ArrayList lons)
throws IOException, ParseException {
lons.add(nextNumber(stream));
lats.add(nextNumber(stream));
if (isNumberNext(stream)) {
nextNumber(stream);
}
}
private static double[][] parseMultiPoint(StreamTokenizer stream) throws IOException, ParseException {
String token = nextEmptyOrOpen(stream);
if (token.equals(EMPTY)) {
return null;
}
ArrayList<Double> lats = new ArrayList();
ArrayList<Double> lons = new ArrayList();
parseCoordinates(stream, lats, lons);
double[][] result = new double[lats.size()][2];
for (int i = 0; i < lats.size(); ++i) {
result[i] = new double[] {lons.get(i), lats.get(i)};
}
return result;
}
private static Line parseLine(StreamTokenizer stream) throws IOException, ParseException {
String token = nextEmptyOrOpen(stream);
if (token.equals(EMPTY)) {
return null;
}
ArrayList<Double> lats = new ArrayList();
ArrayList<Double> lons = new ArrayList();
parseCoordinates(stream, lats, lons);
return new Line(lats.stream().mapToDouble(i->i).toArray(), lons.stream().mapToDouble(i->i).toArray());
}
private static Line[] parseMultiLine(StreamTokenizer stream) throws IOException, ParseException {
String token = nextEmptyOrOpen(stream);
if (token.equals(EMPTY)) {
return null;
}
ArrayList<Line> lines = new ArrayList();
lines.add(parseLine(stream));
while (nextCloserOrComma(stream).equals(COMMA)) {
lines.add(parseLine(stream));
}
return lines.toArray(new Line[lines.size()]);
}
private static Polygon parsePolygonHole(StreamTokenizer stream) throws IOException, ParseException {
ArrayList<Double> lats = new ArrayList();
ArrayList<Double> lons = new ArrayList();
parseCoordinates(stream, lats, lons);
return new Polygon(lats.stream().mapToDouble(i->i).toArray(), lons.stream().mapToDouble(i->i).toArray());
}
private static Polygon parsePolygon(StreamTokenizer stream) throws IOException, ParseException {
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
return null;
}
nextOpener(stream);
ArrayList<Double> lats = new ArrayList();
ArrayList<Double> lons = new ArrayList();
parseCoordinates(stream, lats, lons);
ArrayList<Polygon> holes = new ArrayList<>();
while (nextCloserOrComma(stream).equals(COMMA)) {
holes.add(parsePolygonHole(stream));
}
if (holes.isEmpty() == false) {
return new Polygon(lats.stream().mapToDouble(i->i).toArray(), lons.stream().mapToDouble(i->i).toArray(), holes.toArray(new Polygon[holes.size()]));
}
return new Polygon(lats.stream().mapToDouble(i->i).toArray(), lons.stream().mapToDouble(i->i).toArray());
}
private static Polygon[] parseMultiPolygon(StreamTokenizer stream) throws IOException, ParseException {
String token = nextEmptyOrOpen(stream);
if (token.equals(EMPTY)) {
return null;
}
ArrayList<Polygon> polygons = new ArrayList();
polygons.add(parsePolygon(stream));
while (nextCloserOrComma(stream).equals(COMMA)) {
polygons.add(parsePolygon(stream));
}
return polygons.toArray(new Polygon[polygons.size()]);
}
private static Rectangle parseBBox(StreamTokenizer stream) throws IOException, ParseException {
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
return null;
}
double minLon = nextNumber(stream);
nextComma(stream);
double maxLon = nextNumber(stream);
nextComma(stream);
double maxLat = nextNumber(stream);
nextComma(stream);
double minLat = nextNumber(stream);
nextCloser(stream);
return new Rectangle(minLat, maxLat, minLon, maxLon);
}
private static Object[] parseGeometryCollection(StreamTokenizer stream) throws IOException, ParseException {
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
return null;
}
ArrayList<Object> geometries = new ArrayList<>();
geometries.add(parseGeometry(stream, ShapeType.GEOMETRYCOLLECTION));
while (nextCloserOrComma(stream).equals(COMMA)) {
geometries.add(parseGeometry(stream, null));
}
return geometries.toArray(new Object[geometries.size()]);
}
private static String nextWord(StreamTokenizer stream) throws ParseException, IOException {
switch (stream.nextToken()) {
case StreamTokenizer.TT_WORD:
final String word = stream.sval;
return word.equalsIgnoreCase(EMPTY) ? EMPTY : word;
case '(': return LPAREN;
case ')': return RPAREN;
case ',': return COMMA;
}
throw new ParseException("expected word but found: " + tokenString(stream), stream.lineno());
}
private static double nextNumber(StreamTokenizer stream) throws IOException, ParseException {
if (stream.nextToken() == StreamTokenizer.TT_WORD) {
if (stream.sval.equalsIgnoreCase(NAN)) {
return Double.NaN;
} else {
try {
return Double.parseDouble(stream.sval);
} catch (NumberFormatException e) {
throw new ParseException("invalid number found: " + stream.sval, stream.lineno());
}
}
}
throw new ParseException("expected number but found: " + tokenString(stream), stream.lineno());
}
private static String tokenString(StreamTokenizer stream) {
switch (stream.ttype) {
case StreamTokenizer.TT_WORD: return stream.sval;
case StreamTokenizer.TT_EOF: return EOF;
case StreamTokenizer.TT_EOL: return EOL;
case StreamTokenizer.TT_NUMBER: return NUMBER;
}
return "'" + (char)stream.ttype + "'";
}
private static boolean isNumberNext(StreamTokenizer stream) throws IOException {
final int type = stream.nextToken();
stream.pushBack();
return type == StreamTokenizer.TT_WORD;
}
private static String nextEmptyOrOpen(StreamTokenizer stream) throws IOException, ParseException {
final String next = nextWord(stream);
if (next.equals(EMPTY) || next.equals(LPAREN)) {
return next;
}
throw new ParseException("expected " + EMPTY + " or " + LPAREN
+ " but found: " + tokenString(stream), stream.lineno());
}
private static String nextCloser(StreamTokenizer stream) throws IOException, ParseException {
if (nextWord(stream).equals(RPAREN)) {
return RPAREN;
}
throw new ParseException("expected " + RPAREN + " but found: " + tokenString(stream), stream.lineno());
}
private static String nextComma(StreamTokenizer stream) throws IOException, ParseException {
if (nextWord(stream).equals(COMMA) == true) {
return COMMA;
}
throw new ParseException("expected " + COMMA + " but found: " + tokenString(stream), stream.lineno());
}
private static String nextOpener(StreamTokenizer stream) throws IOException, ParseException {
if (nextWord(stream).equals(LPAREN)) {
return LPAREN;
}
throw new ParseException("expected " + LPAREN + " but found: " + tokenString(stream), stream.lineno());
}
private static String nextCloserOrComma(StreamTokenizer stream) throws IOException, ParseException {
String token = nextWord(stream);
if (token.equals(COMMA) || token.equals(RPAREN)) {
return token;
}
throw new ParseException("expected " + COMMA + " or " + RPAREN
+ " but found: " + tokenString(stream), stream.lineno());
}
private static void checkEOF(StreamTokenizer stream) throws ParseException, IOException {
if (stream.nextToken() != StreamTokenizer.TT_EOF) {
throw new ParseException("expected end of WKT string but found additional text: "
+ tokenString(stream), stream.lineno());
}
}
public enum ShapeType {
POINT("point"),
MULTIPOINT("multipoint"),
LINESTRING("linestring"),
MULTILINESTRING("multilinestring"),
POLYGON("polygon"),
MULTIPOLYGON("multipolygon"),
GEOMETRYCOLLECTION("geometrycollection"),
ENVELOPE("envelope");
private final String shapeName;
private static Map<String, ShapeType> shapeTypeMap = new HashMap<>();
private static final String BBOX = "BBOX";
static {
for (ShapeType type : values()) {
shapeTypeMap.put(type.shapeName, type);
}
shapeTypeMap.put(ENVELOPE.wktName().toLowerCase(Locale.ROOT), ENVELOPE);
}
ShapeType(String shapeName) {
this.shapeName = shapeName;
}
protected String typename() {
return shapeName;
}
public String wktName() {
return this == ENVELOPE ? BBOX : this.shapeName;
}
public static ShapeType forName(String shapename) {
String typename = shapename.toLowerCase(Locale.ROOT);
for (ShapeType type : values()) {
if(type.shapeName.equals(typename)) {
return type;
}
}
throw new IllegalArgumentException("unknown geo_shape ["+shapename+"]");
}
}
}