How to load a Sextante raster layer and read its cell values
Jump to navigation
Jump to search
The following plugin code allows you to read a Setante raster values and print its cell values
package org.openjump.addsextanterasterlayer;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jump.workbench.WorkbenchContext;
import com.vividsolutions.jump.workbench.model.Layer;
import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
import com.vividsolutions.jump.workbench.plugin.PlugInContext;
import com.vividsolutions.jump.workbench.ui.Viewport;
import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller;
import com.vividsolutions.jump.workbench.ui.renderer.LayerRendererFactory;
import com.vividsolutions.jump.workbench.ui.renderer.RenderingManager;
import org.openjump.core.rasterimage.RasterImageLayer;
import org.openjump.core.rasterimage.sextante.OpenJUMPSextanteRasterLayer;
import org.openjump.core.rasterimage.sextante.rasterWrappers.GridExtent;
import java.awt.Point;
import java.awt.Rectangle;
public class SextanteLayerPlugIn extends AbstractPlugIn{
public SextanteLayerPlugIn() {
// empty constructor
}
public void initialize(PlugInContext context) throws Exception {
FeatureInstaller featureInstaller = new FeatureInstaller(context.getWorkbenchContext());
featureInstaller.addMainMenuItem(
this, //exe
new String[] {"SextanteRaster"}, //menu path
"Add Raster Layer", //name methode .getName recieved by AbstractPlugIn
false, //checkbox
null, //icon
createEnableCheck(context.getWorkbenchContext())); //enable check
}
public static MultiEnableCheck createEnableCheck(WorkbenchContext workbenchContext) {
EnableCheckFactory checkFactory = new EnableCheckFactory(workbenchContext);
return new MultiEnableCheck()
.add(checkFactory.createWindowWithLayerNamePanelMustBeActiveCheck());
}
Viewport viewport = context.getLayerViewPanel().getViewport();
Rectangle visibleRect = viewport.getPanel().getVisibleRect();
int visibleX1 = visibleRect.x;
int visibleY1 = visibleRect.y;
int visibleX2 = visibleX1 + visibleRect.width;
int visibleY2 = visibleY1 + visibleRect.height;
Coordinate upperLeftVisible = viewport.toModelCoordinate(new Point(0,0));
Coordinate lowerRightVisible = viewport.toModelCoordinate(new Point(visibleX2, visibleY2));
Envelope newVisibleEnv = new Envelope(upperLeftVisible.x, lowerRightVisible.x, upperLeftVisible.y, lowerRightVisible.y);
RasterImageLayer rast = new RasterImageLayer("landsat",
context.getLayerManager(),
"/home/rashadkm/lsat.png",
null, null, newVisibleEnv);
OpenJUMPSextanteRasterLayer rasterlayer = new OpenJUMPSextanteRasterLayer();
rasterlayer.create(rast,true);
context.addLayerSextanteRaster("Raster Layer", rast);
GridExtent gridExtent = new GridExtent(rasterlayer);
rasterlayer.setWindowExtent(gridExtent);
int iNX = gridExtent.getNX();
int iNY = gridExtent.getNY();
for(int y = 0; y < iNY; y++){
for(int x = 0; x < iNX; x++){
System.out.println(rasterlayer.getCellValueAsDouble(x, y));
}
}
return true;
}
}