Skip to content

Commit 6225f20

Browse files
committed
Add compatibility for GDAL version 1.7.2 and 2.4.0.
1 parent 81731e8 commit 6225f20

File tree

2 files changed

+74
-37
lines changed

2 files changed

+74
-37
lines changed

src/gov/nasa/worldwind/data/GDALDataRaster.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
import org.gdal.gdal.*;
3939
import org.gdal.gdalconst.gdalconst;
4040
import org.gdal.osr.SpatialReference;
41-
import org.gdal.osr.osrConstants;
42-
4341
import java.awt.geom.*;
4442
import java.io.*;
4543
import java.nio.*;
@@ -277,7 +275,7 @@ protected SpatialReference readSpatialReference(Dataset ds)
277275
{
278276
proj = this.getStringValue(AVKey.SPATIAL_REFERENCE_WKT);
279277
srs = new SpatialReference(proj);
280-
srs.SetAxisMappingStrategy(osrConstants.OAMS_AUTHORITY_COMPLIANT);
278+
GDALUtils.setGDAL3axis(srs);
281279
}
282280

283281
return srs;
@@ -506,10 +504,14 @@ protected Dataset createMaskDataset(int width, int height, Sector sector)
506504
Dataset ds = drvMem.Create("roi-mask", width, height, 1, gdalconst.GDT_UInt32);
507505
Band band = ds.GetRasterBand(1);
508506

509-
// For GDAL 3.x, don't mark band as alpha.
510-
// See https://lists.osgeo.org/pipermail/gdal-dev/2019-June/050518.html
511-
//band.SetColorInterpretation(gdalconst.GCI_AlphaBand);
512-
507+
/*
508+
* Leave the band color interp undefined, otherwise handling of alpha
509+
* won't work as desired for GDAL version 3.1 and up. Doesn't cause a
510+
* problem with versions 1.7.2 and 2.4.0. Check with 'InstallImagery' app.
511+
* See https://lists.osgeo.org/pipermail/gdal-dev/2019-June/050518.html
512+
*
513+
* band.SetColorInterpretation(gdalconst.GCI_AlphaBand);
514+
*/
513515
double missingSignal = (double) GDALUtils.ALPHA_MASK;
514516
band.SetNoDataValue(missingSignal);
515517
band.Fill(missingSignal);
@@ -530,7 +532,7 @@ protected Dataset createMaskDataset(int width, int height, Sector sector)
530532
* The purpose of this method is to create the best suited dataset for the requested area. The dataset may contain
531533
* overviews, so instead of retrieving raster from the highest resolution source, we will compose a temporary
532534
* dataset from an overview, and/or we may clip only the requested area. This will accelerate reprojection (if
533-
* needed), because the reporjection will be done on much smaller dataset.
535+
* needed), because the reprojection will be done on much smaller dataset.
534536
*
535537
* @param reqWidth width of the requested area
536538
* @param reqHeight height of the requested area
@@ -1002,7 +1004,20 @@ else if (srcNumOfBands >= 3)
10021004
band.SetColorInterpretation(colorInt);
10031005
}
10041006

1005-
if (colorInt == gdalconst.GCI_AlphaBand)
1007+
// Special handling for different GDAL versions. The way ReprojectImage works
1008+
// with respect to alpha layers was broken, probably starting with version 1.8.0
1009+
// https://lists.osgeo.org/pipermail/gdal-dev/2019-May/050175.html
1010+
// It was changed again version 3.1
1011+
// https://lists.osgeo.org/pipermail/gdal-dev/2019-June/050517.html
1012+
// https://github.com/OSGeo/gdal/commit/b427cb23ff1bc2a5ba2f6634fc3bd90816bb7789
1013+
//
1014+
// When there's no alpha band in the source, set the destination dataset alpha band
1015+
// to no transparency, so that when it's used in ReprojectImage, the resulting
1016+
// image is opaque. Otherwise, leave the existing band alone.
1017+
if ((colorInt == gdalconst.GCI_AlphaBand)
1018+
&& ((GDALUtils.getGDALversion() < 18)
1019+
|| (GDALUtils.getGDALversion() >= 31)
1020+
|| (null == srcBand)))
10061021
{
10071022
band.Fill((double) GDALUtils.ALPHA_MASK);
10081023
}

src/gov/nasa/worldwind/util/gdal/GDALUtils.java

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.awt.image.*;
4646
import java.io.*;
4747
import java.lang.reflect.Field;
48+
import java.lang.reflect.InvocationTargetException;
4849
import java.lang.reflect.Method;
4950
import java.nio.*;
5051
import java.util.*;
@@ -88,6 +89,25 @@ public class GDALUtils
8889

