Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rule based Styling in openlayers 3

Tags:

openlayers-3

I am trying to do the rule based styling in openlayers 3, here is my code:

<!DOCTYPE html>
<html>
<head>
<title>CONFIG</title>


<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://openlayers.org/en/v3.8.2/css/ol.css" type="text/css">
<script src="http://openlayers.org/en/v3.8.2/build/ol.js"></script>

</head>
<body>
<div class="container-fluid">

<div class="row-fluid">
  <div class="span12">
    <div id="map" class="map"></div>
  </div>
</div>
<script>

    var layerStyle   = {
         default: {
        style: new ol.style.Style({
        stroke: new ol.style.Stroke({
        color: 'rgba(0, 0, 0, 1.0)',
        width: 0.5
            }),
        fill: new ol.style.Fill({
        color: 'rgba(0, 255, 0, 0.5)'
            })
          })
        },

        filter: [{
        type: "AND",
        rules: [{
            type: "==",
            property: "CODE",
            value: "16"
        }, {
            type: "==",
            property: "NAME",
            value: "CHIKMAGALUR"
        }],
        symbolizers: {
        style: new ol.style.Style({
        stroke: new ol.style.Stroke({
                color: 'rgba(0, 255, 0, 0.5)',
                width: 0.5
            }),
        fill: new ol.style.Fill({
                color: 'red'
            })
          })
        }
    } ]
    };

    var geojson_layer = new ol.layer.Vector({
    source: new ol.source.Vector({
        url: 'file.geojson',
        format: new ol.format.GeoJSON()
    }),
});
var vectorLayer = new ol.source.Vector({
        features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
    });

        if ('CODE'=='16' && 'NAME'=='CHIKMAGALUR') {
        style: symbolizers;
        }




        var map = new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    }),
   vectorLayer, 
  ],
  target: 'map',
  controls: ol.control.defaults({
    attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
      collapsible: false
    })
  }),
  view: new ol.View({
    center: [0, 0],
    zoom: 5
  })
});

</script>
</body>
</html>

I am trying trying to get the style from symbolizer, but I am getting error. Like openlayer2 I used the filters for AND operation but I stucked. Can someone help me on this??

like image 281
Rajan Avatar asked Sep 24 '15 15:09

Rajan


1 Answers

In OL3 vector layers have a style attribute that can be a Style object, an array of Style objects or a function that returns a Style object or an array of Style objects. You probably want to use a style function for what you are trying to do.

See the following example for how this can be done:

http://openlayers.org/en/v3.20.1/examples/earthquake-clusters.html

like image 128
Bob Holmes Avatar answered Oct 21 '22 06:10

Bob Holmes