OpenLayers GeoJSON – How to Style Circle Radius Based on GeoJSON Property in OpenLayers 3

geojsonopenlayers

I want to represent the weight of each point of my geojson collection by a circle with a scalable radius.

Here an sample of my GeoJSON file:

"features": [
{ "type": "Feature", "properties": { "id": 20401, "radius": 0.032878 }, "geometry": { "type": "Point", "coordinates": [ 6.569957243078316, 46.744609055776721 ] } },
{ "type": "Feature", "properties": { "id": 20402, "radius": 0.024677 }, "geometry": { "type": "Point", "coordinates": [ 6.56996417375066, 46.744609055776721 ] } },

Is there a way to extract the radius property?

actually I represent my points like this

var points = new ol.layer.Vector({
         source: new ol.source.Vector({
         projection : 'EPSG:4326',
         url: 'weeds.geojson',
         format: new ol.format.GeoJSON(),
      })
  });

It's the default representation who appear on the map.

Best Answer

Like @bartvde pointed out you can use an ol.StyleFunction() (see API):

var points = new ol.layer.Vector({
    source: new ol.source.Vector({
        // ...
        style: function(feature, viewResolution){
            // access your radius property
            var r = feature.get('radius');
            // create your style...
            var style = new ol.style.Style({
                // ...
                image: new ol.style.Circle({
                    // ...
                    radius: r
                })
            });
            return style;
        }
    })
});