0

I have gx_mappannel with id:mappannel. I added two layers to this panel: OSM layer and one vector layer, here is the sample code:

          Ext.onReady(function(){
              ...
              {
                xtype: "gx_mappanel",
                ref: "mapPanel",
                id:"mappanel",
                region: "center",
                title: 'my title',
                map:{
                    maxExtent: extnt
                    numZoomLevels:zomlevel,

                    projection: myprojection,
                    center:theCenter
                  },

                 layers:[new OpenLayers.Layer.OSM(),

                       vlayer= new OpenLayers.Layer.Vector("vlayer",
                           {
                            projection:myprojection,
                            strategies:[new OpenLayers.Strategy.Fixed()],
                            protocol:new OpenLayers.Protocol.HTTP({
                                url: "mygeojson", 
                                format: new OpenLayers.Format.GeoJSON()
                            }),

                          })

                     ]
             },
            .....
        }

I checked that the two layers are added to the pannel using this code

           myPanel = Ext.getCmp("mappanel");
           console.log(mapPanel.map.layers.length);

The console prints 2, and that is right.

The vector layer produces geojson data of the form

"features":[{"geometry":{"coordinates":[938423.3125,6653195.5],"type":"Point"},"type":"feature","properties":{"count":7}},{"geometry":{"coordinates":[903914.25,6653195.5],"type":"Point"},"type":"feature","properties":{"count":7}},{"geometry":{"coordinates":[888329.5625,6644321.5],"type":"Point"},"type":"feature","properties":{"count":5}},....

I want to iterate over the vector layer and calculate sum of the "count" values. I mean this one-("properties":{"count":7}). I made some tries but not successful yet.

Can any one help?

0
syntax in general:

var layer_sum=0;
    for(var i=0;i<vlayer.features.length;i++)
    { 
    layer_sum=layer_sum+vlayer.features[i].attributes.count;
    }
    console.log(layer_sum);

depending on the question if you want to parse values as int / float you could replace the row then with

 layer_sum=layer_sum+vlayer.features[i].attributes.count;

with

 layer_sum=layer_sum+parseInt(vlayer.features[i].attributes.count);

or

layer_sum=layer_sum+parseFloat(vlayer.features[i].attributes.count);

PS: make sure that all features are added to the layer before you start the for loop, for example with listening to the loadend event

vlayer.events.register("loadend",map,function()
 {
   console.log(vlayer.features.length);

 }
 );

I just set up a jsfiddle to demonstate what i mean: http://jsfiddle.net/expedio/3any8tk8/

  • Hi, Thomas thank you for fast reply. for the line:i=0;features.length returns zero. But when I run my code the geojson data is available. how can I use "featuresadded event' to test this? – Enkosa Jan 28 '15 at 9:32
  • can you just run vlayer.features.length from firebug or your developer tools after the layer is loaded? – Thomas B Jan 28 '15 at 9:39
  • i meant the loadend event (corrected now in my answer). see my additional lines of code above – Thomas B Jan 28 '15 at 9:45
  • Hi Thomas The code: vlayer.events.register("loadend",mappanel.map,function() { console.log(vlayer.features.length); } ); I modified map as mappanel.map and it shows that length is 585. but the other code:var layer_sum=0; for(var i=0;i<player.features.length;i++) { layer_sum=layer_sum+parseInt(vlayer.features[i].attributes.count); } console.log(layer_sum); gives me length zero, is there something missed? – Enkosa Jan 28 '15 at 10:10
  • you have written player.features.length instead of vlayer.features.length – Thomas B Jan 28 '15 at 10:15

Your Answer

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

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