WFS Filter – Why Spatial Filter (DWITHIN) is Not Working in WFS

filterst-dwithinweb-mappingwfs

I am working using spatial filter Dwithin for WFS. I have worked using a simple filter using to show no of earthquake data for the year 2020 in GeoJSON format from the given WFS:-
https://emidius.mi.ingv.it/services/italy/wfs/?service=wfs&version=2.0.0&request=GetFeature&typeNames=italy:CPTI_current&outputFormat=application/json&FILTER=%3CFilter%3E%3CPropertyIsEqualTo%3E%3CPropertyName%3EYear%3C/PropertyName%3E%3CLiteral%3E2020%3C/Literal%3E%3C/PropertyIsEqualTo%3E%3C/Filter%3E

However, I could get the value of no of earthquake points at a distance 500m from coordinates 14.964, 37.696 in GeoJSON format.The output is all GeoJSON data being selected which is not true. The link with spatial filter (that I have used for the provided WFS) is as shown:-
https://emidius.mi.ingv.it/services/italy/wfs/?service=wfs&version=2.0.0&request=GetFeature&typeNames=italy:CPTI_current&outputFormat=application/json&%20FILTER=%3C%20Filter%3E%3CWithin%3E%3CPropertyName%3Egeometry%3C/PropertyName%3E%3CPoint%3E%3Ccoordinates%3E14.964,%2037.696%3C/%20coordinates%20%3E%3C/Point%3E%3CDistance%20units=%27m%27%3E5000%3C/Distance%3E%3C/Within%3E%3C/Filter%3E

Best Answer

There are multiple problems with your filter:

  1. The space before FILTER prevents the server parsing and using it
  2. You spelled DWithin wrong (Within is a different filter entirely)
  3. You are using the wrong geometry name (it's geom)
  4. Your unit needs to be meters
  5. Your point should not have a comma separating the coordinates.
  6. Probably a bunch of other stuff

So I switched to using CQL

https://emidius.mi.ingv.it/services/italy/wfs/?service=wfs&version=2.0.0&request=GetFeature&typeNames=italy:CPTI_current&outputFormat=application/json&CQL_FILTER=dwithin(geom,%20POINT(14.964%2037.696),%20500,%20meters)

which works but says there are no earthquakes with in 5 km of your point, but does find 46 if I flip the axis order of your point to give a dwithin(geom, POINT(37.696 14.964), 5, kilometers) and a final URL of:

https://emidius.mi.ingv.it/services/italy/wfs/?service=wfs&version=2.0.0&request=GetFeature&typeNames=italy:CPTI_current&outputFormat=application/json&CQL_FILTER=dwithin(geom,%20POINT(37.696%2014.964),%205,%20kilometers)

Or if you prefer the OGC representation:

<?xml version="1.0" encoding="UTF-8"?>                                                                                  
<ogc:Filter xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc">
  <ogc:DWithin>                                                                                                         
    <ogc:PropertyName>geom</ogc:PropertyName>                                                                           
    <gml:Point>                                                                                                         
      <gml:coord>                                                                                                       
        <gml:X>37.696</gml:X>                                                                                           
        <gml:Y>14.964</gml:Y>                                                                                           
      </gml:coord>                                                                                                      
    </gml:Point>                                                                                                        
    <ogc:Distance units="kilometers">5.0</ogc:Distance>                                                                 
  </ogc:DWithin>                                                                                                        
</ogc:Filter>                                                                                                           
Related Question