Search in sources :

Example 46 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class SolrProviderTest method testSpatialQueryAcrossInternationalDateLine.

@Test
public void testSpatialQueryAcrossInternationalDateLine() throws Exception {
    deleteAllIn(provider);
    MetacardImpl metacard = new MockMetacard(Library.getFlagstaffRecord());
    metacard.setLocation(MIDWAY_ISLANDS_POINT_WKT);
    List<Metacard> list = Arrays.asList((Metacard) metacard);
    create(list);
    /** POSITIVE - Counter Clockwise Orientation **/
    Filter filter = filterBuilder.attribute(Metacard.GEOGRAPHY).intersecting().wkt(ACROSS_INTERNATIONAL_DATELINE_LARGE_CCW_WKT);
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
    assertEquals("Failed to find the correct record. ", 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));
    }
    /** POSITIVE - Clockwise Orientation **/
    filter = filterBuilder.attribute(Metacard.GEOGRAPHY).intersecting().wkt(ACROSS_INTERNATIONAL_DATELINE_LARGE_CW_WKT);
    sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
    assertEquals("Failed to find the correct record. ", 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));
    }
    /** NEGATIVE **/
    filter = filterBuilder.attribute(Metacard.GEOGRAPHY).intersecting().wkt(ACROSS_INTERNATIONAL_DATELINE_SMALL_WKT);
    sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
    assertEquals("Should not find a record. ", 0, sourceResponse.getResults().size());
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SourceResponse(ddf.catalog.operation.SourceResponse) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 47 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class SolrProviderTest method testSpatialDistanceCalculationExactPoint.

@Test
public void testSpatialDistanceCalculationExactPoint() throws Exception {
    deleteAllIn(provider);
    // given
    double radiusInKilometers = 50;
    double radiusInMeters = radiusInKilometers * METERS_PER_KM;
    Filter positiveFilter = filterBuilder.attribute(Metacard.GEOGRAPHY).withinBuffer().wkt(LAS_VEGAS_POINT_WKT, radiusInMeters);
    MetacardImpl metacard = new MockMetacard(Library.getFlagstaffRecord());
    metacard.setLocation(LAS_VEGAS_POINT_WKT);
    List<Metacard> list = Arrays.asList((Metacard) metacard);
    create(list);
    QueryImpl query = new QueryImpl(positiveFilter);
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(Result.DISTANCE, SortOrder.ASCENDING));
    // when
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
    // then
    assertEquals("Failed to find metacard WKT with filter", 1, sourceResponse.getResults().size());
    Result result = sourceResponse.getResults().get(0);
    assertThat(result.getDistanceInMeters(), is(notNullValue()));
    assertThat("Point radius search should be less than the radius given.", result.getDistanceInMeters(), is(lessThanOrEqualTo(radiusInMeters)));
    double oneMeter = 1.0;
    assertThat("The distance should be close to zero since we are right upon the point.", result.getDistanceInMeters(), is(lessThanOrEqualTo(oneMeter)));
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 48 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class SolrProviderTest method spatialDistanceCalculationBetweenTwoPoints.

