[gdal-dev] writing a subregion with GDALRasterIO

Joaquim Luis jluis at ualg.pt
Mon May 14 06:25:46 EDT 2012


On 13-05-2012 20:28, Even Rouault wrote:
> Le mardi 08 mai 2012 15:52:11, Joaquim Luis a écrit :
>> Hi,
>>
>> I am having some troubles in writing a sub-region on an array with
>> GDALRasterIO.
>>
>> The story is this. GMT internal grid arrays have a padding of 2 rows and
>> 2 columns around the 'true data region' that is used for boundary
>> conditions but that need to be striped off upon saving on file.
>>
>> The following works well when I previously moved all real data in the
>> 'data' array to the beginning of the array. That is, when I sort of
>> removed the the padding zone.
>>
>>     hDstDS = GDALCreate( hDriver, "mem", nx, ny, n_bands, typeCLASS, NULL );
>> ....
>>      GDALRasterIO( hBand, GF_Write, 0, 0, nx, ny, data, nx, ny,
>> typeCLASS, 0, 0 );
>>
>> However, I would like to avoid that step and read directly the zone of
>> interest from 'data', which because of the padding has actually
>> (nx+4)x(ny+4) elements. So I tried
>>
>>       GDALRasterIO( hBand, GF_Write, 2, 2, nx, ny, data, nx+4, ny+4,
>> typeCLASS, 0, 0 );
> -->  This call is indeed not valid according to the semantics of GDALRasterIO()
> and the error message you get is expected.
>
> Untested, but you could rather try something like (assuming your datatype is
> Byte)
>
> GDALRasterIO( hBand, GF_Write, 0, 0, nx, ny, data + 2 * nx + 2, nx, ny,
>   typeCLASS, 0, nx + 4 );
>
> to offset the data buffer to the (2,2) pixel and providing a line stride of nx +
> 4.

Thanks Even,

The data was actually a float and we can't do the "data + 2 * nx + 2" 
shift trick inside the GDALRasterIO call because it's (void *) there but 
I applied the idea where due and it works fine.

Joaquim



More information about the gdal-dev mailing list