2

I have an OpenLayers map with a WMS background layer. When clicking the map, I need to draw the WFS feature located on the click.

Note that the vector layer is not loaded yet. I must load/request the mentioned feature on clicking.

How can I make a WFS getFeature() request with the click coordinates as a query parameter?

Is there is a better approach?

3

Both are good alternatives. The direct WFS request should include a Contains filter, or with point-in-polygon search the Intersects filter is as usable.

<wfs:Query typeName="topp:states">
    <Filter>
      <Intersects>
        <PropertyName>the_geom</PropertyName>
          <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
            <gml:coordinates>-74.817265,40.5296504</gml:coordinates>
          </gml:Point>
        </Intersects>
      </Filter>
  </wfs:Query>
</wfs:GetFeature>

You can even use GET instead of POST in the request but URL-encoding the XML filter makes it not look a bit ugly

http://localhost:8080/geoserver/wfs?request=GetFeature&version=1.0.0&typeName=topp:states&outputFormat=GML2&FILTER=%3CFilter%20xmlns=%22http://www.opengis.net/ogc%22%20xmlns:gml=%22http://www.opengis.net/gml%22%3E%3CIntersects%3E%3CPropertyName%3Ethe_geom%3C/PropertyName%3E%3Cgml:Point%20srsName=%22EPSG:4326%22%3E%3Cgml:coordinates%3E-74.817265,40.5296504%3C/gml:coordinates%3E%3C/gml:Point%3E%3C/Intersects%3E%3C/Filter%3E 

Be prepared to spend some time with testing the right order of lat and lon coordinates if you start making filters because unfortunately that depends on your target server, the coordinate reference systen, and WFS version.

Using ID from GetFeatureInfo may feel double trouble but on the other hand WFS version 2.0 defines an mandatory stored query GetFeatureById (some more information in https://docs.geoserver.org/stable/en/user/services/wfs/reference.html).

The syntax of the GetFeatureById is rather simple, here an example taken from the ESRI site https://enterprise.arcgis.com/en/server/latest/publish-services/windows/communicating-with-a-wfs-service-in-a-web-browser.htm

http://gisserver.domain.com/arcgis/services/SampleWorldCities/MapServer/WFSServer?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&STOREDQUERY_ID=urn:ogc:def:query:OGC-WFS::GetFeatureById&ID=Cities.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.