Search in sources :

Example 1 with SortProperty

use of org.opengis.filter.SortProperty in project geotoolkit by Geomatys.

the class StringToSortByConverter method apply.

/**
 *Convert a String into an SortProperty array.
 *
 * <p> The input String must be format like :
 *  "property1:asc/desc, property2:asc/desc, ..."
 * </p>
 *
 * <p>
 * Examples:
 * <ul>
 * <li><code>id:asc, name:desc</code></li>
 * </ul>
 * </p>
 */
@Override
public SortProperty[] apply(final String s) throws UnconvertibleObjectException {
    if (s == null)
        throw new UnconvertibleObjectException("Empty SortProperty");
    final String[] sorters = s.split(",");
    final int nbSorter = sorters.length;
    final SortProperty[] sortBy = new SortProperty[nbSorter];
    // each sorter
    for (int i = 0; i < nbSorter; i++) {
        // one property sorter "property:order"
        final String[] aSorter = sorters[i].split(":");
        final String property = aSorter[0];
        FilterFactory FF = FilterUtilities.FF;
        if (aSorter[1].equalsIgnoreCase("asc")) {
            sortBy[i] = FF.sort(FF.property(property), SortOrder.ASCENDING);
        } else if (aSorter[1].equalsIgnoreCase("desc")) {
            sortBy[i] = FF.sort(FF.property(property), SortOrder.DESCENDING);
        } else {
            throw new UnconvertibleObjectException("Invalid SortProperty");
        }
    }
    return sortBy;
}
Also used : UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) SortProperty(org.opengis.filter.SortProperty) FilterFactory(org.opengis.filter.FilterFactory)

Example 2 with SortProperty

use of org.opengis.filter.SortProperty in project geotoolkit by Geomatys.

the class FeatureStreamsTest method testSortByIterator.

@Test
public void testSortByIterator() {
    SortProperty[] sorts = new SortProperty[] { FF.sort(FF.property("att_string"), SortOrder.ASCENDING) };
    FeatureCollection collection = buildSimpleFeatureCollection();
    FeatureIterator ite = FeatureStreams.sort(collection.iterator(), sorts);
    assertEquals(3, FeatureStoreUtilities.calculateCount(ite));
    ite = FeatureStreams.sort(collection.iterator(), sorts);
    assertEquals(id3, ite.next().getPropertyValue(AttributeConvention.IDENTIFIER));
    assertEquals(id1, ite.next().getPropertyValue(AttributeConvention.IDENTIFIER));
    assertEquals(id2, ite.next().getPropertyValue(AttributeConvention.IDENTIFIER));
    try {
        ite.next();
        fail("Should have raise a no such element exception.");
    } catch (NoSuchElementException ex) {
    // ok
    }
    // check has next do not iterate
    ite = FeatureStreams.sort(collection.iterator(), sorts);
    testIterationOnNext(ite, 3);
    // check sub iterator is properly closed
    CheckCloseFeatureIterator checkIte = new CheckCloseFeatureIterator(collection.iterator());
    assertFalse(checkIte.isClosed());
    ite = FeatureStreams.sort(checkIte, sorts);
    while (ite.hasNext()) ite.next();
    ite.close();
    assertTrue(checkIte.isClosed());
}
Also used : CheckCloseFeatureIterator(org.geotoolkit.storage.feature.CheckCloseFeatureIterator) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) SortProperty(org.opengis.filter.SortProperty) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) NoSuchElementException(java.util.NoSuchElementException) CheckCloseFeatureIterator(org.geotoolkit.storage.feature.CheckCloseFeatureIterator) Test(org.junit.Test)

Example 3 with SortProperty

use of org.opengis.filter.SortProperty in project geotoolkit by Geomatys.

the class OGCforSLD110Test method testCustom2.

@Test
public void testCustom2() throws JAXBException, FactoryException {
    StyleXmlIO util = new StyleXmlIO();
    final SortProperty sort = util.readSortBy(OGCforSLD110Test.class.getResource("/org/geotoolkit/test/filter/sortby.xml"), org.geotoolkit.sld.xml.Specification.Filter.V_1_1_0);
    assertEquals("sf:str4Property", sort.getValueReference().getXPath());
    assertEquals(SortOrder.ASCENDING, sort.getSortOrder());
}
Also used : SortProperty(org.opengis.filter.SortProperty) StyleXmlIO(org.geotoolkit.sld.xml.StyleXmlIO) Test(org.junit.Test)

Example 4 with SortProperty

use of org.opengis.filter.SortProperty in project geotoolkit by Geomatys.

the class SortByConverterTest method buildResultSortBy.

private SortProperty[] buildResultSortBy() throws FactoryException {
    FilterFactory ff = FilterUtilities.FF;
    SortProperty[] sorters = new SortProperty[2];
    sorters[0] = ff.sort(ff.property("name"), SortOrder.ASCENDING);
    sorters[1] = ff.sort(ff.property("age"), SortOrder.DESCENDING);
    return sorters;
}
Also used : SortProperty(org.opengis.filter.SortProperty) FilterFactory(org.opengis.filter.FilterFactory)

