Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Saturday, November 06, 2004

Fwd: BeanShell to create an image layer from a file or URL !!! gif/jpeg/png

Fwd: BeanShell to create an image layer from a file or URL !!! gif/jpeg/png

This is kind of neat. As you can see from the screenshot, one
application is adding logos to a map!


---------- Forwarded message ----------
From: Jonathan Aquino
Date: Fri, 5 Nov 2004 23:35:15 -0800
Subject: BeanShell to create an image layer from a file or URL !!! gif/jpeg/png
To: JUMP Users Discussion

This awesome idea was requested by Andrew Collins and took me two
hours to write. It's a BeanShell script to create an image layer from
a GIF, JPEG, or PNG file or URL !!!

1. Click View > BeanShell.
2. Copy the code below into the clipboard.
3. Paste the code into the BeanShell window with Ctrl+V. JUMP will
create the image layer!

minx = 100; maxx = 952; miny = 100; maxy = 489; //Image coordinates
filenameOrURL = new
//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.Viewport;
mediaTracker = new MediaTracker(wc.layerViewPanel);
mediaTracker.addImage(image, 0);
envelope = new Envelope(minx, maxx, miny, maxy);
wc.layerManager.addLayer(StandardCategoryNames.WORKING, layerName,
.addStyle(new Style() {
public void paint(Feature f, Graphics2D g, Viewport viewport) {
originalTransform = g.getTransform();
try {
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(minx, maxy));
g.translate(upperLeftCorner.x/xScale, upperLeftCorner.y/yScale);
g.drawImage(image, 0, 0, null);
finally {
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;



Post a Comment

<< Home