-
Notifications
You must be signed in to change notification settings - Fork 5
WMS and WFS Service
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:
-
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.
- Set the SQL statement to
SELECT todofuken,shikuchoson,ooaza,chiban,go,ST_POINT(x,y,4326)
AS geom FROM geocoder( '%address%' )
-
Set the SQL view parameters by clicking on the
Guess parameters from SQL
link. Theaddress
parameter should appear. It is best to set a desiredValidation regular expression
, and for this example[^;']+
is set to prevent SQL injection attacks. -
Set the Attributes list by clicking on the
Refresh
link. The fieldstodofuken,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.
- When the Bounding Box parameters have been set, the new layer can now be saved and used.
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番地
- 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番地
-
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% )
-
Set the SQL view parameters by clicking on the
Guess parameters from SQL
link. Thelon
andlat
parameters should appear. Add a default value for these parameters and set theValidation regular to
[^;']+` to prevent SQL injection attacks. -
Set the Attributes list by clicking on the
Refresh
link. The fieldstodofuken,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 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
More information on GeoServer's Parametric SQL can be seen in GeoServer's Documentation