Java Code Examples for org.geotools.data.Query#setSortBy()

The following examples show how to use org.geotools.data.Query#setSortBy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticFeatureFilterIT.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testNaturalSortingAsc() throws Exception {
    init();
    Query q = new Query(featureSource.getSchema().getTypeName());
    q.setSortBy(new SortBy[] { SortBy.NATURAL_ORDER });
    SimpleFeatureIterator features = featureSource.getFeatures(q).features();
    String prevId = null;
    while (features.hasNext()) {
        String currId = features.next().getID();
        if (prevId != null)
            assertTrue(prevId.compareTo(currId) <= 0);
        prevId = currId;
    }
    features.close();
}
 
Example 2
Source File: ElasticFeatureFilterIT.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testNaturalSortingDesc() throws Exception {
    init();
    Query q = new Query(featureSource.getSchema().getTypeName());
    q.setSortBy(new SortBy[] { SortBy.REVERSE_ORDER });
    SimpleFeatureIterator features = featureSource.getFeatures(q).features();
    String prevId = null;
    while (features.hasNext()) {
        String currId = features.next().getID();
        if (prevId != null)
            assertTrue(prevId.compareTo(currId) >= 0);
        prevId = currId;
    }
    features.close();
}
 
Example 3
Source File: ElasticFeatureFilterIT.java    From elasticgeo with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testGetFeaturesWithSort() throws Exception {
    init();
    FilterFactory ff = dataStore.getFilterFactory();
    SortBy sort = ff.sort("vendor_s", SortOrder.ASCENDING);
    Query query = new Query();
    query.setSortBy(new SortBy[] { sort });

    SimpleFeatureCollection features = featureSource.getFeatures(query);
    assertEquals(11, features.size());

    SimpleFeatureIterator iterator = features.features();
    SimpleFeature f;
    try {
        assertTrue(iterator.hasNext());
        f = iterator.next();
        assertEquals("Asus", f.getAttribute("vendor_s"));
        assertTrue(iterator.hasNext());
        f = iterator.next();
        assertEquals("Cisco", f.getAttribute("vendor_s"));
        assertTrue(iterator.hasNext());
        f = iterator.next();
        assertEquals("Cisco", f.getAttribute("vendor_s"));
    } finally {
        iterator.close();
    }

    sort = ff.sort("vendor_s", SortOrder.DESCENDING);
    query.setSortBy(new SortBy[] { sort });
    features = featureSource.getFeatures(query);
    iterator = features.features();
    try {
        assertTrue(iterator.hasNext());
        f = iterator.next();
        assertEquals("TP-Link", f.getAttribute("vendor_s"));
        assertTrue(iterator.hasNext());
        f = iterator.next();
        assertEquals("Linksys", f.getAttribute("vendor_s"));
        assertTrue(iterator.hasNext());
        f = iterator.next();
        assertEquals("Linksys", f.getAttribute("vendor_s"));
    } finally {
        iterator.close();
    }
}