@@ -54,6 +54,8 @@ public class TerrainElevationData {
5454 private int rasterHeight = -1 ;
5555 private boolean hasCoverageNoData = false ;
5656 private double coverageNoDataValue = Double .NaN ;
57+ private double inverseLongitudeRange = Double .NaN ;
58+ private double inverseLatitudeRange = Double .NaN ;
5759 private boolean rasterInitializationLogged = false ;
5860 private boolean useDirectRasterAccess = false ;
5961 private int rasterDataKind = 0 ;
@@ -96,6 +98,8 @@ public void deleteCoverage() {
9698 this .rasterSampleModelTranslateY = 0 ;
9799 this .hasCoverageNoData = false ;
98100 this .coverageNoDataValue = Double .NaN ;
101+ this .inverseLongitudeRange = Double .NaN ;
102+ this .inverseLatitudeRange = Double .NaN ;
99103 this .rasterInitializationLogged = false ;
100104 this .useDirectRasterAccess = false ;
101105 this .rasterDataKind = RASTER_KIND_GENERIC ;
@@ -189,8 +193,9 @@ public double getElevation(double lonDeg, double latDeg, boolean[] intersects) {
189193 }
190194 Vector2i size = gridCoverage2DSize ;
191195
192- double unitaryX = (lonDeg - this .geographicExtension .getMinLongitudeDeg ()) / this .geographicExtension .getLongitudeRangeDegree ();
193- double unitaryY = 1.0 - (latDeg - this .geographicExtension .getMinLatitudeDeg ()) / this .geographicExtension .getLatitudeRangeDegree ();
196+ ensureGeographicScaleInitialized ();
197+ double unitaryX = (lonDeg - this .geographicExtension .getMinLongitudeDeg ()) * this .inverseLongitudeRange ;
198+ double unitaryY = 1.0 - (latDeg - this .geographicExtension .getMinLatitudeDeg ()) * this .inverseLatitudeRange ;
194199
195200 int geoTiffRasterHeight = size .y ;
196201 int geoTiffRasterWidth = size .x ;
@@ -251,10 +256,10 @@ private double calcBilinearInterpolation(double x, double y, int geoTiffWidth, i
251256 rowNext = geoTiffHeight - 1 ;
252257 }
253258
254- double value00 = readGridValueDirect (column , row );
255- double value01 = readGridValueDirect (column , rowNext );
256- double value10 = readGridValueDirect (columnNext , row );
257- double value11 = readGridValueDirect (columnNext , rowNext );
259+ double value00 = readGridValueFast (column , row );
260+ double value01 = readGridValueFast (column , rowNext );
261+ double value10 = readGridValueFast (columnNext , row );
262+ double value11 = readGridValueFast (columnNext , rowNext );
258263
259264 // Ignore noDataValue samples.
260265 double noDataValue = globalOptions .getNoDataValue ();
@@ -273,6 +278,17 @@ private double calcBilinearInterpolation(double x, double y, int geoTiffWidth, i
273278 return value0 + factorX * (value1 - value0 );
274279 }
275280
281+ private void ensureGeographicScaleInitialized () {
282+ if (!Double .isNaN (this .inverseLongitudeRange ) && !Double .isNaN (this .inverseLatitudeRange )) {
283+ return ;
284+ }
285+
286+ double longitudeRange = this .geographicExtension .getLongitudeRangeDegree ();
287+ double latitudeRange = this .geographicExtension .getLatitudeRangeDegree ();
288+ this .inverseLongitudeRange = longitudeRange != 0.0 ? 1.0 / longitudeRange : 0.0 ;
289+ this .inverseLatitudeRange = latitudeRange != 0.0 ? 1.0 / latitudeRange : 0.0 ;
290+ }
291+
276292 private boolean ensureRasterInitialized () {
277293 if (this .raster != null ) {
278294 return true ;
@@ -404,6 +420,17 @@ private double readGridValueDirect(int x, int y) {
404420 return value ;
405421 }
406422
423+ private double readGridValueFast (int x , int y ) {
424+ double value = useDirectRasterAccess ? readSampleUnchecked (x , y ) : raster .getSampleDouble (x , y , 0 );
425+ if (Double .isNaN (value )) {
426+ return globalOptions .getNoDataValue ();
427+ }
428+ if (hasCoverageNoData && value == coverageNoDataValue ) {
429+ return globalOptions .getNoDataValue ();
430+ }
431+ return value ;
432+ }
433+
407434 private double readSample (int x , int y ) {
408435 if (!useDirectRasterAccess ) {
409436 return raster .getSampleDouble (x , y , 0 );
0 commit comments