BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell dynamically executes standard Java syntax and extends it with common scripting conveniences such as loose types, commands, and method closures like those in Perl and JavaScript.
In the context of Jump, BeanShell enables the automation of repetative tasks or to accomplish tasks that are not possible using the GUI without the need for plugin modules or modification of the application source code.
It is a good idea to surround all your scripts in a set of braces [WHY?]. Ie
In BeanShell you may access JavaBean properties as if they were fields. For example wc.layerManager is the same as
wc.getLayerManager().
There is no facility to save and load a script. If you plan to reuse your scripts, develop the script in a text editor and then paste into the BeanShell window as needed.
Saving viewport as image
It is possible to save the current viewport as an image with
File → Save Image As…
However sometimes it may be desireable to save an image with dimensions greater than that of the screen. This can be accomplished with the following script. Replace the first line (ie “outputFile=...”) with the name and path of the output image file. The next line (ie widthInPixels=...) sets the width of the image. The height is calculated automatically.
{
outputFile = "/my/path/myOutputImage.png";
widthInPixels = 1200;
import com.vividsolutions.jump.workbench.ui.LayerPrinter;
printer = new LayerPrinter();
env = wc.getLayerViewPanel().getViewport().getEnvelopeInModelCoordinates();
image = printer.print(wc.getLayerManager().getLayers(), env, widthInPixels);
import javax.imageio.ImageIO;
ImageIO.write(image, "png", new File(outputFile));
}
This plugin requires Java Advanced Imaging. This is available from http://java.sun.com/
Loading an image into the task
This script will load in image into the current task. Change minx, maxx, miny, maxy and filenameOrURL as desired.
{
minx = 100; maxx = 376; miny = 100; maxy = 210; //Image coordinates
filenameOrURL = new URL("http://www.google.ca/intl/en_ca/images/logo.gif");
//filenameOrURL = "c:/junk/Arrow.jpg";
layerName = "Image";
image = Toolkit.getDefaultToolkit().getImage(filenameOrURL);
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jump.feature.*;
import com.vividsolutions.jump.geom.EnvelopeUtil;
import com.vividsolutions.jump.workbench.model.*;
import com.vividsolutions.jump.workbench.ui.renderer.style.Style;
import com.vividsolutions.jump.workbench.ui.Viewport;
mediaTracker = new MediaTracker(wc.layerViewPanel);
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
wc.layerManager.addLayer(StandardCategoryNames.WORKING, layerName,
FeatureDatasetFactory.createFromGeometry(
Collections.singleton(EnvelopeUtil.toGeometry(
new Envelope(minx, maxx, miny, maxy))))).addStyle(new Style() {
public void paint(Feature f, Graphics2D g, Viewport viewport) {
originalTransform = g.getTransform();
try {
envelope = f.geometry.envelope;
xScale = viewport.scale*envelope.width/image.getWidth(null);
yScale = viewport.scale*envelope.height/image.getHeight(null);
g.scale(xScale, yScale);
upperLeftCorner = viewport.toViewPoint(new
Coordinate(envelope.minx, envelope.maxy));
g.translate(upperLeftCorner.x/xScale, upperLeftCorner.y/yScale);
g.drawImage(image, 0, 0, null);
}
finally {
g.setTransform(originalTransform);
}
}
public void initialize(Layer layer) {}
public Object clone() { throw new UnsupportedOperationException(); }
public void setEnabled(boolean enabled) {}
public boolean isEnabled() { return true; }
});
wc.layerManager.getLayer(layerName).getBasicStyle().renderingFill = false;
wc.layerManager.getLayer(layerName).getBasicStyle().renderingLine = false;
}
Working with layers and features
Some of the following scripts are not very useful. Instead they are indented as examples which can be easily adapted to perform useful tasks.