0

Is it possible to draw polygon inside specific boundaries?

draw_interaction = new ol.interaction.Draw({
    source: vector_layer.getSource(),
    type: /** @type {ol.geom.GeometryType} */ ($geom_type.val())
});

map.addInteraction(draw_interaction);

Specify boundaries area in yellow is it possible to add new Geometry Type inside yellow area only? https://jsfiddle.net/vishal05/wb3m90eb/

1 Answer 1

1

You can use ol.geom.Point#getCoordinates() and ol.geom.Polygon#intersectsCoordinate(coordinate) methods together.

I changed your code like this:

function addDrawInteraction() {
    // remove other interactions
    map.removeInteraction(select_interaction);
    map.removeInteraction(modify_interaction);

    // create the interaction
    draw_interaction = new ol.interaction.Draw({
        source: vector_layer.getSource(),
        type: /** @type {ol.geom.GeometryType} */ ($geom_type.val())
    });
    // add it to the map
    map.addInteraction(draw_interaction);
    var isInsideThePolygon=true;
    // when a new feature has been drawn...
    draw_interaction.on('drawend', function(event) {
            if(isInsideThePolygon){
          // create a unique id
          // it is later needed to delete features
          var id = uid();
          // give the feature this id
          event.feature.setId(id);
          // save the changed data
          saveData();
        }
        else{
            setTimeout(function(){
            vector_layer.getSource().removeFeature(event.feature);
          }, 10
          );
        }
    });

    draw_interaction.on('drawstart', function(event) {        
        var drawnPointCoordinates= event.feature.getGeometry().getCoordinates();
        var polyGeometry=polyFeature.getGeometry();
        isInsideThePolygon=polyGeometry.intersectsCoordinate(drawnPointCoordinates);       
    });

}

New JsFiddle link

5
  • it is give me error like this "addrawInteraction() function is not define" on change Geometry type Commented Aug 16, 2017 at 6:58
  • Could you run my jsFiddle link?
    – himyata
    Commented Aug 16, 2017 at 10:10
  • Yes intersectsCoordinate method is only used with openlayer 4.0.1 and above and my version is 3.6.1 is theri any possiblity to used in 3.6.1? Commented Aug 21, 2017 at 7:22
  • What about update your version?
    – himyata
    Commented Aug 21, 2017 at 7:39
  • Their are so many method is deprecated in OpenLayer 4.0.1 so is their any possibility to use intersectsCoordinate method inside 3.6.1? Commented Aug 22, 2017 at 5:28

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.