Skip to content

WMS and WFS Service

Mario Basa edited this page Aug 19, 2024 · 5 revisions

Creating a WMS/WFS service for pgGeocoder is quite straightforward when using GeoServer and its Parametric SQL View Layers. To configure GeoServer to serve both the geocoder and reverse_geocoder functionalities, the following can be done:

Geocoder

Setup

  • Create a PostgreSQL Store on the database that contains pgGeocoder application and its data.

  • Create a new Layer, and click on the Configure new SQL view... link.

Screenshot 2024-08-14 at 15 02 48
  • Set the SQL statement to
SELECT todofuken,shikuchoson,ooaza,chiban,go,ST_POINT(x,y,4326) 
 AS geom FROM geocoder( '%address%' )
Screenshot 2024-08-14 at 15 06 11
  • Set the SQL view parameters by clicking on the Guess parameters from SQL link. The address parameter should appear. It is best to set a desired Validation regular expression, and for this example [^;']+ is set to prevent SQL injection attacks.

  • Set the Attributes list by clicking on the Refresh link. The fields todofuken,shikuchoson,ooaza,chiban,go,geom should appear.

  • Once the SQL View parameters have been set, press Save which will bring back the New Layer creation page.

  • From there, set the Bounding Box of the Layer. Since the geocoder returns a 4326 projection, the basic world bounding parameters -180,-90,180,90 can be used.

Screenshot 2024-08-14 at 15 07 22
  • When the Bounding Box parameters have been set, the new layer can now be saved and used.

Usage

Once the layer has been created, it is now possible to dynamically geocode an address and have a map returned in the case of WMS, or have the geocoded address' attributes retured for WFS. This is done by setting a value to the SQL parameter that was previously configured and passing that value into the request URL with the viewparams added parameter.

So to display the point of the Kyoto Address 京都市上京区竹屋町通り松屋町西入ル藁屋町535番地, the URL with the viewparams parameter will be:

http://localhost:8080/geoserver/?....&viewparams=address:京都市上京区竹屋町通り松屋町西入ル藁屋町535番地
  • WMS Sample URL with the Geocoder Request
http://localhost:8080/geoserver/geocoder/wms?service=WMS&version=1.1.0&request=GetMap&layers=geocoder:pale,geocoder:geocoder&bbox=1.4437799E7%2C3596807.25%2C1.5857083E7%2C5361010.0&width=900&height=500&srs=EPSG%3A3857&styles=&format=application/openlayers&viewparams=address:京都市上京区竹屋町通り松屋町西入ル藁屋町535番地
Screenshot 2024-08-14 at 16 08 07
  • WFS Sample URL with the Geocoder Request
http://localhost:8080/geoserver/wfs?service=WFS&version=1.1.0&request=GetFeature&typenames=geocoder:geocoder&viewparams=address:京都市上京区竹屋町通り松屋町西入ル藁屋町535番地
Screenshot 2024-08-14 at 16 18 50

Reverse Geocoder

Setup

  • Creating a new layer for Reverse Geocoder will be very similar to the layer creation process of the Geocoder Layer.

  • Create a new Layer, and click on the Configure new SQL view... link.

  • Set the SQL Statement to:

select code,todofuken,shikuchoson,ooaza,chiban,go,ST_POINT(x,y,4326) as geom 
  from reverse_geocoder( %lon%, %lat% )
Screenshot 2024-08-14 at 15 57 23
  • Set the SQL view parameters by clicking on the Guess parameters from SQL link. The lon and lat parameters should appear. Add a default value for these parameters and set the Validation regular to [^;']+` to prevent SQL injection attacks.

  • Set the Attributes list by clicking on the Refresh link. The fields todofuken,shikuchoson,ooaza,chiban,go,geom should appear.

  • Once the SQL View parameters have been set, press Save which will bring back the New Layer creation page.

  • From there, set the Bounding Box of the Layer. Since the geocoder returns a 4326 projection, the basic world bounding parameters -180,-90,180,90 can be used.

Usage

Usage will be the same as that of the Geocoder, wherein the lon and lat values needed by the Reverse Geocoder is sent to the request URL parameter viewparams. For example:

http://localhost:8080/geoserver/?....&viewparams=lat:35.016256;lon:135.748032
  • WFS Sample URL with the Reverse Geocoder Request
http://localhost:8080/geoserver/wfs?service=WFS&version=1.1.0&request=GetFeature&typenames=geocoder:reverse_geocoder&viewparams=lat:35.016256;lon:135.748032
Screenshot 2024-08-14 at 16 25 00

Note

More information on GeoServer's Parametric SQL can be seen in GeoServer's Documentation