6

I have written a script allowing to display a few WMS and WFS layers and I want to make my 2 WFS layers editable.

I have all my controls and can draw / modify / drag / delete my features but, on one of my layers, the changes are not saved, despite the saveStrategy set to "auto: true".

I give my code here. The layer adresspt_wfs works fine, and the layer bygg_ovr_wfs can't save my new edits. If anyone has an idea?

var map;
var wmsurl = "http://localhost:8080/geoserver/wms";
var wfsurl = "http://localhost:8080/geoserver/wfs";
var projection = new OpenLayers.Projection("EPSG:900913");
var displayProjection = new OpenLayers.Projection("EPSG:4326");

function init() {
  var bounds = new OpenLayers.Bounds(
    1450000, 7490000, 1490000, 7510000);

  var format = 'image/png';

  var options = {
    projection: projection,
    displayProjection: displayProjection,
    maxExtent: bounds,
    units: 'm'
  };

  map = new OpenLayers.Map('map',options);

  // All the base layers:

  var osm = new OpenLayers.Layer.OSM('OpenStreetMap','',
  { zoomOffset: 11, resolutions: [76.4370282714844,38.2185141357422,19.1092570678711,9.55462853393555,4.77731426696777,2.38865713348389,1.19432856674194] });

  var gphys = new OpenLayers.Layer.Google(
  "Google Physical",
  {'sphericalMercator': true, minZoomLevel: 11, maxZoomLevel: 17, type: G_PHYSICAL_MAP});

  var gstr = new OpenLayers.Layer.Google(
  "Google Streets",
  {'sphericalMercator': true, minZoomLevel: 11, maxZoomLevel: 17});

  var ghyb = new OpenLayers.Layer.Google(
  "Google Hybrid",
  {'sphericalMercator': true, minZoomLevel: 11, maxZoomLevel: 17, type: G_HYBRID_MAP});

  var gsat = new OpenLayers.Layer.Google(
  "Google Satellite",
  {'sphericalMercator': true, minZoomLevel: 11, maxZoomLevel: 17, type: G_SATELLITE_MAP});

  var dem = new OpenLayers.Layer.WMS(
  "ASTER Gdem", wmsurl,
  { layers: 'ASTGTM2_N55E013_dem',
      format: format,
      srs:'EPSG:4326',
      transparent:'false'
  },
  {isBaseLayer: true });

  var nobase = new OpenLayers.Layer.WMS(
  "No base map", wmsurl,
  { layers: 'luma_project:background',
    srs: 'EPSG:4326',
    transparent: 'true'
  },
  {isBaseLayer: true, maxExtent: bounds, maxResolution: 'auto'});

  // All the non-base layers (WMS):

  var transportation = new OpenLayers.Layer.WMS(
    "Lund transportation network", wmsurl,
    { layers: 'lu_transportation',
      format: format,
      srs:'EPSG:4326',
      transparent:'true',
      zoomOffset: 11,
    },
    {isBaseLayer: false, visibility: true});

  var adresspt = new OpenLayers.Layer.WMS(
    "Adress points", wmsurl,
    { layers: 'adresspt',
      format: format,
      srs:'EPSG:4326',
      transparent:'true',
      zoomOffset: 11
    },
    {isBaseLayer: false, visibility: false});

  var bygg_ovr = new OpenLayers.Layer.WMS(
    "Buildings", wmsurl,
    { layers: 'bygg_ovr',
      format: format,
      srs:'EPSG:4326',
      transparent:'true',
      zoomOffset: 11
    },
    {isBaseLayer: false, visibility: false});

  // All the non-base layers (WFS):

  var styles = new OpenLayers.StyleMap({
    "default": new OpenLayers.Style({
    fillColor: '#ee9900'
    }),
    "select": new OpenLayers.Style({
    fillColor: 'red'
    })
  });

  var saveStrategy = new OpenLayers.Strategy.Save({auto:true});

  var bygg_ovr_wfs = new OpenLayers.Layer.Vector("Buildings (edition)", {
    stylemap: styles,
    visibility: false,
    strategies: [
      new OpenLayers.Strategy.BBOX(), 
      saveStrategy
      ],
    protocol: new OpenLayers.Protocol.WFS({
      url: wfsurl,
      featurePrefix:"luma_project",
      featureType: "bygg_ovr",
      featureNS: "http://localhost:8080/luma_project",
      srsName: "EPSG:900913",
      geometryName: "the_geom",
      version: "1.1.0"
    })
  });

  var adresspt_wfs = new OpenLayers.Layer.Vector("Adress points (edition)", {
    visibility: false,
    strategies: [
      new OpenLayers.Strategy.BBOX(), 
      saveStrategy
      ],
    protocol: new OpenLayers.Protocol.WFS({
      url: wfsurl,
      featurePrefix:"luma_project",
      featureType: "adresspt",
      featureNS: "http://localhost:8080/luma_project",
      srsName: "EPSG:900913",
      geometryName: "the_geom",
      version: "1.1.0"
    })
  });

  // Identification and edition of layers

  // Edition

  editControls = {
    building_draw: new OpenLayers.Control.DrawFeature(bygg_ovr_wfs, OpenLayers.Handler.Polygon),
    building_drag: new OpenLayers.Control.DragFeature(bygg_ovr_wfs),
    building_modify: new OpenLayers.Control.ModifyFeature(bygg_ovr_wfs),
    building_delete: new DeleteFeature(bygg_ovr_wfs, {title: 'Delete building'}),
    building_click: new OpenLayers.Control.WMSGetFeatureInfo({
        url: wmsurl, 
        title: 'Identify features by clicking',
        layers: [bygg_ovr]
    }),
    building_hover: new OpenLayers.Control.WMSGetFeatureInfo({
        url: wmsurl, 
        title: 'Identify features by hover',
        layers: [bygg_ovr],
        hover: true,
        // defining a custom format options here
        formatOptions: {
            typeName: 'bygg_ovr', 
            featureNS: 'http://localhost:8080/luma_project'
        }
    }),
    adresspt_draw: new OpenLayers.Control.DrawFeature(adresspt_wfs, OpenLayers.Handler.Point),
    adresspt_drag: new OpenLayers.Control.DragFeature(adresspt_wfs),
    adresspt_delete: new DeleteFeature(adresspt_wfs, {title: 'Delete adress point'}),
    adresspt_click: new OpenLayers.Control.WMSGetFeatureInfo({
        url: wmsurl, 
        title: 'Identify features by clicking',
        layers: [adresspt]
    }),
    adresspt_hover: new OpenLayers.Control.WMSGetFeatureInfo({
        url: wmsurl, 
        title: 'Identify features by hover',
        layers: [adresspt],
        hover: true,
        // defining a custom format options here
        formatOptions: {
            typeName: 'adresspt', 
            featureNS: 'http://localhost:8080/luma_project'
        }
    })
  };

  for(var i in editControls) {
    map.addControl(editControls[i]);
    editControls[i].events.register("getfeatureinfo", this, showInfo);
  } 

  // Add the layers on the map

  map.addLayers([osm,gphys,gstr,ghyb,gsat,dem,nobase,transportation,bygg_ovr,bygg_ovr_wfs,adresspt,adresspt_wfs]);


  // Add the controls on the map

  map.addControl(new OpenLayers.Control.Navigation());
  map.addControl(new OpenLayers.Control.LayerSwitcher());
  map.addControl(new OpenLayers.Control.OverviewMap());
  map.addControl(new OpenLayers.Control.Scale($('scale')));
        map.addControl(new OpenLayers.Control.MousePosition({element: $('location')}));

        map.zoomToExtent(bounds);


} // End of init()

