[gdal-dev] Problem with gdal1.8 Java bindings gdal.ReprojectImage, produces no data

William Kang weliam.cloud at gmail.com
Sun May 8 17:49:18 EDT 2011

Hi Even,
Thanks a lot for your reply. Your answer makes things much clearer for
this problem.

I thought the gdal.ReporjectImage would calculate the new geoTransform
coefficients automatically. And the answer I found in this post,
confused me more.

I will try the method you suggested and let you know the rest. Thanks a lot.


On Sun, May 8, 2011 at 4:44 AM, Even Rouault
<even.rouault at mines-paris.org> wrote:
> William,
> Using ReprojectImage() requires some involved preliminary steps. It is
> generally not appropriate to reuse the dimensions of the in_ds for the out_ds,
> because the shape of the reprojected image is generally not the same as the in
> image. But the real error is to reuse the in geotransform as the out
> geotransform. It doesn't make any sense when the in and out projections are
> not the same. Unfortunately there's no easy way of guessing the out
> geotransform.
> A simple approximation would be to compute the coordinates of the 4 corners of
> the in image, reproject them to the dstRef and compute from them the out
> geotransform.
> What you would need is the GDALSuggestedWarpOutput2() function of
> http://trac.osgeo.org/gdal/browser/trunk/gdal/alg/gdaltransformer.cpp, which
> is used by the gdalwarp utility, but it is not available from Java. It is
> generally called with pfnTransformer = GDALGenImgProjTransform and
> hTransformArg = GDALCreateGenImgProjTransformer2( hSrcDS, NULL, papszOptions )
> for your use case.
> But I'm thinking of an easier way. You could use gdal.AutoCreateWarpedVRT()
> that will create a in-memory VRT dataset using the above methods to guess the
> appropriate dimensions and geotransform. Then, you can CreateCopy() into a
> "real" dataset.
> See
> http://gdal.org/java/org/gdal/gdal/gdal.html#AutoCreateWarpedVRT(org.gdal.gdal.Dataset,
> %20java.lang.String,%20java.lang.String)
> Something like :
> Dataset vrt_ds = gdal.AutoCreateWarpedVRT(in_ds, null, dstRef.ExportToWkt());
> Dataset out_ds = gdal.GetDriverByName("GTiff").CreateCopy(outPath, vrt_ds);
> vrt_ds.delete();
> out_ds.delete();
> This will be a bit slower than ReprojectImage() but this should work.
> Best regards,
> Even
>> Hi folks,
>> I am new to this mailing list. Thanks all for your great job on GDAL/OGR.
>> I have a problem with the 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 error popping up at all. The code is as 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.
>> William
>> _______________________________________________
>> gdal-dev mailing list
>> gdal-dev at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/gdal-dev

More information about the gdal-dev mailing list