Search in sources :

Example 21 with Literal

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);
    }
}
Also used : TemporalFilter(ddf.catalog.impl.filter.TemporalFilter) Literal(org.opengis.filter.expression.Literal) Period(org.opengis.temporal.Period) Date(java.util.Date) PeriodDuration(org.opengis.temporal.PeriodDuration) DefaultPeriodDuration(org.geotools.temporal.object.DefaultPeriodDuration) DefaultPeriodDuration(org.geotools.temporal.object.DefaultPeriodDuration)

Example 22 with Literal

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++;
    }
}
Also used : HashMap(java.util.HashMap) SurfaceImpl(org.geotools.geometry.jts.spatialschema.geometry.primitive.SurfaceImpl) IntersectsImpl(org.geotools.filter.spatial.IntersectsImpl) TemporalFilter(ddf.catalog.impl.filter.TemporalFilter) BBoxSpatialFilter(org.codice.ddf.opensearch.query.filter.BBoxSpatialFilter) PolygonSpatialFilter(org.codice.ddf.opensearch.query.filter.PolygonSpatialFilter) Filter(org.opengis.filter.Filter) Coordinate(com.vividsolutions.jts.geom.Coordinate) Literal(org.opengis.filter.expression.Literal) Test(org.junit.Test)

Example 23 with Literal

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());
    }
}
Also used : DuringImpl(org.geotools.filter.temporal.DuringImpl) Literal(org.opengis.filter.expression.Literal) Period(org.opengis.temporal.Period) Date(java.util.Date)

Example 24 with Literal

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);
}
Also used : TemporalFilter(ddf.catalog.impl.filter.TemporalFilter) BBoxSpatialFilter(org.codice.ddf.opensearch.query.filter.BBoxSpatialFilter) PolygonSpatialFilter(org.codice.ddf.opensearch.query.filter.PolygonSpatialFilter) Filter(org.opengis.filter.Filter) HashMap(java.util.HashMap) Literal(org.opengis.filter.expression.Literal) DWithinImpl(org.geotools.filter.spatial.DWithinImpl) PointImpl(org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl) Test(org.junit.Test)

Example 25 with Literal

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);
}
Also used : PropertyIsEqualToLiteral(ddf.catalog.filter.impl.PropertyIsEqualToLiteral) Literal(org.opengis.filter.expression.Literal) SpatialDistanceFilter(ddf.catalog.impl.filter.SpatialDistanceFilter) PointImpl(org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl)

Aggregations

Literal (org.opengis.filter.expression.Literal)26 Date (java.util.Date)14 Test (org.junit.Test)9 Filter (org.opengis.filter.Filter)9 SurfaceImpl (org.geotools.geometry.jts.spatialschema.geometry.primitive.SurfaceImpl)6 Coordinate (com.vividsolutions.jts.geom.Coordinate)5 TemporalFilter (ddf.catalog.impl.filter.TemporalFilter)5 Expression (org.opengis.filter.expression.Expression)5 AttributeType (ddf.catalog.data.AttributeType)4 FilterDelegate (ddf.catalog.filter.FilterDelegate)4 PropertyIsEqualToLiteral (ddf.catalog.filter.impl.PropertyIsEqualToLiteral)4 CswQueryFactoryTest (org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest)4 LiteralExpressionImpl (org.geotools.filter.LiteralExpressionImpl)4 ExcludeFilter (org.opengis.filter.ExcludeFilter)4 IncludeFilter (org.opengis.filter.IncludeFilter)4 Point (com.vividsolutions.jts.geom.Point)3 SimpleDateFormat (java.text.SimpleDateFormat)3 HashMap (java.util.HashMap)3 BBoxSpatialFilter (org.codice.ddf.opensearch.query.filter.BBoxSpatialFilter)3 PolygonSpatialFilter (org.codice.ddf.opensearch.query.filter.PolygonSpatialFilter)3