use of org.opengis.filter.expression.Literal in project ddf by codice.
the class TwitterFilterVisitor method handleTemporal.
private void handleTemporal(BinaryTemporalOperator filter) {
Literal literalWrapper = (Literal) filter.getExpression2();
LOGGER.debug("literalWrapper.getValue() = {}", literalWrapper.getValue());
Object literal = literalWrapper.evaluate(null);
if (literal instanceof Period) {
Period period = (Period) literal;
// Extract the start and end dates from the filter
Date start = period.getBeginning().getPosition().getDate();
Date end = period.getEnding().getPosition().getDate();
temporalSearch = new TemporalFilter(start, end);
filters.add(filter);
} else if (literal instanceof PeriodDuration) {
DefaultPeriodDuration duration = (DefaultPeriodDuration) literal;
// Extract the start and end dates from the filter
Date end = Calendar.getInstance().getTime();
Date start = new Date(end.getTime() - duration.getTimeInMillis());
temporalSearch = new TemporalFilter(start, end);
filters.add(filter);
}
}
use of org.opengis.filter.expression.Literal in project ddf by codice.
the class OpenSearchQueryTest method testPolygonSpatialFilter.
@Test
public void testPolygonSpatialFilter() throws Exception {
String latLon = "0,10,0,30,20,30,20,10,0,10";
String lonLat = "10,0,30,0,30,20,10,20,10,0";
OpenSearchQuery query = new OpenSearchQuery(null, 0, 10, "relevance", "desc", 30000, FILTER_BUILDER);
query.addPolygonSpatialFilter(latLon);
Filter filter = query.getFilter();
// String filterXml = getFilterAsXml( filter );
VerificationVisitor verificationVisitor = new VerificationVisitor();
filter.accept(verificationVisitor, null);
HashMap<String, FilterStatus> map = (HashMap<String, FilterStatus>) verificationVisitor.getMap();
printFilterStatusMap(map);
// List<Filter> filters = getFilters( map, ContainsImpl.class.getName() );
List<Filter> filters = getFilters(map, IntersectsImpl.class.getName());
assertEquals(1, filters.size());
// ContainsImpl containsFilter = (ContainsImpl) filters.get( 0 );
IntersectsImpl containsFilter = (IntersectsImpl) filters.get(0);
// The geometric point is wrapped in a <Literal> element, so have to
// get geometry expression as literal and then evaluate it to get the
// geometry.
// Example:
// <ogc:Literal>org.geotools.geometry.jts.spatialschema.geometry.primitive.SurfaceImpl@64a7c45e</ogc:Literal>
Literal literalWrapper = (Literal) containsFilter.getExpression2();
// Luckily we know what type the geometry expression should be, so we can cast it
SurfaceImpl polygon = (SurfaceImpl) literalWrapper.evaluate(null);
// WKT is lon/lat, polygon is lat/lon
String[] expectedCoords = lonLat.split(",");
Coordinate[] coords = polygon.getJTSGeometry().getCoordinates();
int i = 0;
for (Coordinate coord : coords) {
LOGGER.debug("coord {}: x = {}, y = {}", (i + 1), coord.x, coord.y);
int index = i * 2 + 1;
assertEquals(Double.parseDouble(expectedCoords[index - 1]), coord.x, DOUBLE_DELTA);
assertEquals(Double.parseDouble(expectedCoords[index]), coord.y, DOUBLE_DELTA);
i++;
}
}
use of org.opengis.filter.expression.Literal in project ddf by codice.
the class OpenSearchQueryTest method verifyTemporalFilter.
private void verifyTemporalFilter(Filter filter, String expectedStartDate, String expectedEndDate, boolean reformatDates) throws Exception {
DuringImpl duringFilter = (DuringImpl) filter;
// The TOverlaps temporal range is wrapped in a <Literal> element, so have to
// get expression as literal and then evaluate it to get the
// temporal data.
// Example:
// <ogc:TOverlaps>
// <ogc:PropertyName>modifiedDate</ogc:PropertyName>
// <ogc:Literal>Period:
// begin:Instant:
// position:Position:
// position:Tue Oct 04 05:48:27 MST 2011
//
//
// end:Instant:
// position:Position:
// position:Tue Oct 04 06:18:27 MST 2011
//
//
// </ogc:Literal>
// </ogc:TOverlaps>
Literal literalWrapper = (Literal) duringFilter.getExpression2();
// Luckily we know what type the temporal expression should be, so we can cast it
Period period = (Period) literalWrapper.evaluate(null);
// Extract the start and end dates from the filter
Date start = period.getBeginning().getPosition().getDate();
Date end = period.getEnding().getPosition().getDate();
if (reformatDates) {
String formattedStartDate = reformatDate(start);
String formattedEndDate = reformatDate(end);
LOGGER.debug("startDate = {}", formattedStartDate);
LOGGER.debug("endDate = {}", formattedEndDate);
assertEquals(expectedStartDate, formattedStartDate);
assertEquals(expectedEndDate, formattedEndDate);
} else {
assertEquals(expectedStartDate, start.toString());
assertEquals(expectedEndDate, end.toString());
}
}
use of org.opengis.filter.expression.Literal in project ddf by codice.
the class OpenSearchQueryTest method testSpatialDistanceFilter.
@Test
public void testSpatialDistanceFilter() throws Exception {
String lon = "10";
String lat = "20";
String radius = "5000";
OpenSearchQuery query = new OpenSearchQuery(null, 0, 10, "relevance", "desc", 30000, FILTER_BUILDER);
query.addSpatialDistanceFilter(lon, lat, radius);
Filter filter = query.getFilter();
// String filterXml = getFilterAsXml( filter );
VerificationVisitor verificationVisitor = new VerificationVisitor();
filter.accept(verificationVisitor, null);
HashMap<String, FilterStatus> map = (HashMap<String, FilterStatus>) verificationVisitor.getMap();
List<Filter> filters = getFilters(map, DWithinImpl.class.getName());
assertEquals(1, filters.size());
DWithinImpl dwithinFilter = (DWithinImpl) filters.get(0);
// The geometric point is wrapped in a <Literal> element, so have to
// get geometry expression as literal and then evaluate it to get the
// geometry.
// Example:
// <ogc:Literal>org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl@dc33f184</ogc:Literal>
Literal literalWrapper = (Literal) dwithinFilter.getExpression2();
// Luckily we know what type the geometry expression should be, so we can cast it
PointImpl point = (PointImpl) literalWrapper.evaluate(null);
double[] coords = point.getCentroid().getCoordinate();
LOGGER.debug("coords[0] = {}, coords[1] = {}", coords[0], coords[1]);
assertEquals(Double.parseDouble(lon), coords[0], DOUBLE_DELTA);
assertEquals(Double.parseDouble(lat), coords[1], DOUBLE_DELTA);
LOGGER.debug("dwithinFilter.getDistance() = {}", dwithinFilter.getDistance());
assertEquals(Double.parseDouble(radius), dwithinFilter.getDistance(), DOUBLE_DELTA);
}
use of org.opengis.filter.expression.Literal in project ddf by codice.
the class OpenSearchFilterVisitor method visit.
/**
* DWithin filter maps to a Point/Radius distance Spatial search criteria.
*/
@Override
public Object visit(DWithin filter, Object data) {
LOGGER.trace("ENTERING: DWithin filter");
if (currentNest == null || NestedTypes.AND.equals(currentNest)) {
// The geometric point is wrapped in a <Literal> element, so have to
// get geometry expression as literal and then evaluate it to get
// the geometry.
// Example:
// <ogc:Literal>org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl@dc33f184</ogc:Literal>
Literal literalWrapper = (Literal) filter.getExpression2();
// Luckily we know what type the geometry expression should be, so
// we
// can cast it
PointImpl point = (PointImpl) literalWrapper.evaluate(null);
double[] coords = point.getCentroid().getCoordinate();
double distance = filter.getDistance();
LOGGER.debug("point: coords[0] = {}, coords[1] = {}", coords[0], coords[1]);
LOGGER.debug("radius = {}", distance);
this.spatialSearch = new SpatialDistanceFilter(coords[0], coords[1], distance);
filters.add(filter);
} else {
LOGGER.debug(ONLY_AND_MSG);
}
LOGGER.trace("EXITING: DWithin filter");
return super.visit(filter, data);
}
Aggregations