Difference between revisions of "Beanshell Attribute Calculator"

From OpenJUMP Wiki
Jump to navigation Jump to search
 
Line 1: Line 1:
 
[[Tools]]
 
[[Tools]]
 +
 +
This plugin makes it possible to add a calculated attribute to a layer.
  
 
== The plugin User interface ==
 
== The plugin User interface ==
Line 5: Line 7:
 
[[File:BeanshellAttributeCalculator.png]]
 
[[File:BeanshellAttributeCalculator.png]]
  
This plugin makes it possible to add a calculated attribute to a layer.
 
 
The dialog box offers several options
 
The dialog box offers several options
  
Line 48: Line 49:
 
* '''GEOMETRY.coordinate.z''' : returns the z value of a coordinate
 
* '''GEOMETRY.coordinate.z''' : returns the z value of a coordinate
  
=== Expressions using attributes ===
+
See [http://tsusiatsoftware.net/jts/javadoc/com/vividsolutions/jts/geom/Geometry.html Geometry javadoc]
 +
 
 +
=== Expressions using string attributes (concatenate, uppercase, lowercase) ===
 +
Consider a layer with the following attributes : first name (String), last name (String)
 +
* '''first_name + " " + last_name''' : returns first name and last name separated by a whitespace
 +
* '''first_name.substring(0,1).toUpperCase() + first_name.substring(1).toLowerCase()''' : change barak or BARAK to Barak
 +
* '''first_name.substring(0,1) + "." + last_name.substring(0,1) + "."''' : change Barak Obama to B.O.
 +
 
 +
See [http://docs.oracle.com/javase/7/docs/api/java/lang/String.html javadoc for String]
 +
 
 +
=== Expressions using string attributes (concatenate heterogeneous attributes) ===
 +
Consider a layer with the following attributes : parcelId (Integer), county (String), state (String)
 +
* '''"" + parcelId + "-" + county + "-" + state''' : the first "" is used so that + is used as the concatenate operator instead of add (result will be a string)
 +
* '''String.format("%05d",new Object[]{parcelId})''' : align parcelId on a 5 character string completed by 0 ('123' -> '00123')
 +
 
 +
See [http://docs.oracle.com/javase/7/docs/api/java/lang/String.html javadoc for String]
 +
 
 +
 
 +
=== Expressions using date attribute ===
 +
Consider a layer with the following attribute : date of birth (Date)
 +
* '''new java.text.SimpleDateFormat("yyyy-MM-dd").format(date_of_birth)''' : print 1961-08-04
 +
* '''new java.text.SimpleDateFormat("dd/MM/yyyy").format(date_of_birth)''' : print 04/08/1961
 +
* '''(new Date().getTime() - new GregorianCalendar(1961,Calendar.AUGUST,4).getTime().getTime())/(365.2425*24*60*60*1000)''' : print barak's age
 +
* '''address.'''
 +
 
 +
See [http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html javadoc for SimpleDateFormat]
 +
 
 +
See [http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html javadoc for GregorianCalendar]

Latest revision as of 11:41, 14 September 2014

Tools

This plugin makes it possible to add a calculated attribute to a layer.

The plugin User interface

BeanshellAttributeCalculator.png

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

See Geometry javadoc

Expressions using string attributes (concatenate, uppercase, lowercase)

Consider a layer with the following attributes : first name (String), last name (String)

  • first_name + " " + last_name : returns first name and last name separated by a whitespace
  • first_name.substring(0,1).toUpperCase() + first_name.substring(1).toLowerCase() : change barak or BARAK to Barak
  • first_name.substring(0,1) + "." + last_name.substring(0,1) + "." : change Barak Obama to B.O.

See javadoc for String

Expressions using string attributes (concatenate heterogeneous attributes)

Consider a layer with the following attributes : parcelId (Integer), county (String), state (String)

  • "" + parcelId + "-" + county + "-" + state : the first "" is used so that + is used as the concatenate operator instead of add (result will be a string)
  • String.format("%05d",new Object[]{parcelId}) : align parcelId on a 5 character string completed by 0 ('123' -> '00123')

See javadoc for String


Expressions using date attribute

Consider a layer with the following attribute : date of birth (Date)

  • new java.text.SimpleDateFormat("yyyy-MM-dd").format(date_of_birth) : print 1961-08-04
  • new java.text.SimpleDateFormat("dd/MM/yyyy").format(date_of_birth) : print 04/08/1961
  • (new Date().getTime() - new GregorianCalendar(1961,Calendar.AUGUST,4).getTime().getTime())/(365.2425*24*60*60*1000) : print barak's age
  • address.

See javadoc for SimpleDateFormat

See javadoc for GregorianCalendar