Skip to content

Commit b6aeb87

Browse files
committed
Update to find GDAL 3.5.3 files, use programmatic setting of proj.db location.
1 parent 78df5d5 commit b6aeb87

File tree

3 files changed

+92
-75
lines changed

3 files changed

+92
-75
lines changed

src/gov/nasa/worldwind/util/MessageStrings.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ gdal.DriverDetails=Driver {0}, {1}, {2}
885885
gdal.InternalError=GDAL error {0} : {1}
886886
gdal.LibraryLoaderReplacedOK=GDAL's library loader replaced OK
887887
gdal.MultipleDataFoldersFound=Multiple GDAL data folders found: {0}
888+
gdal.MultipleProjDbFoldersFound=Multiple GDAL proj.db folders found: {0}
888889
gdal.NativeLibraryAlreadyLoaded=Native GDAL library is already loaded
889890
gdal.NativeLibraryNotLoaded=Native GDAL library {0} not loaded: {1}
890891
gdal.GDALNotAvailable=GDAL library is not available

src/gov/nasa/worldwind/util/gdal/GDALDataFinder.java renamed to src/gov/nasa/worldwind/util/gdal/GDALFileFinder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
* @author Lado Garakanidze
3535
* @version $Id: GDALDataFinder.java 1171 2013-02-11 21:45:02Z dcollins $
3636
*/
37-
class GDALDataFinder extends GDALAbstractFileFilter
37+
class GDALFileFinder extends GDALAbstractFileFilter
3838
{
39-
public GDALDataFinder()
39+
public GDALFileFinder(String fn)
4040
{
41-
super("gdal_datum.csv");
41+
super(fn);
4242
}
4343

4444
public boolean accept(File pathname)

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

Lines changed: 88 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.awt.color.*;
4545
import java.awt.image.*;
4646
import java.io.*;
47+
import java.lang.reflect.Method;
4748
import java.nio.*;
4849
import java.util.*;
4950
import java.util.concurrent.atomic.AtomicBoolean;
@@ -86,41 +87,30 @@ public class GDALUtils
8687

8788
static
8889
{
89-
try
90-
{
91-
boolean runningAsJavaWebStart = (System.getProperty("javawebstart.version", null) != null);
90+
try {
91+
String[] searchDirs;
92+
if (Configuration.isWindowsOS())
93+
searchDirs = new String[] { getCurrentDirectory(), "C:\\Program Files\\GDAL" };
94+
else
95+
searchDirs = new String[] { getCurrentDirectory(), "/usr/share/gdal", "/usr/lib", "/usr/lib/gdal" };
9296

93-
if (!runningAsJavaWebStart)
94-
{
95-
String[] searchDirs;
96-
if (Configuration.isWindowsOS())
97-
{
98-
searchDirs = new String[] { getCurrentDirectory(), "C:\\Program Files\\GDAL" };
99-
}
100-
else
101-
{
102-
searchDirs = new String[] { getCurrentDirectory(), "/usr/share/gdal", "/usr/lib", "/usr/lib/gdal" };
103-
}
97+
boolean runningAsJavaWebStart = (System.getProperty("javawebstart.version", null) != null);
10498

99+
if (!runningAsJavaWebStart) {
105100
// If the environment variables are set, no need to set configuration options.
106101
String dataFolder = System.getenv(GDAL_DATA_PATH);
107-
if (dataFolder == null)
108-
{
109-
for (String dir : searchDirs)
110-
{
102+
if (dataFolder == null) {
103+
for (String dir : searchDirs) {
111104
dataFolder = findGdalDataFolder(dir);
112-
if (dataFolder != null)
113-
{
105+
if (dataFolder != null) {
114106
String msg = Logging.getMessage("gdal.SharedDataFolderFound", dataFolder, Logging.getMessage("gdal.FolderDiscovered"));
115107
Logging.logger().info(msg);
116108
gdal.SetConfigOption(GDAL_DATA_PATH, dataFolder);
117109
break;
118110
}
119111
}
120112
if (dataFolder == null)
121-
{
122113
Logging.logger().log(Level.WARNING, "gdal.SharedDataFolderNotFound");
123-
}
124114
} else {
125115
String msg = Logging.getMessage("gdal.SharedDataFolderFound", dataFolder, Logging.getMessage("gdal.FolderFromEnv", GDAL_DATA_PATH));
126116
Logging.logger().info(msg);
@@ -129,21 +119,17 @@ public class GDALUtils
129119
// Try for GDAL_DRIVER_PATH
130120
String drvpath = System.getenv(GDAL_DRIVER_PATH);
131121
if (drvpath == null) {
132-
for (String dir : searchDirs)
133-
{
122+
for (String dir : searchDirs) {
134123
drvpath = findGdalPlugins(dir);
135-
if (drvpath != null)
136-
{
124+
if (drvpath != null) {
137125
String msg = Logging.getMessage("gdal.PluginFolderFound", drvpath, Logging.getMessage("gdal.FolderDiscovered"));
138126
Logging.logger().info(msg);
139127
gdal.SetConfigOption(GDAL_DRIVER_PATH, drvpath);
140128
break;
141129
}
142130
}
143131
if (drvpath == null)
144-
{
145132
Logging.logger().log(Level.WARNING, "gdal.PluginFolderNotFound");
146-
}
147133
} else {
148134
String msg = Logging.getMessage("gdal.PluginFolderFound", drvpath, Logging.getMessage("gdal.FolderFromEnv", GDAL_DRIVER_PATH));
149135
Logging.logger().info(msg);
@@ -163,38 +149,52 @@ public class GDALUtils
163149
Logging.logger().info(msg);
164150

165151
// For GDAL 3.x, the PROJ6 library is used, which requires the location of the 'proj.db' file.
166-
// Future GDAL releases will allow programatic setting of the location of the 'proj.db' file.
167-
// For GDAL 3.0.0, the user must set the PROJ_LIB envirnment variable for the location.
168-
// For now, just give a warning.
169152
// References:
170153
// https://github.com/OSGeo/gdal/issues/1191
171154
// https://github.com/OSGeo/gdal/pull/1658/
172155
//
173-
String versionNum = gdal.VersionInfo("VERSION_NUM");
174-
if (Integer.parseInt(versionNum.substring(0,1)) >= 3) {
175-
if (System.getenv("PROJ_LIB") == null) {
176-
System.err.println("*** ERROR - GDAL requires PROJ_LIB env var to locate 'proj.db'");
177-
}
178-
}
179-
listAllRegisteredDrivers();
156+
String projdbPath = System.getenv("PROJ_LIB");
157+
if (projdbPath != null) {
158+
Logging.logger().info("env PROJ_LIB = " + projdbPath);
159+
} else {
160+
String versionNum = gdal.VersionInfo("VERSION_NUM");
161+
if (Integer.parseInt(versionNum.substring(0,1)) >= 3) {
162+
Method setProj = null;
163+
try {
164+
setProj = org.gdal.osr.osr.class.getMethod("SetPROJSearchPath", String.class);
165+
} catch (NoSuchMethodException e) {}
166+
167+
if (setProj != null) {
168+
// Search for proj.db
169+
for (String dir : searchDirs) {
170+
projdbPath = findGdalProjDB(dir);
171+
if (projdbPath != null) {
172+
setProj.invoke(null, projdbPath);
173+
Logging.logger().info("proj.db in " + projdbPath + " (discovered)");
174+
break;
175+
}
176+
}
177+
}
178+
}
179+
if (projdbPath == null)
180+
Logging.logger().severe("*** ERROR - GDAL requires PROJ_LIB env var to locate 'proj.db'");
181+
}
182+
183+
listAllRegisteredDrivers();
180184

181185
gdalIsAvailable.set(true);
182-
}
183-
catch (Throwable throwable)
184-
{
186+
187+
} catch (Throwable throwable) {
185188
String reason = Logging.getMessage("generic.LibraryNotFound", "GDAL");
186189
String msg = Logging.getMessage("generic.LibraryNotLoaded", "GDAL", reason);
187190
Logging.logger().warning(msg);
188191
String throwableMessage = (throwable.getMessage() != null) ? throwable.getMessage() : "";
189192
Logging.logger().log(Level.WARNING, throwableMessage, throwable);
190193
Logging.logger().info(JAVA_LIBRARY_PATH + "=" + System.getProperty(JAVA_LIBRARY_PATH));
191194
Logging.logger().info("user.dir" + "=" + getCurrentDirectory());
192-
if (Configuration.isWindowsOS())
193-
{
195+
if (Configuration.isWindowsOS()) {
194196
Logging.logger().info("PATH" + "=" + System.getenv("PATH"));
195-
}
196-
else
197-
{
197+
} else {
198198
Logging.logger().info("LD_LIBRARY_PATH" + "=" + System.getenv("LD_LIBRARY_PATH"));
199199
}
200200
}
@@ -204,8 +204,7 @@ protected static String getCurrentDirectory()
204204
{
205205
String cwd = System.getProperty("user.dir");
206206

207-
if (cwd == null || cwd.length() == 0)
208-
{
207+
if (cwd == null || cwd.length() == 0) {
209208
String message = Logging.getMessage("generic.UsersHomeDirectoryNotKnown");
210209
Logging.logger().severe(message);
211210
throw new WWRuntimeException(message);
@@ -229,52 +228,69 @@ public boolean accept(File pathname)
229228
File[] filenames = (new File(dir)).listFiles(filter);
230229

231230
if (filenames != null && filenames.length > 0)
232-
{
233231
return filenames[0].getAbsolutePath();
234-
}
235232
else
236-
{
237233
return null;
238-
}
239234
}
240235

241236
protected static String findGdalDataFolder(String dir)
242237
{
243-
try
244-
{
238+
try {
245239
FileTree fileTree = new FileTree(new File(dir));
246240
fileTree.setMode(FileTree.FILES_AND_DIRECTORIES);
247241

248-
GDALDataFinder filter = new GDALDataFinder();
249-
fileTree.asList(filter);
250-
ArrayList<String> folders = filter.getFolders();
251-
252-
if (!folders.isEmpty()) {
253-
if (folders.size() > 1) {
254-
String msg = Logging.getMessage("gdal.MultipleDataFoldersFound", folders.get(1));
255-
Logging.logger().warning(msg);
256-
}
257-
return folders.get(0);
258-
}
259-
}
260-
catch (Throwable t)
261-
{
242+
String[] datumNames = { "gdal_datum.csv", "gt_datum.csv" };
243+
for (String s : datumNames) {
244+
GDALFileFinder filter = new GDALFileFinder(s);
245+
fileTree.asList(filter);
246+
ArrayList<String> folders = filter.getFolders();
247+
248+
if (!folders.isEmpty()) {
249+
if (folders.size() > 1) {
250+
String msg = Logging.getMessage("gdal.MultipleDataFoldersFound", folders.get(1));
251+
Logging.logger().warning(msg);
252+
}
253+
return folders.get(0);
254+
}
255+
}
256+
} catch (Throwable t) {
262257
Logging.logger().severe(t.getMessage());
263258
}
264259
return null;
265260
}
261+
262+
protected static String findGdalProjDB(String dir)
263+
{
264+
try {
265+
FileTree fileTree = new FileTree(new File(dir));
266+
fileTree.setMode(FileTree.FILES_AND_DIRECTORIES);
267+
268+
GDALFileFinder filter = new GDALFileFinder("proj.db");
269+
fileTree.asList(filter);
270+
ArrayList<String> folders = filter.getFolders();
271+
272+
if (!folders.isEmpty()) {
273+
if (folders.size() > 1) {
274+
String msg = Logging.getMessage("gdal.MultipleProjDbFoldersFound", folders.get(1));
275+
Logging.logger().warning(msg);
276+
}
277+
return folders.get(0);
278+
}
279+
} catch (Throwable t) {
280+
Logging.logger().severe(t.getMessage());
281+
}
282+
283+
return null;
284+
}
266285

267286
protected static String buildPathString(String[] folders)
268287
{
269288
String del = File.pathSeparator;
270289
StringBuffer path = new StringBuffer();
271290

272-
if (null != folders && folders.length > 0)
273-
{
291+
if (null != folders && folders.length > 0) {
274292
for (String folder : folders)
275-
{
276293
path.append(folder).append(del);
277-
}
278294
}
279295

280296
return path.toString();

0 commit comments

Comments
 (0)