huizezhang-sherry / cubble

A tidy structure for spatio-temporal vector data
https://huizezhang-sherry.github.io/cubble/
Other
55 stars 9 forks source link

CRS handling in print method after st_transform() showing warning and wrong CRS on header #32

Closed loreabad6 closed 1 month ago

loreabad6 commented 1 month ago

When doing a st_transform() on a cubble the projection transformation works as expected but a warning is displayed as if using st_crs<-. This seems to be called in the print method and also resulting in the wrong CRS name in the # spatial line in the header.

library(cubble)
library(tsibble)
library(sf)

sf_nested <- make_cubble(
  spatial = stations_sf, temporal = meteo, 
  key = id, index = date)

sf_nested |> sf::st_transform(crs = "EPSG:3857")
#> Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
#> that
#> # cubble:   key: id [3], index: date, nested form, [sf]
#> # spatial:  [16122635.6225205, -4576600.8687746, 16152057.3639371,
#> #   -4532279.35567565], WGS 84
#> # temporal: date [date], prcp [dbl], tmax [dbl], tmin [dbl]
#>   id           elev name   wmo_id  long   lat            geometry ts      
#>   <chr>       <dbl> <chr>   <dbl> <dbl> <dbl>         <POINT [°]> <list>  
#> 1 ASN00086038  78.4 essen…  95866  145. -37.7 (16130929 -4541016) <tibble>
#> 2 ASN00086077  12.1 moora…  94870  145. -38.0 (16152057 -4576601) <tibble>
#> 3 ASN00086282 113.  melbo…  94866  145. -37.7 (16122636 -4532279) <tibble>

For reference, when the original sf is transformed this does not happen.

stations_sf |> sf::st_transform(crs = "EPSG:3857")
#> Simple feature collection with 3 features and 4 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 16122640 ymin: -4576601 xmax: 16152060 ymax: -4532279
#> Projected CRS: WGS 84 / Pseudo-Mercator
#> # A tibble: 3 × 5
#>   id           elev name              wmo_id            geometry
#> * <chr>       <dbl> <chr>              <dbl>         <POINT [m]>
#> 1 ASN00086038  78.4 essendon airport   95866 (16130929 -4541016)
#> 2 ASN00086077  12.1 moorabbin airport  94870 (16152057 -4576601)
#> 3 ASN00086282 113.  melbourne airport  94866 (16122636 -4532279)
huizezhang-sherry commented 1 month ago

Thanks for picking this up, the CRS print, along with the warning, has been fixed

library(cubble)
#> Registered S3 method overwritten by 'tsibble':
#>   method               from 
#>   as_tibble.grouped_df dplyr
#> 
#> Attaching package: 'cubble'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(tsibble)
#> Warning: package 'tsibble' was built under R version 4.3.3
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:cubble':
#> 
#>     index, index_var, is_tsibble
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union
library(sf)
#> Warning: package 'sf' was built under R version 4.3.2
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE

sf_nested <- make_cubble(
  spatial = stations_sf, temporal = meteo, 
  key = id, index = date)

sf_nested |> sf::st_transform(crs = "EPSG:3857")
#> # cubble:   key: id [3], index: date, nested form, [sf]
#> # spatial:  [16122635.62, -4576600.87, 16152057.36, -4532279.36], WGS 84 /
#> #   Pseudo-Mercator
#> # temporal: date [date], prcp [dbl], tmax [dbl], tmin [dbl]
#>   id           elev name   wmo_id  long   lat            geometry ts      
#>   <chr>       <dbl> <chr>   <dbl> <dbl> <dbl>         <POINT [m]> <list>  
#> 1 ASN00086038  78.4 essen…  95866  145. -37.7 (16130929 -4541016) <tibble>
#> 2 ASN00086077  12.1 moora…  94870  145. -38.0 (16152057 -4576601) <tibble>
#> 3 ASN00086282 113.  melbo…  94866  145. -37.7 (16122636 -4532279) <tibble>

Created on 2024-07-09 with reprex v2.1.0

loreabad6 commented 1 month ago

That's great! Thanks Sherry!