r-quantities / units

Measurement units for R
https://r-quantities.github.io/units
173 stars 27 forks source link

extracting units from `projargs` #291

Closed James-Thorson-NOAA closed 2 years ago

James-Thorson-NOAA commented 2 years ago

First off, thanks for developing this wonderful and very useful package! I'm integrating it into my multivariate spatio-temporal package VAST.

VAST makes extensive use of spatial packages (mainly sp) including projections. Projections are defined via projargs and resulting coordinates often have units of m km etc. Coordinates (and associated units) are then used in calculations of area, position, etc.

Is there any interest in enabling units to parse projargs strings to enable units to infer the associated units?

edzer commented 2 years ago

Thanks! proj4strings ("projargs") are being phased out; see e.g. https://r-spatial.org/r/2020/03/17/wkt.html . You should definitely consider abandoning sp and move to sf, there CRS have methods to extract units, e.g.

> library(sf)
Linking to GEOS 3.9.1, GDAL 3.3.2, PROJ 7.2.1
> st_crs(3051)$units
[1] "m"

and measures (like length, area) return units objects:

> st_as_sfc("POLYGON((0 0,1 0,1 1,0 1,1 1))", crs = 4326) |> st_area()
12364036567 [m^2]

Spatial data was my prime motivation for doing units; I wrote the first version in parallel with the first version of sf.

James-Thorson-NOAA commented 2 years ago

I figured you might have some thoughts based on your cross-authorship :) And exciting to see how tightly integrated units and sf are designed to be. I'll start switching over. and thanks for pointing the way.