I am attempting to read extents in R from a number of raster digital elevation models downloaded from here: http://www.tngis.org/elevation.htm
In doing so, I'm getting incorrect extents for a third (105) of the files in which the extents exactly match those of other files (alamo.dem=waterville.dem, armorel.dem=shooks_gap.dem etc.) :
library(raster) extent(raster("alamo.dem"))==extent(raster("waterville.dem"))  TRUE
In the above case, the extent of alamo.dem is correct, but the extent of waterville.dem is incorrect and should be more like (not exactly) (850440,861752,3963165,3977490). I know this because the waterville.dem correctly displays in QGIS even though its metadata extent is still wrong:
This must mean there is something in the file that correctly aligns the raster in QGIS other than the metadata and that R is not using this correct alignment information.
I'm wondering how to access this correct extent information to overwrite the incorrect extent information.
I've tried reading the binary data in R as well as opening the .dem in a word processor but I can't find anything that looks like the correct extent.
UPDATE: as mdsumner suggested below,I tried opening waterville.dem in QGIS in its own map and got the expected, both the map extent AND the metadata extent were wrong.
When the map CRS is changed to UTM Zone 16N (EPSG 26916 or 26716), the map extent is correct for waterville. Thus, it seems that although the given crs for waterville is UTM zone 17N, its information is stored as if its in UTM zone 16N. Whoever created the dem incorrectly attributed it to UTM 17N when it should have been UTM 16N.
Although the stars package works as shown by mdsummer, the same can be done in raster:
library(raster) r <- raster("waterville.dem") r2 <- projectRaster(r,crs="+proj=utm +zone=16 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs") extent(r2) class : Extent xmin : 849536.3 xmax : 862083.7 ymin : 3962859 ymax : 3977785
This is the correct extent. Thus, the way to avoid this is to systematically check if each dem is in the correct CRS, and correct accordingly if not.