Skip to content

Commit 9e6e504

Browse files
committed
Fix lat/lon order for coodinate transformations for GDAL 3.x
1 parent 802d1e9 commit 9e6e504

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import gov.nasa.worldwind.geom.*;
3333
import gov.nasa.worldwind.util.Logging;
3434
import gov.nasa.worldwind.util.gdal.GDALUtils;
35+
import gov.nasa.worldwind.util.gdal.GDALUtils.LatLonOrder;
36+
3537
import org.gdal.gdal.Dataset;
3638
import org.gdal.osr.*;
3739

@@ -111,8 +113,10 @@ public Area(SpatialReference srs, Dataset ds) throws IllegalArgumentException
111113
if (null == srs)
112114
{
113115
String wkt = ds.GetProjectionRef();
114-
if (null != wkt && wkt.length() > 0)
116+
if (null != wkt && wkt.length() > 0) {
115117
srs = new SpatialReference(wkt);
118+
GDALUtils.setGDAL3axis(srs);
119+
}
116120

117121
if (null == srs)
118122
{

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.gdal.gdal.*;
3939
import org.gdal.gdalconst.gdalconst;
4040
import org.gdal.osr.SpatialReference;
41+
import org.gdal.osr.osrConstants;
4142

4243
import java.awt.geom.*;
4344
import java.io.*;
@@ -269,12 +270,14 @@ protected SpatialReference readSpatialReference(Dataset ds)
269270
if (!WWUtil.isEmpty(proj))
270271
{
271272
srs = new SpatialReference(proj);
273+
GDALUtils.setGDAL3axis(srs);
272274
}
273275

274276
if ((null == srs || srs.IsLocal() == 1) && this.hasKey(AVKey.SPATIAL_REFERENCE_WKT))
275277
{
276278
proj = this.getStringValue(AVKey.SPATIAL_REFERENCE_WKT);
277279
srs = new SpatialReference(proj);
280+
srs.SetAxisMappingStrategy(osrConstants.OAMS_AUTHORITY_COMPLIANT);
278281
}
279282

280283
return srs;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import gov.nasa.worldwind.avlist.*;
3232
import gov.nasa.worldwind.exception.WWRuntimeException;
3333
import gov.nasa.worldwind.util.*;
34+
import gov.nasa.worldwind.util.gdal.GDALUtils;
35+
3436
import org.gdal.gdal.Dataset;
3537
import org.gdal.osr.SpatialReference;
3638

@@ -348,6 +350,7 @@ else if (zone.endsWith("S"))
348350
try
349351
{
350352
SpatialReference srs = new SpatialReference();
353+
GDALUtils.setGDAL3axis(srs);
351354
srs.ImportFromProj4(proj4.toString());
352355
destParams.setValue(AVKey.SPATIAL_REFERENCE_WKT, srs.ExportToWkt());
353356
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,16 @@ protected void initLevelSetParameters(AVList params)
281281
// Compute a sector that bounds the data rasters. Make sure the sector does not exceed the limits of
282282
// latitude and longitude.
283283
sector = this.computeBoundingSector(this.dataRasterList);
284-
if (sector != null)
284+
if (sector != null)
285+
{
285286
sector = sector.intersection(Sector.FULL_SPHERE);
287+
if (sector == null)
288+
{
289+
String msg = Logging.getMessage("generic.CannotCreateRaster", dataRasterList.get(0).toString());
290+
Logging.logger().severe(msg);
291+
return;
292+
}
293+
}
286294
params.setValue(AVKey.SECTOR, sector);
287295
}
288296

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

Lines changed: 31 additions & 0 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.Field;
4748
import java.lang.reflect.Method;
4849
import java.nio.*;
4950
import java.util.*;
@@ -84,6 +85,9 @@ public class GDALUtils
8485
protected static final String GDAL_DATA_PATH = "GDAL_DATA";
8586

8687
protected static final AtomicBoolean gdalIsAvailable = new AtomicBoolean(false);
88+
89+
public enum LatLonOrder { latLonCRSauthority, longitudeLatitude };
90+
private static LatLonOrder latLonOrder = LatLonOrder.latLonCRSauthority;
8791

8892
static
8993
{
@@ -199,6 +203,25 @@ public class GDALUtils
199203
}
200204
}
201205
}
206+
207+
// For GDAL 3.0, set the transformation axis order. Coordinate transformations
208+
// for vesion 2.x and 3.x have lat/lon reversed
209+
// See https://gdal.org/tutorials/osr_api_tut.html#crs-and-axis-order
210+
public static void setGDAL3axis(SpatialReference srs)
211+
{
212+
// Force all transformations to return longitude, latitude
213+
try {
214+
Class<?>[] a = { int.class };
215+
Method setAxis = srs.getClass().getMethod("SetAxisMappingStrategy", a);
216+
Field v = org.gdal.osr.osrConstants.class.getField("OAMS_TRADITIONAL_GIS_ORDER");
217+
setAxis.invoke(srs, v.getInt(v));
218+
latLonOrder = LatLonOrder.longitudeLatitude;
219+
} catch (Exception e) {
220+
e.printStackTrace();
221+
}
222+
}
223+
224+
public static LatLonOrder getLatLonOrder() { return latLonOrder; }
202225

203226
protected static String getCurrentDirectory()
204227
{
@@ -1164,6 +1187,7 @@ public static SpatialReference createGeographicSRS() throws WWRuntimeException
11641187
}
11651188

11661189
SpatialReference srs = new SpatialReference();
1190+
GDALUtils.setGDAL3axis(srs);
11671191
srs.ImportFromProj4("+proj=latlong +datum=WGS84 +no_defs");
11681192
return srs;
11691193
}
@@ -1468,6 +1492,7 @@ else if (dataType == gdalconst.GDT_UInt32)
14681492
{
14691493
params.setValue(AVKey.SPATIAL_REFERENCE_WKT, proj_wkt);
14701494
srs = new SpatialReference(proj_wkt);
1495+
GDALUtils.setGDAL3axis(srs);
14711496
}
14721497

14731498
double[] gt = new double[6];
@@ -1504,6 +1529,12 @@ else if (Angle.isValidLongitude(minX) && Angle.isValidLatitude(maxY)
15041529
if (null == srs)
15051530
{
15061531
srs = createGeographicSRS();
1532+
// For GDAL 3.0, set the transformation axis order
1533+
try {
1534+
Method setAxis = srs.getClass().getMethod("SetAxisMappingStrategy", Integer.class);
1535+
Field v = org.gdal.osr.osrConstants.class.getField("OAMS_TRADITIONAL_GIS_ORDER");
1536+
setAxis.invoke(srs, v);
1537+
} catch (Exception e) {}
15071538
}
15081539
else if (srs.IsGeographic() == 0)
15091540
{

0 commit comments

Comments
 (0)