update to one routine

This commit is contained in:
afischerdev 2023-05-10 12:56:21 +02:00
parent a2c5ed68fc
commit c20b2ba686
2 changed files with 61 additions and 97 deletions

View file

@ -29,6 +29,7 @@ public class ConvertLidarTile {
try { try {
for (; ; ) { for (; ; ) {
ZipEntry ze = zis.getNextEntry(); ZipEntry ze = zis.getNextEntry();
if (ze == null) break;
if (ze.getName().endsWith(".hgt")) { if (ze.getName().endsWith(".hgt")) {
readHgtFromStream(zis, rowOffset, colOffset, 1200); readHgtFromStream(zis, rowOffset, colOffset, 1200);
return; return;
@ -65,34 +66,6 @@ public class ConvertLidarTile {
} }
} }
private static void readHgtFromFile(File f, int rowOffset, int colOffset)
throws Exception {
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
for (int ir = 0; ir < ROW_LENGTH + 1; ir++) {
int row = rowOffset + ir;
for (int ic = 0; ic < ROW_LENGTH + 1; ic++) {
int col = colOffset + ic;
int i1 = dis.read(); // msb first!
int i0 = dis.read();
if (i0 == -1 || i1 == -1)
throw new RuntimeException("unexcepted end of file reading hgt entry!");
short val = (short) ((i1 << 8) | i0);
if (val == NODATA2) {
val = NODATA;
}
setPixel(row, col, val);
}
}
}
private static void setPixel(int row, int col, short val) { private static void setPixel(int row, int col, short val) {
if (row >= 0 && row < NROWS && col >= 0 && col < NCOLS) { if (row >= 0 && row < NROWS && col >= 0 && col < NCOLS) {
imagePixels[row * NCOLS + col] = val; imagePixels[row * NCOLS + col] = val;
@ -217,57 +190,20 @@ public class ConvertLidarTile {
doConvert(args[1], ilon_base, ilat_base, filename30); doConvert(args[1], ilon_base, ilat_base, filename30);
} }
public SrtmRaster getRasterDirect(File f, double lon, double lat) throws Exception { public SrtmRaster getRaster(File f, double lon, double lat) throws Exception {
if (f.length() > ((SRTM3_ROW_LENGTH + 1) * (SRTM3_ROW_LENGTH + 1) * 2)) {
ROW_LENGTH = SRTM1_ROW_LENGTH;
} else {
ROW_LENGTH = SRTM3_ROW_LENGTH;
}
System.out.println("read file " + f + " rl " + ROW_LENGTH);
// stay a 1x1 raster
NROWS = ROW_LENGTH + 1;
NCOLS = ROW_LENGTH + 1;
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;
}
}
readHgtFromFile(f, 0, 0);
boolean halfCol5 = false; // no halfcol tiles in lidar data (?)
SrtmRaster raster = new SrtmRaster();
raster.nrows = NROWS;
raster.ncols = NCOLS;
raster.halfcol = halfCol5;
raster.noDataValue = NODATA;
raster.cellsize = 1. / (double) ROW_LENGTH;
raster.xllcorner = (int) (lon < 0 ? lon - 1 : lon); //onDegreeStart - raster.cellsize;
raster.yllcorner = (int) (lat < 0 ? lat - 1 : lat); //latDegreeStart - raster.cellsize;
raster.eval_array = imagePixels;
return raster;
}
public SrtmRaster getRasterZip(File f, double lon, double lat) throws Exception {
if (f.getName().toLowerCase().endsWith(".zip")) {
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(f))); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(f)));
try { try {
for (; ; ) { for (; ; ) {
ZipEntry ze = zis.getNextEntry(); ZipEntry ze = zis.getNextEntry();
if (ze == null) break;
if (ze.getName().toLowerCase().endsWith(".hgt")) { if (ze.getName().toLowerCase().endsWith(".hgt")) {
if (ze.getSize() > ((SRTM3_ROW_LENGTH + 1) * (SRTM3_ROW_LENGTH + 1) * 2)) { if (ze.getSize() > ((SRTM3_ROW_LENGTH + 1) * (SRTM3_ROW_LENGTH + 1) * 2)) {
ROW_LENGTH = SRTM1_ROW_LENGTH; ROW_LENGTH = SRTM1_ROW_LENGTH;
} else { } else {
ROW_LENGTH = SRTM3_ROW_LENGTH; ROW_LENGTH = SRTM3_ROW_LENGTH;
} }
System.out.println("read file " + f + " rl " + ROW_LENGTH);
// stay a 1x1 raster // stay a 1x1 raster
NROWS = ROW_LENGTH + 1; NROWS = ROW_LENGTH + 1;
NCOLS = ROW_LENGTH + 1; NCOLS = ROW_LENGTH + 1;
@ -287,7 +223,33 @@ public class ConvertLidarTile {
} finally { } finally {
zis.close(); zis.close();
} }
} else {
if (f.length() > ((SRTM3_ROW_LENGTH + 1) * (SRTM3_ROW_LENGTH + 1) * 2)) {
ROW_LENGTH = SRTM1_ROW_LENGTH;
} 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 !
// prefill as NODATA
for (int row = 0; row < NROWS; row++) {
for (int col = 0; col < NCOLS; col++) {
imagePixels[row * NCOLS + col] = NODATA;
}
}
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 (?) boolean halfCol5 = false; // no halfcol tiles in lidar data (?)
@ -304,4 +266,5 @@ public class ConvertLidarTile {
return raster; return raster;
} }
} }

View file

@ -7,6 +7,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import btools.util.CompactLongSet; import btools.util.CompactLongSet;
@ -217,13 +218,13 @@ public class PosUnifier extends MapCreatorBase {
if (lastSrtmRaster == null) { if (lastSrtmRaster == null) {
File f = new File(new File(srtmdir), filename + ".hgt"); File f = new File(new File(srtmdir), filename + ".hgt");
if (f.exists()) { if (f.exists()) {
lastSrtmRaster = new ConvertLidarTile().getRasterDirect(f, lon, lat); lastSrtmRaster = new ConvertLidarTile().getRaster(f, lon, lat);
srtmmap.put(filename, lastSrtmRaster); srtmmap.put(filename, lastSrtmRaster);
return lastSrtmRaster; return lastSrtmRaster;
} }
f = new File(new File(srtmdir), filename + ".zip"); f = new File(new File(srtmdir), filename + ".zip");
if (f.exists()) { if (f.exists()) {
lastSrtmRaster = new ConvertLidarTile().getRasterZip(f, lon, lat); lastSrtmRaster = new ConvertLidarTile().getRaster(f, lon, lat);
srtmmap.put(filename, lastSrtmRaster); srtmmap.put(filename, lastSrtmRaster);
return lastSrtmRaster; return lastSrtmRaster;
} }
@ -246,11 +247,11 @@ public class PosUnifier extends MapCreatorBase {
lon = -lon + 1; lon = -lon + 1;
} }
return String.format("%s%02d%s%03d", latPref, lat, lonPref, lon); return String.format(Locale.US, "%s%02d%s%03d", latPref, lat, lonPref, lon);
} }
private void resetSrtm() { private void resetSrtm() {
srtmmap = new HashMap<String, SrtmRaster>(); srtmmap = new HashMap<>();
lastSrtmLonIdx = -1; lastSrtmLonIdx = -1;
lastSrtmLatIdx = -1; lastSrtmLatIdx = -1;
lastSrtmRaster = null; lastSrtmRaster = null;