1

I have a vector layer where some features are found entirely within other features and therefore I can't select that feature because the other feature is on top of it. Is there a way to reverse the order of the features of a vector layer after they have already been loaded? This will likely solve about 99% of issues that may occur with my mouseover popup.

Edit to add code:

I'm trying to do this on demand after the layer is already loaded.

var refreshStrategy = new OpenLayers.Strategy.Refresh( {
    interval : 60 * 1000
  });   
var vLayer = new OpenLayers.Layer.Vector("Hazards with Population",
      {
        projection : "EPSG:4326",
        strategies : [new OpenLayers.Strategy.Fixed(), refreshStrategy],
        protocol : new OpenLayers.Protocol.HTTP(
        {          
          url : "./getFeatures.php",              
          format : new OpenLayers.Format.GeoJSON(),
          rendererOptions : {
            zIndexing : true
          }
        })
      });
1
  • 1
    How are you currently getting your data/what have you got at the moment? What is the problem preventing you from reversing the data you are stuffing into the layer? Commented Apr 25, 2013 at 14:54

1 Answer 1

3

Supposing you have access and can modify your getFeatures.php, simply reverse the array where the data is being formatted as GeoJSON.

If it's coming from a database, reverse your order by clauses. Or if it's coming from some other source, reverse the array just before formatting it as GeoJSON:

$data = array(item1, item2, item3, ...); // Your array of data the way it's loaded currently
$reversed = array_reverse($data); // Reverse the array
makeGeoJSON($reversed); // Transfor it into GeoJSON the way you're doing currently

If you don't have access to the php code, you probably can achieve the same effect by adding an event listener to the beforefeaturesadded event on the layer:

vectorLayer.events.register('beforefeaturesadded', vectorLayer, function (data) {
    data.features.reverse();
});
4
  • That would be nice, but I don't have access to the getFeatures.php. I was hoping there was a javascript way to flip the feature order.
    – Jonathan
    Commented Apr 25, 2013 at 15:56
  • 1
    Updated my answer to include a javascript-only solution. Commented Apr 25, 2013 at 16:12
  • Thanks Rene! I should have been more clear in my initial question. I already have them loaded and want to switch them after the load. Like when a user fires an event. I updated the question to reflect that.
    – Jonathan
    Commented Apr 29, 2013 at 14:56
  • @René Wolferink is right. featureArrayList.reverse() should help you...
    – urcm
    Commented Apr 29, 2013 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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