Difference between revisions of "Beanshell:Create an image layer from a file or URL"

From OpenJUMP Wiki
Jump to navigation Jump to search
(Created page with '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…')
 
m
 
Line 1: Line 1:
 
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  
 
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.
+
# Click View > BeanShell.
2. Copy the code below into the clipboard.
+
# Copy the code below into the clipboard.
3. Paste the code into the BeanShell window with Ctrl+V. JUMP will
+
# Paste the code into the BeanShell window with Ctrl+V. JUMP will create the image layer!
create the image layer!
 
  
 
  {
 
  {

Latest revision as of 02:12, 12 October 2009

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
URL("http://www.apsu.edu/field_biology/center/sym2001/fig1p66.jpg");
//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);
envelope = new Envelope(minx, maxx, miny, maxy);
wc.layerManager.addLayer(StandardCategoryNames.WORKING, layerName,
FeatureDatasetFactory.createFromGeometry(
Collections.singleton(EnvelopeUtil.toGeometry(envelope))))
.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 {
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;