Beanshell:Set an attribute fore every feature of a Layer

From OpenJUMP Wiki
Revision as of 01:54, 12 October 2009 by Mentaer (talk | contribs) (Created page with 'This script will add 1.0 to the "z" attribute of every feature in layer "elevation". It is assumed that "z" is of type DOUBLE. A ''java.lang.IllegalArgumentException'' will be t…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This script will add 1.0 to the "z" attribute of every feature in layer "elevation". It is assumed that "z" is of type DOUBLE.

A java.lang.IllegalArgumentException will be thrown if attribute "z" is not found in layer "elevation".

{
  import com.vividsolutions.jump.workbench.model.Layer;
  Layer layer = wc.getLayerManager().getLayer("elevation");
  if (layer == null) {
    System.err.println ("layer not found");
    return;
  }
  FeatureSchema schema = layer.getFeatureCollectionWrapper().getFeatureSchema(); 
  int zIndex = schema.getAttributeIndex("z");
  if ( schema.getAttributeType(zIndex) != AttributeType.DOUBLE ) { 
    System.err.println ("attribute 'z' not of type DOUBLE"); 
    return;
  }
  // Loop for each object in layer
  Iterator iter =layer.getFeatureCollectionWrapper().iterator();
  while (iter.hasNext()) { 
    BasicFeature f = (BasicFeature)iter.next();
    Double d = f.getAttribute(zIndex);
    f.setAttribute(zIndex, new Double(d.doubleValue() + 1.0));
  }
}