I am trying to manage features across different layers.

Features are displaying weather parameters at some points on the map. Hence there is a wind layer, a temperature layer, etc. which all are vector layers styled differently. One point connects one of the features per layer, shaping something like a "column" of different features: a point connects to a wind feature on the wind layer, a temperature feature on the temperature layer, and so on.

Data for the features come from a common GeoJSON file, containing a geographic part and a properties part. It looks like this:

    { "type": "FeatureCollection", 
  "features": [ 
      { "type":     "Feature", 
        "geometry": { 
             "type": "Point", 
             "coordinates" : [-26.142, 48.341] 
        "properties": { 
             "idx" : 0, 
             "name" : "Fetone", 
             "old" : "626", 
             "date" : "2014-06-27 05:50:00", 
             "dbid" : "10370210", 
             "obsid" : "1", 
             "zoom" : "80", 
             "type" : "1", 
             "temp" : "27", 
             "speed" : "6", 
             "dir" : "180", 
             "hum" : "75",
             "pres" : "1011.00"
      // ... ... ...
      { "type":     "Feature", 
        "geometry": { 
             "type": "Point", 
             "coordinates" : [20.125, 62.011] 
        "properties": { 
             "idx" : 200, 
             "name" : "Tohature", 
             "old" : "1826", 
             "date" : "2014-06-27 05:30:00", 
             "dbid" : "10367123", 
             "obsid" : "512", 
             "zoom" : "1", 
             "type" : "4", 
             "temp" : "19", 
             "speed" : "0", 
             "dir" : "", 
             "hum" : "99",
             "pres" : "1015.00"

The way that I distribute these data is by essentially copying the properties to all the layers:

var layer1 = new OpenLayers.Layer.Vector("Wind Layer"); // uses "speed" and "dir" properties to style its features
var layer2 = new OpenLayers.Layer.Vector("Temperature Layer"); // uses "temp" property 
var layer3 = new OpenLayers.Layer.Vector("Humidity Layer"); // uses "hum"
var layer4 = new OpenLayers.Layer.Vector("Pressure Layer"); // uses "pres"
var layer5 = new OpenLayers.Layer.Vector("Position Layer", { // uses "type", "name" and other properties
                    strategies: [ 
                        new OpenLayers.Strategy.Refresh({interval:48000}),
                        new OpenLayers.Strategy.BBOX(),
                        new OpenLayers.Strategy.Fixed() 
                    styleMap: myStyles,
                    protocol: new OpenLayers.Protocol.HTTP({
                        url: someURL,
                        async: true,
                        handleResponse: function(r) {
                            var x=r.priv.responseText;
                            if(/^\s*$/.test(x)) { alert("Empty response!"); }    
                            var i;
                            var arr=[layer1, layer2, layer3, layer4];
                            var j=new OpenLayers.Format.GeoJSON({'internalProjection': mercator,'externalProjection': wgs84 });
                            for(i in arr) {
                            // this is needed for selecting the corresponding feature when a select event happens on layer1 or layer5
                            for(i=0;i<layer5.features.length;i++) { layer1.features[i].idx=layer5.features[i].idx; }
                    renderers: ["SVG","Canvas"], rendererOptions: {zIndexing: true}

However, this seems like taking up too much space, because each layer takes the same copy of the data, and only uses them differently for the style. How can I reduce this amount of data?

The next problem is the selection. I try to keep a property "idx" (from "index") with the same number between connected features, which is used to highlight as selected the associated feature whenever I select a feature. Is there something more efficient in this way, handling a collection of connected features between layers? I don't want an arbitrary selection of features on multiple layers, I want the selection of specific pre-arranged "columns" of features.

Any help is welcome.

  • Where is the data coming from, if you have control o fthe data source, you could just download the appropriate GeoJSON for each layer. – Mark Cupitt Jun 27 '14 at 11:17
  • These data come from my mysql database. I have thought about specialized GeoJSONs for each layer, but I soon realized that many layers have properties which are "too common", only a few of them would be actually eliminated. The example in the question here is a little simplified. I am going to solve it this way if no better solution comes up, anyhow. Now I am trying to figure out if I can make a points' array of the properties and let the layers access them for their data, but it seems like doing the same thing as with GeoJSON loading. – user32916 Jun 29 '14 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.