I have a problem with gdal.ReprojectImage from gdal1.8 Java bindings. When I perform the reprojection, the projected image is totally black. And the origin of the projected image is wrong too. There were no errors popping up at all. The code is the following:

           Dataset in_ds = gdal.Open(inPath, gdalconst.GA_ReadOnly);
           Dataset out_ds = in_ds.GetDriver().Create(outPath,in_ds.getRasterXSize(), in_ds.getRasterYSize(), in_ds.getRasterCount());

           try {
                   SpatialReference dstRef = new SpatialReference("");


                   if (gdal.ReprojectImage(in_ds, out_ds) == gdalconst.CE_Failure)
                           System.out.println("something is wrong");
           } finally {

What is going on here?


1 Answer 1


It looks like it's the line:


You're setting the destination's projection to (presumably) a different one to the source, but giving them the same coordinates. So your image is being reprojected to somewhere off the edge of your raster.

First of all, check that your source image has a projection - if not, you'll have to give it one in code (or provide it as a parameter to the ReprojectImage() function). Next, you'll have to know the bounding area of your destination image in the projection it has been assigned. If you don't know this beforehand, you'll have to take at least the four corner points of your source image (and ideally several points along each edge), reproject them (look here for a basic tutorial), and find the bounding box.

From this and knowing the size in pixels of your destination image, you can figure out the 6 georeference parameters - don't forget that almost always the y-pixel size is negative (and if like me, you use them all the time and still can't remember the order they go in, look here). The you can assign it to the destination image with SetGeoTransform(). Then ReprojectImage() will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.