Skip to content

Commit

Permalink
[Geo] Add Well Known Text (WKT) Parsing Support to ShapeBuilders
Browse files Browse the repository at this point in the history
This commit adds WKT support to Geo ShapeBuilders.

This supports the following format:

POINT (30 10)
LINESTRING (30 10, 10 30, 40 40)
BBOX (-10, 10, 10, -10)
POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))
POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10), (20 30, 35 35, 30 20, 20 30))
MULTIPOINT ((10 40), (40 30), (20 20), (30 10))
MULTIPOINT (10 40, 40 30, 20 20, 30 10)
MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))
MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))
MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35), (30 20, 20 15, 20 25, 30 20)))
GEOMETRYCOLLECTION (POINT (30 10), MULTIPOINT ((10 40), (40 30), (20 20), (30 10)))

closes #9120
  • Loading branch information
nknize committed Dec 5, 2017
1 parent 5b03e3b commit 8bcf539
Show file tree
Hide file tree
Showing 14 changed files with 933 additions and 56 deletions.
17 changes: 17 additions & 0 deletions core/src/main/java/org/elasticsearch/common/geo/GeoShapeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ CoordinateNode validate(CoordinateNode coordinates, boolean coerce) {
}
return coordinates;
}

@Override
public String wktName() {
return BBOX;
}
},
CIRCLE("circle") {
@Override
Expand Down Expand Up @@ -273,11 +278,13 @@ CoordinateNode validate(CoordinateNode coordinates, boolean coerce) {

private final String shapename;
private static Map<String, GeoShapeType> shapeTypeMap = new HashMap<>();
private static final String BBOX = "BBOX";

static {
for (GeoShapeType type : values()) {
shapeTypeMap.put(type.shapename, type);
}
shapeTypeMap.put(ENVELOPE.wktName().toLowerCase(Locale.ROOT), ENVELOPE);
}

GeoShapeType(String shapename) {
Expand All @@ -300,6 +307,11 @@ public abstract ShapeBuilder getBuilder(CoordinateNode coordinates, DistanceUnit
ShapeBuilder.Orientation orientation, boolean coerce);
abstract CoordinateNode validate(CoordinateNode coordinates, boolean coerce);

/** wkt shape name */
public String wktName() {
return this.shapename;
}

public static List<Entry> getShapeWriteables() {
List<Entry> namedWriteables = new ArrayList<>();
namedWriteables.add(new Entry(ShapeBuilder.class, PointBuilder.TYPE.shapeName(), PointBuilder::new));
Expand All @@ -313,4 +325,9 @@ public static List<Entry> getShapeWriteables() {
namedWriteables.add(new Entry(ShapeBuilder.class, GeometryCollectionBuilder.TYPE.shapeName(), GeometryCollectionBuilder::new));
return namedWriteables;
}

@Override
public String toString() {
return this.shapename;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ public GeoShapeType type() {
return TYPE;
}

@Override
public String toWKT() {
throw new UnsupportedOperationException("The WKT spec does not support CIRCLE geometry");
}

@Override
public int hashCode() {
return Objects.hash(center, radius, unit.ordinal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.common.geo.builders;

import org.elasticsearch.common.geo.GeoShapeType;
import org.elasticsearch.common.geo.parsers.GeoWKTParser;
import org.elasticsearch.common.geo.parsers.ShapeParser;
import org.locationtech.spatial4j.shape.Rectangle;
import com.vividsolutions.jts.geom.Coordinate;
Expand Down Expand Up @@ -70,6 +71,28 @@ public Coordinate bottomRight() {
return this.bottomRight;
}

@Override
protected StringBuilder contentToWKT() {
StringBuilder sb = new StringBuilder();

sb.append(GeoWKTParser.LPAREN);
// minX, maxX, maxY, minY
sb.append(topLeft.x);
sb.append(GeoWKTParser.COMMA);
sb.append(GeoWKTParser.SPACE);
sb.append(bottomRight.x);
sb.append(GeoWKTParser.COMMA);
sb.append(GeoWKTParser.SPACE);
// TODO support Z??
sb.append(topLeft.y);
sb.append(GeoWKTParser.COMMA);
sb.append(GeoWKTParser.SPACE);
sb.append(bottomRight.y);
sb.append(GeoWKTParser.RPAREN);

return sb;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.common.geo.GeoShapeType;
import org.elasticsearch.common.geo.parsers.ShapeParser;
import org.elasticsearch.common.geo.parsers.GeoWKTParser;
import org.locationtech.spatial4j.shape.Shape;

import org.elasticsearch.ElasticsearchException;
Expand Down Expand Up @@ -136,6 +137,23 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder.endObject();
}

@Override
protected StringBuilder contentToWKT() {
StringBuilder sb = new StringBuilder();
if (shapes.isEmpty()) {
sb.append(GeoWKTParser.EMPTY);
} else {
sb.append(GeoWKTParser.LPAREN);
sb.append(shapes.get(0).toWKT());
for (int i = 1; i < shapes.size(); ++i) {
sb.append(GeoWKTParser.COMMA);
sb.append(shapes.get(i).toWKT());
}
sb.append(GeoWKTParser.RPAREN);
}
return sb;
}

@Override
public GeoShapeType type() {
return TYPE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package org.elasticsearch.common.geo.builders;

import org.elasticsearch.common.geo.GeoShapeType;
import org.elasticsearch.common.geo.parsers.GeoWKTParser;
import org.elasticsearch.common.geo.parsers.ShapeParser;
import org.locationtech.spatial4j.shape.Shape;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.LineString;
Expand Down Expand Up @@ -82,6 +82,25 @@ public GeoShapeType type() {
return TYPE;
}

@Override
protected StringBuilder contentToWKT() {
final StringBuilder sb = new StringBuilder();
if (lines.isEmpty()) {
sb.append(GeoWKTParser.EMPTY);
} else {
sb.append(GeoWKTParser.LPAREN);
if (lines.size() > 0) {
sb.append(ShapeBuilder.coordinateListToWKT(lines.get(0).coordinates));
}
for (int i = 1; i < lines.size(); ++i) {
sb.append(GeoWKTParser.COMMA);
sb.append(ShapeBuilder.coordinateListToWKT(lines.get(i).coordinates));
}
sb.append(GeoWKTParser.RPAREN);
}
return sb;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.common.geo.GeoShapeType;
import org.elasticsearch.common.geo.parsers.ShapeParser;
import org.elasticsearch.common.geo.parsers.GeoWKTParser;
import org.locationtech.spatial4j.shape.Shape;
import com.vividsolutions.jts.geom.Coordinate;

Expand Down Expand Up @@ -101,6 +102,37 @@ public List<PolygonBuilder> polygons() {
return polygons;
}

private static String polygonCoordinatesToWKT(PolygonBuilder polygon) {
StringBuilder sb = new StringBuilder();
sb.append(GeoWKTParser.LPAREN);
sb.append(ShapeBuilder.coordinateListToWKT(polygon.shell().coordinates));
for (LineStringBuilder hole : polygon.holes()) {
sb.append(GeoWKTParser.COMMA);
sb.append(ShapeBuilder.coordinateListToWKT(hole.coordinates));
}
sb.append(GeoWKTParser.RPAREN);
return sb.toString();
}

@Override
protected StringBuilder contentToWKT() {
final StringBuilder sb = new StringBuilder();
if (polygons.isEmpty()) {
sb.append(GeoWKTParser.EMPTY);
} else {
sb.append(GeoWKTParser.LPAREN);
if (polygons.size() > 0) {
sb.append(polygonCoordinatesToWKT(polygons.get(0)));
}
for (int i = 1; i < polygons.size(); ++i) {
sb.append(GeoWKTParser.COMMA);
sb.append(polygonCoordinatesToWKT(polygons.get(i)));
}
sb.append(GeoWKTParser.RPAREN);
}
return sb;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,19 @@ private static void translate(Coordinate[] points) {
}
}

@Override
protected StringBuilder contentToWKT() {
StringBuilder sb = new StringBuilder();
sb.append('(');
sb.append(ShapeBuilder.coordinateListToWKT(shell.coordinates));
for (LineStringBuilder hole : holes) {
sb.append(", ");
sb.append(ShapeBuilder.coordinateListToWKT(hole.coordinates));
}
sb.append(')');
return sb;
}

@Override
public int hashCode() {
return Objects.hash(shell, holes, orientation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.Assertions;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.geo.GeoShapeType;
import org.elasticsearch.common.geo.parsers.GeoWKTParser;
import org.elasticsearch.common.io.stream.NamedWriteable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
Expand Down Expand Up @@ -339,6 +340,47 @@ public String toString() {
}
}

protected StringBuilder contentToWKT() {
return coordinateListToWKT(this.coordinates);
}

public String toWKT() {
StringBuilder sb = new StringBuilder();
sb.append(type().wktName());
sb.append(GeoWKTParser.SPACE);
sb.append(contentToWKT());
return sb.toString();
}

protected static StringBuilder coordinateListToWKT(final List<Coordinate> coordinates) {
final StringBuilder sb = new StringBuilder();

if (coordinates.isEmpty()) {
sb.append(GeoWKTParser.EMPTY);
} else {
// walk through coordinates:
sb.append(GeoWKTParser.LPAREN);
sb.append(coordinateToWKT(coordinates.get(0)));
for (int i = 1; i < coordinates.size(); ++i) {
sb.append(GeoWKTParser.COMMA);
sb.append(GeoWKTParser.SPACE);
sb.append(coordinateToWKT(coordinates.get(i)));
}
sb.append(GeoWKTParser.RPAREN);
}

return sb;
}

private static String coordinateToWKT(final Coordinate coordinate) {
final StringBuilder sb = new StringBuilder();
sb.append(coordinate.x + GeoWKTParser.SPACE + coordinate.y);
if (Double.isNaN(coordinate.z) == false) {
sb.append(GeoWKTParser.SPACE + coordinate.z);
}
return sb.toString();
}

protected static final IntersectionOrder INTERSECTION_ORDER = new IntersectionOrder();

private static final class IntersectionOrder implements Comparator<Edge> {
Expand Down
Loading

0 comments on commit 8bcf539

Please sign in to comment.