Difference between revisions of "Beanshell Attribute Calculator"

From OpenJUMP Wiki
Jump to navigation Jump to search
(Created page with "Tools == The plugin User interface == File:BeanshellAttributeCalculator.png This plugin makes it possible to add a calculated attribute to a layer. The dialog box o...")
 
Line 8: Line 8:
 
The dialog box offers several options
 
The dialog box offers several options
  
* '''Source Layer''' - Select the name of the future attribute.
+
* '''Source Layer''' - Select the layer to which you want to add an attribute.
 
 
  
 
* '''CALC''' - Enter the name of the future calculated attribute.
 
* '''CALC''' - Enter the name of the future calculated attribute.
  
 +
* '''New AttributeType''' - Type of the new attribute. It must be consistent with the value returned by the beanshell expression. If you choose String, the expression result will be automatically converted though.
  
* '''New AttributeType''' - Type of the new attribute. It must be consistent with the value returned by the beanshell expression. String type can be used safely as any beanshell expression can be converted to String if needed.
+
* '''Dynamic (computed as needed)''' - If checked, the computed attribute will be dynamically computed. Static and dynamic attributes have very different behaviour.
 
+
**''Static (box unchecked)'' : values are evaluated only once, when the plugin is executed. It will not be re-evaluated, even if the values involved in the expression change.
 
+
**''Dynamic (box checked)'' : expression is cached and attribute value is evaluated every time it is read. A dynamic attribute is read-only. For example, if a calculated attribute uname is added, based on the dynamic expression 'name.toUpperCase()', where name is an attribute of the feature, a modification of attribute name will automatically be reflected on attribute uname.
* '''Dynamic (computed as needed)''' - If checked, the computed attribute will be dynamically computed. Static and dynamic attributes are very different.
 
**''Static (box unchecked)'' : value is evaluated for each feature when the plugin is executed and result values are statically stored in the feature (it will not be re-evaluated if expression parameters change in the future).
 
**''Dynamic (box checked)'' : expression is cached and attribute value is evaluated every time it is read. For this reason, a dynamic attribute is read-only. If the expression relies on the feature geometry and the geometry change in the future, the attribute value will also change. If you display a layer with a style relying on a dynamic attribute (ex. label or color theming), you can easily check that values are re-evaluated every time a parameter of the expression changes.
 
  
 +
