Gdal source code analysis (6) gdal development and debugging

Source: Internet
Author: User
I wrote it at the beginning: I have been too busy recently and have no time to update it. Today I am taking the time to update my new article. My previous blog is 4, this is 6, and I have a fifth article in the middle, we will update the python tool set in gdal as soon as possible. 1. Simple call

There are a lot of materials on the Internet about the use of gdal, mainly to be familiar with the gdal's organizational structure, Class and Class Functions, etc. If you are familiar with gdal, you will have to talk about it. The most commonly used is the dynamic library gdal. Of course you can also use the static library. Here is a brief introduction to using the dynamic gdal library for development.

Start Vs and create a new project. In the Properties dialog box of the project, locate [configuration properties]-[C/C ++]-[general] and add a directory to the right, enter the path of the include folder of gdal here, for example:

2. In the "properties" dialog box, choose "configuration properties"> "linker"> "general" and click "add database directory" on the right, enter the path of the Lib folder of gdal here, for example:

Third, enter gdal_ I .lib in configuration properties-connector-input and additional dependencies on the right. Click OK. Now, all the environments using gdal are built, and the rest is in yourCodeGdal is used.

Paste the following code (Code taken from gdal official guide: http://gdal.org/gdal_tutorial.html) to the CPP file in the newly created Project, save it and compile it. Under normal circumstances, a prompt is displayed, and then run, the image information is output on the console.

# Include "gdal_priv.h" # include "cpl_conv.h" // For cplmalloc () int main () {// registered file format gdalallregister (); const char * pszfile = "C: \ test. IMG "; gdaldataset * podataset; // open the image podataset = (gdaldataset *) gdalopen (pszfile, ga_readonly) in read-only mode; If (podataset = NULL) {printf (" file: % s cannot be enabled! \ N ", pszfile); Return 0;} // printf (" Driver: % S/% s \ n ", podataset-> getdriver () -> getdescription (), podataset-> getdriver ()-> getmetadataitem (gdal_dmd_longname )); // printf ("size is % DX % d \ n", podataset-> getrasterxsize (), podataset-> getrasterysize (), podataset-> getrastercount (); // output the projection information of the image if (podataset-> getprojectionref ()! = NULL) printf ("projectionis '% s' \ n", podataset-> getprojectionref (); // outputs the coordinates and resolution of the image. Double adfgeotransform [6]; if (podataset-> getgeotransform (adfgeotransform) = ce_none) {printf ("Origin = (%. 6f, %. 6f) \ n ", adfgeotransform [0], adfgeotransform [3]); printf (" pixelsize = (%. 6f, %. 6f) \ n ", adfgeotransform [1], adfgeotransform [5]);} gdalrasterband * poband; int nblockxsize, nblockysize; int bgotmin, Bgotmax; double adfminmax [2]; // read the first band poband = podataset-> getrasterband (1 ); // obtain the image block size and output poband-> getblocksize (& nblockxsize, & nblockysize); printf ("Block = % DX % dtype = % s, colorinterp = % s \ n ", nblockxsize, nblockysize, gdalgetdatatypename (poband-> getrasterdatatype (), gdalgetcolorinterpretationname (poband-> getcolorinterpretation ())); // obtain the maximum and minimum values of this band. If the acquisition fails, calculate adfminmax [0] = poband-> getminimum (& Bgotmin); adfminmax [1] = poband-> getmaximum (& bgotmax); If (! (Bgotmin & bgotmax) gdalcomputerasterminmax (gdalrasterbandh) poband, true, adfminmax); printf ("min = %. 3fd, max = %. 3f \ n ", adfminmax [0], adfminmax [1]); // output the pyramid information of the image if (poband-> getoverviewcount ()> 0) printf ("band has % d overviews. \ n ", poband-> getoverviewcount (); // output the image color table information if (poband-> getcolortable ()! = NULL) printf ("band hasa color table with % d entries. \ n ", poband-> getcolortable ()-> getcolorentrycount (); float * pafscanline; int nxsize = poband-> getxsize (); // read the first row of image data pafscanline = (float *) cplmalloc (sizeof (float) * nxsize); poband-> rasterio (gf_read, 0, 0, nxsize, 1, pafscanline, nxsize, 1, gdt_float32, 0, 0); cplfree (pafscanline); // close the file gdalclose (gdaldataseth) podataset );}

Ii. gdal Source code Debugging

In many cases, we need to look at the internal implementation of gdal. Of course, we can directly view the source code of gdal, but directly look at the source code, which cannot be well understood. At this time, we need to debug and view the variable content in the source code. To debug the source code of gdal, you need the debug version of gdal and the PDB debugging files when compiling gdal. Of course, you can also add the source code of gdal to your project, but this is too time-consuming and laborious.

Below is a very simple method, which can be directly debugged into the source code of gdal. First, compile the debug version of gdal and compile the generated files, including gdal18.dll, gdal_ I .exp, gdal_ I .lib, and gdal. lib, gdal18.pdb, gdal18.ilk, gdal18.exp and other files, copy the files starting with gdal18 to the directory generated by your project, and then debug your ownProgramWhen you execute the gdalopen function (or other gdal functions), press the F11 Key To Go To The gdal source code to debug the gdal code.

Iii. Example of gdal

1. Use gdal for image cropping, refer to http://blog.csdn.net/liminlu0314/article/details/6136512

2. Use gdal for image re-sampling, refer to http://blog.csdn.net/liminlu0314/article/details/6130064

3. Create a pyramid using gdal, refer to http://blog.csdn.net/liminlu0314/article/details/6127755

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.