1

I am using OpenLayers modify interaction with events modifystart and modifyend. I am trying to remember state of feature before it is modified. I do it this way:

var modificirane_cestice=new Object()

modify.on('modifystart',function(e){
    e.features.forEach(function(feature){
        if(modificirane_cestice[ogc_fid]){
            return }
        else{
            modificirane_cestice[ogc_fid]=new Array()
            modificirane_cestice[ogc_fid].push(new ol.Feature({geometry:feature.getGeometry()}))
            }

    })
    })

 modify.on('modifyend',function(e){
    e.features.forEach(function(feature){
    izmjenjena_geometrija=feature.getGeometry()

    modificirane_cestice[ogc_fid].push(new ol.Feature({geometry:izmjenjena_geometrija}))
    posljednja_geometrija=modificirane_cestice[ogc_fid].length-1
    pg_pov=ol.sphere.getArea(modificirane_cestice[ogc_fid][0].getGeometry())
    zadnja_pov=ol.sphere.getArea(modificirane_cestice[ogc_fid][posljednja_geometrija].getGeometry())
    console.log(pg_pov+".... zadnja pov...."+zadnja_pov)
    postotak=pg_pov/zadnja_pov
    console.log(postotak)
    if(postotak<0.9){
        console.log("postotak prelazi 10% i iznosi: "+(1-postotak)*100+"%")
    }
    })
    })

I expected to get two different areas and features but in the end I got the same one. I know there is problem in referencing feature object but don't know how to solve it. I want to create a new object which are independent on their source object.

3
  • 1
    I think you will need to use geometry:feature.getGeometry().clone() otherwise the features will share the same geometry object and a change to one will change both
    – Mike
    Jun 11, 2019 at 10:32
  • @Mike Thanks, it works now :)
    – Goran
    Jun 11, 2019 at 10:36
  • 1
    @Mike Please, put your comment as an answer so it can be accepted.
    – TomazicM
    Jun 11, 2019 at 12:42

1 Answer 1

4

You need to use geometry:feature.getGeometry().clone() otherwise the features will share the same geometry object and a change to one will change both.

6
  • thanks for the answer, i got the opposite case, i've loaded features from a geojson object from a url, whenever i use modify interaction over it it just creates new vertices while preserving the existing ones, any help on this? thanks Nov 23, 2022 at 11:51
  • @Mohab Khaled Do you only want to move existing vertices? Then you should set insertVertexCondition: never to prevent new ones being added (or use a modifier key to made it harder to accidentally add new vertices).
    – Mike
    Nov 23, 2022 at 13:45
  • never is string? Nov 23, 2022 at 13:55
  • used ol.events.condition.never but still getting new vertices, how can i upload this so you can see what i mean, currently i have a gif showing the case Nov 23, 2022 at 13:59
  • also used false with the same result Nov 23, 2022 at 14:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.