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,19 +14,17 @@ 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");
} }
@ -35,27 +32,20 @@ public class AscRasterReader
/** /**
* 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);
urlConn.setReadTimeout(1200000);
urlConn.setAllowUserInteraction(false);
urlConn.setDoOutput(true);
input = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
*/ */
// using Manzi's data Transfer to overcome https issues // using Manzi's data Transfer to overcome https issues
try { try {
@ -63,20 +53,24 @@ public class AscRasterReader
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
AnalysisLogger.getLogger().debug("Error: Bad URI " + filename); AnalysisLogger.getLogger().debug("Error: Bad URI " + filename);
} }
} } else {
else {
AnalysisLogger.getLogger().debug("Getting file from local file"); AnalysisLogger.getLogger().debug("Getting file from local file");
input = new BufferedReader(new FileReader(filename)); input = new BufferedReader(new FileReader(filename));
} }
while( input.ready() ) try {
// while( input.ready() )
while (true)
{ {
String line = input.readLine(); String line = input.readLine();
if (line==null)
break;
if (line != null && line.length() > 0) if (line != null && line.length() > 0)
line = line.trim(); line = line.trim();
Matcher headMatch = header.matcher(line); Matcher headMatch = header.matcher(line);
// Match all the heads // Match all the heads
if( headMatch.matches() ) if (headMatch.matches()) {
{
String head = headMatch.group(1); String head = headMatch.group(1);
String value = headMatch.group(2); String value = headMatch.group(2);
if (head.equalsIgnoreCase("nrows")) if (head.equalsIgnoreCase("nrows"))
@ -97,47 +91,48 @@ public class AscRasterReader
raster.dy = Double.parseDouble(value); raster.dy = Double.parseDouble(value);
else else
System.out.println("Unknown setting: " + line); 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(); line = line.trim();
// System.out.println( "Got data row: " + line ); // System.out.println( "Got data row: " + line );
String[] inData = line.split("\\s+"); String[] inData = line.split("\\s+");
double[] numData = new double[raster.cols]; double[] numData = new double[raster.cols];
if (inData.length != numData.length) { if (inData.length != numData.length) {
System.out.println(inData); System.out.println(inData);
throw new RuntimeException( "Wrong number of columns: Expected " + throw new RuntimeException("Wrong number of columns: Expected " + raster.cols + " got " + inData.length + " for line \n" + line);
raster.cols + " got " + inData.length + " for line \n" + line );
} }
for( int col = 0; col < raster.cols; col ++ ) for (int col = 0; col < raster.cols; col++) {
{ if (inData[col].equals(noData))
if( inData[col].equals( noData )) numData[col] = Double.NaN; numData[col] = Double.NaN;
else numData[col] = Double.parseDouble( inData[col] ); else
numData[col] = Double.parseDouble(inData[col]);
} }
data[row] = numData; data[row] = numData;
// Ugly backward input structure... // Ugly backward input structure...
if( input.ready() ) line = input.readLine(); line = input.readLine();
else break; // if (input.ready())
if (line==null)
break;
// else
// break;
row++; row++;
} }
if (row != raster.rows - 1) if (row != raster.rows - 1)
throw new RuntimeException("Wrong number of rows: expected " + raster.rows + " got " + (row + 1)); throw new RuntimeException("Wrong number of rows: expected " + raster.rows + " got " + (row + 1));
raster.data = data; raster.data = data;
} } else {
else
{
if (line.length() >= 0 && !line.matches("^\\s*$")) if (line.length() >= 0 && !line.matches("^\\s*$"))
System.out.println( "Unknown line: " + line); AnalysisLogger.getLogger().debug("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)