1

I am refactoring my code which extensively relied on raster package to now use the newer and better terra package. I was making use of the isLonLat() function that returns TRUE if the crs of a raster object is angular and FALSE if it's planar.

Is there a way to do this using terra instead?

I want to make my code future-proof and the reliance of this function on the now deprecated proj4string is not acceptable.

2
  • Have you looked as sf::st_is_longlat? It will take any CRS, say for terra or raster you could use sf::st_is_longlat(sf::st_crs(terra::crs(x))) Feb 4, 2022 at 14:57
  • 1
    Do ?terra and then "find" (Ctrl-F) on Windows and type lonlat Feb 5, 2022 at 4:25

2 Answers 2

4

Well, you have terra::is.lonlat() https://www.rdocumentation.org/packages/terra/versions/1.5-17/topics/is.lonlat

1

I believe your use case should be covered by sf::st_is_longlat() function; it is primarily aimed at vector data, but since rasters (in both {terra} and {raster} flavors) have CRS methods it works for them too..

library(sf)
library(terra)

s <- rast(system.file("ex/logo.tif", package="terra"))

st_is_longlat(s)
# [1] FALSE
4
  • This works. I don't mind adding sf in my list of dependencies since I will need the package further down in my script anyway. However, I feel terra developers should ensure this functionality is covered natively. Feb 4, 2022 at 14:47
  • @FaustinGashakamba There is no need to ensure this functionality is covered natively because sf is already a "Suggests" dependency for terra and this can easily be facilitated using sf::st_is_longlat(sf::st_crs(terra::crs(x))). In package development you want to avoid redundancy. The sf::st_crs(terra::crs(x)) call parses the terra crs string to a formatted WKT that sf then recognizes. It even looks like in the recent release of terra, the string is automatically parsed to sf crs handling so, simply st_crs(x) and is_longlat(x) Feb 4, 2022 at 14:56
  • @JeffreyEvans That's reassuring then. Anyway, I just found out I could also use sf::st_crs(spatial_object)$IsGeographic to ascertain whether the crs of an object is planar or angular. I am still a learner; so I you have to bear with me. Thanks. Feb 4, 2022 at 15:50
  • 2
    @FaustinGashakamba I have found that the "named elements" that are associated with the WKT object are very inconsistent and depend on how the CRS is defined. So, you cannot always rely on name objects such as $IsGeographic, $epsg and $proj4string always being available. For code stability I would invoke a function to return this value. Feb 4, 2022 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.