Gdal rasterio reads band data

Source: Internet
Author: User

Previously, gdal was used to read data sets and band information. The most important thing to use gdal is to read the band data of the image, because the processing of the image is the processing of the data (or pixel. Here we will discuss how gdal reads band data.

Reference: http://www.gdal.org/gdal_tutorial.html

Here there is a Chinese translation version, the translation is still possible: http://opencv-extension-library.googlecode.com/svn/doc/gdal-doc/gdal_tutorial.html

Read band data

The interface for gdal to read band data is rasterio, which is an important function. Both gdaldataset and gdalrasterband have this function, when rasterio in the gdaldataset class is used, the data can be read in the specified band sequence and in a certain band sequence. The rasterio in the gdalrasterband class can read the data in this band, you can read, read, or sample all data. The raterio function of the gdalrasterband class is used. The following describes the function. The function prototype is as follows:

Cplerr gdalrasterband: rasterio (
Gdalrwflag erwflag, // read/write flag.Gf_read: reads data to the cache.Gf_write: writes cached data to the band of the dataset.
Int nxoff, // start X-direction pixel position
Int nyoff, // start pixel position in Y direction
Int nxsize, // The X-direction pixel size of the data
Int nysize, // The Y-direction pixel size of the data. Note: The preceding four Parameters specify the location and size of the data to be read.
Void * pdata, // Cache
Int nbufxsize, // The X-direction pixel size cached
Int nbufysize, // The Y-direction pixel size of the cache
Gdaldatatype ebuftype, // data type, specifying the data type in the cache
Int npixelspace, // read the byte offset of each pixel, that is, the distance between the next read pixel and the read pixel. The default value is 0.
Int nlinespace // read the byte offset of each row of pixels. The default value is 0.
)    

For parameter meanings, see comments,

When npixelspace is 0, it indicates the number of ebuftype bytes.

When nlinespace is 0, it indicates that the offset bytes are ebuftype * nbufxsize bytes.

Sampling implementation: Set the last two parameters, set npixelspace to specify the number of pixels in each row to read, set nlinespace to specify the number of rows to read, pay attention to the cache size.

Example of reading all data:

// Dataset registration and object acquisition are not described here. See the previous article.
Int sizex = podataset-> getrasterxsize ();
Int sizey = podataset-> getrasterysize ();
Unsigned char * pmemdata1;
Pmemdata = (unsigned char *) cplmalloc (sizex * sizey );
Poband = podataset-> getrasterband (1 );
Poband-> rasterio (gf_read,
0, 0,
Sizex, sizey,
Pmemdata,
Sizex, sizey,
Gdt_byte,
0, 0 );
CPLFree(pMemData);

Sometimes, each row needs to be aligned in bytes, that is, the number of bytes in each row is a multiple of 8, that is, the number of digits is a multiple of 32 (a 32-bit system memory unit is a 32-bit). In the above case, as follows:

Byteperline = (sizex * 8 = 31)/32*4 that is, the number of bits in each line of pmemdata is a multiple of 32. (why do you think so)

After the byte alignment of each line, the pmemdata line is not sizex, it should be byteperline, and the above Code should be changed.

Multipart read:

In this case, if the image is large, generally the remote sensing image, especially the data size of the high-resolution image, is large, and the direct reading of all images will increase the memory overhead. In this case, you can use the block reading method, for example, each 100 bytes is read as a data block, which can reduce the overhead. The following is an example of a block (taking every 100 rows as an example ):

Int sizex = podataset-> getrasterxsize ();
Int sizey = podataset-> getrasterysize ();
Unsigned char * pmemdata1;
// 100 rows
Pmemdata = (unsigned char *) cplmalloc (sizex * 100 );
Poband = podataset-> getrasterband (1 );
For (INT I = 0; I <sizey/100; I ++)
{
Poband-> rasterio (gf_read,
0, I * 100,
Sizex, 100,
Pmemdata,
Sizex, 100,
Gdt_byte,
0, 0 );
}
CPLFree(pMemData);

Note: The code above contains the last less than 100 rows of data that are not read. Here, we only want to explain how to block the data, and the rest of the data will not work. Actually, you must add it.

Remember to release the memory applied after reading.
The rasterio function of the gdalrasterband class can meet most of the requirements and is easy to understand. It can be used several times for a large number of bands. The rasterio of the gdaldataset class is not used at present. It only has a few more parameters and is more powerful. Use it again when necessary.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.