function showInfo(evt) {
       $('responseText').innerHTML = evt.text;
} // End of showInfo()


function toggleControlEdit(element) {
  for(key in editControls) {
    var control = editControls[key];
    if(element.value == key && element.checked) {
      control.activate();
    } 
    else {
      control.deactivate();
    }
  }
} // End of toogleControlEdit()

// This control has been found here:
// http://www.peterrobins.co.uk/it/oledit.html

DeleteFeature = OpenLayers.Class(OpenLayers.Control, {
 initialize: function(layer, options) {
    OpenLayers.Control.prototype.initialize.apply(this, [options]);
    this.layer = layer;
    this.handler = new OpenLayers.Handler.Feature(
        this, layer, {click: this.clickFeature}
    );
},
clickFeature: function(feature) {
    // if feature doesn't have a fid, destroy it
    if(feature.fid == undefined) {
        this.layer.destroyFeatures([feature]);
    } else {
        feature.state = OpenLayers.State.DELETE;
        this.layer.events.triggerEvent("afterfeaturemodified", {feature: feature});
        feature.renderIntent = "select";
        this.layer.drawFeature(feature);
    }
},
setMap: function(map) {
    this.handler.setMap(map);
    OpenLayers.Control.prototype.setMap.apply(this, arguments);
},
CLASS_NAME: "OpenLayers.Control.DeleteFeature"
}) // End of DeleteFeature

Thanks in advance!

1
  • Paste your script to jslint.com and check. At beginning it will look evil-strict but once you'll get used to it it can be your best friend. Commented Feb 20, 2012 at 11:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.