Reputation: 39
I am an R user but a novice in C++. I need a C++ workflow that returns the value of a GeoTiff at a specific (x, y) coordinate. The GeoTiff contains 1 layer. The layer is a 2-dimensional array. Each cell contains a floating point number (FLT4S) that (in my case) defines the depth of the seabed in a specific location. The coordinate reference system is Universal Transverse Mercator with units in metres.
In R, it is easy to read a GeoTiff and extract the value of the GeoTiff in a specific location using the terra
package:
library(terra)
# Load GeoTiff
# * This is a map of the seabed depth off the west coast of Scotland
f <- "https://raw.githubusercontent.com/edwardlavender/patter/main/inst/extdata/dat_gebco.tif"
(r <- terra::rast(f))
#> class : SpatRaster
#> dimensions : 264, 190, 1 (nrow, ncol, nlyr)
#> resolution : 100, 100 (x, y)
#> extent : 695492.1, 714492.1, 6246657, 6273057 (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / UTM zone 29N (EPSG:32629)
#> source : dat_gebco.tif
#> name : map_value
#> min value : 0.1994156
#> max value : 201.8856354
# Extract value (depth) at (x, y)
# (Coordinates are in metres, matching GeoTiff)
terra::extract(r, cbind(707087.7, 6266045))
#> map_value
#> 1 191.285
How can I do this in C++? I know terra
uses C++ under the hood. terra::extract()
ultimately calls a SpatRaster::extract*()
C++ function*, but I can't work out what the minimal workflow for achieving the above is. Note that in my case both the coordinate pair and the GeoTiff (UTM projection) have units of metres, which may simplify the required code.
*See https://github.com/rspatial/terra/blob/master/src/extract.cpp
Upvotes: 1
Views: 122
Reputation: 47481
The main hurdle is to read the GeoTiff and be able to extract a particular cell (row/col). terra uses the GDAL library to read GeoTiff so have a look at that. Their docs have C++ examples. The math to compute the cell (or row/col) from the coordinates is basic, with some additional rules for coordinates on boundaries. Have a look at terra::cellFromXY
or terra::rowFromY
and terra::colFromX
.
Here is a lot of help that shows how to do this with the TIFF package.
Your question is very broad and shows no effort. ChatGPT c.s. is probably a better place to ask for a minimal workflow for this. Here we can answer specific questions about your code, we generally do not write your code for you.
Upvotes: 0