Deduplicate code
This commit is contained in:
parent
8c6be04228
commit
7c184a03f9
1 changed files with 45 additions and 65 deletions
|
@ -5,22 +5,25 @@ import java.io.BufferedOutputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
public class ConvertLidarTile {
|
public class ConvertLidarTile {
|
||||||
public static int NROWS;
|
private static int NROWS;
|
||||||
public static int NCOLS;
|
private static int NCOLS;
|
||||||
public static int ROW_LENGTH;
|
|
||||||
|
|
||||||
public static final short NODATA2 = -32767; // hgt-formats nodata
|
public static final short NODATA2 = -32767; // hgt-formats nodata
|
||||||
public static final short NODATA = Short.MIN_VALUE;
|
public static final short NODATA = Short.MIN_VALUE;
|
||||||
|
|
||||||
public static final int SRTM3_ROW_LENGTH = 1200; // number of elevation values per line
|
private static final int SRTM_BORDER_OVERLAP = 1;
|
||||||
public static final int SRTM1_ROW_LENGTH = 3600; //-- New file resolution is 3601x3601
|
private static final int SRTM3_ROW_LENGTH = 1201; // 3 arc second resolution (90m)
|
||||||
|
private static final int SRTM3_FILE_SIZE = SRTM3_ROW_LENGTH * SRTM3_ROW_LENGTH * Short.BYTES;
|
||||||
|
private static final int SRTM1_ROW_LENGTH = 3601; // 1 arc second resolution (30m)
|
||||||
|
|
||||||
static short[] imagePixels;
|
static short[] imagePixels;
|
||||||
|
|
||||||
|
@ -31,7 +34,7 @@ public class ConvertLidarTile {
|
||||||
ZipEntry ze = zis.getNextEntry();
|
ZipEntry ze = zis.getNextEntry();
|
||||||
if (ze == null) break;
|
if (ze == null) break;
|
||||||
if (ze.getName().endsWith(".hgt")) {
|
if (ze.getName().endsWith(".hgt")) {
|
||||||
readHgtFromStream(zis, rowOffset, colOffset, 1200);
|
readHgtFromStream(zis, rowOffset, colOffset, SRTM3_ROW_LENGTH);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,13 +43,13 @@ public class ConvertLidarTile {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void readHgtFromStream(InputStream is, int rowOffset, int colOffset, int row_length)
|
private static void readHgtFromStream(InputStream is, int rowOffset, int colOffset, int rowLength)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
|
DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
|
||||||
for (int ir = 0; ir < row_length + 1; ir++) {
|
for (int ir = 0; ir < rowLength; ir++) {
|
||||||
int row = rowOffset + ir;
|
int row = rowOffset + ir;
|
||||||
|
|
||||||
for (int ic = 0; ic < row_length + 1; ic++) {
|
for (int ic = 0; ic < rowLength; ic++) {
|
||||||
int col = colOffset + ic;
|
int col = colOffset + ic;
|
||||||
|
|
||||||
int i1 = dis.read(); // msb first!
|
int i1 = dis.read(); // msb first!
|
||||||
|
@ -191,80 +194,57 @@ public class ConvertLidarTile {
|
||||||
}
|
}
|
||||||
|
|
||||||
public SrtmRaster getRaster(File f, double lon, double lat) throws Exception {
|
public SrtmRaster getRaster(File f, double lon, double lat) throws Exception {
|
||||||
|
long fileSize;
|
||||||
|
InputStream inputStream;
|
||||||
|
|
||||||
if (f.getName().toLowerCase().endsWith(".zip")) {
|
if (f.getName().toLowerCase().endsWith(".zip")) {
|
||||||
|
String HGT_FILE_EXT = ".hgt";
|
||||||
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(f)));
|
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(f)));
|
||||||
try {
|
|
||||||
for (; ; ) {
|
for (; ; ) {
|
||||||
ZipEntry ze = zis.getNextEntry();
|
ZipEntry ze = zis.getNextEntry();
|
||||||
if (ze == null) break;
|
if (ze == null) {
|
||||||
if (ze.getName().toLowerCase().endsWith(".hgt")) {
|
throw new FileNotFoundException(f.getName() + " doesn't contain a " + HGT_FILE_EXT + " file.");
|
||||||
if (ze.getSize() > ((SRTM3_ROW_LENGTH + 1) * (SRTM3_ROW_LENGTH + 1) * 2)) {
|
|
||||||
ROW_LENGTH = SRTM1_ROW_LENGTH;
|
|
||||||
} else {
|
|
||||||
ROW_LENGTH = SRTM3_ROW_LENGTH;
|
|
||||||
}
|
}
|
||||||
// stay a 1x1 raster
|
if (ze.getName().toLowerCase().endsWith(HGT_FILE_EXT)) {
|
||||||
NROWS = ROW_LENGTH + 1;
|
fileSize = ze.getSize();
|
||||||
NCOLS = ROW_LENGTH + 1;
|
inputStream = zis;
|
||||||
|
|
||||||
imagePixels = new short[NROWS * NCOLS]; // 650 MB !
|
|
||||||
|
|
||||||
// prefill as NODATA
|
|
||||||
for (int row = 0; row < NROWS; row++) {
|
|
||||||
for (int col = 0; col < NCOLS; col++) {
|
|
||||||
imagePixels[row * NCOLS + col] = NODATA;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
readHgtFromStream(zis, 0, 0, ROW_LENGTH);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
zis.close();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (f.length() > ((SRTM3_ROW_LENGTH + 1) * (SRTM3_ROW_LENGTH + 1) * 2)) {
|
fileSize = f.length();
|
||||||
ROW_LENGTH = SRTM1_ROW_LENGTH;
|
inputStream = new FileInputStream(f);
|
||||||
} else {
|
|
||||||
ROW_LENGTH = SRTM3_ROW_LENGTH;
|
|
||||||
}
|
}
|
||||||
// stay a 1x1 raster
|
|
||||||
NROWS = ROW_LENGTH + 1;
|
|
||||||
NCOLS = ROW_LENGTH + 1;
|
|
||||||
|
|
||||||
imagePixels = new short[NROWS * NCOLS]; // 650 MB !
|
int rowLength;
|
||||||
|
if (fileSize > SRTM3_FILE_SIZE) {
|
||||||
|
rowLength = SRTM1_ROW_LENGTH;
|
||||||
|
} else {
|
||||||
|
rowLength = SRTM3_ROW_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
// stay at 1 deg * 1 deg raster
|
||||||
|
NROWS = rowLength;
|
||||||
|
NCOLS = rowLength;
|
||||||
|
|
||||||
|
imagePixels = new short[NROWS * NCOLS];
|
||||||
|
|
||||||
// prefill as NODATA
|
// prefill as NODATA
|
||||||
for (int row = 0; row < NROWS; row++) {
|
Arrays.fill(imagePixels, NODATA);
|
||||||
for (int col = 0; col < NCOLS; col++) {
|
readHgtFromStream(inputStream, 0, 0, rowLength);
|
||||||
imagePixels[row * NCOLS + col] = NODATA;
|
inputStream.close();
|
||||||
}
|
|
||||||
}
|
|
||||||
FileInputStream dis = new FileInputStream(f);
|
|
||||||
try {
|
|
||||||
readHgtFromStream(dis, 0, 0, ROW_LENGTH);
|
|
||||||
} finally {
|
|
||||||
dis.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("read file " + f + " rl " + ROW_LENGTH);
|
|
||||||
|
|
||||||
boolean halfCol5 = false; // no halfcol tiles in lidar data (?)
|
|
||||||
|
|
||||||
SrtmRaster raster = new SrtmRaster();
|
SrtmRaster raster = new SrtmRaster();
|
||||||
raster.nrows = NROWS;
|
raster.nrows = NROWS;
|
||||||
raster.ncols = NCOLS;
|
raster.ncols = NCOLS;
|
||||||
raster.halfcol = halfCol5;
|
raster.halfcol = false; // assume full resolution
|
||||||
raster.noDataValue = NODATA;
|
raster.noDataValue = NODATA;
|
||||||
raster.cellsize = 1. / (double) ROW_LENGTH;
|
raster.cellsize = 1. / (double) (rowLength - SRTM_BORDER_OVERLAP);
|
||||||
raster.xllcorner = (int) (lon < 0 ? lon - 1 : lon); //onDegreeStart - raster.cellsize;
|
raster.xllcorner = (int) (lon < 0 ? lon - 1 : lon); //onDegreeStart - raster.cellsize;
|
||||||
raster.yllcorner = (int) (lat < 0 ? lat - 1 : lat); //latDegreeStart - raster.cellsize;
|
raster.yllcorner = (int) (lat < 0 ? lat - 1 : lat); //latDegreeStart - raster.cellsize;
|
||||||
raster.eval_array = imagePixels;
|
raster.eval_array = imagePixels;
|
||||||
|
|
||||||
return raster;
|
return raster;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue