orbisgis / cts

Projection library in Java
GNU Lesser General Public License v3.0
50 stars 16 forks source link

Infinite recursion occurs when attempting to project from NAD83 projection to WGS84 projection #98

Closed dhauverNS closed 5 years ago

dhauverNS commented 5 years ago

If a GeodeticDatum doesn't have a default transformation to WGS84 defined, a call to its getGeocentricTransformations method will infinitely recurse when GeodeticDatum.WGS84 is passed in as a parameter. For example: GeodeticDatum.NAD83.getGeocentricTransformations(GeodeticDatum.WGS84);

The recursion happens on GeodeticDatum line 453: if (!getGeocentricTransformations(GeodeticDatum.WGS84).isEmpty() &&

This manifests itself when CoordinateOperationFactory#createCoordinateOperations is called to try to obtain the coordinate operations for transforming coordinates from a projection using the NAD83 or NAD87 datums to one which is using the WGS84 datum.

I discovered this when trying to project from an Albers Equal Area projection to the EPSG:3857 web mercator projection. I used the following parameters, obtained from Esri's proj4 projections available at https://github.com/Esri/projection-engine-db-doc/blob/master/proj4/esri

# area: (lat: 24.41, 49.38) - (lon: -124.79, -66.91) [USA - CONUS - onshore]
# USA_Contiguous_Albers_Equal_Area_Conic_USGS_version [NAD 1983 USGS Contiguous USA Albers]
<102039> +proj=aea +datum=NAD83 +x_0=0.0 +y_0=0.0 +lon_0=96dW +lat_0=23dN +lat_1=29d30'N +lat_2=45d30'N +towgs84=-0.9956000824677655,1.901299877314078,0.5215002840524426,0.02591500053005733,0.009425998542707753,0.01159900118427752,-0.00062000005129903 +no_defs  no_defs <>
ebocher commented 5 years ago

Thanks feel free to submit a patch

dhauverNS commented 5 years ago

Unfortunately, I don't currently have capacity to work on developing a fix. Due to this bug, I ended up using the https://github.com/locationtech/proj4j library instead. However, I thought that the maintainers of this library and other potential users might at least want to be aware of this bug's existence. Sorry I can't provide further help at this time.

mukoki commented 5 years ago

Thank you for the precise report, The recursion problem should be solved by this PR. Anyway, the situation with NAD83 is a bit complex. Usually, in CTS, the transformation from a datum to another is defined in the source datum. For large areas, when transformation parameters are not unique, CTS can use a grid based transformation, but currently, it only supports NTv2 format, not NADCON. For this reason, toWGS84 parameter of NAD83 has been left to null in the library (NAD83 is supposed to be the old original NAD83). That was leading to the infinite recursion problem. There was another problem : as the CRS was initialized with nad83 it had no toWGS84 parameter associated (parameters from the ESRI definition for this CRS was ignored. Now, if toWGS84 is part of the CRS definition (as it is in the ESRI file ), it can override the null parameter associated to nad83 in the core library.

ebocher commented 5 years ago

Fixed thanks a lot to @mukoki