Monday, 7 September 2015

Modify WFS with OpenLayers


I want to edit object on WFS layer but have one problem. I use OpenLayers.Control.ModifyFeature(myVecLayer) to editing.By the logic i do this.




  1. Press button and create WFS layer with chosen object

  2. Add OpenLayers.Control.ModifyFeature to map controls and activate this

  3. Edit object

  4. Press button again and remove WFS layer with my object, deactivate OpenLayers.Control.ModifyFeature control.


  5. Select next object (or same object) and do "1", "2"


    And object not editing.
    But if I do not remove layer after editing, but just add new WFS layer to map OpenLayers.Control.ModifyFeature control works





My code:


function ff(){
if(counter==0){
//WFS
var myStyle = OpenLayers.Util.extend({},
OpenLayers.Feature.Vector.style['default']);
myStyle.strokeWidth = 1.5;
myStyle.strokeColor = "#ff0000";
myStyle.fillOpacity = 0.1;
OpenLayers.ProxyHost = "proxy.cgi?url=";

var renderer = OpenLayers.Util.getParameters(window.location.href).renderer;
renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;
myVecLayer = new OpenLayers.Layer.Vector("Редактируемый участок", {
strategies: [new OpenLayers.Strategy.BBOX(), new OpenLayers.Strategy.Save()],
style: myStyle,
protocol: new OpenLayers.Protocol.WFS({
version: "1.0.0",
srsName:"EPSG:900913",
url: "http://localhost:8080/geoserver/wfs",
featureType: "filedata",

featureNS: "http://www.opengeospatial.net/cite",
defaultFilter : new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: 'id',
value: "162",
})
}),
renderers: renderer
})
map.addLayers([myVecLayer]);



if(ppp==null){
if (edit==null){
edit = new OpenLayers.Control.ModifyFeature(myVecLayer);
map.addControl(edit);
ppp=map.getControlsByClass("OpenLayers.Control.ModifyFeature");
}

alert(ppp);

}
// ppp.autoActivate=true;
for(var i=0;i ppp[i].activate();
alert("2 "+ppp[i].active);
}

counter=1;

}


Its will nice if u help.



Answer



OK. I find answer to my question. Just need to use filters.


//WFS
var myStyle = OpenLayers.Util.extend({},
OpenLayers.Feature.Vector.style['default']);
myStyle.strokeWidth = 1.5;
myStyle.strokeColor = "#ff0000";
myStyle.fillOpacity = 0.1;

OpenLayers.ProxyHost = "proxy.cgi?url=";
var renderer = OpenLayers.Util.getParameters(window.location.href).renderer;
renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;
myVecLayer = new OpenLayers.Layer.Vector("Редактируемый участок", {
strategies: [new OpenLayers.Strategy.BBOX(), SS],
style: myStyle,
protocol: new OpenLayers.Protocol.WFS({
version: "1.0.0",
srsName:"EPSG:900913",
url: "http://localhost:8080/geoserver/wfs",

featureType: "filedata",
featureNS: "http://www.opengeospatial.net/cite",
defaultFilter : filter
}),
renderers: renderer
})

Filters+Editing


if(counter==0){
if(ppp==null){

if (edit==null){
edit = new OpenLayers.Control.ModifyFeature(myVecLayer);
map.addControl(edit);
ppp=map.getControlsByClass("OpenLayers.Control.ModifyFeature");
}
}

map.addLayers([myVecLayer]);
Ffilter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,

property: 'id',
value: '170',
})
myVecLayer.filter=Ffilter;
myVecLayer.refresh({force: true});
for(var i=0;i ppp[i].activate();
}
counter=1;


}
else {
//FS.deactivate();
// SS.save();
//myVecLayer.destroyFeatures();
map.removeLayer(myVecLayer);
for(var i=0;i ppp[i].deactivate();
}
//ppp.deactivate();

counter=0;
}

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...