8990
public enum LatLonOrder { latLonCRSauthority, longitudeLatitude };
9091
private static LatLonOrder latLonOrder = LatLonOrder.latLonCRSauthority;
92+
private static int GDALversion = 0; // integer to avoid floating point compare
93+
/**
94+
* return major*10 + minor, e.g., 34 is version 3.4
95+
*/
96+
public static int getGDALversion() {
97+
return GDALversion;
98+
}
99+
private static void initGDALversion()
100+
{
101+
int major, minor;
102+
String versionNum = gdal.VersionInfo("VERSION_NUM");
103+
major = Integer.parseInt(versionNum.substring(0,1));
104+
if (versionNum.length() == 4) {
105+
minor = Integer.parseInt(versionNum.substring(1,2));
106+
} else {
107+
minor = Integer.parseInt(versionNum.substring(2,3));
108+
}
109+
GDALversion = major*10 + minor;
110+
}
91111

92112
static
93113
{
@@ -152,36 +172,35 @@ public enum LatLonOrder { latLonCRSauthority, longitudeLatitude };
152172
String msg = Logging.getMessage("generic.LibraryLoadedOK", gdal.VersionInfo("--version"));
153173
Logging.logger().info(msg);
154174

175+
initGDALversion();
176+
155177
// For GDAL 3.x, the PROJ6 library is used, which requires the location of the 'proj.db' file.
156178
// References:
157179
// https://github.com/OSGeo/gdal/issues/1191
158180
// https://github.com/OSGeo/gdal/pull/1658/
159181
//
160-
String projdbPath = System.getenv("PROJ_LIB");
161-
if (projdbPath != null) {
162-
Logging.logger().info("env PROJ_LIB = " + projdbPath);
163-
} else {
164-
String versionNum = gdal.VersionInfo("VERSION_NUM");
165-
if (Integer.parseInt(versionNum.substring(0,1)) >= 3) {
166-
Method setProj = null;
167-
try {
168-
setProj = org.gdal.osr.osr.class.getMethod("SetPROJSearchPath", String.class);
169-
} catch (NoSuchMethodException e) {}
170-
171-
if (setProj != null) {
172-
// Search for proj.db
173-
for (String dir : searchDirs) {
174-
projdbPath = findGdalProjDB(dir);
175-
if (projdbPath != null) {
176-
setProj.invoke(null, projdbPath);
177-
Logging.logger().info("proj.db in " + projdbPath + " (discovered)");
178-
break;
179-
}
180-
}
181-
}
182-
}
183-
if (projdbPath == null)
184-
Logging.logger().severe("*** ERROR - GDAL requires PROJ_LIB env var to locate 'proj.db'");
182+
183+
if (GDALversion >= 30) {
184+
String projdbPath = System.getenv("PROJ_LIB");
185+
if (projdbPath != null) {
186+
Logging.logger().info("env PROJ_LIB = " + projdbPath);
187+
} else {
188+
// For GDAL 3.x, can set location programmatically
189+
try {
190+
Method setProj = org.gdal.osr.osr.class.getMethod("SetPROJSearchPath", String.class);
191+
// Search for proj.db
192+
for (String dir : searchDirs) {
193+
projdbPath = findGdalProjDB(dir);
194+
if (projdbPath != null) {
195+
setProj.invoke(null, projdbPath);
196+
Logging.logger().info("proj.db in " + projdbPath + " (discovered)");
197+
break;
198+
}
199+
}
200+
} catch (NoSuchMethodException e) {}
201+
}
202+
if (projdbPath == null)
203+
Logging.logger().severe("*** ERROR - GDAL requires PROJ_LIB env var to locate 'proj.db'");
185204
}
186205

187206
listAllRegisteredDrivers();
@@ -216,9 +235,12 @@ public static void setGDAL3axis(SpatialReference srs)
216235
Field v = org.gdal.osr.osrConstants.class.getField("OAMS_TRADITIONAL_GIS_ORDER");
217236
setAxis.invoke(srs, v.getInt(v));
218237
latLonOrder = LatLonOrder.longitudeLatitude;
219-
} catch (Exception e) {
220-
e.printStackTrace();
221-
}
238+
} catch (NoSuchMethodException e) {
239+
} catch (NoSuchFieldException e) {
240+
} catch (InvocationTargetException e) {
241+
} catch (IllegalAccessException e) {
242+
} catch (IllegalArgumentException e) {
243+
}
222244
}
223245

224246
public static LatLonOrder getLatLonOrder() { return latLonOrder; }

0 commit comments

Comments
 (0)