Tuesday, September 27, 2011

OpenLayers with WFS and GeoServer

Today I set up a geometry table in PostGIS and published it with GeoServer as a WFS. My plan was to save features into this layer with WFST, but it worked out very strange: All attributes went through down to the database, but the geometry column was null! I suspected a problem with PostGIS, but I had no problems manually inserting geometries with ST_GeomFromText.

After analyzing the communication between OL and GeoServer with FireBug, I realized that there was something wrong with the geometry column name. I named it "geom" but OL was sending it as "the_geom", which is the default naming.

A look into OL documentation revealed me that you can give it as a parameter geometryName to OpenLayers.Protocol.WFS

        var wfs = new OpenLayers.Layer.Vector("Polygon", {
                strategies : [ new OpenLayers.Strategy.BBOX(), saveStrategy ],
                protocol : new OpenLayers.Protocol.WFS( {
                        version: "1.1.0",
                        srsName: "EPSG:4326",
                        url : "http://localhost:6060/geoserver/wfs",
                        featureType : "buildings",
                        featureNS : "http://www.bjoernhoefling.de/",
                        schema: "http://localhost:6060/geoserver/wfs/DescribeFea
tureType?version=1.1.0&typename=bjoern:buildings",
                    geometryName: 'geom'
                })
        });
        map.addLayer(wfs);