WFS for the masses: adding support for paging and sorting in GeoServer

Today we are going to introduce you our latest contribution to GeoServer, WFS paging and sorting for retrieving features.
First off, let’s take a step back and see what sorting and paging support is available in the official OGC protocols:
  • Neither WMS 1.1 nor WMS 1.3 (or SLD/SE for that matter), have any ability to order the results so that features are painted in a certain order. If features can be organized in categories filters and FeatureTypeStyle elements can do the trick, but that won’t work over continous fields
  • Same goes for WPS 1.0, which can return significant amounts of vector data that might be useful to page over
  • WFS 1.0 does not support either, WFS 1.1 supports sorting, WFS 2.0 supports sorting and paging via the sortBy and startIndex/maxFeature parameters
The last stable GeoServer release does not support WFS 2.0, and allows for sorting only on DBMS based stores. In our latest contribution to the stable series we removed all limitations concerning WFS paging and sorting support:
  • WFS 1.0 and 1.1 can now support sorting on top of each and every store kind, using the sortBy parameter as a vendor extension
  • WFS 1.0 and 1.1 support paging on top of each and every store, using the startIndex/maxFeatures parameters
Protocol and paging wise we back-ported the work added on trunk along with WFS 2.0 support, and then we merged in some previous work on generic sorting we did for the aggregating store.
Technically speaking, in case the store does not support sorting natively (e.g., shapefile) we gather the features into an optimized merge/sort algorithm that never keeps more than 1000 features in memory, and uses secondary storage to scale up to larger result sets.
But, enough talking, let’s see some examples against the states demo layer, using the CSV as the output format for brevity.
First ten features:
http://localhost:8080/geoserver/topp/owsservice=WFS&version=1.0.0&request=GetFeature
&typeName=topp:states&outputFormat=csv&propertyName=STATE_NAME,PERSONS
&maxFeatures=10
FID,STATE_NAME,PERSONS
states.1,Illinois,11430602
states.2,District of Columbia,606900
states.3,Delaware,666168
states.4,West Virginia,1793477
states.5,Maryland,4781468
states.6,Colorado,3294394
states.7,Kentucky,4551524
states.8,Kansas,2477574
states.9,Virginia,6180651
states.10,Missouri,5117073
The next 10 features:
http://localhost:8080/geoserver/topp/ows?service=WFS&version=1.0.0&request=GetFeature
&typeName=topp:states&outputFormat=csv&propertyName=STATE_NAME,PERSONS
&maxFeatures=10&startIndex=10
FID,STATE_NAME,PERSONS
states.11,Arizona,3665228
states.12,Oklahoma,3145585
states.13,North Carolina,6628629
states.14,Tennessee,4829958
states.15,Texas,17122020
states.16,New Mexico,1379559
states.17,Alabama,4040587
states.18,Mississippi,2573216
states.19,Georgia,6457339
states.20,South Carolina,3486703
The first ten states with most people (sort on PERSONS, descending):
http://localhost:8080/geoserver/topp/ows?service=WFS&version=1.0.0&request=GetFeature
&typeName=topp:states&outputFormat=csv&propertyName=STATE_NAME,PERSONS
&maxFeatures=10&sortBy=PERSONS%20D
FID,STATE_NAME,PERSONS
states.47,California,29760021
states.39,New York,18235907
states.15,Texas,17122020
states.23,Florida,12937926
states.40,Pennsylvania,11881643
states.1,Illinois,11430602
states.48,Ohio,9980887
states.24,Michigan,9295297
states.43,New Jersey,7484736
states.13,North Carolina,6628629
The second page of the above result set:
http://localhost:8080/geoserver/topp/ows?service=WFS&version=1.0.0&request=GetFeature
&typeName=topp:states&outputFormat=csv&propertyName=STATE_NAME,PERSONS
&maxFeatures=10&startIndex=10&sortBy=PERSONS%20D
FID,STATE_NAME,PERSONS
states.19,Georgia,6457339
states.9,Virginia,6180651
states.37,Massachusetts,6016425
states.44,Indiana,5544159
states.10,Missouri,5117073
states.49,Washington,4866692
states.14,Tennessee,4829958
states.30,Wisconsin,4796441
states.5,Maryland,4781468
states.7,Kentucky,4551524
These modifications are already available in the GeoServer 2.1.3 release.
There is more work to be done in this area, ordering features before paiting them in WMS, and paging WPS results would both be nice additions. Interested? Let us know!
The GeoSolutions team,
 
  • Good stuff. Glad to see the work we started improved upon and ported back to the stable branch. Great work guys!

  • Ciao Justin, thank you for doing the heavy-lifting on this 😉