Example 5 with SortProperty

use of org.opengis.filter.SortProperty in project geotoolkit by Geomatys.

the class QueryUtilities method subQuery.

/**
 * Combine two queries in the way that the resulting query act
 * as if it was a sub query result.
 * For example if the original query has a start index of 10 and the
 * sub-query a start index of 5, the resulting startIndex will be 15.
 * The type name of the first query will override the one of the second.
 */
public static Query subQuery(final Query original, final Query second) {
    if (original == null || second == null) {
        throw new NullArgumentException("Both query must not be null.");
    }
    final Query qb = new Query();
    qb.setTypeName(original.getTypeName());
    // use the more restrictive max features field---------------------------
    long max = original.getLimit().orElse(-1);
    if (second.getLimit().isPresent()) {
        if (max == -1) {
            max = second.getLimit().getAsLong();
        } else {
            max = Math.min(max, second.getLimit().getAsLong());
        }
    }
    if (max >= 0) {
        qb.setLimit(max);
    }
    // join attributes names-------------------------------------------------
    final String[] propNames = retainAttributes(original.getPropertyNames(), second.getPropertyNames());
    qb.setProperties(propNames);
    // join filters----------------------------------------------------------
    Filter filter = original.getSelection();
    Filter filter2 = second.getSelection();
    if (filter == null)
        filter = Filter.include();
    if (filter2 == null)
        filter2 = Filter.include();
    if (filter.equals(Filter.include())) {
        filter = filter2;
    } else if (!filter2.equals(Filter.include())) {
        filter = FF.and(filter, filter2);
    }
    qb.setSelection(filter);
    // group start index ----------------------------------------------------
    long start = original.getOffset() + second.getOffset();
    qb.setOffset(start);
    // ordering -------------------------------------------------------------
    final List<SortProperty> sorts = new ArrayList<>();
    SortProperty[] sts = getSortProperties(original.getSortBy());
    if (sts != null) {
        sorts.addAll(Arrays.asList(sts));
    }
    sts = getSortProperties(second.getSortBy());
    if (sts != null) {
        sorts.addAll(Arrays.asList(sts));
    }
    qb.setSortBy(sorts.toArray(new SortProperty[sorts.size()]));
    // hints of the second query---------------------------------------------
    qb.setHints(second.getHints());
    // copy the resolution parameter-----------------------------------------
    final double[] resFirst = original.getResolution();
    final double[] resSecond = second.getResolution();
    if (resFirst == null || Double.isNaN(resFirst[0])) {
        qb.setResolution(resSecond);
    } else {
        qb.setResolution(resFirst);
    }
    // mix versions, second query version takes precedence.
    if (original.getVersionDate() != null)
        qb.setVersionDate(original.getVersionDate());
    if (original.getVersionLabel() != null)
        qb.setVersionLabel(original.getVersionLabel());
    if (second.getVersionDate() != null)
        qb.setVersionDate(second.getVersionDate());
    if (second.getVersionLabel() != null)
        qb.setVersionLabel(second.getVersionLabel());
    return qb;
}
Also used : SortProperty(org.opengis.filter.SortProperty) FeatureQuery(org.apache.sis.storage.FeatureQuery) Filter(org.opengis.filter.Filter) ArrayList(java.util.ArrayList) UnmodifiableArrayList(org.apache.sis.internal.util.UnmodifiableArrayList) NullArgumentException(org.apache.sis.util.NullArgumentException)

Aggregations

SortProperty (org.opengis.filter.SortProperty)15 ArrayList (java.util.ArrayList)5 Filter (org.opengis.filter.Filter)4 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)3 Test (org.junit.Test)3 FeatureType (org.opengis.feature.FeatureType)3 ValueReference (org.opengis.filter.ValueReference)3 NoSuchElementException (java.util.NoSuchElementException)2 UnmodifiableArrayList (org.apache.sis.internal.util.UnmodifiableArrayList)2 DataStoreException (org.apache.sis.storage.DataStoreException)2 FeatureQuery (org.apache.sis.storage.FeatureQuery)2 Hints (org.geotoolkit.factory.Hints)2 TransformMapper (org.geotoolkit.feature.TransformMapper)2 ViewMapper (org.geotoolkit.feature.ViewMapper)2 GeometryScaleTransformer (org.geotoolkit.geometry.jts.transform.GeometryScaleTransformer)2 CheckCloseFeatureIterator (org.geotoolkit.storage.feature.CheckCloseFeatureIterator)2 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)2 MismatchedFeatureException (org.opengis.feature.MismatchedFeatureException)2 FilterFactory (org.opengis.filter.FilterFactory)2 Collection (java.util.Collection)1