From 580b7b2421b0285ca8a2ae98eaeeadb491f6ed59 Mon Sep 17 00:00:00 2001 From: afischerdev Date: Sat, 6 May 2023 17:48:54 +0200 Subject: [PATCH 1/3] add hgt reader --- .../java/btools/mapcreator/HgtReader.java | 269 ++++++++++++++++++ .../java/btools/mapcreator/PosUnifier.java | 51 +++- 2 files changed, 316 insertions(+), 4 deletions(-) create mode 100644 brouter-map-creator/src/main/java/btools/mapcreator/HgtReader.java diff --git a/brouter-map-creator/src/main/java/btools/mapcreator/HgtReader.java b/brouter-map-creator/src/main/java/btools/mapcreator/HgtReader.java new file mode 100644 index 0000000..b232bf3 --- /dev/null +++ b/brouter-map-creator/src/main/java/btools/mapcreator/HgtReader.java @@ -0,0 +1,269 @@ +// License: GPL. For details, see LICENSE file. +package btools.mapcreator; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.ShortBuffer; +import java.nio.channels.FileChannel; +import java.util.HashMap; +import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +/** + * adapted from https://github.com/JOSM/josm-plugins/blob/master/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/HgtReader.java + *

+ * Class HgtReader reads data from SRTM HGT files. Currently this class is restricted to a resolution of 3 arc seconds. + *