private void spatialDistanceCalculationBetweenTwoPoints(String attribute, String sortBy) throws Exception {
    deleteAllIn(provider);
    // given
    double radiusInKilometers = 500;
    double radiusInMeters = radiusInKilometers * METERS_PER_KM;
    Filter positiveFilter = filterBuilder.attribute(attribute).withinBuffer().wkt(PHOENIX_POINT_WKT, radiusInMeters);
    MetacardImpl metacard;
    if (attribute.equals(Metacard.GEOGRAPHY)) {
        metacard = new MockMetacard(Library.getFlagstaffRecord());
    } else {
        metacard = new MockMetacard(Library.getFlagstaffRecord(), new MetacardTypeImpl("distanceTest", BasicTypes.BASIC_METACARD, Sets.newSet(new AttributeDescriptorImpl(attribute, true, true, true, true, BasicTypes.GEO_TYPE))));
    }
    metacard.setAttribute(attribute, LAS_VEGAS_POINT_WKT);
    List<Metacard> list = Arrays.asList((Metacard) metacard);
    create(list);
    QueryImpl query = new QueryImpl(positiveFilter);
    query.setSortBy(new ddf.catalog.filter.impl.SortByImpl(sortBy, SortOrder.ASCENDING));
    // when
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
    // then
    assertEquals("Failed to find metacard WKT with filter", 1, sourceResponse.getResults().size());
    Result result = sourceResponse.getResults().get(0);
    assertThat(result.getDistanceInMeters(), is(notNullValue()));
    assertThat("Point radius search should be less than the radius given.", result.getDistanceInMeters(), is(lessThanOrEqualTo(radiusInMeters)));
    // expected distance calculated from
    // http://www.movable-type.co.uk/scripts/latlong.html
    double expectedDistanceBetweenCitiesInMeters = 412700;
    // +/-0.1%
    double precisionPercentage = .001;
    double lowerBound = expectedDistanceBetweenCitiesInMeters * (1 - precisionPercentage);
    double upperBound = expectedDistanceBetweenCitiesInMeters * (1 + precisionPercentage);
    assertThat("The distance returned should at least be above the lower bound of error.", result.getDistanceInMeters(), is(greaterThanOrEqualTo(lowerBound)));
    assertThat("The distance returned should at least be below the upper bound of error.", result.getDistanceInMeters(), is(lessThanOrEqualTo(upperBound)));
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl)

Example 49 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class SolrProviderTest method testStartIndexWithSorting.

@Test
public void testStartIndexWithSorting() throws Exception {
    deleteAllIn(provider);
    List<Metacard> metacards = new ArrayList<Metacard>();
    DateTime dt = new DateTime(1985, 1, 1, 1, 1, 1, 1, DateTimeZone.UTC);
    TreeSet<Date> calculatedDates = new TreeSet<Date>();
    for (int j = 0; j < 10; j++) {
        for (int i = 0; i < 100; i = i + 10) {
            MetacardImpl metacard = new MockMetacard(Library.getFlagstaffRecord());
            // ingest sporadically the effective dates so the default return
            // order won't be ordered
            Date calculatedDate = dt.plusDays(100 - i + 10 - j).toDate();
            calculatedDates.add(calculatedDate);
            metacard.setEffectiveDate(calculatedDate);
            metacards.add(metacard);
        }
    }
    // The TreeSet will sort them, the array will give me access to everyone
    // without an iterator
    Date[] dates = new Date[calculatedDates.size()];
    calculatedDates.toArray(dates);
    /** CREATE **/
    CreateResponse response = create(metacards);
    LOGGER.info("CREATED {} records.", response.getCreatedMetacards().size());
    CommonQueryBuilder queryBuilder = new CommonQueryBuilder();
    QueryImpl query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);
    int maxSize = 20;
    int startIndex = 2;
    // STARTINDEX=2, MAXSIZE=20
    query.setPageSize(maxSize);
    query.setStartIndex(startIndex);
    SortByImpl sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE), org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortBy);
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(maxSize, sourceResponse.getResults().size());
    for (int i = 0; i < sourceResponse.getResults().size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        Date effectiveDate = r.getMetacard().getEffectiveDate();
        DateTime currentDate = new DateTime(effectiveDate.getTime());
        LOGGER.debug("Testing current index: {}", startIndex + i);
        assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(), currentDate.getDayOfYear());
    }
    // STARTINDEX=20, MAXSIZE=5
    // a match-all queryByProperty
    query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);
    maxSize = 5;
    startIndex = 20;
    query.setPageSize(maxSize);
    query.setStartIndex(startIndex);
    sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE), org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortBy);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(maxSize, sourceResponse.getResults().size());
    for (int i = 0; i < sourceResponse.getResults().size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        Date effectiveDate = r.getMetacard().getEffectiveDate();
        DateTime currentDate = new DateTime(effectiveDate.getTime());
        LOGGER.debug("Testing current index: {}", startIndex + i);
        assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(), currentDate.getDayOfYear());
    }
    // STARTINDEX=80, MAXSIZE=20
    // a match-all queryByProperty
    query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);
    maxSize = 20;
    startIndex = 80;
    query.setPageSize(maxSize);
    query.setStartIndex(startIndex);
    sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE), org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortBy);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(maxSize, sourceResponse.getResults().size());
    for (int i = 0; i < sourceResponse.getResults().size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        Date effectiveDate = r.getMetacard().getEffectiveDate();
        DateTime currentDate = new DateTime(effectiveDate.getTime());
        LOGGER.debug("Testing current index: {}", startIndex + i);
        assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(), currentDate.getDayOfYear());
    }
    // STARTINDEX=1, MAXSIZE=100
    // a match-all queryByProperty
    query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);
    maxSize = 100;
    startIndex = 1;
    query.setPageSize(maxSize);
    query.setStartIndex(startIndex);
    sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE), org.opengis.filter.sort.SortOrder.ASCENDING);
    query.setSortBy(sortBy);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals(maxSize, sourceResponse.getResults().size());
    for (int i = 0; i < sourceResponse.getResults().size(); i++) {
        Result r = sourceResponse.getResults().get(i);
        Date effectiveDate = r.getMetacard().getEffectiveDate();
        DateTime currentDate = new DateTime(effectiveDate.getTime());
        LOGGER.debug("Testing current index: {}", startIndex + i);
        assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(), currentDate.getDayOfYear());
    }
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) CreateResponse(ddf.catalog.operation.CreateResponse) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) Date(java.util.Date) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SortByImpl(org.geotools.filter.SortByImpl) TreeSet(java.util.TreeSet) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 50 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class SolrProviderTest method testSpatialNearestNeighbor.

