1

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("");
                   dstRef.ImportFromEPSG(26919);

                   out_ds.SetProjection(dstRef.ExportToWkt());
                   out_ds.SetGeoTransform(in_ds.GetGeoTransform());

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

What is going on here?

0

1 Answer 1

3

It looks like it's the line:

out_ds.SetGeoTransform(in_ds.GetGeoTransform());

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.