0

I am using GeoTools to query a GeoServer WFS using the following code:

    Map connectionParameters = new HashMap();
    connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", String.format("%s&request=GetCapabilities", wfsUrl));
    connectionParameters.put("WFSDataStoreFactory:TIMEOUT", connectionTimeout*1000);
    DataStore data = DataStoreFinder.getDataStore(connectionParameters);
    SimpleFeatureType schema = data.getSchema("airports");
    FeatureSource<SimpleFeatureType, SimpleFeature> source = data.getFeatureSource( typeName );
    String geomName = schema.getGeometryDescriptor().getLocalName();
    Envelope bbox = new Envelope( -100.0, -70, 25, 40 );

    FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints());
    Object polygon = JTS.toGeometry(bbox);
    Intersects filter = ff.intersects( ff.property( geomName ), ff.literal( polygon ) );

    Query query = new DefaultQuery( typeName, filter, new String[]{ geomName } );
    query.setSortBy(new SortBy[]{ff.sort("country", SortOrder.ASCENDING)});
    FeatureCollection<SimpleFeatureType, SimpleFeature> features = source.getFeatures( query );

The query returns features, but sorting never works. A toString() on the query returns something like:

feature type: airports filter: [[ geom intersects POLYGON ((-117.8395870053188 31.58066038426718, -115.5957867039628 31.58066038426718, -115.5957867039628 33.27328332002949, -117.8395870053188 33.27328332002949, -117.8395870053188 31.58066038426718)) ]] [properties: ALL ] [sort by: country ASCENDING]

which indicates that sorting is part of the query.

The following direct WFS query works:

http://thing:8080/geoserver/ix/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=airports&srsName=epsg:4326&bbox=-117.8395870053188,31.58066038426718,-115.5957867039628,33.27328332002949&sortBy=country

Any suggestions?

3
  • what happens if you include country in the Query attribute list? e.g. Query query = new DefaultQuery( typeName, filter, new String[]{ geomName, "country" } ); – Ian Turton Feb 18 '15 at 8:42
  • I get an exception if I don't specify the attribute I'm sorting on. See gitlab.com/snippets/3277 – Ian Turton Feb 18 '15 at 9:34
  • Thanks for the answer... This change just reduced the number of properties returned, but still not sorted by country. Here is the output:{ { "rows": [ { "geom": "{\"type\":\"Point\",\"coords\":[-116.602,31.795]}", "country": "Mexico" }, { "geom": "{\"type\":\"Point\",\"coords\":[-116.97,32.541]}", "country": "Mexico" }, { "geom": "{\"type\":\"Point\",\"coords\":[-117.215,32.699]}", "country": "United States" }, { "geom": "{\"type\":\"Point\",\"coords\":[-116.624,31.855]}", "country": "Mexico" }, { "geom": "{\"type\":\"Point\",\"coords\":[-116.915,33.039]}", "country": "United States" } ] } – Patricio Feb 18 '15 at 15:32
1

I figured out the problem. If you use WFS version 1.0.0 sortBy does not work. Using version 1.1.0 works.

1
  • can you share your code for a MWE? - I suspect that is a bug. – Ian Turton Feb 19 '15 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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