WFS Query – Return Only the NumberOfFeatures in a WFS Query

geoservergetfeaturewfs

I'm running a GetFeature WFS query in GeoServer (looking for features within a polygon) but all I actually want is the total number of features. Is there a way of just returning this info (in the hope that the query will run a little faster) rather than all the records?

The query is returning over 10,000 features so I hope it would make a difference if possible.

Best Answer

You can set the parameter resultType to "hits" and you will get the number of features in the query similar to the count() function in SQL.

HTTP Get request example:

http://localhost:8080/geoserver/wfs?request=GetFeature&typeName=topp:states&version=1.1.0&resultType=hits

HTTP Post request example with a filter:

<wfs:GetFeature service="WFS" version="1.1.0"
  resultType="hits"
  xmlns:topp="http://www.openplans.org/topp"
  xmlns:wfs="http://www.opengis.net/wfs"
  xmlns:ogc="http://www.opengis.net/ogc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opengis.net/wfs
                      http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">
  <wfs:Query typeName="topp:states">
    <ogc:Filter>
       <ogc:FeatureId fid="states.3"/>
    </ogc:Filter>
    </wfs:Query>
</wfs:GetFeature>
Related Question