[OpenLayers-Users] Iterating through features in a vector layer?

Robert Sanson Robert.Sanson at asurequality.com
Mon Jan 23 20:29:54 EST 2012


I am using OpenLayers v2.11, and have a vector layer loaded from  geometry in WKT format separated from attributes using pipe delimited text from a database (via a Python cgi-bin script).

I am trying to write a function that iterates through all of the features and then POST updates back to the database when certain conditions are met.
 My difficulty is that layer.features.length is returning double the number of features.

Here is my code that creates the layer and loads the features:

function setHTML4cr(response4) {
	var cropnum = 0;
    var lines, line;
    var cropid, name, crds;
    var cropgeom;
    if (response4.responseText.toUpperCase().indexOf('ERROR') != -1) {
      alert(response4.responseText);
    }
    else {
      crops = new OpenLayers.Layer.Vector("Crops",
                  {isBaseLayer: false,
                   styleMap: crop_style,
                   rendererOptions: {zIndexing: true},
                   extractAttributes: true });
      var lines = response4.responseText.split('\n');
      for ( var i=0;i<lines.length;i++) {
        line = lines[i].split("|");
        if ( line.length == 3 ) {
          cropid = line[0];
          name = line[1];
          crds = line[2];
          cropgeom = wkt_format.read(crds);
          cropgeom.attributes.id = cropid;
          cropgeom.attributes.name = name;
          cropgeom.attributes.pstate = "pre";
          crops.addFeatures(cropgeom);
		cropnum = cropnum + 1;
        }
      }
      map.addLayer(crops);
      alert("Crops layer loaded and ready for digitising with " + cropnum + " features!");
}

This is reporting the correct number of features, and the features are drawn on the map.

However, this function is reporting double the number of features:

function savecrop() {
	var f;
    alert(crops.features.length);
	 var allf = [];
    for (i = 0; i < crops.features.length; i++) {
	f = crops.features[i];
	allf.push(f.attributes.id + "|" + f.attributes.name + "|" + f.attributes.pstate + "|" + f.state);
    }
    alert(allf.join("\n"));
}

Many thanks,

Robert





This email and any attachments are confidential and intended solely for the addressee(s). If you are not the intended recipient, please notify us immediately and then delete this email from your system.

This message has been scanned for Malware and Viruses by Websense Hosted Security.
www.websense.com


More information about the Users mailing list