[OpenLayers-Users] Copy feature

Greg Allensworth gregor at greeninfo.org
Thu Jan 31 07:52:34 PST 2013


On 1/31/2013 2:38 AM, maurotello wrote:
> Hello, I need to copy features to another layer and then edit, realizao
> the procedure but I can not get the correct coordinates of each feature,
> some example of this?
> thanks

I do something similar in a few of my apps: load a vector feature from 
the HIGHLIGHTS layer into the EDITABLES layer, which has a ModifyFeature 
Control.

The technique I use is super simple:

1. There are two Vector layers: HIGHLIGHTS and EDITABLES.

2. getHighlightGeometryByID() fetches the correct Feature based on an 
attribute. I gave them an "id" attribute, you may go by a name or other 
ID field.

3. makeEditableGeometryByID() fetches the Feature, empties out 
EDITABLES, then clones and loads the Feature into EDITABLES.



function getHighlightGeometryByID(id) {
     for (var i=0, l=HIGHLIGHTS.features.length; i<l; i++) {
         var feature = HIGHLIGHTS.features[i];
         if (feature.attributes.id == id) return feature;
     }
     return null;
}

function makeEditableGeometryByID(id) {
     // fetch the HIGHLIGHTS geometry with the given id, clone it into 
the EDITABLES layer
     // this empties the EDITABLES layer first, so this newly-selected 
feature is the only one

     var old_feature = getHighlightGeometryByID(id);
     if (! old_feature) return;

     var new_feature = old_feature.clone()
     EDITABLES.removeAllFeatures();
     EDITABLES.addFeatures([ new_feature ]);

     return new_feature;
}



-- 
Greg Allensworth, Web GIS Developer
BS  A+  Network+  Security+  Linux+  Server+
GreenInfo Network - Information and Mapping in the Public Interest
564 Market Street, Suite 510  San Francisco CA 94104
PH: 415-979-0343 x302  FX: 415-979-0371    email: gregor at greeninfo.org
Web: www.GreenInfo.org     www.MapsPortal.org

Subscribe to MapLines, our e-newsletter, at www.GreenInfo.org


More information about the Users mailing list