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

jk nz jk_nzd at hotmail.com
Mon Apr 26 20:39:38 EDT 2004


HI again,

This is the last part I'm having trouble understanding .. I'm not sure what 
is in locationFileMtrx, and how pixel offsets are calculated from this..

Sample code:

  for (i = 0; i < fileRows; i++) {
    for (j = 0; j < fileCols; j++) {
      outfile << fixed << setprecision (2) << locationFileMtrx[i][j] << 
"\t";
    }

    xPixel = static_cast<int>((padfTransform[0] -  
locationFileMtrx[i][xCoordCol])/(-1*padfTransform[1]));
    yLine = static_cast<int>(((padfTransform[3] -  
locationFileMtrx[i][yCoordCol])/(-1*padfTransform[5])));

    for (int layer = 1; layer <= poDataset->GetRasterCount(); layer++){
      poBand = poDataset->GetRasterBand( layer );

      poBand = poDataset->GetRasterBand( layer );
      poBand->RasterIO( GF_Read, xPixel, yLine, 1, 1, pafScanline, 1, 1, 
GDT_Float32, 0, 0 );
      outfile << *pafScanline << "\t";
    }
    outfile << endl;
  }



My code looks something like this (its in Pascal):

          pafScanline := CPLMalloc(SizeOf(Byte) * nX);   // nX = 
GetRasterBandXSize
          try
            for i := 0 to nY - 1 do // nY = GetRasterBandYSize
            begin
              // read in line i
              GDALRasterIO(RasterBand, GF_Read, 0, i, nX, 1, pafScanline, 
nX, 1, GDT_Byte, 0, 0);

              byteptr := @pafScanline;  // byteptr = a typed pointer to a 
byte

              for j := 0 to nX - 1 do
              begin
                inc(byteptr);          // inc(byteptr) moves byteptr to 
point to the next byte in memory
                pixel := byteptr^;  //pixel = whatever byteptr points to

                DoSomething(pixel);

              end;
            end;

GDALinfo about my test file:

Driver: AIG/Arc/Info Binary Grid
Size is 289, 356
Coordinate System is `'
Origin = (2719217.059208,6191721.084434)
Pixel Size = (100.00000000,-100.00000000)
Corner Coordinates:
Upper Left  ( 2719217.059, 6191721.084)
Lower Left  ( 2719217.059, 6156121.084)
Upper Right ( 2748117.059, 6191721.084)
Lower Right ( 2748117.059, 6156121.084)
Center      ( 2733667.059, 6173921.084)
Band 1 Block=256x4 Type=Byte, ColorInterp=Undefined
  Min=0.000 Max=8.000
  NoData Value=255

Regards

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 >>

_________________________________________________________________
Watch movie trailers online with the Xtra Broadband Channel  
http://xtra.co.nz/broadband




More information about the Gdal-dev mailing list