1

I am using the following code to parse GML file in java:

File gmlFile = new File("path/to/gml/file");    
InputStream gmlInStream = new FileInputStream(gmlFile);
GML gml = new GML(Version.WFS1_0);
SimpleFeatureCollection featureCollection = gml.decodeFeatureCollection(gmlInStream);
SimpleFeatureIterator iterator = featureCollection.features();
while (iterator.hasNext()) {
       SimpleFeature feature = iterator.next();
       //Do something with the feature
}
iterator.close();

I need to have the returned features collection be sorted by a specific feature's attribute. How to do that?

1 Answer 1

1

You need to provide a SortBy to your Query object.

So for example the following code sorts the US States dataset by population (PERSONS):

    URL u = TestData.url("shapes/statepop.shp");
    FileDataStore store = FileDataStoreFinder.getDataStore(u);
    SimpleFeatureType schema = store.getSchema();
    Query query = new Query(schema.getTypeName(), Filter.INCLUDE, 
         new String[] { "STATE_NAME", "PERSONS", "WORKERS" });
    FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
    query.setSortBy(new SortBy[] { ff.sort("PERSONS", SortOrder.ASCENDING) });
    SimpleFeatureCollection features = store.getFeatureSource().getFeatures(query);
    try (SimpleFeatureIterator itr = features.features()) {
      while (itr.hasNext()) {
        SimpleFeature feature = itr.next();
        System.out.println(feature.getAttribute("STATE_NAME") + "\t" + feature.getAttribute("PERSONS"));
      }
    }

Or if, as in your case, you have a FeatureCollection and no FeatureSource you can wrap them using the DataUtilities class:

File gmlFile = new File("/home/ian/Downloads/states.xml");
InputStream gmlInStream = new FileInputStream(gmlFile);
GML gml = new GML(Version.WFS1_0);
features = gml.decodeFeatureCollection(gmlInStream);
query = new Query();
query.setSortBy(new SortBy[] { ff.sort("name", SortOrder.DESCENDING) });
features = DataUtilities.source(features).getFeatures(query);
try (SimpleFeatureIterator itr = features.features()) {
  while (itr.hasNext()) {
    SimpleFeature feature = itr.next();
    System.out.println(feature.getAttribute("name") + "\t" + feature.getAttribute("pop_2018"));
  }
}
5
  • Thanks for your reply. I am not able to create a Datastore from the GML file, I tried the following but it return null. FileDataStore store = FileDataStoreFinder.getDataStore(gmlFile.toURL());
    – Faz B
    Commented Jan 19, 2020 at 11:24
  • That is just set up in my example, you need the 2nd way of creating the featureCollection I show followed by the iterator loop
    – Ian Turton
    Commented Jan 19, 2020 at 11:30
  • But your 2nd way needs a query object, and the query object needs a schema object, and the schema object needs a DataStore :). So how can I create a DataStore from a GML file? or if you have any other solution?
    – Faz B
    Commented Jan 19, 2020 at 14:45
  • Use features.getSchema() to get the schema of your feature collection to create the Query with
    – Ian Turton
    Commented Jan 20, 2020 at 11:22
  • Thanks a lot @Ian Turton, it works perfectly.
    – Faz B
    Commented Jan 22, 2020 at 5:07

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.