Search in sources :

Example 6 with SortByImpl

use of org.geotools.filter.SortByImpl in project ddf by codice.

the class SolrProviderTest method testSpatialPointRadius.

@Test
public void testSpatialPointRadius() throws Exception {
    deleteAllIn(provider);
    MetacardImpl metacard1 = new MockMetacard(Library.getFlagstaffRecord());
    MetacardImpl metacard2 = new MockMetacard(Library.getTampaRecord());
    MetacardImpl metacard3 = new MockMetacard(Library.getShowLowRecord());
    // Add in the geometry
    metacard1.setLocation(FLAGSTAFF_AIRPORT_POINT_WKT);
    metacard2.setLocation(TAMPA_AIRPORT_POINT_WKT);
    metacard3.setLocation(SHOW_LOW_AIRPORT_POINT_WKT);
    List<Metacard> list = Arrays.asList((Metacard) metacard1, metacard2, metacard3);
    /** CREATE **/
    create(list);
    Filter filter = filterBuilder.attribute(Metacard.ID).is().like().text("*");
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
    assertEquals("Failed to find all records.", 3, sourceResponse.getResults().size());
    CommonQueryBuilder builder = new CommonQueryBuilder();
    // Right on Flagstaff
    QueryImpl query = builder.pointRadius(-111.67121887207031, 35.138454437255859, 10.0);
    query.setPageSize(1);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals("Failed to find Flagstaff record only.", 1, sourceResponse.getResults().size());
    for (Result r : sourceResponse.getResults()) {
        assertTrue("Wrong record, Flagstaff keyword was not found.", ALL_RESULTS != r.getMetacard().getMetadata().indexOf(FLAGSTAFF_QUERY_PHRASE));
        LOGGER.info("Distance to Flagstaff: {}", r.getDistanceInMeters());
    // assertTrue(r.getDistanceInMeters() != null);
    }
    // Right on Flagstaff, finding 2 records with 195 km radius
    query = builder.pointRadius(-111.67121887207031, 35.138454437255859, 195000);
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl("foo", SortOrder.ASCENDING));
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals("Failed to find the two records.", 2, sourceResponse.getResults().size());
    ArrayList<Result> results = new ArrayList<Result>();
    for (Result r : sourceResponse.getResults()) {
        results.add(r);
    }
    // querybuilder
    for (int i = 0; i < 2; i++) {
        Result result = results.get(i);
        LOGGER.info("Distance of [{}]]: {}", i, result.getDistanceInMeters());
        if (i == 0) {
            assertTrue("Grabbed the wrong record.", ALL_RESULTS != result.getMetacard().getMetadata().indexOf(FLAGSTAFF_QUERY_PHRASE));
        }
        if (i == 1) {
            assertTrue("Grabbed the wrong record - should be Show Low.", ALL_RESULTS != result.getMetacard().getMetadata().indexOf("Show Low"));
        }
    }
    // NEGATIVE CASE
    query = builder.pointRadius(80.1, 25, 195000);
    query.setPageSize(3);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals("Should have not found any records.", 0, sourceResponse.getResults().size());
    // FEET
    double[] coords = { -111.67121887207031, 35.138454437255859 };
    query = new QueryImpl(builder.filterFactory.dwithin(Metacard.ANY_GEO, new PointImpl(new DirectPositionImpl(coords), DefaultGeographicCRS.WGS84), 195000, UomOgcMapping.FOOT.name()));
    query.setStartIndex(1);
    SortByImpl sortby = new SortByImpl(builder.filterFactory.property(Result.DISTANCE), org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortby);
    query.setPageSize(3);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(1, sourceResponse.getResults().size());
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) ArrayList(java.util.ArrayList) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) DirectPositionImpl(org.geotools.geometry.jts.spatialschema.geometry.DirectPositionImpl) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) SortByImpl(org.geotools.filter.SortByImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) PointImpl(org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl) Test(org.junit.Test)

Aggregations

QueryImpl (ddf.catalog.operation.impl.QueryImpl)6 SortByImpl (org.geotools.filter.SortByImpl)6 Result (ddf.catalog.data.Result)3 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)3 ArrayList (java.util.ArrayList)3 Metacard (ddf.catalog.data.Metacard)2 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)2 PropertyNameImpl (ddf.catalog.filter.impl.PropertyNameImpl)2 SourceResponse (ddf.catalog.operation.SourceResponse)2 DirectPositionImpl (org.geotools.geometry.jts.spatialschema.geometry.DirectPositionImpl)2 PointImpl (org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl)2 Test (org.junit.Test)2 Filter (org.opengis.filter.Filter)2 PropertyName (org.opengis.filter.expression.PropertyName)2 SortBy (org.opengis.filter.sort.SortBy)2 CreateResponse (ddf.catalog.operation.CreateResponse)1 QueryRequest (ddf.catalog.operation.QueryRequest)1 QueryResponse (ddf.catalog.operation.QueryResponse)1 SecurityServiceException (ddf.security.service.SecurityServiceException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1