| 
45 | 45 | import java.awt.image.*;  | 
46 | 46 | import java.io.*;  | 
47 | 47 | import java.lang.reflect.Field;  | 
 | 48 | +import java.lang.reflect.InvocationTargetException;  | 
48 | 49 | import java.lang.reflect.Method;  | 
49 | 50 | import java.nio.*;  | 
50 | 51 | import java.util.*;  | 
@@ -88,6 +89,25 @@ public class GDALUtils  | 
88 | 89 | 
 
  | 
89 | 90 |     public enum LatLonOrder { latLonCRSauthority, longitudeLatitude };  | 
90 | 91 |     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 | +    }  | 
91 | 111 | 
 
  | 
92 | 112 |     static  | 
93 | 113 |     {  | 
@@ -152,36 +172,35 @@ public enum LatLonOrder { latLonCRSauthority, longitudeLatitude };  | 
152 | 172 |             String msg = Logging.getMessage("generic.LibraryLoadedOK", gdal.VersionInfo("--version"));  | 
153 | 173 |             Logging.logger().info(msg);  | 
154 | 174 | 
 
  | 
 | 175 | +            initGDALversion();  | 
 | 176 | + | 
155 | 177 |             // For GDAL 3.x, the PROJ6 library is used, which requires the location of the 'proj.db' file.  | 
156 | 178 |             // References:  | 
157 | 179 |             //      https://github.com/OSGeo/gdal/issues/1191  | 
158 | 180 |             //      https://github.com/OSGeo/gdal/pull/1658/  | 
159 | 181 |             //  | 
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'");            			  | 
185 | 204 |         	}  | 
186 | 205 | 
 
  | 
187 | 206 |         	listAllRegisteredDrivers();  | 
@@ -216,9 +235,12 @@ public static void setGDAL3axis(SpatialReference srs)  | 
216 | 235 |         	Field v = org.gdal.osr.osrConstants.class.getField("OAMS_TRADITIONAL_GIS_ORDER");  | 
217 | 236 |         	setAxis.invoke(srs, v.getInt(v));  | 
218 | 237 |         	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 | +		}  | 
222 | 244 |     }  | 
223 | 245 | 
 
  | 
224 | 246 |     public static LatLonOrder getLatLonOrder() { return latLonOrder; }  | 
 | 
0 commit comments