Search in sources :

Example 6 with Literal

use of org.opengis.filter.expression.Literal in project ddf by codice.

the class OpenSearchQueryTest method testBboxSpatialFilter.

@Test
public void testBboxSpatialFilter() throws Exception {
    String bboxCorners = "0,10,20,30";
    OpenSearchQuery query = new OpenSearchQuery(null, 0, 10, "relevance", "desc", 30000, FILTER_BUILDER);
    query.addBBoxSpatialFilter(bboxCorners);
    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 bbox = (SurfaceImpl) literalWrapper.evaluate(null);
    String[] expectedCoords = bboxCorners.split(",");
    double[] lowerCornerCoords = bbox.getEnvelope().getLowerCorner().getCoordinate();
    LOGGER.debug("lowerCornerCoords:  [0] = {},   [1] = {}", lowerCornerCoords[0], lowerCornerCoords[1]);
    assertEquals(Double.parseDouble(expectedCoords[0]), lowerCornerCoords[0], DOUBLE_DELTA);
    assertEquals(Double.parseDouble(expectedCoords[1]), lowerCornerCoords[1], DOUBLE_DELTA);
    double[] upperCornerCoords = bbox.getEnvelope().getUpperCorner().getCoordinate();
    LOGGER.debug("upperCornerCoords:  [0] = {},   [1] = {}", upperCornerCoords[0], upperCornerCoords[1]);
    assertEquals(Double.parseDouble(expectedCoords[2]), upperCornerCoords[0], DOUBLE_DELTA);
    assertEquals(Double.parseDouble(expectedCoords[3]), upperCornerCoords[1], DOUBLE_DELTA);
}
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) Literal(org.opengis.filter.expression.Literal) Test(org.junit.Test)

Example 7 with Literal

use of org.opengis.filter.expression.Literal in project ddf by codice.

the class TestCswRecordMapperFilterVisitor method testLiteralWithUnknownType.

@Test
public void testLiteralWithUnknownType() {
    Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    String dateString = formatter.format(date);
    Literal val = factory.literal(dateString);
    Literal literal = (Literal) visitor.visit(val, attrExpr);
    assertThat(literal.getValue(), instanceOf(String.class));
    assertThat(literal.getValue(), is(dateString));
}
Also used : Literal(org.opengis.filter.expression.Literal) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Example 8 with Literal

use of org.opengis.filter.expression.Literal in project ddf by codice.

the class TestCswRecordMapperFilterVisitor method testIntersectsUtm.

@Test
public void testIntersectsUtm() throws FactoryException, TransformException {
    double lon = 33.45;
    double lat = 25.22;
    double easting = 545328.48;
    double northing = 2789384.24;
    CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:32636");
    GeometryFactory geoFactory = new GeometryFactory();
    Geometry utmPoint = geoFactory.createPoint(new Coordinate(easting, northing));
    utmPoint.setUserData(sourceCRS);
    Expression pt1 = factory.literal(geoFactory.createPoint(new Coordinate(1, 2)));
    Expression pt2 = factory.literal(utmPoint);
    Intersects filter = factory.intersects(pt1, pt2);
    visitor.visit(filter, null);
    assertThat(pt2, instanceOf(Literal.class));
    Literal literalExpression = (Literal) pt2;
    assertThat(literalExpression.getValue(), instanceOf(Geometry.class));
    Geometry geometry = (Geometry) literalExpression.getValue();
    assertThat(geometry.getCoordinates()[0].x, closeTo(lon, .00001));
    assertThat(geometry.getCoordinates()[0].y, closeTo(lat, .00001));
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Coordinate(com.vividsolutions.jts.geom.Coordinate) Expression(org.opengis.filter.expression.Expression) Intersects(org.opengis.filter.spatial.Intersects) Literal(org.opengis.filter.expression.Literal) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Example 9 with Literal

use of org.opengis.filter.expression.Literal in project ddf by codice.

the class TestCswRecordMapperFilterVisitor method testLiteralWithMappableType.

@Test
public void testLiteralWithMappableType() {
    Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    String dateString = formatter.format(date);
    Literal val = factory.literal(dateString);
    Literal literal = (Literal) visitor.visit(val, created);
    assertThat(literal.getValue(), instanceOf(Date.class));
    assertThat(literal.getValue(), is(date));
}
Also used : Literal(org.opengis.filter.expression.Literal) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Example 10 with Literal

use of org.opengis.filter.expression.Literal in project ddf by codice.

the class OpenSearchFilterVisitor method handleTemporal.

private void handleTemporal(BinaryTemporalOperator filter) {
    Literal literalWrapper = (Literal) filter.getExpression2();
    LOGGER.trace("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) PropertyIsEqualToLiteral(ddf.catalog.filter.impl.PropertyIsEqualToLiteral) 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)

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