2

In Openlayers 6, is it possible to set z-index at the feature level instead of the layer level to get a result like this:

Given 3 layers:

  1. Layer of red triangles
  2. Layer of blue rectangles
  3. Layer of green stars

enter image description here

The idea is to be able to stack vertically a mixed of features coming from different layers. Thanks

Philiz
  • 429
  • 5
  • 25
  • you can try to set a different style for every feature. with a style function you can change the z-index https://openlayers.org/en/latest/apidoc/module-ol_style_Style-Style.html – Sebastian Nov 02 '21 at 13:55
  • 2
    You cannot override layer zIndex. Similar to clustering from multiple source https://stackoverflow.com/questions/38179782/clustering-across-layers you could create a combined layer and synchronise adding and removing features from the parent layers. Set the opacity of the parent layers to 0 so they are loaded but not visible. – Mike Nov 03 '21 at 13:43

1 Answers1

2

function styleFunction(featuretype){
     var style;
     style= new ol.style.Style({
         stroke: new ol.style.Stroke({
             color: "rgba(255,255,0,1)",
             width: 8,
         }),
         fill : new ol.style.Fill({
             color: "rgba(255,0,0,1)",
         })
     });
     return style;
 }

var style100 = new ol.style.Style({
         stroke: new ol.style.Stroke({
             color: "rgba(255,255,0,1)",
             width: 8,
         }),
         fill : new ol.style.Fill({
             color: "rgba(255,0,0,1)",
         }),
         zIndex:100
     });

var style1 = new ol.style.Style({
         stroke: new ol.style.Stroke({
             color: "rgba(255,255,0,1)",
             width: 8,
         }),
         fill : new ol.style.Fill({
             color: "rgba(255,0,0,1)",
         }),
         zIndex:1
     });

var vectorS = new ol.source.Vector({});

var thing = new ol.geom.Polygon( [[
    ol.proj.transform([-16,-22], 'EPSG:4326', 'EPSG:3857'),
    ol.proj.transform([-44,-55], 'EPSG:4326', 'EPSG:3857'),
    ol.proj.transform([-88,75], 'EPSG:4326', 'EPSG:3857')
]]);
var featurething = new ol.Feature({
    name: "Thing",
    geometry: thing
});


var thing2 = new ol.geom.Polygon( [[
    ol.proj.transform([-10,-15], 'EPSG:4326', 'EPSG:3857'),
    ol.proj.transform([-60,-70], 'EPSG:4326', 'EPSG:3857'),
    ol.proj.transform([-87,74], 'EPSG:4326', 'EPSG:3857')
]]);
var featurething2 = new ol.Feature({
    name: "Thing2",
    geometry: thing2
});

vectorS.addFeature( featurething );
vectorS.addFeature( featurething2 );

var vectorL = new ol.layer.Vector({
  source: vectorS,
  style : styleFunction("test")
});

var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          }),
          vectorL
        ],
        view: new ol.View({
          center: ol.proj.fromLonLat([-16, -22]),
          zoom: 2
        })
      });
 var source = vectorL.getSource();
 var features = source.getFeatures();
 
features[0].setStyle(style100);
features[1].setStyle(style1);
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/css/ol.css" type="text/css">
    <style>
      .map {
        height: 400px;
        width: 100%;
      }
    </style>
    <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/build/ol.js"></script>
    <title>OpenLayers example</title>
  </head>
  <body>
    <h2>My Map</h2>
    <div id="map" class="map"></div>
  </body>
</html>

if you switch features[0].setStyle(style100); features[1].setStyle(style1); to features[1].setStyle(style100); features[0].setStyle(style1);

the zIndex of the features will change but you can not change to zIndex between layers. features from a layer with a higher zIndex will always on top of features from a layer with a smaller zIndex

Sebastian
  • 315
  • 1
  • 10
  • And what if all layers have the same z-index but each feature has a style with a different z-index? – Philiz Nov 03 '21 at 13:39