2

I am new to OpenLayers and I wanted to add a modify feature function in my application, for this I need to know the feature type of the layer (i.e. point, polygon or line). Because as per the feature type I have to give permission to the user to draw a line, point or polygon.

So is there a way that I can get the layer feature type?

  • Are you using OpenLayers 2 or 3? Unfortunately there aren't any rules that forces vector layers to contain only one type of features. Maybe your best chance is to write a rule for it manually, then you can bind permissions to the name of the layers you wrote the rules for. Otherwise you have to iterate through each feature in the layer and ask for their type. – Gabor Farkas Sep 16 '14 at 11:40
  • We are using OpenLayers 3 – user34591 Sep 16 '14 at 11:46
1

As suggested in comments Layers, to be more precise, Sources don't restrict to be of one geometry type. If you want that you can extend ol.source.Vector to only allow one geometry type. Let's say we only want to include ol.geom.Point's in ol.source.Vector :

function PointSource (args){
  ol.source.Vector.call(this, args);
  this.typeOfGeometry = 'Point';
  this.on('addfeature', function(e){
    if(e.feature.getGeometry() instanceof ol.geom.Point)
      console.log('point');
    else {
      console.log('no point');
      this.removeFeature(e.feature);
    }
  });
}

PointSource.prototype = Object.create(ol.source.Vector.prototype);
PointSource.prototype.constructor = PointSource;

Now, you could do :

var myPointsLayer = new ol.layer.Vector({
  source : new PointSource({...})
});

See a fiddle in action : https://jsfiddle.net/cxykqvwf/

0

OpenLayers3 ol.interction.Draw has a type property where you can restrict the type of features you can draw on a given layer.

With this restriction, you can check the type of the first feature of a layer before allow editing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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