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

Add support for search within polygon Geoshape #441

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,39 @@ public static final Geoshape box(final double southWestLatitude, final double so
return box((float)southWestLatitude,(float)southWestLongitude,(float)northEastLatitude,(float)northEastLongitude);
}

/**
* Constructs a new polygon shape which is identified by its coordinates
* @param coordinates
* @return
*/
public static final Geoshape polygon(final float ... coordinates) {
Preconditions.checkArgument(coordinates.length % 2 == 0, "Odd number of coordinates provided");
Preconditions.checkArgument(coordinates.length >= 6, "Too few coordinate pairs provided");
float[] latitudes = new float[coordinates.length / 2];
float[] longitudes = new float[coordinates.length / 2];
for (int i = 0; i < coordinates.length; i++) {
if (i % 2 == 0) {
latitudes[i / 2] = coordinates[i];
} else {
longitudes[i / 2] = coordinates[i];
}
}
return new Geoshape(new float[][]{ latitudes, longitudes });
}

/**
* Constructs a new polygon shape which is identified by its coordinates
* @param coordinates
* @return
*/
public static final Geoshape polygon(final double ... coordinates) {
float[] floatCoordinates = new float[coordinates.length];
for (int i = 0; i < coordinates.length; i++) {
floatCoordinates[i] = (float)coordinates[i];
}
return polygon(floatCoordinates);
}

/**
* Whether the given coordinates mark a point on earth.
* @param latitude
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,13 @@ public FilterBuilder getFilter(Condition<?> condition) {
Geoshape.Point southwest = shape.getPoint(0);
Geoshape.Point northeast = shape.getPoint(1);
return FilterBuilders.geoBoundingBoxFilter(key).bottomRight(southwest.getLatitude(), northeast.getLongitude()).topLeft(northeast.getLatitude(), southwest.getLongitude());
} else if (shape.getType() == Geoshape.Type.POLYGON) {
GeoPolygonFilterBuilder polygonFilter = FilterBuilders.geoPolygonFilter(key);
for (int i = 0; i < shape.size(); i++) {
Geoshape.Point point = shape.getPoint(i);
polygonFilter.addPoint(point.getLatitude(), point.getLongitude());
}
return polygonFilter;
} else
throw new IllegalArgumentException("Unsupported or invalid search shape type: " + shape.getType());
} else throw new IllegalArgumentException("Unsupported type: " + value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ private void storeTest(String... stores) throws Exception {
assertEquals(2, result.size());
assertEquals(ImmutableSet.of("doc1", "doc2"), ImmutableSet.copyOf(result));

result = tx.query(new IndexQuery(store, PredicateCondition.of("location", Geo.WITHIN, Geoshape.polygon(48.5,0.0, 47.5,-0.5, 47.5,0.5))));
assertEquals(1, result.size());
assertEquals(ImmutableSet.of("doc1"), ImmutableSet.copyOf(result));

result = tx.query(new IndexQuery(store, And.of(PredicateCondition.of("text", Text.CONTAINS, "tomorrow"), PredicateCondition.of("location", Geo.WITHIN, Geoshape.circle(48.5, 0.5, 200.00)))));
assertEquals(ImmutableSet.of("doc2"), ImmutableSet.copyOf(result));

Expand Down