walkerke / crsuggest

Get appropriate CRS suggestions for your spatial data in R
Other
128 stars 7 forks source link

terra support #7

Closed Nowosad closed 2 years ago

Nowosad commented 2 years ago

Hi @walkerke do you have any plans to add terra support for this package? It already accepts raster's objects and it would be great to have the terra support as well.

walkerke commented 2 years ago

yep happy to take a look at this!

Nowosad commented 2 years ago

Thanks for working on this @walkerke. I tried to test it, but got an error:

devtools::install_github("walkerke/crsuggest")
#> Skipping install of 'crsuggest' from a github remote, the SHA1 (76432244) has not changed since last install.
#>   Use `force = TRUE` to force installation
library(terra)
library(sf)
library(crsuggest)
#> Using the EPSG Dataset v10.019, a product of the International Association of Oil & Gas Producers. 
#> Please view the terms of use at https://epsg.org/terms-of-use.html.

nz_elev = rast(system.file("raster/nz_elev.tif", package = "spDataLarge"))
crs(nz_elev) = NA

guess_crs(nz_elev, target_location = c(174, -40))
#> Error in UseMethod("st_union"): no applicable method for 'st_union' applied to an object of class "SpatRaster"

Created on 2022-06-11 by the reprex package (v2.0.1)

walkerke commented 2 years ago

I haven't added support for anything other than sf in guess_crs() yet; I'll take a look at this!

walkerke commented 2 years ago

@Nowosad This now tentatively works:

library(terra)
#> terra 1.5.34
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 7.0.0; sf_use_s2() is TRUE
library(crsuggest)
#> Using the EPSG Dataset v10.019, a product of the International Association of Oil & Gas Producers. 
#> Please view the terms of use at https://epsg.org/terms-of-use.html.

nz_elev = rast(system.file("raster/nz_elev.tif", package = "spDataLarge"))
crs(nz_elev) = NA

guess_crs(nz_elev, target_location = c(174, -40))
#> Evaluating CRS options...
#> The 'best guess' for the CRS of your data is EPSG code 27259.
#> Use `sf::st_crs(your_data) <- 27259` to use this CRS for your data.
#> View the returned dataset for other possible options.
#> # A tibble: 10 × 2
#>    crs_code dist_km
#>    <chr>      <dbl>
#>  1 27259       803.
#>  2 32759       803.
#>  3 2134        803.
#>  4 32359       803.
#>  5 32559       803.
#>  6 32760      1313.
#>  7 2135       1313.
#>  8 27260      1313.
#>  9 32360      1313.
#> 10 32560      1313.

Created on 2022-06-12 by the reprex package (v2.0.1)

view_crs(27259)

image

The problem still is that the message that prints out is not sensitive to the input object, it assumes that it is of class sf. So I'll need to detect the input object class and print out appropriate instructions for assigning a CRS. Along with this I'll deprecate the argument input_sf and change to simply input.

walkerke commented 2 years ago

This now works and gives appropriate instructions:

library(terra)
library(sf)
library(crsuggest)

nz_elev = rast(system.file("raster/nz_elev.tif", package = "spDataLarge"))
crs(nz_elev) = NA

guess_crs(nz_elev, target_location = c(174, -40))
Evaluating CRS options...
The 'best guess' for the CRS of your data is EPSG code 27259.
Use `terra::crs(your_data) <- 'EPSG:27259'` to use this CRS for your data.
View the returned dataset for other possible options.

I'll be sending off to CRAN shortly!

Nowosad commented 2 years ago

Great! Thank you a lot -- very useful.