More

GeoTools WFS Query sortBy not working for GeoServer WFS

GeoTools WFS Query sortBy not working for GeoServer WFS


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 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 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?


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


Watch the video: Web Features Service WFS crash course. GeoServer with Leaflet. Tekson