When using gdal to process geographic images, it is inevitable that you will encounter a problem with the geographical coordinates of the images, geographic Images are essentially different from common images. What if gdal is used to process information related to geographical coordinates? The following is a brief description.
1: How do I use the row and column numbers to calculate the geographical coordinates of an image? Or how to locate an image in a certain position through geographic coordinates?
2: How do I obtain the four-to-one range of images? Or if the region of the image is calculated using the specified geographic range?
To solve the preceding three problems, you must first know and understand the gdal data model. Among them, projection and six parameters are very important. These two functions can be obtained using the geotransform () function and the getprojectionref () function in the gdaldataset class. The first parameter gets the six parameters of the image (my own name is an affined parameter), and the second is the projection of the image (that is, the spatial reference system ). Next, let's talk about the first six parameters. The six parameters are actually a set of conversion coefficients of the image's row and column numbers and geographic coordinates. The following uses GT to represent the six parameters. The mathematical relationship between the row and column numbers of the image and the geographical coordinates of the image is as follows:
Xgeo = gt (0) + xpixel * gt (1) + yline * gt (2) ygeo = gt (3) + xpixel * gt (4) + yline * gt (5)
In the preceding formula, xgeo and ygeo represent the geographical coordinates of the image. xpixel represents the column number of the image, and yline represents the row number of the image. gt (I) is the preceding six parameters, there are six values in total. These six values can be roughly divided into three groups, GT (0) and gt (3) are the first group, indicating the geographical coordinates in the upper left corner of the image; gt (1) and gt (5) is the second group, indicating the horizontal and vertical resolution of the image (generally the two values are equal, the opposite symbol, the horizontal resolution is positive, the vertical resolution is negative); gt (2) and gt (4) are the third group, indicating the image rotation coefficient. For general images, both values are 0.
Why does the image's GT (0) and gt (3) represent the coordinates in the upper left corner of the image? For the image's row and column number coordinate system, the origin of the coordinates is in the upper left corner, therefore, the row and column number in the upper left corner is (0, 0). The following formula is used to bring the coordinates:
Xgeo = gt (0) ygeo = gt (3)
So gt (0) and gt (3) represent the coordinates in the upper left corner of the image.
GT (1) and gt (5) indicate the horizontal and vertical resolutions of the image. The resolution of an image is the area that each pixel can represent. It is generally a square grid, so there is no difference between two adjacent pixel coordinates. Based on this principle, two coordinates are used for verification. Assume that the coordinate of the row and column numbers of the current vertex is a (I, j), and the coordinate of the adjacent right vertex is B (I + 1, J ). Calculate the horizontal geographic coordinates of A and B respectively, and calculate the difference value, that is:
DX = xgeob-xgeoa = [gt (0) + (I + 1) * gt (1) + J * gt (2)]-[gt (0) + I * gt (1) + J * gt (2)] = gt (0)-GT (0) + (I + 1) * gt (1) -I * gt (1) + J * gt (2)-J * gt (2) = (I + 1) * gt (1)-I * gt (1) = gt (1)
Similarly, we can get DY = gt (5 ).
For a standard image (the standard image here means that the values of GT (2) and gt (4) are both 0), and 1 indicates that the coordinate of the row and column numbers of the image is xoy, each grid represents a pixel area of the image. I indicates the column number, J indicates the row number, and the column coordinate in the lower right corner of the light blue is (I, j). In the figure, the vertical length of the Red Square is dy, the horizontal length is dx, indicating the resolution of the image (GT (0), GT (3)).
Figure 1 Description of the row and column numbers and their geographic coordinates of a standard image
With the above description, we can easily convert the row and column numbers of images and geographical coordinates.CodeAs shown in the following figure, there are two, one is positive and the other is inverse.
Bool projection2imagerowcol (double * adfgeotransform, double dprojx, double dprojy, Int & icol, Int & irow) {try {double dtemp = adfgeotransform [1] * adfgeotransform [5]-adfgeotransform [2] * adfgeotransform [4]; double dcol = 0.0, Drow = 0.0; dcol = (adfgeotransform [5] * (dprojx-adfgeotransform [0])-adfgeotransform [2] * (dprojy-adfgeotransform [3])/dtemp + 0.5; drow = (adfgeotransform [1] * (dprojy-adfgeotransform [3])-adfgeotransform [4] * (dprojx-adfgeotransform [0])/dtemp + 0.5; icol = static_cast <int> (dcol); irow = static_cast <int> (Drow); Return true;} catch (...) {return false ;}} bool imagerowcol2projection (double * adfgeotransform, int icol, int irow, double & dprojx, double & dprojy) {// adfgeotransform [6] array adfgeotransform stores some parameters in the affinic transformation, for their meanings, see the X coordinate in the upper left corner of // adfgeotransform [0]. // adfgeotransform [1] /// adfgeotransform [2] rotation angle, 0 indicates the image "Facing Up north" // adfgeotransform [3] Y coordinate in the upper left corner // adfgeotransform [4] rotation angle, 0 indicates the image "Facing Up north" // adfgeotransform [5] North-South resolution try {dprojx = adfgeotransform [0] + adfgeotransform [1] * icol + adfgeotransform [2] * irow; dprojy = adfgeotransform [3] + adfgeotransform [4] * icol + adfgeotransform [5] * irow; return true;} catch (...) {return false ;}}
Now let's go back to the first two questions. The first problem is the mutual conversion between the row and column numbers and geographical coordinates of the image. The above code can be used to solve the problem. For the second problem, it can be converted to the first problem. The second problem is actually the coordinate transformation between the two points, which are the points in the upper left corner and the lower right corner. For example, the first one is how to calculate the four to the range of the image. The four to the range of the image can be seen from figure 1. The four to the four is actually the rectangular area with the coordinates in the upper left corner and the lower right corner of the image, then, convert the row and column numbers in the upper left corner and the lower right corner according to the formula above to obtain the four-to-Range. The second question is to calculate the coordinates.
Let's talk about the coordinates of Geographic Images. We need to explain the projection information above (spatial reference information, the coordinate ranges in the six parameters correspond to the spatial reference. For example, if the spatial reference is a WGS84 elliptical body, the general unit of the six parameters is degree, if it is a Beijing 54-percentile projection, the unit of the six parameters is meters.