Gianpaolo Coro 2014-03-21 16:26:14 +00:00
parent 78571b689a
commit fd6d81dd19
1 changed files with 97 additions and 102 deletions

View File

@ -1,6 +1,5 @@
package org.gcube.dataanalysis.geo.connectors.asc; package org.gcube.dataanalysis.geo.connectors.asc;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
@ -15,132 +14,128 @@ import java.util.regex.Pattern;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.data.transfer.common.TransferUtil; import org.gcube.data.transfer.common.TransferUtil;
/** /**
* A class which reads an ESRI ASCII raster file into a Raster * A class which reads an ESRI ASCII raster file into a Raster
*
* @author dmrust * @author dmrust
* *
*/ */
public class AscRasterReader public class AscRasterReader {
{
String noData = AscRaster.DEFAULT_NODATA; String noData = AscRaster.DEFAULT_NODATA;
Pattern header = Pattern.compile( "^(\\w+)\\s+(-?\\d+(.\\d+)?)"); Pattern header = Pattern.compile("^(\\w+)\\s+(-?\\d+(.\\d+)?)");
public static void main( String[] args ) throws IOException public static void main(String[] args) throws IOException {
{
AscRasterReader rt = new AscRasterReader(); AscRasterReader rt = new AscRasterReader();
rt.readRaster( "data/test.asc" ); rt.readRaster("data/test.asc");
} }
/** /**
* The most useful method - reads a raster file, and returns a Raster object. * The most useful method - reads a raster file, and returns a Raster object.
* *
* Throws standard IOExceptions associated with opening and reading files, and * Throws standard IOExceptions associated with opening and reading files, and RuntimeExceptions if there are problems with the file format
* RuntimeExceptions if there are problems with the file format *
* @param filename * @param filename
* @return the Raster object read in from the file * @return the Raster object read in from the file
* @throws IOException * @throws IOException
*/ */
public AscRaster readRaster( String filename ) throws IOException, RuntimeException public AscRaster readRaster(String filename) throws IOException, RuntimeException {
{
AscRaster raster = new AscRaster(); AscRaster raster = new AscRaster();
BufferedReader input = null; BufferedReader input = null;
URLConnection urlConn =null; URLConnection urlConn = null;
if (filename.startsWith("http")){ if (filename.startsWith("http")) {
AnalysisLogger.getLogger().debug("Getting file from http"); AnalysisLogger.getLogger().debug("Getting file from http");
/* /*
URL fileurl = new URL(filename); * URL fileurl = new URL(filename); urlConn = fileurl.openConnection(); urlConn.setConnectTimeout(120000); urlConn.setReadTimeout(1200000); urlConn.setAllowUserInteraction(false); urlConn.setDoOutput(true); input = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
urlConn = fileurl.openConnection(); */
urlConn.setConnectTimeout(120000); // using Manzi's data Transfer to overcome https issues
urlConn.setReadTimeout(1200000); try {
urlConn.setAllowUserInteraction(false); input = new BufferedReader(new InputStreamReader(TransferUtil.getInputStream(new URI(filename), 120000)));
urlConn.setDoOutput(true);
input = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
*/
//using Manzi's data Transfer to overcome https issues
try {
input = new BufferedReader(new InputStreamReader(TransferUtil.getInputStream(new URI(filename),120000)));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
AnalysisLogger.getLogger().debug("Error: Bad URI "+filename); AnalysisLogger.getLogger().debug("Error: Bad URI " + filename);
} }
} else {
AnalysisLogger.getLogger().debug("Getting file from local file");
input = new BufferedReader(new FileReader(filename));
} }
else { try {
AnalysisLogger.getLogger().debug("Getting file from local file");
input = new BufferedReader( new FileReader( filename ) ); // while( input.ready() )
} while (true)
while( input.ready() ) {
{ String line = input.readLine();
String line = input.readLine(); if (line==null)
if (line!=null && line.length()>0) break;
line = line.trim(); if (line != null && line.length() > 0)
Matcher headMatch = header.matcher( line ); line = line.trim();
//Match all the heads
if( headMatch.matches() ) Matcher headMatch = header.matcher(line);
{ // Match all the heads
String head = headMatch.group( 1 ); if (headMatch.matches()) {
String value = headMatch.group( 2 ); String head = headMatch.group(1);
if( head.equalsIgnoreCase( "nrows" ) ) String value = headMatch.group(2);
raster.rows = Integer.parseInt( value ); if (head.equalsIgnoreCase("nrows"))
else if ( head.equalsIgnoreCase( "ncols" ) ) raster.rows = Integer.parseInt(value);
raster.cols = Integer.parseInt( value ); else if (head.equalsIgnoreCase("ncols"))
else if ( head.equalsIgnoreCase( "xllcorner" ) ) raster.cols = Integer.parseInt(value);
raster.xll = Double.parseDouble( value ); else if (head.equalsIgnoreCase("xllcorner"))
else if ( head.equalsIgnoreCase( "yllcorner" ) ) raster.xll = Double.parseDouble(value);
raster.yll = Double.parseDouble( value ); else if (head.equalsIgnoreCase("yllcorner"))
else if ( head.equalsIgnoreCase( "NODATA_value" ) ) raster.yll = Double.parseDouble(value);
raster.NDATA = value; else if (head.equalsIgnoreCase("NODATA_value"))
else if ( head.equals( "cellsize" ) ) raster.NDATA = value;
raster.cellsize = Double.parseDouble( value ); else if (head.equals("cellsize"))
else if ( head.equals( "dx" ) ) raster.cellsize = Double.parseDouble(value);
raster.dx = Double.parseDouble( value ); else if (head.equals("dx"))
else if ( head.equals( "dy" ) ) raster.dx = Double.parseDouble(value);
raster.dy = Double.parseDouble( value ); else if (head.equals("dy"))
else raster.dy = Double.parseDouble(value);
System.out.println( "Unknown setting: " + line ); else
} System.out.println("Unknown setting: " + line);
else if( line.matches( "^-?\\d+.*" )) } else if (line.matches("^-?\\d+.*")) {
{ // System.out.println( "Processing data section");
//System.out.println( "Processing data section"); // Check that data is set up!
//Check that data is set up! // Start processing numbers!
//Start processing numbers! int row = 0;
int row = 0; double[][] data = new double[raster.rows][];
double[][] data = new double[raster.rows][]; while (true) {
while( true ) line = line.trim();
{ // System.out.println( "Got data row: " + line );
line=line.trim(); String[] inData = line.split("\\s+");
//System.out.println( "Got data row: " + line ); double[] numData = new double[raster.cols];
String[] inData = line.split( "\\s+" ); if (inData.length != numData.length) {
double[] numData = new double[raster.cols]; System.out.println(inData);
if( inData.length != numData.length ){ throw new RuntimeException("Wrong number of columns: Expected " + raster.cols + " got " + inData.length + " for line \n" + line);
System.out.println(inData); }
throw new RuntimeException( "Wrong number of columns: Expected " + for (int col = 0; col < raster.cols; col++) {
raster.cols + " got " + inData.length + " for line \n" + line ); if (inData[col].equals(noData))
numData[col] = Double.NaN;
else
numData[col] = Double.parseDouble(inData[col]);
}
data[row] = numData;
// Ugly backward input structure...
line = input.readLine();
// if (input.ready())
if (line==null)
break;
// else
// break;
row++;
} }
for( int col = 0; col < raster.cols; col ++ ) if (row != raster.rows - 1)
{ throw new RuntimeException("Wrong number of rows: expected " + raster.rows + " got " + (row + 1));
if( inData[col].equals( noData )) numData[col] = Double.NaN; raster.data = data;
else numData[col] = Double.parseDouble( inData[col] ); } else {
} if (line.length() >= 0 && !line.matches("^\\s*$"))
data[row] = numData; AnalysisLogger.getLogger().debug("Unknown line: " + line);
//Ugly backward input structure...
if( input.ready() ) line = input.readLine();
else break;
row++;
} }
if( row != raster.rows - 1)
throw new RuntimeException( "Wrong number of rows: expected " + raster.rows + " got " + (row+1) );
raster.data = data;
}
else
{
if( line.length() >= 0 && ! line.matches( "^\\s*$" ))
System.out.println( "Unknown line: " + line);
} }
} catch (Exception e) {
AnalysisLogger.getLogger().debug("ASC Reader: Finished to read the stream");
} }
if (input != null) {
if (input!=null){
input.close(); input.close();
if (urlConn!=null && urlConn.getInputStream()!=null) if (urlConn != null && urlConn.getInputStream() != null)
urlConn.getInputStream().close(); urlConn.getInputStream().close();
} }
return raster; return raster;