6

I have a featurecollection:

SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(dbFeatureType);
SimpleFeatureCollection collection = FeatureCollections.newCollection();
while (mifReader.hasNext()){      
  in = (SimpleFeatureImpl) mifReader.next();
  SimpleFeature dbFeature = featureBuilder.buildFeature(null);
  for(AttributeDescriptor ad : adList){
    String name = ad.getLocalName();
    Object attrValue;
    attrValue = in.getAttribute(name);
    if(attrValue instanceof String){
      String string3 = new String((attrValue.toString()).getBytes("ISO-8859-1"), "cp1251");
      if(name.trim().equalsIgnoreCase(kadname.trim())){
        dbFeature.setAttribute("kadnum", string3);
      }
    }
    if (attrValue instanceof Geometry){
        idn++;
        com.vividsolutions.jts.geom.Geometry geom = (Geometry) in.getAttribute(name);
        dbFeature.setAttribute("id", idn);
        System.out.println("after insrt="+idn);
        dbFeature.setAttribute("deleted", deleted);
        dbFeature.setAttribute("the_geom", geom);
        dbFeature.setAttribute("status_id", status_id);

    }
    collection.add(dbFeature);
    }
}

Its all okay. But when I put this collection in Data Base i get a reversed items. So i want to sort or reverse items in this collection. But how to do this?
I try to use this:

        collection.sort( new SortBy() 
{ 
    @Override
            public PropertyName getPropertyName() { return YourPropertyNameImpl("id") ; } 
    @Override
            public SortOrder getSortOrder() { return SortOrder.ASCENDING; }
});

But I don't understand what is YourPropertyNameImpl.

UPDATE

Okey. I try to put features from my collection to another collection:

    Iterator iterator = null;
    int count;
    iterator = collection.iterator();
    while( iterator.hasNext()){
        SimpleFeature feature = (SimpleFeature) iterator.next();
        coll.add(feature);
        System.out.println("Coll= "+coll.features().next().getProperties("id"));
    }

But get same result(. In this case i need to read collection from last element to first. Its possible?

12
  • replace SortOrder.ASCENDING; with SortOrder.DESCENDING ?
    – Ian Turton
    Commented Oct 24, 2012 at 9:16
  • What its mean? Sorry im new in GDAL.
    – Kliver Max
    Commented Oct 24, 2012 at 9:19
  • I think that this is GeoTools not GDAL, but where you see the line with ASCENDING in it replace that word with DESCENDING and the collection will be sorted in the other order.
    – Ian Turton
    Commented Oct 24, 2012 at 9:22
  • Okey. Can you tell me about YourPropertyNameImpl("id") what its mean? I try to use SimpleFeatureImpl but cant get a PropertyName.
    – Kliver Max
    Commented Oct 24, 2012 at 9:26
  • can you add a link to where you found this code so I can see the context?
    – Ian Turton
    Commented Oct 24, 2012 at 9:38

1 Answer 1

2

Just to help anyone else who ends up here as I did recently! There is a bug in GeoTools that prevents DESCENDING working as a sort order. See this PR for the fix which will be applied shortly.

UPDATE

This is now fixed in all GeoTools releases from 14.4 and 15.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.