+ * SRTM data files are available at the NASA SRTM site + * + * @author Oliver Wieland <oliver.wieland@online.de> + */ +public class HgtReader { + final static boolean DEBUG = false; + + private static final int SECONDS_PER_MINUTE = 60; + + public static final String HGT_EXT = ".hgt"; + public static final String ZIP_EXT = ".zip"; + + // alter these values for different SRTM resolutions + public static final int HGT3_RES = 3; // resolution in arc seconds + public static final int HGT3_ROW_LENGTH = 1201; // number of elevation values per line + public static final int HGT_VOID = -32768; // magic number which indicates 'void data' in HGT file + public static final int HGT1_RES = 1; // <<- The new SRTM is 1-ARCSEC + public static final int HGT1_ROW_LENGTH = 3601; //-- New file resolution is 3601x3601 + /** + * The 'no elevation' data magic. + */ + public static double NO_ELEVATION = Double.NaN; + + private static String srtmFolder = ""; + + private static final Map cache = new HashMap<>(); + + public HgtReader(String folder) { + srtmFolder = folder; + } + + public static double getElevationFromHgt(double lat, double lon) { + try { + String file = getHgtFileName(lat, lon); + if (DEBUG) System.out.println("SRTM buffer " + file + " for " + lat + " " + lon); + + // given area in cache? + if (!cache.containsKey(file)) { + + // fill initial cache value. If no file is found, then + // we use it as a marker to indicate 'file has been searched + // but is not there' + cache.put(file, null); + // Try all resource directories + //for (String location : Main.pref.getAllPossiblePreferenceDirs()) + { + String fullPath = new File(srtmFolder, file + HGT_EXT).getPath(); + File f = new File(fullPath); + if (f.exists()) { + // found something: read HGT file... + ShortBuffer data = readHgtFile(fullPath); + // ... and store result in cache + cache.put(file, data); + //break; + } else { + fullPath = new File(srtmFolder, file + ZIP_EXT).getPath(); + f = new File(fullPath); + if (f.exists()) { + ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(f))); + try { + for (; ; ) { + ZipEntry ze = zis.getNextEntry(); + if (ze == null) break; + if (ze.getName().toLowerCase().endsWith(HGT_EXT)) { + // System.out.println("read zip " + ze.getName()); + ShortBuffer data = readHgtStream(zis); + // ... and store result in cache + cache.put(file, data); + break; + } + zis.closeEntry(); + } + } finally { + zis.close(); + } + } + } + } + } + + // read elevation value + return readElevation(lat, lon); + } catch (FileNotFoundException e) { + System.err.println("SRTM Get elevation from HGT " + lat + ", " + lon + " failed: => " + e.getMessage()); + // no problem... file not there + return NO_ELEVATION; + } catch (Exception ioe) { + // oops... + ioe.printStackTrace(System.err); + // fallback + return NO_ELEVATION; + } + } + + @SuppressWarnings("resource") + private static ShortBuffer readHgtFile(String file) throws Exception { + if (file == null) throw new Exception("no srmt file " + file); + + FileChannel fc = null; + ShortBuffer sb = null; + try { + // Eclipse complains here about resource leak on 'fc' - even with 'finally' clause??? + fc = new FileInputStream(file).getChannel(); + // choose the right endianness + + ByteBuffer bb = ByteBuffer.allocateDirect((int) fc.size()); + while (bb.remaining() > 0) fc.read(bb); + + bb.flip(); + //sb = bb.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); + sb = bb.order(ByteOrder.BIG_ENDIAN).asShortBuffer(); + } finally { + if (fc != null) fc.close(); + } + + return sb; + } + + // @SuppressWarnings("resource") + private static ShortBuffer readHgtStream(InputStream zis) throws Exception { + if (zis == null) throw new Exception("no srmt stream "); + + ShortBuffer sb = null; + try { + // choose the right endianness + + byte[] bytes = zis.readAllBytes(); + ByteBuffer bb = ByteBuffer.allocate(bytes.length); + bb.put(bytes, 0, bytes.length); + //while (bb.remaining() > 0) zis.read(bb, 0, size); + + //ByteBuffer bb = ByteBuffer.allocate(zis.available()); + //Channels.newChannel(zis).read(bb); + bb.flip(); + //sb = bb.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); + sb = bb.order(ByteOrder.BIG_ENDIAN).asShortBuffer(); + } finally { + + } + + return sb; + } + + /** + * Reads the elevation value for the given coordinate. + *

+ * See also stackexchange.com + * + * @param lat, lon the coordinate to get the elevation data for + * @return the elevation value or Double.NaN, if no value is present + */ + public static double readElevation(double lat, double lon) { + String tag = getHgtFileName(lat, lon); + + ShortBuffer sb = cache.get(tag); + + if (sb == null) { + return NO_ELEVATION; + } + + if (DEBUG) System.out.println("SRTM buffer size " + sb.capacity() + " limit " + sb.limit()); + try { + int rowLength = HGT3_ROW_LENGTH; + int resolution = HGT3_RES; + if (sb.capacity() > (HGT3_ROW_LENGTH * HGT3_ROW_LENGTH)) { + rowLength = HGT1_ROW_LENGTH; + resolution = HGT1_RES; + } + // see http://gis.stackexchange.com/questions/43743/how-to-extract-elevation-from-hgt-file + double fLat = frac(lat) * SECONDS_PER_MINUTE; + double fLon = frac(lon) * SECONDS_PER_MINUTE; + + // compute offset within HGT file + int row = (int) Math.round((fLat) * SECONDS_PER_MINUTE / resolution); + int col = (int) Math.round((fLon) * SECONDS_PER_MINUTE / resolution); + if (lon < 0) col = rowLength - col - 1; + if (lat > 0) row = rowLength - row - 1; + + + //row = rowLength - row; + int cell = (rowLength * (row)) + col; + //int cell = ((rowLength * (latitude)) + longitude); + + if (DEBUG) + System.out.println("Read SRTM elevation data from row/col/cell " + row + "," + col + ", " + cell + ", " + sb.limit()); + + // valid position in buffer? + if (cell < sb.limit()) { + short ele = sb.get(cell); + // check for data voids + if (ele == HGT_VOID) { + return NO_ELEVATION; + } else { + return ele; + } + } else { + return NO_ELEVATION; + } + } catch (Exception e) { + System.err.println("error at " + lon + " " + lat + " "); + e.printStackTrace(); + } + return NO_ELEVATION; + } + + /** + * Gets the associated HGT file name for the given way point. Usually the + * format is [N|S]nn[W|E]mmm.hgt where nn is the integral latitude + * without decimals and mmm is the longitude. + * + * @param llat,llon the coordinate to get the filename for + * @return the file name of the HGT file + */ + public static String getHgtFileName(double llat, double llon) { + int lat = (int) llat; + int lon = (int) llon; + + String latPref = "N"; + if (lat < 0) { + latPref = "S"; + lat = -lat + 1; + } + String lonPref = "E"; + if (lon < 0) { + lonPref = "W"; + lon = -lon + 1; + } + + return String.format("%s%02d%s%03d", latPref, lat, lonPref, lon); + } + + public static double frac(double d) { + long iPart; + double fPart; + + // Get user input + iPart = (long) d; + fPart = d - iPart; + return Math.abs(fPart); + } + + public static void clear() { + if (cache != null) { + cache.clear(); + } + } +} diff --git a/brouter-map-creator/src/main/java/btools/mapcreator/PosUnifier.java b/brouter-map-creator/src/main/java/btools/mapcreator/PosUnifier.java index 2943052..923e912 100644 --- a/brouter-map-creator/src/main/java/btools/mapcreator/PosUnifier.java +++ b/brouter-map-creator/src/main/java/btools/mapcreator/PosUnifier.java @@ -32,12 +32,31 @@ public class PosUnifier extends MapCreatorBase { private int lastSrtmLatIdx; private SrtmRaster lastSrtmRaster; private String srtmdir; + private HgtReader hgtReader; private CompactLongSet borderNids; public static void main(String[] args) throws Exception { System.out.println("*** PosUnifier: Unify position values and enhance elevation"); - if (args.length != 5) { + if (args.length == 3) { + PosUnifier posu = new PosUnifier(); + posu.srtmdir = (args[0]); + posu.srtmmap = new HashMap<>(); + double lon = Double.parseDouble(args[1]); + double lat = Double.parseDouble(args[2]); + + NodeData n = new NodeData(1, lon, lat); + SrtmRaster srtm = posu.srtmForNode(n.ilon, n.ilat); + short selev = Short.MIN_VALUE; + if (srtm == null) { + selev = posu.hgtForNode(n.ilon, n.ilat); + } else { + selev = srtm.getElevation(n.ilon, n.ilat); + } + posu.resetSrtm(); + System.out.println("-----> selv for " + lat + ", "+ lon + " = " + selev + " = " + (selev/4.)); + return; + } else if (args.length != 5) { System.out.println("usage: java PosUnifier "); return; } @@ -80,7 +99,12 @@ public class PosUnifier extends MapCreatorBase { @Override public void nextNode(NodeData n) throws Exception { SrtmRaster srtm = srtmForNode(n.ilon, n.ilat); - n.selev = srtm == null ? Short.MIN_VALUE : srtm.getElevation(n.ilon, n.ilat); + n.selev = Short.MIN_VALUE; + if (srtm == null) { + n.selev = hgtForNode(n.ilon, n.ilat); + } else { + n.selev = srtm.getElevation(n.ilon, n.ilat); + } findUniquePos(n); @@ -93,6 +117,7 @@ public class PosUnifier extends MapCreatorBase { @Override public void nodeFileEnd(File nodeFile) throws Exception { nodesOutStream.close(); + resetSrtm(); } private boolean checkAdd(int lon, int lat) { @@ -157,7 +182,7 @@ public class PosUnifier extends MapCreatorBase { lastSrtmRaster = srtmmap.get(filename); if (lastSrtmRaster == null && !srtmmap.containsKey(filename)) { File f = new File(new File(srtmdir), filename + ".bef"); - System.out.println("checking: " + f + " ilon=" + ilon + " ilat=" + ilat); + //System.out.println("checking: " + f + " ilon=" + ilon + " ilat=" + ilat); if (f.exists()) { System.out.println("*** reading: " + f); try { @@ -172,7 +197,7 @@ public class PosUnifier extends MapCreatorBase { } f = new File(new File(srtmdir), filename + ".zip"); - System.out.println("reading: " + f + " ilon=" + ilon + " ilat=" + ilat); + // System.out.println("reading: " + f + " ilon=" + ilon + " ilat=" + ilat); if (f.exists()) { try { lastSrtmRaster = new SrtmData(f).getRaster(); @@ -185,11 +210,29 @@ public class PosUnifier extends MapCreatorBase { return lastSrtmRaster; } + private short hgtForNode(int ilon, int ilat) throws Exception { + double lon = (ilon - 180000000)/1000000.; + double lat = (ilat - 90000000)/1000000.; + + try { + if (hgtReader == null) hgtReader = new HgtReader(srtmdir); + double res = hgtReader.getElevationFromHgt(lat, lon); + // System.out.println("**** reading " + res + " ****"); + if (Double.isNaN(res)) return Short.MIN_VALUE; + return (short) (res*4); + } catch (Exception e) { + System.out.println("**** ERROR reading hgt " + lon + " " + lat + " ****"); + } + + return Short.MIN_VALUE; + } + private void resetSrtm() { srtmmap = new HashMap(); lastSrtmLonIdx = -1; lastSrtmLatIdx = -1; lastSrtmRaster = null; + if (hgtReader != null) hgtReader.clear(); } } From a2c5ed68fcfb4aaa98dbc1fbe00651cd53caa584 Mon Sep 17 00:00:00 2001 From: afischerdev Date: Mon, 8 May 2023 16:18:24 +0200 Subject: [PATCH 2/3] change hgt to ConvertLidarTile --- .../btools/mapcreator/ConvertLidarTile.java | 126 +++++++++++++++++- .../java/btools/mapcreator/PosUnifier.java | 69 ++++++---- 2 files changed, 167 insertions(+), 28 deletions(-) diff --git a/brouter-map-creator/src/main/java/btools/mapcreator/ConvertLidarTile.java b/brouter-map-creator/src/main/java/btools/mapcreator/ConvertLidarTile.java index 65f18aa..d06de33 100644 --- a/brouter-map-creator/src/main/java/btools/mapcreator/ConvertLidarTile.java +++ b/brouter-map-creator/src/main/java/btools/mapcreator/ConvertLidarTile.java @@ -14,10 +14,14 @@ import java.util.zip.ZipInputStream; public class ConvertLidarTile { public static int NROWS; public static int NCOLS; + public static int ROW_LENGTH; public static final short NODATA2 = -32767; // hgt-formats nodata public static final short NODATA = Short.MIN_VALUE; + public static final int SRTM3_ROW_LENGTH = 1200; // number of elevation values per line + public static final int SRTM1_ROW_LENGTH = 3600; //-- New file resolution is 3601x3601 + static short[] imagePixels; private static void readHgtZip(String filename, int rowOffset, int colOffset) throws Exception { @@ -26,7 +30,7 @@ public class ConvertLidarTile { for (; ; ) { ZipEntry ze = zis.getNextEntry(); if (ze.getName().endsWith(".hgt")) { - readHgtFromStream(zis, rowOffset, colOffset); + readHgtFromStream(zis, rowOffset, colOffset, 1200); return; } } @@ -35,13 +39,40 @@ public class ConvertLidarTile { } } - private static void readHgtFromStream(InputStream is, int rowOffset, int colOffset) + private static void readHgtFromStream(InputStream is, int rowOffset, int colOffset, int row_length) throws Exception { DataInputStream dis = new DataInputStream(new BufferedInputStream(is)); - for (int ir = 0; ir < 1201; ir++) { + for (int ir = 0; ir < row_length + 1; ir++) { int row = rowOffset + ir; - for (int ic = 0; ic < 1201; ic++) { + 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 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! @@ -186,4 +217,91 @@ public class ConvertLidarTile { doConvert(args[1], ilon_base, ilat_base, filename30); } + public SrtmRaster getRasterDirect(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 { + + ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(f))); + try { + for (; ; ) { + ZipEntry ze = zis.getNextEntry(); + if (ze.getName().toLowerCase().endsWith(".hgt")) { + if (ze.getSize() > ((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; + } + } + readHgtFromStream(zis, 0, 0, ROW_LENGTH); + break; + } + } + } finally { + zis.close(); + } + + + 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; + + } } diff --git a/brouter-map-creator/src/main/java/btools/mapcreator/PosUnifier.java b/brouter-map-creator/src/main/java/btools/mapcreator/PosUnifier.java index 923e912..22df5e6 100644 --- a/brouter-map-creator/src/main/java/btools/mapcreator/PosUnifier.java +++ b/brouter-map-creator/src/main/java/btools/mapcreator/PosUnifier.java @@ -32,7 +32,6 @@ public class PosUnifier extends MapCreatorBase { private int lastSrtmLatIdx; private SrtmRaster lastSrtmRaster; private String srtmdir; - private HgtReader hgtReader; private CompactLongSet borderNids; @@ -46,15 +45,14 @@ public class PosUnifier extends MapCreatorBase { double lat = Double.parseDouble(args[2]); NodeData n = new NodeData(1, lon, lat); - SrtmRaster srtm = posu.srtmForNode(n.ilon, n.ilat); + SrtmRaster srtm = posu.hgtForNode(n.ilon, n.ilat); short selev = Short.MIN_VALUE; if (srtm == null) { - selev = posu.hgtForNode(n.ilon, n.ilat); - } else { - selev = srtm.getElevation(n.ilon, n.ilat); + srtm = posu.srtmForNode(n.ilon, n.ilat); } + if (srtm != null) selev = srtm.getElevation(n.ilon, n.ilat); posu.resetSrtm(); - System.out.println("-----> selv for " + lat + ", "+ lon + " = " + selev + " = " + (selev/4.)); + System.out.println("-----> selv for " + lat + ", " + lon + " = " + selev + " = " + (selev / 4.)); return; } else if (args.length != 5) { System.out.println("usage: java PosUnifier "); @@ -98,14 +96,13 @@ public class PosUnifier extends MapCreatorBase { @Override public void nextNode(NodeData n) throws Exception { - SrtmRaster srtm = srtmForNode(n.ilon, n.ilat); n.selev = Short.MIN_VALUE; + SrtmRaster srtm = hgtForNode(n.ilon, n.ilat); if (srtm == null) { - n.selev = hgtForNode(n.ilon, n.ilat); - } else { - n.selev = srtm.getElevation(n.ilon, n.ilat); + srtm = srtmForNode(n.ilon, n.ilat); } + if (srtm != null) n.selev = srtm.getElevation(n.ilon, n.ilat); findUniquePos(n); n.writeTo(nodesOutStream); @@ -182,7 +179,6 @@ public class PosUnifier extends MapCreatorBase { lastSrtmRaster = srtmmap.get(filename); if (lastSrtmRaster == null && !srtmmap.containsKey(filename)) { File f = new File(new File(srtmdir), filename + ".bef"); - //System.out.println("checking: " + f + " ilon=" + ilon + " ilat=" + ilat); if (f.exists()) { System.out.println("*** reading: " + f); try { @@ -201,6 +197,8 @@ public class PosUnifier extends MapCreatorBase { if (f.exists()) { try { lastSrtmRaster = new SrtmData(f).getRaster(); + srtmmap.put(filename, lastSrtmRaster); + return lastSrtmRaster; } catch (Exception e) { System.out.println("**** ERROR reading " + f + " ****"); } @@ -210,21 +208,45 @@ public class PosUnifier extends MapCreatorBase { return lastSrtmRaster; } - private short hgtForNode(int ilon, int ilat) throws Exception { - double lon = (ilon - 180000000)/1000000.; - double lat = (ilat - 90000000)/1000000.; + private SrtmRaster hgtForNode(int ilon, int ilat) throws Exception { + double lon = (ilon - 180000000) / 1000000.; + double lat = (ilat - 90000000) / 1000000.; - try { - if (hgtReader == null) hgtReader = new HgtReader(srtmdir); - double res = hgtReader.getElevationFromHgt(lat, lon); - // System.out.println("**** reading " + res + " ****"); - if (Double.isNaN(res)) return Short.MIN_VALUE; - return (short) (res*4); - } catch (Exception e) { - System.out.println("**** ERROR reading hgt " + lon + " " + lat + " ****"); + String filename = buildHgtFilename(lat, lon); + lastSrtmRaster = srtmmap.get(filename); + if (lastSrtmRaster == null) { + File f = new File(new File(srtmdir), filename + ".hgt"); + if (f.exists()) { + lastSrtmRaster = new ConvertLidarTile().getRasterDirect(f, lon, lat); + srtmmap.put(filename, lastSrtmRaster); + return lastSrtmRaster; + } + f = new File(new File(srtmdir), filename + ".zip"); + if (f.exists()) { + lastSrtmRaster = new ConvertLidarTile().getRasterZip(f, lon, lat); + srtmmap.put(filename, lastSrtmRaster); + return lastSrtmRaster; + } + } + return lastSrtmRaster; + } + + private String buildHgtFilename(double llat, double llon) { + int lat = (int) llat; + int lon = (int) llon; + + String latPref = "N"; + if (lat < 0) { + latPref = "S"; + lat = -lat + 1; + } + String lonPref = "E"; + if (lon < 0) { + lonPref = "W"; + lon = -lon + 1; } - return Short.MIN_VALUE; + return String.format("%s%02d%s%03d", latPref, lat, lonPref, lon); } private void resetSrtm() { @@ -232,7 +254,6 @@ public class PosUnifier extends MapCreatorBase { lastSrtmLonIdx = -1; lastSrtmLatIdx = -1; lastSrtmRaster = null; - if (hgtReader != null) hgtReader.clear(); } } From c20b2ba686dd1b85bcd319c9c3640671ff05c8d6 Mon Sep 17 00:00:00 2001 From: afischerdev Date: Wed, 10 May 2023 12:56:21 +0200 Subject: [PATCH 3/3] update to one routine --- .../btools/mapcreator/ConvertLidarTile.java | 149 +++++++----------- .../java/btools/mapcreator/PosUnifier.java | 9 +- 2 files changed, 61 insertions(+), 97 deletions(-) diff --git a/brouter-map-creator/src/main/java/btools/mapcreator/ConvertLidarTile.java b/brouter-map-creator/src/main/java/btools/mapcreator/ConvertLidarTile.java index d06de33..65307ce 100644 --- a/brouter-map-creator/src/main/java/btools/mapcreator/ConvertLidarTile.java +++ b/brouter-map-creator/src/main/java/btools/mapcreator/ConvertLidarTile.java @@ -29,6 +29,7 @@ public class ConvertLidarTile { try { for (; ; ) { ZipEntry ze = zis.getNextEntry(); + if (ze == null) break; if (ze.getName().endsWith(".hgt")) { readHgtFromStream(zis, rowOffset, colOffset, 1200); 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) { if (row >= 0 && row < NROWS && col >= 0 && col < NCOLS) { imagePixels[row * NCOLS + col] = val; @@ -217,78 +190,67 @@ public class ConvertLidarTile { doConvert(args[1], ilon_base, ilat_base, filename30); } - public SrtmRaster getRasterDirect(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); + public SrtmRaster getRaster(File f, double lon, double lat) throws Exception { - // 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 { - - ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(f))); - try { - for (; ; ) { - ZipEntry ze = zis.getNextEntry(); - if (ze.getName().toLowerCase().endsWith(".hgt")) { - if (ze.getSize() > ((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; + if (f.getName().toLowerCase().endsWith(".zip")) { + ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(f))); + try { + for (; ; ) { + ZipEntry ze = zis.getNextEntry(); + if (ze == null) break; + if (ze.getName().toLowerCase().endsWith(".hgt")) { + 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 + 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; + } + } + readHgtFromStream(zis, 0, 0, ROW_LENGTH); + break; } - readHgtFromStream(zis, 0, 0, ROW_LENGTH); - break; + } + } finally { + 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; } } - } finally { - zis.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(); @@ -304,4 +266,5 @@ public class ConvertLidarTile { return raster; } + } diff --git a/brouter-map-creator/src/main/java/btools/mapcreator/PosUnifier.java b/brouter-map-creator/src/main/java/btools/mapcreator/PosUnifier.java index 22df5e6..6fade10 100644 --- a/brouter-map-creator/src/main/java/btools/mapcreator/PosUnifier.java +++ b/brouter-map-creator/src/main/java/btools/mapcreator/PosUnifier.java @@ -7,6 +7,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.HashMap; +import java.util.Locale; import java.util.Map; import btools.util.CompactLongSet; @@ -217,13 +218,13 @@ public class PosUnifier extends MapCreatorBase { if (lastSrtmRaster == null) { File f = new File(new File(srtmdir), filename + ".hgt"); if (f.exists()) { - lastSrtmRaster = new ConvertLidarTile().getRasterDirect(f, lon, lat); + lastSrtmRaster = new ConvertLidarTile().getRaster(f, lon, lat); srtmmap.put(filename, lastSrtmRaster); return lastSrtmRaster; } f = new File(new File(srtmdir), filename + ".zip"); if (f.exists()) { - lastSrtmRaster = new ConvertLidarTile().getRasterZip(f, lon, lat); + lastSrtmRaster = new ConvertLidarTile().getRaster(f, lon, lat); srtmmap.put(filename, lastSrtmRaster); return lastSrtmRaster; } @@ -246,11 +247,11 @@ public class PosUnifier extends MapCreatorBase { 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() { - srtmmap = new HashMap(); + srtmmap = new HashMap<>(); lastSrtmLonIdx = -1; lastSrtmLatIdx = -1; lastSrtmRaster = null;