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;
}
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());
}
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());
}
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;
}
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;
}
Aggregations