Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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();
           }

Does anybody know what's going on here? Thanks a lot.

share|improve this question
    
Solution explained here : lists.osgeo.org/pipermail/gdal-dev/2011-May/028700.html –  user2949 May 11 '11 at 11:37
    
@Even Thank you for the link. For a response to be a proper reply it needs to stand on its own, so it has been converted to a comment. It would be a big improvement if you could supply a summary of the solution you are linking to. –  whuber May 11 '11 at 13:37
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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