From fc9c70ad78e4425a729417d16d9627376f47e7e6 Mon Sep 17 00:00:00 2001 From: Douglas Gillespie <50671166+douggillespie@users.noreply.github.com> Date: Mon, 12 Dec 2022 17:01:06 +0000 Subject: [PATCH] Fix Gebco grid data overlay offset --- src/Map/gridbaselayer/GebcoNETCDF.java | 22 ++++++++++++--------- src/Map/gridbaselayer/GridSwingPainter.java | 10 +++++----- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Map/gridbaselayer/GebcoNETCDF.java b/src/Map/gridbaselayer/GebcoNETCDF.java index 807b0813..74cd4b26 100644 --- a/src/Map/gridbaselayer/GebcoNETCDF.java +++ b/src/Map/gridbaselayer/GebcoNETCDF.java @@ -111,15 +111,15 @@ public class GebcoNETCDF { * @param bin * @return */ - double binToValue(double[] valueRange, int nBin, int bin) { - return valueRange[0] + (valueRange[1]-valueRange[0])*(double) bin / (double) (nBin-1); + double binToValue(double[] valueRange, int nBin, double bin) { + return valueRange[0] + (valueRange[1]-valueRange[0])*(double) bin / (double) (nBin); } - double latBinToValue(int latBin) { + double latBinToValue(double latBin) { return binToValue(latRange, nLat, latBin); } - double lonBinToValue(int lonBin) { + double lonBinToValue(double lonBin) { return binToValue(lonRange, nLon, lonBin); } @@ -131,9 +131,9 @@ public class GebcoNETCDF { public MapRasterImage createImage(int[] latRangeBins, int[] lonRangeBins, ColourArray heightColours, ColourArray depthColours, int hop) { latRangeBins[0] = Math.max(latRangeBins[0], 0); - latRangeBins[1] = Math.min(latRangeBins[1], nLat-1); + latRangeBins[1] = Math.min(latRangeBins[1], nLat); lonRangeBins[0] = Math.max(lonRangeBins[0], 0); - lonRangeBins[1] = Math.min(lonRangeBins[1], nLon-1); + lonRangeBins[1] = Math.min(lonRangeBins[1], nLon); hop = Math.max(1, hop); int nLatBins = (latRangeBins[1]-latRangeBins[0])/hop; int nLonBins = (lonRangeBins[1]-lonRangeBins[0])/hop; @@ -184,9 +184,10 @@ public class GebcoNETCDF { double[] latRange = new double[2]; double[] lonRange = new double[2]; + double[] edges = {-.5, +.5}; for (int i = 0; i < 2; i++) { - latRange[i] = latBinToValue(latRangeBins[i]); - lonRange[i] = lonBinToValue(lonRangeBins[i]); + latRange[i] = latBinToValue(latRangeBins[i]+edges[i]); + lonRange[i] = lonBinToValue(lonRangeBins[i]+edges[i]); } return new MapRasterImage(latRange, lonRange, image); @@ -233,11 +234,14 @@ public class GebcoNETCDF { public static GebcoNETCDF makeGebcoNCDFFile(File ncFile) { if (ncFile.exists() == false) { System.out.println("Bathymetry file " + ncFile.getAbsolutePath() + " cannot be found"); + return null; } NetcdfFile ncf; try { ncf = new NetcdfFile(ncFile, true); - return new GebcoNETCDF(ncf); + GebcoNETCDF gebcoRaster = new GebcoNETCDF(ncf); +// ncf.close(); + return gebcoRaster; } catch (IOException e) { e.printStackTrace(); } diff --git a/src/Map/gridbaselayer/GridSwingPainter.java b/src/Map/gridbaselayer/GridSwingPainter.java index 8aa7a3ae..e3395db9 100644 --- a/src/Map/gridbaselayer/GridSwingPainter.java +++ b/src/Map/gridbaselayer/GridSwingPainter.java @@ -67,11 +67,11 @@ public class GridSwingPainter { * now go back and recalculate the latlongs of those pixel values since * we need the lat longs of those bounds to be more precise. */ - double pixsOffs = -.5; - pLatMin = lat[0] + (lat[1]-lat[0]) * (double) (nLat-pyMax-pixsOffs) / (nLat-1); - pLatMax = lat[0] + (lat[1]-lat[0]) * (double) (nLat-pyMin-pixsOffs) / (nLat-1); - pLonMin = lon[0] + (lon[1]-lon[0]) * (double) (pxMin+pixsOffs) / (nLon-1); - pLonMax = lon[0] + (lon[1]-lon[0]) * (double) (pxMax+pixsOffs) / (nLon-1); + double pixsOffs = 0;//-.5; + pLatMin = lat[0] + (lat[1]-lat[0]) * (double) (nLat-pyMax-pixsOffs) / (nLat); + pLatMax = lat[0] + (lat[1]-lat[0]) * (double) (nLat-pyMin-pixsOffs) / (nLat); + pLonMin = lon[0] + (lon[1]-lon[0]) * (double) (pxMin+pixsOffs) / (nLon); + pLonMax = lon[0] + (lon[1]-lon[0]) * (double) (pxMax+pixsOffs) / (nLon); // pxMin = Math.max(0, Math.min(pxMin, lon.length-1)); // pxMax = Math.max(0, Math.min(pxMax, lon.length-1)); // pyMin = Math.max(0, Math.min(pyMin, lat.length-1));