[gdal-dev] Problem with gdal1.8 Java bindings gdal.ReprojectImage,
produces no data
William Kang
weliam.cloud at gmail.com
Mon May 9 15:32:39 EDT 2011
Hi Even,
After I set it to 0.125, the speed is comparable to the gdalwarp.exe.
Thanks a lot for your suggestions.
Aside from this topic, I have some questions about the reprojection
process in gdal. My understanding is that the getGeoTransform returns
the affine coefficients needed to perform the projection change. If we
can calculate the four corner points by using the geoTransformer and
get their coordinates in the new projection coordinate system, we can
calculate the corresponding bounding box and affine transformation
coefficients.
But I have read documents and your first reply email suggested that
some sampling processes along the image edge are necessary to
calculate the proper bounding box. Why is that? Isn't the four corner
points enough to determine the new bounding box? Thanks a lot for your
help.
Cao
On Mon, May 9, 2011 at 2:43 PM, Even Rouault
<even.rouault at mines-paris.org> wrote:
> 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