I'm trying to understand how to interpret the position of pixel data within a GDAL raster, specifically as it applies to files containing height data. I'm create Triangular Regular Network (TRN) for terrain from various GDAL sources and need to generate the terrain as precisely as possible.
I've read the follow sources several times, but I have failed to understand whether they answer my questions:
Is there a standard for the coordinates of pixels in georeferenced rasters ?
Seeking tool to generate Mesh from DTM?
Does a value in srtm dem file refer to the center or corner of a cell?
The GDAL documentation has this to say about the GeoTransform:
Xgeo = GT(0) + Xpixel*GT(1) + Yline*GT(2)
Ygeo = GT(3) + Xpixel*GT(4) + Yline*GT(5)
In case of north up images, the GT(2) and GT(4) coefficients are zero, and the GT(1) is pixel width, and GT(5) is pixel height. The (GT(0),GT(3)) position is the top left corner of the top left pixel of the raster.
Note that the pixel/line coordinates in the above are from (0.0,0.0) at the top left corner of the top left pixel to (width_in_pixels,height_in_pixels) at the bottom right corner of the bottom right pixel. The pixel/line location of the center of the top left pixel would therefore be (0.5,0.5).
I understand the last paragraph in the quote above. However, my confusion begins when trying to understand the geo-referenced location of a pixels value within the pixel itself (height in my case) and the interaction of PixelIsPoint vs PixelIsArea.
So, by way example, if we consider a very simple raster:
float RasterData[4][4] = {
{ 1.0f, 2.0f, 1.0f, 3.0f },
{ 1.5f, 1.5f, 2.0f, 2.5f },
{ 2.0f, 2.0f, 2.5f, 2.0f },
{ 1.5f, 2.0f, 2.0f, 1.5f }
};
float GeoTransform[6] = {
0.0f, // X origin
1.0f, // Xaxis.x of transform
0.0f, // Xaxis.y of transform
0.0f, // Y origin
0.0f, // Yaxis.x of transform
1.0f // Yaxis.y of transform
};
By the GDAL documentation,
- the top-left corner of
RasterData[0][0]
has a geo-referenced position of (0.0, 0,0) - the bottom-right corner of
RasterData[3][3]
has a geo-referenced position of (4.0, 4.0) - the center of
RasterData[0][0]
has a geo-referenced position of (0.5, 0.5)
This part I believe I understand well enough.
However, I do not understand how to determine the (geo-referenced) location of a pixels data within the pixel itself, nor do I understand whether PixelIsPoint / PixelIsArea affects the location of the pixel corners, or the location of a pixels data within the pixel itself.
For example, I'm not entirely certain whether this is the correct way to create a vertex from this dataset:
// loop pixel_y 0 -> height (integer)
// loop pixel_x 0 -> width (integer)
vertex.x = GeoTransform[0] + GeoTransform[1] * pixel_x + GeoTransform[2] * pixel_y;
vertex.y = GeoTransform[3] + GeoTransform[4] * pixel_x + GeoTransform[5] * pixel_y;
vertex.z = RasterData[y][x];
The method above assumes the value of RasterData[y][x]
is geo-referenced at the top-left corner point of the pixel -- this is where my doubt begins.
So my question is:
How do I determine the correct geo-referenced position of a given pixel value RasterData[y][x]
when interpreting the data under PixelIsPoint versus PixelIsArea?
IE: I want to take RasterData[y][x]
and GeoTransform
and generate (X,Y,Z) vertex positions where:
- The X coordinate is the appropriate geo-referenced position for
RasterData[y][x]
- The Y coordinate is the appropriate geo-referenced position for
RasterData[y][x]
- The Z coordinate is the height value contained in
RasterData[y][x]