use of org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.PreparedFilterGeometry in project geowave by locationtech.
the class SpatialTemporalFilterExpressionTest method testSpatialExpressions.
@Test
public void testSpatialExpressions() {
final DataTypeAdapter<TestType> adapter = new TestTypeBasicDataAdapter();
final TestType entry = new TestType(GeometryUtils.GEOMETRY_FACTORY.createPoint(new Coordinate(20, 20)), new Date(), "test");
final TestType entryNulls = new TestType(null, null, null);
final SpatialLiteral bboxLit = SpatialLiteral.of(new Envelope(0, 5, 0, 5));
final SpatialLiteral preparedBboxLit = SpatialLiteral.of(new Envelope(0, 5, 0, 5));
preparedBboxLit.prepare(GeometryUtils.getDefaultCRS());
final SpatialLiteral polygonLit = SpatialLiteral.of(GeometryUtils.GEOMETRY_FACTORY.createPolygon(new Coordinate[] { new Coordinate(0, 5), new Coordinate(0, 10), new Coordinate(5, 10), new Coordinate(5, 5), new Coordinate(0, 5) }));
final SpatialLiteral preparedPolygonLit = SpatialLiteral.of(GeometryUtils.GEOMETRY_FACTORY.createPolygon(new Coordinate[] { new Coordinate(0, 5), new Coordinate(0, 10), new Coordinate(5, 10), new Coordinate(5, 5), new Coordinate(0, 5) }));
preparedPolygonLit.prepare(GeometryUtils.getDefaultCRS());
final SpatialLiteral referencedBboxLit = SpatialLiteral.of(new ReferencedEnvelope(0, 25, 0, 25, GeometryUtils.getDefaultCRS()));
final SpatialLiteral referencedBboxLit2 = SpatialLiteral.of(new ReferencedEnvelope(4, 25, 4, 25, GeometryUtils.getDefaultCRS()));
final SpatialFieldValue spatialField = SpatialFieldValue.of("geom");
// Test comparisons
assertTrue(bboxLit.isEqualTo(GeometryUtils.GEOMETRY_FACTORY.createPolygon(new Coordinate[] { new Coordinate(0, 0), new Coordinate(0, 5), new Coordinate(5, 5), new Coordinate(5, 0), new Coordinate(0, 0) })).evaluate(adapter, entry));
assertTrue(bboxLit.isEqualTo(preparedBboxLit).evaluate(adapter, entry));
assertTrue(preparedBboxLit.isEqualTo(bboxLit).evaluate(adapter, entry));
assertTrue(bboxLit.isEqualTo(new Envelope(0, 5, 0, 5)).evaluate(adapter, entry));
assertFalse(spatialField.isEqualTo(referencedBboxLit).evaluate(adapter, entry));
assertFalse(spatialField.isEqualTo(null).evaluate(adapter, entry));
assertTrue(spatialField.isEqualTo(null).evaluate(adapter, entryNulls));
assertFalse(spatialField.isEqualTo(bboxLit).evaluate(adapter, entryNulls));
assertFalse(spatialField.isNull().evaluate(adapter, entry));
assertTrue(spatialField.isNull().evaluate(adapter, entryNulls));
assertFalse(bboxLit.isNotEqualTo(new ReferencedEnvelope(0, 5, 0, 5, GeometryUtils.getDefaultCRS())).evaluate(adapter, entry));
assertFalse(bboxLit.isNotEqualTo(preparedBboxLit).evaluate(adapter, entry));
assertTrue(bboxLit.isNotEqualTo(polygonLit).evaluate(adapter, entry));
assertFalse(polygonLit.isNotEqualTo(polygonLit).evaluate(adapter, entry));
assertTrue(spatialField.isNotEqualTo(bboxLit).evaluate(adapter, entryNulls));
assertFalse(spatialField.isNotEqualTo(null).evaluate(adapter, entryNulls));
assertTrue(spatialField.isNotEqualTo(null).evaluate(adapter, entry));
assertTrue(SpatialLiteral.of(null).isNull().evaluate(adapter, entry));
// Preparing null or already prepared geometries should not fail
preparedBboxLit.prepare(GeometryUtils.getDefaultCRS());
SpatialLiteral.of(null).prepare(GeometryUtils.getDefaultCRS());
try {
SpatialLiteral.of("invalid");
fail();
} catch (RuntimeException e) {
// expected
}
// Test functions
assertTrue(spatialField.bbox(19, 19, 21, 21).evaluate(adapter, entry));
assertFalse(spatialField.bbox(0, 0, 5, 5).evaluate(adapter, entry));
assertTrue(bboxLit.touches(polygonLit).evaluate(adapter, entry));
assertTrue(preparedBboxLit.touches(polygonLit).evaluate(adapter, entry));
assertTrue(preparedBboxLit.touches(preparedPolygonLit).evaluate(adapter, entry));
assertTrue(bboxLit.touches(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(spatialField.touches(polygonLit).evaluate(adapter, entry));
assertFalse(spatialField.touches(polygonLit).evaluate(adapter, entryNulls));
assertFalse(polygonLit.touches(spatialField).evaluate(adapter, entryNulls));
assertFalse(spatialField.touches(preparedPolygonLit).evaluate(adapter, entry));
assertTrue(bboxLit.intersects(referencedBboxLit).evaluate(adapter, entry));
assertTrue(preparedBboxLit.intersects(polygonLit).evaluate(adapter, entry));
assertTrue(preparedBboxLit.intersects(preparedPolygonLit).evaluate(adapter, entry));
assertTrue(bboxLit.intersects(preparedPolygonLit).evaluate(adapter, entry));
assertTrue(spatialField.intersects(referencedBboxLit).evaluate(adapter, entry));
assertFalse(spatialField.intersects(referencedBboxLit).evaluate(adapter, entryNulls));
assertFalse(polygonLit.intersects(spatialField).evaluate(adapter, entryNulls));
assertFalse(spatialField.intersects(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(bboxLit.disjoint(referencedBboxLit).evaluate(adapter, entry));
assertFalse(preparedBboxLit.disjoint(polygonLit).evaluate(adapter, entry));
assertFalse(preparedBboxLit.disjoint(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(bboxLit.disjoint(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(spatialField.disjoint(referencedBboxLit).evaluate(adapter, entry));
assertFalse(spatialField.disjoint(referencedBboxLit).evaluate(adapter, entryNulls));
assertFalse(polygonLit.disjoint(spatialField).evaluate(adapter, entryNulls));
assertTrue(spatialField.disjoint(preparedPolygonLit).evaluate(adapter, entry));
assertTrue(bboxLit.disjoint(spatialField).evaluate(adapter, entry));
assertFalse(bboxLit.contains(referencedBboxLit).evaluate(adapter, entry));
assertTrue(referencedBboxLit.contains(bboxLit).evaluate(adapter, entry));
assertFalse(preparedBboxLit.contains(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(bboxLit.contains(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(spatialField.contains(referencedBboxLit).evaluate(adapter, entry));
assertFalse(spatialField.contains(referencedBboxLit).evaluate(adapter, entryNulls));
assertFalse(polygonLit.contains(spatialField).evaluate(adapter, entryNulls));
assertFalse(spatialField.contains(preparedPolygonLit).evaluate(adapter, entry));
assertTrue(referencedBboxLit.contains(spatialField).evaluate(adapter, entry));
assertFalse(bboxLit.crosses(referencedBboxLit).evaluate(adapter, entry));
assertFalse(referencedBboxLit.crosses(bboxLit).evaluate(adapter, entry));
assertFalse(preparedBboxLit.crosses(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(bboxLit.crosses(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(spatialField.crosses(referencedBboxLit).evaluate(adapter, entry));
assertFalse(spatialField.crosses(referencedBboxLit).evaluate(adapter, entryNulls));
assertFalse(polygonLit.crosses(spatialField).evaluate(adapter, entryNulls));
assertFalse(spatialField.crosses(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(referencedBboxLit.crosses(spatialField).evaluate(adapter, entry));
assertTrue(SpatialLiteral.of(GeometryUtils.GEOMETRY_FACTORY.createLineString(new Coordinate[] { new Coordinate(0, 0), new Coordinate(5, 5) })).crosses(SpatialLiteral.of(GeometryUtils.GEOMETRY_FACTORY.createLineString(new Coordinate[] { new Coordinate(5, 0), new Coordinate(0, 5) }))).evaluate(adapter, entry));
assertTrue(bboxLit.overlaps(referencedBboxLit2).evaluate(adapter, entry));
assertTrue(referencedBboxLit2.overlaps(bboxLit).evaluate(adapter, entry));
assertFalse(preparedBboxLit.overlaps(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(bboxLit.overlaps(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(spatialField.overlaps(referencedBboxLit).evaluate(adapter, entry));
assertFalse(spatialField.overlaps(referencedBboxLit).evaluate(adapter, entryNulls));
assertFalse(polygonLit.overlaps(spatialField).evaluate(adapter, entryNulls));
assertFalse(spatialField.overlaps(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(referencedBboxLit.overlaps(spatialField).evaluate(adapter, entry));
assertTrue(bboxLit.within(referencedBboxLit).evaluate(adapter, entry));
assertFalse(referencedBboxLit.within(bboxLit).evaluate(adapter, entry));
assertFalse(preparedBboxLit.within(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(bboxLit.within(preparedPolygonLit).evaluate(adapter, entry));
assertTrue(spatialField.within(referencedBboxLit).evaluate(adapter, entry));
assertFalse(spatialField.within(referencedBboxLit).evaluate(adapter, entryNulls));
assertFalse(polygonLit.within(spatialField).evaluate(adapter, entryNulls));
assertFalse(spatialField.within(preparedPolygonLit).evaluate(adapter, entry));
assertFalse(referencedBboxLit.within(spatialField).evaluate(adapter, entry));
// Test CRS transforms
// This looks like it should be true, but spatial expressions need to be prepared for the query,
// the spatial field could be any CRS because it would be determined by the index and not the
// field descriptor
assertFalse(spatialField.bbox(2115070, 2154935, 2337709, 2391878, GeometryUtils.decodeCRS("EPSG:3857")).evaluate(adapter, entry));
// This looks like it should be false, but the expression hasn't been prepared for the query.
assertTrue(spatialField.bbox(0, 0, 556597, 557305, GeometryUtils.decodeCRS("EPSG:3857")).evaluate(adapter, entry));
try {
bboxLit.isEqualTo(5).evaluate(adapter, entry);
fail();
} catch (RuntimeException e) {
// expected
}
try {
bboxLit.isNotEqualTo(5).evaluate(adapter, entry);
fail();
} catch (RuntimeException e) {
// expected
}
// Test serialization
byte[] bytes = PersistenceUtils.toBinary(spatialField.bbox(-5, -8, 5, 8));
final BBox bbox = (BBox) PersistenceUtils.fromBinary(bytes);
assertTrue(bbox.getExpression1() instanceof SpatialFieldValue);
assertEquals("geom", ((SpatialFieldValue) bbox.getExpression1()).getFieldName());
assertTrue(bbox.getExpression2() instanceof SpatialLiteral);
assertTrue(((SpatialLiteral) bbox.getExpression2()).getValue() instanceof UnpreparedFilterGeometry);
assertTrue(((UnpreparedFilterGeometry) ((SpatialLiteral) bbox.getExpression2()).getValue()).getGeometry().equalsTopo(GeometryUtils.GEOMETRY_FACTORY.toGeometry(new Envelope(-5, 5, -8, 8))));
bytes = PersistenceUtils.toBinary(spatialField.crosses(bboxLit));
final Crosses crosses = (Crosses) PersistenceUtils.fromBinary(bytes);
assertTrue(crosses.getExpression1() instanceof SpatialFieldValue);
assertEquals("geom", ((SpatialFieldValue) crosses.getExpression1()).getFieldName());
assertTrue(crosses.getExpression2() instanceof SpatialLiteral);
assertTrue(((SpatialLiteral) crosses.getExpression2()).getValue() instanceof UnpreparedFilterGeometry);
assertTrue(((UnpreparedFilterGeometry) ((SpatialLiteral) crosses.getExpression2()).getValue()).getGeometry().equalsTopo(GeometryUtils.GEOMETRY_FACTORY.toGeometry(new Envelope(0, 5, 0, 5))));
bytes = PersistenceUtils.toBinary(spatialField.disjoint(preparedBboxLit));
final Disjoint disjoint = (Disjoint) PersistenceUtils.fromBinary(bytes);
assertTrue(disjoint.getExpression1() instanceof SpatialFieldValue);
assertEquals("geom", ((SpatialFieldValue) disjoint.getExpression1()).getFieldName());
assertTrue(disjoint.getExpression2() instanceof SpatialLiteral);
assertTrue(((SpatialLiteral) disjoint.getExpression2()).getValue() instanceof PreparedFilterGeometry);
assertTrue(((PreparedFilterGeometry) ((SpatialLiteral) disjoint.getExpression2()).getValue()).getGeometry().equalsTopo(GeometryUtils.GEOMETRY_FACTORY.toGeometry(new Envelope(0, 5, 0, 5))));
bytes = PersistenceUtils.toBinary(spatialField.intersects(preparedBboxLit));
final Intersects intersects = (Intersects) PersistenceUtils.fromBinary(bytes);
assertTrue(intersects.getExpression1() instanceof SpatialFieldValue);
assertEquals("geom", ((SpatialFieldValue) intersects.getExpression1()).getFieldName());
assertTrue(intersects.getExpression2() instanceof SpatialLiteral);
assertTrue(((SpatialLiteral) intersects.getExpression2()).getValue() instanceof PreparedFilterGeometry);
assertTrue(((PreparedFilterGeometry) ((SpatialLiteral) intersects.getExpression2()).getValue()).getGeometry().equalsTopo(GeometryUtils.GEOMETRY_FACTORY.toGeometry(new Envelope(0, 5, 0, 5))));
bytes = PersistenceUtils.toBinary(spatialField.overlaps(preparedBboxLit));
final Overlaps overlaps = (Overlaps) PersistenceUtils.fromBinary(bytes);
assertTrue(overlaps.getExpression1() instanceof SpatialFieldValue);
assertEquals("geom", ((SpatialFieldValue) overlaps.getExpression1()).getFieldName());
assertTrue(overlaps.getExpression2() instanceof SpatialLiteral);
assertTrue(((SpatialLiteral) overlaps.getExpression2()).getValue() instanceof PreparedFilterGeometry);
assertTrue(((PreparedFilterGeometry) ((SpatialLiteral) overlaps.getExpression2()).getValue()).getGeometry().equalsTopo(GeometryUtils.GEOMETRY_FACTORY.toGeometry(new Envelope(0, 5, 0, 5))));
bytes = PersistenceUtils.toBinary(spatialField.contains(SpatialLiteral.of(null)));
final SpatialContains contains = (SpatialContains) PersistenceUtils.fromBinary(bytes);
assertTrue(contains.getExpression1() instanceof SpatialFieldValue);
assertEquals("geom", ((SpatialFieldValue) contains.getExpression1()).getFieldName());
assertTrue(contains.getExpression2() instanceof SpatialLiteral);
assertNull(((SpatialLiteral) contains.getExpression2()).getValue());
bytes = PersistenceUtils.toBinary(spatialField.touches(preparedBboxLit));
final Touches touches = (Touches) PersistenceUtils.fromBinary(bytes);
assertTrue(touches.getExpression1() instanceof SpatialFieldValue);
assertEquals("geom", ((SpatialFieldValue) touches.getExpression1()).getFieldName());
assertTrue(touches.getExpression2() instanceof SpatialLiteral);
assertTrue(((SpatialLiteral) touches.getExpression2()).getValue() instanceof PreparedFilterGeometry);
assertTrue(((PreparedFilterGeometry) ((SpatialLiteral) touches.getExpression2()).getValue()).getGeometry().equalsTopo(GeometryUtils.GEOMETRY_FACTORY.toGeometry(new Envelope(0, 5, 0, 5))));
bytes = PersistenceUtils.toBinary(spatialField.within(preparedBboxLit));
final Within within = (Within) PersistenceUtils.fromBinary(bytes);
assertTrue(within.getExpression1() instanceof SpatialFieldValue);
assertEquals("geom", ((SpatialFieldValue) within.getExpression1()).getFieldName());
assertTrue(within.getExpression2() instanceof SpatialLiteral);
assertTrue(((SpatialLiteral) within.getExpression2()).getValue() instanceof PreparedFilterGeometry);
assertTrue(((PreparedFilterGeometry) ((SpatialLiteral) within.getExpression2()).getValue()).getGeometry().equalsTopo(GeometryUtils.GEOMETRY_FACTORY.toGeometry(new Envelope(0, 5, 0, 5))));
bytes = PersistenceUtils.toBinary(spatialField.isEqualTo(preparedBboxLit));
final SpatialEqualTo equalTo = (SpatialEqualTo) PersistenceUtils.fromBinary(bytes);
assertTrue(equalTo.getExpression1() instanceof SpatialFieldValue);
assertEquals("geom", ((SpatialFieldValue) equalTo.getExpression1()).getFieldName());
assertTrue(equalTo.getExpression2() instanceof SpatialLiteral);
assertTrue(((SpatialLiteral) equalTo.getExpression2()).getValue() instanceof PreparedFilterGeometry);
assertTrue(((PreparedFilterGeometry) ((SpatialLiteral) equalTo.getExpression2()).getValue()).getGeometry().equalsTopo(GeometryUtils.GEOMETRY_FACTORY.toGeometry(new Envelope(0, 5, 0, 5))));
bytes = PersistenceUtils.toBinary(spatialField.isNotEqualTo(preparedBboxLit));
final SpatialNotEqualTo notEqualTo = (SpatialNotEqualTo) PersistenceUtils.fromBinary(bytes);
assertTrue(notEqualTo.getExpression1() instanceof SpatialFieldValue);
assertEquals("geom", ((SpatialFieldValue) notEqualTo.getExpression1()).getFieldName());
assertTrue(notEqualTo.getExpression2() instanceof SpatialLiteral);
assertTrue(((SpatialLiteral) notEqualTo.getExpression2()).getValue() instanceof PreparedFilterGeometry);
assertTrue(((PreparedFilterGeometry) ((SpatialLiteral) notEqualTo.getExpression2()).getValue()).getGeometry().equalsTopo(GeometryUtils.GEOMETRY_FACTORY.toGeometry(new Envelope(0, 5, 0, 5))));
}
Aggregations