Search in sources :

Example 1 with IntersectsImpl

use of org.geotools.filter.spatial.IntersectsImpl 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 2 with IntersectsImpl

use of org.geotools.filter.spatial.IntersectsImpl in project ddf by codice.

the class OpenSearchQueryTest method testMultipleSpatialFilter.

@Test
public void testMultipleSpatialFilter() {
    OpenSearchQuery query = new OpenSearchQuery(0, 10, "relevance", "desc", 30000, FILTER_BUILDER);
    query.addGeometrySpatialFilter(GEOMETRY_WKT);
    query.addPolygonSpatialFilter("30.943,-120.032,35.039,-120.032,35.039,-110.856,30.943,-110.856,30.943,-120.032");
    query.addBBoxSpatialFilter("-120.032,30.943,-110.856,35.039");
    query.addPointRadiusSpatialFilter("117.3425", "33.9283", "5000");
    query.addPolygonSpatialFilter("-30,100,-35,100,-35,110,-30,110,-30,100");
    Filter filter = query.getFilter();
    assertThat(filter, instanceOf(OrImpl.class));
    OrImpl topFilter = (OrImpl) filter;
    List<Filter> spatialFilters = topFilter.getChildren();
    assertThat(spatialFilters.size(), is(5));
    for (Filter spatialFilter : spatialFilters) {
        if (spatialFilter instanceof DWithinImpl) {
            assertThat(spatialFilter, notNullValue());
            DWithinImpl dWithin = (DWithinImpl) spatialFilter;
            assertThat(dWithin.getDistance(), is(5000.0));
            Literal literal = (Literal) dWithin.getExpression2();
            PointImpl point = (PointImpl) literal.getValue();
            String wkt = WKT_WRITER.write(point.getJTSGeometry());
            assertThat(wkt, is(POINT_WKT));
        } else if (spatialFilter instanceof IntersectsImpl) {
            assertThat(spatialFilter, notNullValue());
            IntersectsImpl intersects = (IntersectsImpl) spatialFilter;
            Literal literal = (Literal) intersects.getExpression2();
            Object geometryExpression = literal.getValue();
            if (geometryExpression instanceof SurfaceImpl) {
                SurfaceImpl surface = (SurfaceImpl) literal.getValue();
                String wkt = WKT_WRITER.write(surface.getJTSGeometry());
                assertThat(wkt, anyOf(is(POLYGON_WKT), is(POLYGON_WKT_2)));
            } else if (geometryExpression instanceof GeometryImpl) {
                org.locationtech.jts.geom.Geometry polygon = ((GeometryImpl) geometryExpression).getJTSGeometry();
                assertThat(WKT_WRITER.write(polygon), is(GEOMETRY_WKT));
            }
        }
    }
}
Also used : GeometryImpl(org.geotools.geometry.jts.spatialschema.geometry.GeometryImpl) SurfaceImpl(org.geotools.geometry.jts.spatialschema.geometry.primitive.SurfaceImpl) IntersectsImpl(org.geotools.filter.spatial.IntersectsImpl) TemporalFilter(ddf.catalog.impl.filter.TemporalFilter) PolygonSpatialFilter(org.codice.ddf.opensearch.endpoint.query.filter.PolygonSpatialFilter) Filter(org.opengis.filter.Filter) BBoxSpatialFilter(org.codice.ddf.opensearch.endpoint.query.filter.BBoxSpatialFilter) Literal(org.opengis.filter.expression.Literal) OrImpl(org.geotools.filter.OrImpl) DWithinImpl(org.geotools.filter.spatial.DWithinImpl) PointImpl(org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl) Test(org.junit.Test)

Example 3 with IntersectsImpl

use of org.geotools.filter.spatial.IntersectsImpl in project ddf by codice.

the class OpenSearchQueryTest method testBboxSpatialFilter.

@Test
public void testBboxSpatialFilter() {
    String bboxCorners = "0,10,20,30";
    OpenSearchQuery query = new OpenSearchQuery(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) PolygonSpatialFilter(org.codice.ddf.opensearch.endpoint.query.filter.PolygonSpatialFilter) Filter(org.opengis.filter.Filter) BBoxSpatialFilter(org.codice.ddf.opensearch.endpoint.query.filter.BBoxSpatialFilter) Literal(org.opengis.filter.expression.Literal) Test(org.junit.Test)

Example 4 with IntersectsImpl

use of org.geotools.filter.spatial.IntersectsImpl 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 5 with IntersectsImpl

use of org.geotools.filter.spatial.IntersectsImpl in project ddf by codice.

the class OpenSearchQueryTest method testPolygonSpatialFilter.

@Test
public void testPolygonSpatialFilter() {
    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(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) PolygonSpatialFilter(org.codice.ddf.opensearch.endpoint.query.filter.PolygonSpatialFilter) Filter(org.opengis.filter.Filter) BBoxSpatialFilter(org.codice.ddf.opensearch.endpoint.query.filter.BBoxSpatialFilter) Coordinate(org.locationtech.jts.geom.Coordinate) Literal(org.opengis.filter.expression.Literal) Test(org.junit.Test)

Aggregations

TemporalFilter (ddf.catalog.impl.filter.TemporalFilter)5 IntersectsImpl (org.geotools.filter.spatial.IntersectsImpl)5 SurfaceImpl (org.geotools.geometry.jts.spatialschema.geometry.primitive.SurfaceImpl)5 Test (org.junit.Test)5 Filter (org.opengis.filter.Filter)5 Literal (org.opengis.filter.expression.Literal)5 HashMap (java.util.HashMap)4 BBoxSpatialFilter (org.codice.ddf.opensearch.endpoint.query.filter.BBoxSpatialFilter)3 PolygonSpatialFilter (org.codice.ddf.opensearch.endpoint.query.filter.PolygonSpatialFilter)3 BBoxSpatialFilter (org.codice.ddf.opensearch.query.filter.BBoxSpatialFilter)2 PolygonSpatialFilter (org.codice.ddf.opensearch.query.filter.PolygonSpatialFilter)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 OrImpl (org.geotools.filter.OrImpl)1 DWithinImpl (org.geotools.filter.spatial.DWithinImpl)1 GeometryImpl (org.geotools.geometry.jts.spatialschema.geometry.GeometryImpl)1 PointImpl (org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl)1 Coordinate (org.locationtech.jts.geom.Coordinate)1