Class WFSHitsCountUtil

java.lang.Object
org.tailormap.api.geotools.wfs.WFSHitsCountUtil

public final class WFSHitsCountUtil extends Object
Counts features with a WFS resultType=hits GetFeature request when GeoTools does not.

GeoTools only does a hits request for WFS 1.x. For WFS 2.0.0 the StrictWFS_2_0_Strategy does not support hits (its pull parser can't read the count), so FeatureSource.getCount(Query) always returns -1 without asking the server. This class does the hits request itself, using the GeoTools request so the filter is encoded the same way as for the GetFeature request for the features, and reads the numberMatched attribute.

A hits request always counts, also on MapServer which returns numberMatched="unknown" in a GetFeature response with features (unless wfs_compute_number_matched is set). Counting can be slow for large layers. See getCount(SimpleFeatureSource, Query, Duration, Duration) for not waiting long for a slow count.

  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    getCount(org.geotools.api.data.SimpleFeatureSource fs, org.geotools.api.data.Query query)
    Count the features of a feature source.
    static int
    getCount(org.geotools.api.data.SimpleFeatureSource fs, org.geotools.api.data.Query query, Duration hitsTimeout, Duration slowHitsTtl)
    Count the features of a feature source like getCount(SimpleFeatureSource, Query), but without waiting long for a slow hits request.
    static int
    Read the number of features from a GetFeature hits response: the numberMatched attribute (WFS 2.0.0) or the numberOfFeatures attribute (WFS 1.x) of the root FeatureCollection element.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getCount

      public static int getCount(org.geotools.api.data.SimpleFeatureSource fs, org.geotools.api.data.Query query) throws IOException
      Count the features of a feature source. Calls FeatureSource.getCount(Query) and if that returns -1 for a WFS which GeoTools does not count with a hits request (WFS 2.0.0), does a hits request.
      Parameters:
      fs - the feature source, may be of any type
      query - the query
      Returns:
      the number of features, or -1 if unknown
      Throws:
      IOException - when FeatureSource.getCount(Query) fails; a failing hits request returns -1
    • getCount

      public static int getCount(org.geotools.api.data.SimpleFeatureSource fs, org.geotools.api.data.Query query, Duration hitsTimeout, Duration slowHitsTtl) throws IOException
      Count the features of a feature source like getCount(SimpleFeatureSource, Query), but without waiting long for a slow hits request. For when the count is nice to have, such as the total for a page of features.

      A hits request which takes longer than hitsTimeout is aborted and the feature type is remembered as slow, so it isn't counted with a hits request for slowHitsTtl and the count is -1 without waiting. Some servers count very slowly for large layers (PDOK NWB wegvakken: ~18 s for a hits request versus 0.1 s for a page of features), while counting is quick for most.

      Parameters:
      fs - the feature source, may be of any type
      query - the query
      hitsTimeout - how long to wait for a hits request, in whole seconds (at least 1 s, the HTTP read timeout)
      slowHitsTtl - how long not to count a feature type after its hits request timed out
      Returns:
      the number of features, or -1 if unknown or too slow to count
      Throws:
      IOException - when FeatureSource.getCount(Query) fails; a failing hits request returns -1
    • parseNumberMatched

      public static int parseNumberMatched(InputStream in) throws XMLStreamException
      Read the number of features from a GetFeature hits response: the numberMatched attribute (WFS 2.0.0) or the numberOfFeatures attribute (WFS 1.x) of the root FeatureCollection element.
      Parameters:
      in - the response
      Returns:
      the number of features, or -1 if the response is not a feature collection (for example an exception report) or the number is not a non-negative integer (for example "unknown")
      Throws:
      XMLStreamException - if the response is not XML