Hi Peppe and other Devs,
I fixed the problem :)
I send you my last version that seems to write an *.asc file as it is
supposed to. At least it looks like OJ itself and QGIS are able to read
it. From my file you can start and adding the stuff you still think is
necessary. I have made some inline comments.
I think you actually got a bit confused with the whole row and columns
thing (I switched the loops). And you should have put the "write-ln"
command after the inner loop is finished, and not in each line.
Also, I realized that when I checked for the noData value, that there
was something wrong. I found then out, that an OJ-Raster has no NoData
value property, while Sextante has (however... OJ has a transparency
setting). This also means, that when reading an ascii file with the
current reader, then this values was not set. So I implemented that
property, and will commit my changes in a few minutes.
The default NoDataValue is now Double.NaN.
This value will also get transferred when creating a Sextante Raster
Image Layer from an image file. However, if a new Sextante Layer is
created (not based on an existing file), then the default noDataValue is
-99999.0 (i.e. one digit more than the ESRI default of -9999)
I hope I did not miss out on something, and nothing breaks now when
working with Sextante. But I don't guess so.
I also attach my ascii test file, that I created from the Wikipedia page.
cheers,
stefan
Am 26.06.13 12:56, schrieb Giuseppe Aruta:
> Really I don't understand loops in Java
> ;-)
>
>
> 2013/6/26 Giuseppe Aruta <giuseppe_ar...@yahoo.it
> <mailto:giuseppe_ar...@yahoo.it>>
>
> Hi Stefan,
> I made some progress, now I am able to save to asc, unfortunately
> each value of the pixel of the ascii is saved an incredible number
> equal to rowXcolumn of the original. So my test file of 101 byte
> becomes a huge file of 2 Gb :-( Surelly I made a misake.
> Then I moved to Switzerland for job, where actully I am. And didn't
> carry with me Eclipse, codes, OpenJMP, etc. I will be back home
> after 5th of July and give a better look (and send the code)
> regards
>
> Peppe
>
>
> 2013/6/26 Stefan Steiniger <sst...@geo.uzh.ch
> <mailto:sst...@geo.uzh.ch>>
>
> Hi Peppe,
>
> did you made any progress on that?
> Or is the code the last version you have?
>
> slds,
> stefan
>
> Am 21.06.13 06:05, schrieb Giuseppe Aruta:
> >
> > Hi all,
> > Recently I worked a bit with Sextante/grids and did some
> specialized
> > short courses. A student of mine used OpenJUMP/Sextante to do
> some
> > analysis. Than he had to convert all the rasters (TIFF
> format) into ESRI
> > Ascii grid files (.asc) as he had to use a more specialized
> software
> > that was accepting only asc. and surfer raster format.
> > Of coarse we used gdal for raster transformation.
> > Well, since Esri ascii file is a very simple text file
> > (http://en.wikipedia.org/wiki/Esri_grid), in these days (
> that I have
> > spare time) I tried to make a plugin "Export raster to Esri
> Ascii grid
> > file" for OpenJUMP (that in turn I would like to add to OJ
> NB). All
> > raster parameters are easy implemented by RasterImageLayer and
> > OpenJUMPSextanteRasterLayer.
> > I had some success and some failures.
> > I ask your help, as developers.
> > I added to this mail the code I wrote (coping partially OJ raster
> > classes and from other software like ImageJ) - The foillowing
> code just
> > shows the transformation, excluding initialize(), icon(),
> name(), etc.
> > This code partially works: the file is saved together with
> the header of
> > the raster (see the *black* part of the code, number of
> colums, rows,
> > x-ycoordinates of upper left corner, cell size and NODATA),
> > The red part of code is not working. This part should lists
> the raster
> > values for each cell, starting at the upper-left corner,
> possibly with
> > decimal values, delimited by space character. I ask you if
> can give me a
> > suggestion for this part.
> > Another part that I would like to implement is the
> MultienableCheck
> > (dark *violet *part of the code). This plugin should be
> activated only
> > if the raster has only one band (I think this is numbered "0" by
> > OpenJUMPSextanteRasterLayer class). Any suggestion is
> accepted: a way to
> > distinguish (within Pirol raster plugin) between grid mono
> band file and
> > others would be quite usefull for future plugins
> > If this plugin is finished I think I will add to OJ NB under
> > Raster>Tools menu.
> > regards and thanks
> >
> > Peppe
> >
> > public class SaveToASCPlugIn extends AbstractPlugIn
> > {
> >
> > protected double[][] data;
> > private Properties properties = null;
> > private static String propertiesFile =
> > LoadSextanteRasterImagePlugIn.getPropertiesFile();
> > private String lastPath;
> > NumberFormat cellFormat = null;
> > public static final String DEFAULT_NODATA = "-9999";
> >
> > public boolean execute(PlugInContext context)
> > throws Exception
> > {
> > JFileChooser fc = new JFCWithEnterAction();
> >
> > fc.setFileFilter(new FileFilter() {
> > public boolean accept(File f) {
> > return (f.isDirectory()) ||
> > (f.getName().toLowerCase().endsWith(".asc"));
> > }
> >
> > public String getDescription() {
> > return "Arc/Info ASCII Grid (.asc)";
> > }
> > });
> > this.properties = new Properties();
> > try {
> > FileInputStream fis = new FileInputStream(propertiesFile);
> > this.properties.load(fis);
> > this.lastPath =
> >
> this.properties.getProperty(LoadSextanteRasterImagePlugIn.KEY_PATH);
> > fis.close();
> > }
> > catch (FileNotFoundException e) {
> >
> >
>
> context.getWorkbenchFrame().warnUser(I18N.get("org.openjump.core.ui.plugin.layer.pirolraster.SaveRasterImageAsImagePlugIn.File-not-found"));
> > }
> > catch (IOException e) {
> > context.getWorkbenchFrame().warnUser(GenericNames.ERROR);
> > }
> >
> > if (this.lastPath != null) {
> > fc.setCurrentDirectory(new File(this.lastPath));
> > }
> > fc.setMultiSelectionEnabled(false);
> >
> > fc.setDialogTitle(getName());
> > int returnVal = fc.showSaveDialog(fc);
> > if (returnVal == 0) {
> > String ascFileName =
> fc.getSelectedFile().getAbsolutePath();
> >
> > if
> (!ascFileName.toLowerCase().endsWith(".asc".toLowerCase())) {
> > ascFileName = ascFileName + ".asc";
> > }
> >
> > File ascFile = new File(ascFileName);
> >
> > FileOutputStream ascOut = new FileOutputStream(ascFile);
> >
> > RasterImageLayer rLayer =
> > (RasterImageLayer)LayerTools.getSelectedLayerable(context,
> > RasterImageLayer.class);
> > OpenJUMPSextanteRasterLayer rstLayer = new
> > OpenJUMPSextanteRasterLayer();
> > rstLayer.create(rLayer);
> >
> > *PrintStream o = new PrintStream(ascOut);*
> > *o.println( "ncols " + rLayer.getOrigImageWidth() );
> > //rstLayer.getNX() );
> > o.println( "nrows " + rLayer.getOrigImageHeight() );
> > //rstLayer.getNY() );
> > o.println( "xllcorner " +
> rLayer.getEnvelope().getMinX() );
> > o.println( "yllcorner " +
> rLayer.getEnvelope().getMinY());
> > o.println( "cellsize " + rstLayer.getLayerCellSize() );
> > o.println( "NODATA_value " + DEFAULT_NODATA );*
> >
> >
> > GridWrapperNotInterpolated gwrapper = new
> > GridWrapperNotInterpolated(rstLayer,
> rstLayer.getLayerGridExtent());
> >
> >
> > int nx = rstLayer.getLayerGridExtent().getNX();
> > int ny = rstLayer.getLayerGridExtent().getNY();
> > for (int x = 0; x < nx; x++) {//cols
> > for (int y = 0; y < ny; y++) {//rows
> > double value =
> gwrapper.getCellValueAsFloat(x, y, 0);
> > StringBuffer b = new StringBuffer();
> > for( int i = 0; i <
> rLayer.getOrigImageWidth(); i++ )
> > {
> > if(
> Double.isNaN(rstLayer.getNoDataValue() )
> > ) b.append( rstLayer.getNoDataValue() );
> > else if( cellFormat != null ) b.append(
> > cellFormat.format( value ));
> > else b.append( cellFormat.format(
> value ) );
> > if( i < rLayer.getOrigImageWidth() -1 )
> > b.append( " " );
> > }
> > o.println( b );
> > }
> > }
> > o.close();
> >
> > rLayer.setImageFileName(ascFileName);
> > rLayer.setNeedToKeepImage(false);
> > }
> >
> > return true;
> > }
> >
> >
> > public void setCellFormat( NumberFormat format )
> > {
> > cellFormat = format;
> > }
> >
> >
> > *public static MultiEnableCheck
> createEnableCheck(WorkbenchContext
> > workbenchContext)
> > {
> > EnableCheckFactory checkFactory = new EnableCheckFactory(
> > workbenchContext);
> > MultiEnableCheck multiEnableCheck = new MultiEnableCheck();
> >
> >
> >
>
> multiEnableCheck.add(checkFactory.createExactlyNLayerablesMustBeSelectedCheck(1,
> > RasterImageLayer.class));
> >
> > return multiEnableCheck;*
> > }
> >
> > }
> >
> >
> >
>
> ------------------------------------------------------------------------------
> > This SF.net email is sponsored by Windows:
> >
> > Build for Windows Store.
> >
> > http://p.sf.net/sfu/windows-dev2dev
> >
> >
> >
> > _______________________________________________
> > Jump-pilot-devel mailing list
> > Jump-pilot-devel@lists.sourceforge.net
> <mailto:Jump-pilot-devel@lists.sourceforge.net>
> > https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel
> >
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
> _______________________________________________
> Jump-pilot-devel mailing list
> Jump-pilot-devel@lists.sourceforge.net
> <mailto:Jump-pilot-devel@lists.sourceforge.net>
> https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel
>
>
>
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
>
>
>
> _______________________________________________
> Jump-pilot-devel mailing list
> Jump-pilot-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel
>
package org.openjump.core.ui.plugin.raster;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.NumberFormat;
import java.util.Properties;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import org.openjump.core.apitools.LayerTools;
import org.openjump.core.rasterimage.RasterImageLayer;
import org.openjump.core.rasterimage.sextante.OpenJUMPSextanteRasterLayer;
import
org.openjump.core.rasterimage.sextante.rasterWrappers.GridWrapperNotInterpolated;
import org.openjump.core.ui.plugin.file.open.JFCWithEnterAction;
import
org.openjump.core.ui.plugin.layer.pirolraster.LoadSextanteRasterImagePlugIn;
import com.vividsolutions.jump.I18N;
import com.vividsolutions.jump.workbench.WorkbenchContext;
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.GenericNames;
import com.vividsolutions.jump.workbench.ui.MenuNames;
import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller;
public class SaveToAsciiPlugin extends AbstractPlugIn{
protected double[][] data;
private Properties properties = null;
private static String propertiesFile =
LoadSextanteRasterImagePlugIn.getPropertiesFile();
private String lastPath;
NumberFormat cellFormat = null;
public double defaultNoData = -9999;
public void initialize(PlugInContext context) throws Exception
{
WorkbenchContext workbenchContext =
context.getWorkbenchContext();
new FeatureInstaller(workbenchContext);
context.getFeatureInstaller().addMainMenuPlugin(
this,
new String[] {MenuNames.RASTER},
I18N.get("Save As ASCII Grid File..."),
false,
null,
createEnableCheck(context.getWorkbenchContext()));
}
public boolean execute(PlugInContext context) throws Exception{
//I am not sure, but I think value that indicates fractions is
standardized as a "." for ASCII grids?
//I.e. in German it is 5,9 while in the US it is 5.9 ???
cellFormat = NumberFormat.getNumberInstance();
cellFormat.setMaximumFractionDigits(3);
cellFormat.setMinimumFractionDigits(0);
JFileChooser fc = new JFCWithEnterAction();
fc.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return (f.isDirectory()) ||
(f.getName().toLowerCase().endsWith(".asc"));
}
public String getDescription() {
return "Arc/Info ASCII Grid (.asc)";
}
});
this.properties = new Properties();
try {
FileInputStream fis = new
FileInputStream(propertiesFile);
this.properties.load(fis);
this.lastPath =
this.properties.getProperty(LoadSextanteRasterImagePlugIn.KEY_PATH);
fis.close();
}
catch (FileNotFoundException e) {
//not sure if it is necessary to show this warning,
because the idea is to rather store when there has been no file before.
context.getWorkbenchFrame().warnUser(I18N.get("org.openjump.core.ui.plugin.layer.pirolraster.SaveRasterImageAsImagePlugIn.File-not-found"));
}
catch (IOException e) {
context.getWorkbenchFrame().warnUser(GenericNames.ERROR);
}
if (this.lastPath != null) {
fc.setCurrentDirectory(new File(this.lastPath));
}
fc.setMultiSelectionEnabled(false);
fc.setDialogTitle(getName());
int returnVal = fc.showSaveDialog(fc);
if (returnVal == 0) {
String ascFileName =
fc.getSelectedFile().getAbsolutePath();
if
(!ascFileName.toLowerCase().endsWith(".asc".toLowerCase())) {
ascFileName = ascFileName + ".asc";
}
File ascFile = new File(ascFileName);
FileOutputStream ascOut = new FileOutputStream(ascFile);
RasterImageLayer rLayer =
(RasterImageLayer)LayerTools.getSelectedLayerable(context,
RasterImageLayer.class);
OpenJUMPSextanteRasterLayer rstLayer = new
OpenJUMPSextanteRasterLayer();
rstLayer.create(rLayer);
defaultNoData = rstLayer.getNoDataValue(); //added this
PrintStream o = new PrintStream(ascOut);
o.println( "ncols " + rLayer.getOrigImageWidth() );
//rstLayer.getNX() );
o.println( "nrows " + rLayer.getOrigImageHeight() );
//rstLayer.getNY() );
o.println( "xllcorner " +
rLayer.getEnvelope().getMinX() );
o.println( "yllcorner " +
rLayer.getEnvelope().getMinY());
o.println( "cellsize " + rstLayer.getLayerCellSize() );
String sNoDataVal = "";
if( Math.floor(defaultNoData) == defaultNoData){
sNoDataVal =
Integer.toString((int)defaultNoData);
}
else{
sNoDataVal = Double.toString(defaultNoData);
}
o.println( "NODATA_value " + sNoDataVal);
GridWrapperNotInterpolated gwrapper = new
GridWrapperNotInterpolated(rstLayer, rstLayer.getLayerGridExtent());
int nx = rstLayer.getLayerGridExtent().getNX();
int ny = rstLayer.getLayerGridExtent().getNY();
for (int y = 0; y < ny; y++) {//rows
StringBuffer b = new StringBuffer();
for (int x = 0; x < nx; x++) {//cols
double value =
gwrapper.getCellValueAsDouble(x, y, 0); //this was before
"getCellValueAsFloa()"... not sure why you would want a float?
if( Double.isNaN(value)) value =
defaultNoData;
// show int as int
if(Math.floor(value) == value){
b.append((int)value + " ");
//cast to int
}
else{
b.append( value + " ");
}
}
o.println( b );
}
o.close();
rLayer.setImageFileName(ascFileName);
rLayer.setNeedToKeepImage(false);
}
return true;
}
public static MultiEnableCheck createEnableCheck(WorkbenchContext
workbenchContext)
{
EnableCheckFactory checkFactory = new
EnableCheckFactory(workbenchContext);
MultiEnableCheck multiEnableCheck = new MultiEnableCheck();
multiEnableCheck.add(checkFactory.createExactlyNLayerablesMustBeSelectedCheck(1,
RasterImageLayer.class));
return multiEnableCheck;
}
}
ncols 4
nrows 6
xllcorner 0.0
yllcorner 0.0
cellsize 50.0
NODATA_value -7777
-7777 -7777 5 2
-7777 20 100 36
3 8 35 10
32 42 50 6
88 75 27 9
13 5 1 -7777
------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:
Build for Windows Store.
http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel