[Gdal-dev] GDAL in delphi .. reading pixel values (Esri grids)

jk nz jk_nzd at hotmail.com
Tue Apr 27 01:56:56 EDT 2004


Hi,

Well I have to end my day without sucess .. trying to read a scanline with 
RasterIO gives a "file not found" error, while trying to read blocks with 
ReadRasterBlock gives incorrect data

I am a newbie at GIS and GDAL and even worse, C++ ;) SO any help would be 
greatly appreciated!

My code for scanline with RasterIO looks like this (I probably go the x and 
y offsets or x and y size wrong:

            for i := 0 to nY - 1 do
            begin
              // read in 1 line at position i
              GDALRasterIO(RasterBand, GF_Read, 0, i, nX, 1, pafScanline, 
nX, 1, GDT_Byte, 0, 0);

              byteptr := @pafScanline;

              for j := 0 to nX - 1 do
              begin
                cell := byteptr^;
                inc(byteptr);

                DoSomething(cell);

              end;
            end;

And my block reading code executes but returns incorrect values:

            for i := 0 to nYBlocks - 1 do // i = Y block index
              for j := 0 to nYBlocks - 1 do // j = X block index
              begin
                GDALReadBlock(RasterBand, j, i, pafScanline);

                // compute which part of the block is valid for partial 
blocks
                if(i * nBlockYSize > nY) then
                  nYValid := nY - i * nBlockYSize
                else
                  nYValid := nBlockYSize;

                if(j * nBlockXSize > nX) then
                  nXValid := nX - j * nBlockXSize
                else
                  nXValid := nBlockXSize;

                byteptr := @pafScanline;

                for k := 0 to nYValid - 1 do // k = Y cell index
                  for l := 0 to nXValid - 1 do // l = X cell index
                  begin
                    cell := byteptr^;
                    inc(byteptr);

                    DoSomething(cell);

                  end; // for l, for k
              end; // for j, for i

There is some bug here  - first value of cell returned was 70, when I know 
it should have been 255 (=no value)

Regards, and thanks

JK.



>From: Andrew Finley <afinley at gis.umn.edu>
>Reply-To: gdal-dev at remotesensing.org
>To: gdal-dev at remotesensing.org
>Subject: Re: [Gdal-dev] GDAL in delphi .. reading pixel values (Esri grids)
>Date: 26 Apr 2004 08:42:50 -0500
>
>Hi JK,
>I think there is a similar chunk of code in the list archives, but here
>it is again.  Attached are two files, getPixelValueGdal.cpp and
>parameterfile.  The parameterfile describes the image location and the
>coordinate file (i.e., point xy locations).  This is set up for ERDAS
>images, I think you will only have to change the driver for it to read
>grids.  Hope this helps.  Let me know if you have any questions.
>-Andy
>
>
>On Mon, 2004-04-26 at 00:48, jk nz wrote:
> > Has anyone tried using GDAL from the Borland Delphi language?
> >
> > I have been mostly sucessful in doing so, but having problems reading 
>pixel
> > values from an esri grid..
> >
> > Does anyone have sample code of reading pixels? C is ok .. I have seen
> > sample of reading blocks but that wasnt quite what I wanted ...
> >
> > I do not know how to extract the pixel values as I dont know how they 
>are
> > laid out in memory .. is it a 2d grid, or a 1d grid?
> >
> > eg is it laid out in memory as
> >
> > row1 [px1][px2][px3][...]
> > row2 (possible some memory gap in between rows) [px10][px11][...]
> >
> > or as row1 [px1][px2][...] (row2, no gap in memory, next pixel is at 
>address
> > pxN+ sizeof(px)) [px10][...]
> >
> > It appears I have to use pointer arithmetic to index the block, ie 
>pixelN :=
> > block[index] works fine if it is a 1d grid in memory, but its not so 
>easy if
> > its a 2d grid, as the operator block[x,y] is not valid in delphi
> >
> > So I'm trying to figure out what happens at the end of a row? is the 
>first
> > element of the next row located in the next memory address or ...?
> >
> > Any help appreciated
> >
> > Regards
> >
> > JK.
> >
> >
> > Here is my code: - everything appears to work, apart from reading 
>individual
> > pixel values.
> >
> >             for i := 0 to nYBlocks - 1 do // i = Y block index
> >               for j := 0 to nYBlocks - 1 do // j = X block index
> >               begin
> >                 GDALReadBlock(RasterBand, j, i, pafScanline);
> >
> >                 // compute which part of the block is valid for partial
> > blocks
> >                 if(i * nBlockYSize > nY) then
> >                   nYValid := nY - i * nBlockYSize
> >                 else
> >                   nYValid := nBlockYSize;
> >
> >                 if(j * nBlockXSize > nX) then
> >                   nXValid := nX - j * nBlockXSize
> >                 else
> >                   nXValid := nBlockXSize;
> >
> >                 doubleptr := @pafScanline;
> >
> >                 for k := 0 to nYValid - 1 do // k = Y cell index
> >                   for l := 0 to nXValid - 1 do // l = X cell index
> >                   begin
> >                     inc(doubleptr);
> >                     cell := doubleptr^;
> >                     // debugging stuff, did we get an ok value or not?
> >                     if cell > 0 then
> >                       ShowMessage(FloatToStr(cell));
> >                   end; // for l, for k
> >               end; // for j, for i
> >
> > _________________________________________________________________
> > Need more speed? Get Xtra JetStream  @ http://xtra.co.nz/jetstream
> >
> > _______________________________________________
> > Gdal-dev mailing list
> > Gdal-dev at remotesensing.org
> > http://remotesensing.org/mailman/listinfo/gdal-dev
>--
>Andrew Finley, Research Fellow
>Department of Forest Resources
>College of Natural Resources
>University of Minnesota
>305 Green Hall
>1530 Cleveland Avenue N.
>St. Paul, MN 55108
>
>Ph 612-624-1714 office
>www.cnr.umn.edu/FR/people/facstaff/finley/index.html
><< getPixelValueGdal.cpp >>
><< parameterfile >>

_________________________________________________________________
Stop worrying about overloading your inbox - get MSN Hotmail Extra Storage! 
http://join.msn.com/?pgmarket=en-us&page=hotmail/es2&ST=1/go/onm00200362ave/direct/01/




More information about the Gdal-dev mailing list