[GIS] How to get layer feature type in OpenLayers

featuresopenlayers

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?

Best Answer

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/