In this recipe, you first created a point PostGIS table and then published it as WFS-T, using GeoServer. You then created a basic OpenLayers application, using the WFS-T layer, allowing the user to add features to the underlying PostGIS layer.
In OpenLayers, the core object needed to implement such a service is the vector layer by defining a WFS protocol. When defining the WFS protocol, you have to provide the WFS version that is using the spatial reference system of the dataset, the URI of the service, the name of the layer (for GeoServer, the name is a combination of the layer workspace, FeaturePrefix, and the layer name, FeatureType), and the name of the geometry field that will be modified. You also can pass to the Vector layer constructor a StyleMap value to define the layer's rendering behavior.
You then tested the application by adding some points to the OpenLayers map and checked that those points were effectively stored in PostGIS. When adding the points using the WFS-T layer, with the help of tools such as Firefox Firebug or Chrome (Chromium) Developer Tools, you could dig in detail into the requests that you are making to the WFS-T and its responses.
For example, when adding a point, you will see that an Insert request is sent to WFS-T. The following XML is sent to the service (note how the point geometry is inserted in the body of the <wfs:Insert> element):
<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs"
service="WFS" version="1.1.0"
xsi:schemaLocation="http://www.opengis.net/wfs
http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <wfs:Insert> <feature:sites xmlns:feature="http://www.packtpub.com/
postgis-cookbook/book"> <feature:the_geom> <gml:Point xmlns:gml="http://www.opengis.net/gml"
srsName="EPSG:4326">
<gml:pos>12.450561523436999 41.94302128455888</gml:pos>
</gml:Point> </feature:the_geom> </feature:sites> </wfs:Insert> </wfs:Transaction>
The <wfs:TransactionResponse> response, as shown in the following code, will be sent from WFS-T if the process has transpired smoothly and the features have been stored (note that the <wfs:totalInserted> element value in this case is set to 1, as only one feature was stored):
<?xml version="1.0" encoding="UTF-8"?> <wfs:TransactionResponse version="1.1.0" ...[CLIP]... > <wfs:TransactionSummary>
<wfs:totalInserted>1</wfs:totalInserted>
<wfs:totalUpdated>0</wfs:totalUpdated>
<wfs:totalDeleted>0</wfs:totalDeleted>
</wfs:TransactionSummary> <wfs:TransactionResults/> <wfs:InsertResults> <wfs:Feature> <ogc:FeatureId fid="sites.17"/> </wfs:Feature> </wfs:InsertResults> </wfs:TransactionResponse>