Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Geo] Add Well Known Text (WKT) Parsing Support to ShapeBuilders #27417

Merged
merged 1 commit into from
Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, this will be lowercase, no? The examples in the issue and PR are all uppercase, does this matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the WKT spec, casing doesn't matter

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking if we also support Z on the parsing side?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet... will be handled in #25738

}
return sb.toString();
}

protected static final IntersectionOrder INTERSECTION_ORDER = new IntersectionOrder();

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