6 votos

WFS-T: los cambios no se guardan en un mapa de OpenLayers

He escrito un script que permite mostrar unas capas WMS y WFS y quiero que mis 2 capas WFS sean editables.

Tengo todos mis controles y puedo dibujar / modificar / arrastrar / eliminar mis características, pero, en una de mis capas, los cambios no se guardan, a pesar de la saveStrategy establecido en "auto: true".

Doy mi código aquí. La capa adresspt_wfs funciona bien, y la capa bygg_ovr_wfs no puedo guardar mis nuevas ediciones. ¿Si alguien tiene una 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

Gracias de antemano.

2voto

Jarrett Meyer Puntos 11147

Pegue su script en http://jslint.com/ y comprueba.

Al principio te parecerá malvado pero una vez que te acostumbres a él puede ser tu mejor amigo.

i-Ciencias.com

I-Ciencias es una comunidad de estudiantes y amantes de la ciencia en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X