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

Even Rouault even.rouault at mines-paris.org
Mon May 9 14:43:35 EDT 2011


Le lundi 09 mai 2011 20:29:26, William Kang a écrit :
> Hi Even,
> I have tried the method you suggested and it worked! I have similar
> result as Ivan tested. For the record, here are two solutions for
> handling this problem:
> 
> *************************************
> 1. Using CreateCopy:
> SpatialReference dstRef = new SpatialReference("");
> dstRef.ImportFromEPSG(26919);
> 
> Dataset in_ds = gdal.Open(inPath, gdalconst.GA_ReadOnly);
> Dataset vrt_ds = gdal.AutoCreateWarpedVRT(in_ds,
> in_ds.GetProjection(), dstRef.ExportToWkt());
> Dataset out_ds = in_ds.GetDriver().CreateCopy(outPath, vrt_ds);
> 
> clean up..
> 
> 2. Using ReprojectImage
> SpatialReference dstRef = new SpatialReference("");
> dstRef.ImportFromEPSG(26919);
> 
> Dataset in_ds = gdal.Open(inPath, gdalconst.GA_ReadOnly);
> Dataset vrt_ds = gdal.AutoCreateWarpedVRT(in_ds,
> in_ds.GetProjection(), dstRef.ExportToWkt());
> Dataset out_ds = out_ds = in_ds.GetDriver().Create(outPath,
> vrt_ds.getRasterXSize(), vrt_ds.getRasterYSize(),
> 					in_ds.getRasterCount());
> 
> out_ds.SetProjection(vrt_ds.GetProjection());
> out_ds.SetGeoTransform(vrt_ds.GetGeoTransform());
> 
> if (gdal.ReprojectImage(in_ds, out_ds) == gdalconst.CE_Failure)
> 		System.out.println("Something in reprojection is wrong");
> 
> clean up..
> ***********************
> 
> The speed of the two above solutions are very similar in both cases
> for a 5000 x 5000 image. However, the tif image I created by using
> creatcopy method is 40k larger than the reprojectimage method.
> 
> One problem is that it is way slower than the gdalwarp.exe in windwos
> 7. Any insights about why the java binding version is so slow? This
> makes me think that I probably should run a command line gdalwarp.exe
> inside java by using java runtime. Any suggestion?

There's no reason why it would be fundamentally slower. But one possibility is 
that the default value of maxError when not explicitely provided is 0, instead 
of 0.125 for gdalwarp utility. Specifying a non null value causes the use of 
an approximate transformer that is much faster than the exact one while 
providing sufficient quality if the error value is small. So I'd suggest you to 
specify 0.125 as the value for the maxError parameter.

> 
> Thanks a lot.
> 
> 
> William
> 
> 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