* '''Beanshell Expression''' : the expression which will be evaluated for each feature. Expression must use java syntax, or beanshell syntax (beanshell syntax is the same as java syntax with some syntactic sugar making it lighter - see [http://www.beanshell.org/docs.html beanshell documentation]). The expression automatically imports following jump and jts packages
 +
** com.vividsolutions.jump.feature.*
 +
** com.vividsolutions.jts.geom.*
 +
** com.vividsolutions.jts.operation.union.UnaryUnionOp
 +
* It also sets the following variables
 +
** wc : the WorkbenchContext
 +
** feature (or Feature or FEATURE) : the current feature
 +
** geometry (or Geometry or GEOMETRY) : the current feature geometry
 +
** <attribute_name> : the value of attribute named attribute_name (or "attribute name") for the current feature. Note that if attribute name contains characters forbidden for a java identifier, you must replace them by '_'.
 +
* Finally, it defines some useful methods
 +
** selection() : returns selected features as a collection
 +
** dataset(String layerName) : return features from layer as a collection
 +
** intersects(Feature feature, Collection features) : returns true if feature intersects one of features, and false otherwise
 +
** distance(Feature feature, Collection features) : returns min distance between feature and features
 +
** round(double d, int i) : returns d rounded to i decimal places
  
* '''Beanshell Expression''' : the expression which will be evaluated for each feature. Expression must use java syntax, or beanshell syntax (beanshell syntax is the same as java syntax with some syntactic sugar making it lighter - see [http://www.beanshell.org/docs.html beanshell documentation]). You can also use some variable names which will be automatically recognized like :
+
* '''Script Snippets''' - The Script Snippet section, on the right side of the dialog, will help you to write simple script expressions. In this section, you will find available attribute names and code snippets. Double click on an element to include it in your expression and avoid typos.
** wc : refers to the workbenchContext
 
** <attribute name> : attribute names will be replaced by the current feature attribute values
 
  
 +
== Examples ==
  
* '''Script Snippets''' - The Script Snippet section, on the right side of the dialog, will help you to write simple script expressions. In this section, you will find available attribute names, including geometry attribute name. Double click on an attribute name to include it in your expression and avoid typos. You will also find small expression which can follow an attribute name or take an attribute name as argument. Examples :
+
=== Expressions using geometry ===
** MyAttribute.toLowerCase()  
+
* '''GEOMETRY.SRID''' : returns geometry srid as an int
 +
* '''GEOMETRY.area''' : returns geometry area as a double
 +
* '''GEOMETRY.area/10000''' : returns geometry area in ha (assuming map units is meter)
 +
* '''GEOMETRY.length''' : returns geometry length as a double
 +
* '''GEOMETRY.length/1000''' : returns geometry length in km (assuming map units is meter)
 +
* '''GEOMETRY.numPoints''' : returns the number of coordinates in the geometry as an int
 +
* '''GEOMETRY.geometryType''' : returns whether geometry i svalid or not as a boolean (choose string attribute type)
 +
* '''GEOMETRY.coordinate.z''' : returns the z value of a coordinate
  
== The plugin User interface ==
+
=== Expressions using attributes ===

Revision as of 23:21, 13 September 2014

Tools

The plugin User interface

BeanshellAttributeCalculator.png

This plugin makes it possible to add a calculated attribute to a layer. The dialog box offers several options

  • Source Layer - Select the layer to which you want to add an attribute.
  • CALC - Enter the name of the future calculated attribute.
  • New AttributeType - Type of the new attribute. It must be consistent with the value returned by the beanshell expression. If you choose String, the expression result will be automatically converted though.
  • Dynamic (computed as needed) - If checked, the computed attribute will be dynamically computed. Static and dynamic attributes have very different behaviour.
    • Static (box unchecked) : values are evaluated only once, when the plugin is executed. It will not be re-evaluated, even if the values involved in the expression change.
    • Dynamic (box checked) : expression is cached and attribute value is evaluated every time it is read. A dynamic attribute is read-only. For example, if a calculated attribute uname is added, based on the dynamic expression 'name.toUpperCase()', where name is an attribute of the feature, a modification of attribute name will automatically be reflected on attribute uname.
  • Beanshell Expression : the expression which will be evaluated for each feature. Expression must use java syntax, or beanshell syntax (beanshell syntax is the same as java syntax with some syntactic sugar making it lighter - see beanshell documentation). The expression automatically imports following jump and jts packages
    • com.vividsolutions.jump.feature.*
    • com.vividsolutions.jts.geom.*
    • com.vividsolutions.jts.operation.union.UnaryUnionOp
  • It also sets the following variables
    • wc : the WorkbenchContext
    • feature (or Feature or FEATURE) : the current feature
    • geometry (or Geometry or GEOMETRY) : the current feature geometry
    • <attribute_name> : the value of attribute named attribute_name (or "attribute name") for the current feature. Note that if attribute name contains characters forbidden for a java identifier, you must replace them by '_'.
  • Finally, it defines some useful methods
    • selection() : returns selected features as a collection
    • dataset(String layerName) : return features from layer as a collection
    • intersects(Feature feature, Collection features) : returns true if feature intersects one of features, and false otherwise
    • distance(Feature feature, Collection features) : returns min distance between feature and features
    • round(double d, int i) : returns d rounded to i decimal places
  • Script Snippets - The Script Snippet section, on the right side of the dialog, will help you to write simple script expressions. In this section, you will find available attribute names and code snippets. Double click on an element to include it in your expression and avoid typos.

Examples

Expressions using geometry

  • GEOMETRY.SRID : returns geometry srid as an int
  • GEOMETRY.area : returns geometry area as a double
  • GEOMETRY.area/10000 : returns geometry area in ha (assuming map units is meter)
  • GEOMETRY.length : returns geometry length as a double
  • GEOMETRY.length/1000 : returns geometry length in km (assuming map units is meter)
  • GEOMETRY.numPoints : returns the number of coordinates in the geometry as an int
  • GEOMETRY.geometryType : returns whether geometry i svalid or not as a boolean (choose string attribute type)
  • GEOMETRY.coordinate.z : returns the z value of a coordinate

Expressions using attributes