Search code examples
javatransformationgdalcoordinate-systems

Error in using gdal java binding


I am using the java binding of gdal to project a set of coordinate points from UTM to lat/log using the CoordinateTransformation Class of gdal. I have written the code as follows:

    SpatialReference oUTM = new SpatialReference();
    SpatialReference oLatLog = new SpatialReference();

    oUTM.SetProjCS("UTM 44/ WGS84");
    oUTM.SetWellKnownGeogCS("WGS84");
    oUTM.SetUTM(44, 1);

    oLatLog = oUTM.CloneGeogCS();

    double[] arr = new double[2];
    arr[0]=10;
    arr[1]=10;

    double x = arr[0] , y = arr[1];

    CoordinateTransformation transform = new CoordinateTransformation(oUTM,oLatLog);

    transform.TransformPoint(arr);

    System.out.println("Before:"+x+" "+y+"\nAfter:"+arr[0]+" "+arr[1]);

But when I run this in my main function, I get the following error: Exception in thread "main" java.lang.UnsatisfiedLinkError:

org.gdal.osr.osrJNI.new_SpatialReference__SWIG_1()J
    at org.gdal.osr.osrJNI.new_SpatialReference__SWIG_1(Native Method)
    at org.gdal.osr.SpatialReference.<init>(SpatialReference.java:117)
    at controller.CrsConverterGDAL.main(CrsConverterGDAL.java:8)

Does anyone know how this can be fixed? Thanks,


Solution

  • I tried executing your source code and I got the error

    Native library load failed. java.lang.UnsatisfiedLinkError: no osrjni in java.library.path

    So just adding gdal.jar in your project build path is not sufficient for executing your program. There are some external native library dependencies.

    Check out this link,https://trac.osgeo.org/gdal/ticket/3598. Here it is mentioned,

    The gdaljni.dll, ogrjni.dll, gdalconstjni.dll and osrjni.dll as well as gdal17.dll and other dependant libraries must be in your path.

    And in order to generate these dll for windows OS and include them in build path, steps are mentioned here, https://trac.osgeo.org/gdal/wiki/GdalOgrInJavaBuildInstructions

    I have not tried the steps mentioned in above link, but they seem straight forward. But if you face some issues let us know, we will try and see.

    Hope this helps.