@Test
public void testSpatialNearestNeighbor() 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(list);
    // Ascending
    Filter positiveFilter = filterBuilder.attribute(Metacard.GEOGRAPHY).beyond().wkt(PHOENIX_POINT_WKT, 0);
    QueryImpl query = new QueryImpl(positiveFilter);
    SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals("Failed to find two records within 1000 nautical miles.", 2, sourceResponse.getResults().size());
    assertTrue("Flagstaff record was not first in ascending order.", sourceResponse.getResults().get(0).getMetacard().getMetadata().indexOf(FLAGSTAFF_QUERY_PHRASE) > 0);
    // Descending
    SortBy sortby = new ddf.catalog.filter.impl.SortByImpl(Result.DISTANCE, org.opengis.filter.sort.SortOrder.DESCENDING);
    query.setSortBy(sortby);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals("Failed to find two records within 1000 nautical miles.", 2, sourceResponse.getResults().size());
    assertTrue("Flagstaff record was not last in descending order.", sourceResponse.getResults().get(1).getMetacard().getMetadata().indexOf(FLAGSTAFF_QUERY_PHRASE) > 0);
    // Using WKT polygon
    positiveFilter = filterBuilder.attribute(Metacard.GEOGRAPHY).beyond().wkt(ARIZONA_POLYGON_WKT, 0);
    query = new QueryImpl(positiveFilter);
    sourceResponse = provider.query(new QueryRequestImpl(query));
    assertEquals("Failed to find two records based on polygon centroid.", 2, sourceResponse.getResults().size());
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SourceResponse(ddf.catalog.operation.SourceResponse) Filter(org.opengis.filter.Filter) SortByImpl(org.geotools.filter.SortByImpl) SortBy(org.opengis.filter.sort.SortBy) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Aggregations

QueryImpl (ddf.catalog.operation.impl.QueryImpl)232 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)186 Test (org.junit.Test)149 Filter (org.opengis.filter.Filter)117 SourceResponse (ddf.catalog.operation.SourceResponse)95 QueryRequest (ddf.catalog.operation.QueryRequest)66 Metacard (ddf.catalog.data.Metacard)61 ArrayList (java.util.ArrayList)50 Result (ddf.catalog.data.Result)49 Matchers.containsString (org.hamcrest.Matchers.containsString)30 Query (ddf.catalog.operation.Query)29 QueryResponse (ddf.catalog.operation.QueryResponse)28 SortByImpl (ddf.catalog.filter.impl.SortByImpl)27 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)25 SortBy (org.opengis.filter.sort.SortBy)25 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)24 Serializable (java.io.Serializable)23 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)22 HashMap (java.util.HashMap)20 Matchers.anyString (org.mockito.Matchers.anyString)20