sfheaders icon indicating copy to clipboard operation
sfheaders copied to clipboard

Add crs argument?

Open Robinlovelace opened this issue 6 years ago • 1 comments

General gist of what I'm after, and would make my life easier, shown below:

library(sfheaders)
x <- matrix( c(1:4), ncol = 2 )
xsfc <- sfc_linestring( x )
sf::st_crs(xsfc)
#> Coordinate Reference System: NA
xsfc <- sfc_linestring( x , crs = 4326)
#> Error in sfc_linestring(x, crs = 4326): unused argument (crs = 4326)
attributes(xsfc)
#> $n_empty
#> [1] 0
#> 
#> $crs
#> Coordinate Reference System: NA
#> 
#> $class
#> [1] "sfc_LINESTRING" "sfc"           
#> 
#> $precision
#> [1] 0
#> 
#> $bbox
#> xmin ymin xmax ymax 
#>    1    3    2    4 
#> 
#> $z_range
#> zmin zmax 
#>   NA   NA 
#> 
#> $m_range
#> mmin mmax 
#>   NA   NA
attr(xsfc, which = "crs") = 4326
sf::st_crs(xsfc)
#> [1] 4326

xsfc2 <- sf::st_sfc(sf::st_linestring(x), crs = 4326)
sf::st_crs(xsfc2)
#> Coordinate Reference System:
#>   EPSG: 4326 
#>   proj4string: "+proj=longlat +datum=WGS84 +no_defs"

Created on 2020-02-08 by the reprex package (v0.3.0)

FYI I'm rewriting stplanr::od2line() in a new package and think using sfheaders, among other changes, can make it 10+ or in some cases ~100+ times faster!

Robinlovelace avatar Feb 08 '20 17:02 Robinlovelace

An issue with implementing this is when crs is numeric, sf does all this extra work behind the scenes using GDAL to get the correct proj string

So just setting the epsg to 4326 will cause sf:: functions to fail

xsfc <- sfc_linestring( x )
attr( xsfc, "crs" ) <- list(epsg = 4326)
sf::st_length( xsfc )
# Error in CPL_length(x) : Index out of bounds: [index='proj4string'].

So the user will also have to set the proj4string (or the equivalent when CRS changes

xsfc <- sfc_linestring( x )
attr( xsfc, "crs" ) <-  list(epsg = 4326, proj4string = "+proj=longlat +datum=WGS84 +no_defs")
xsfc
sf::st_length( xsfc )
# 1.414214 [m] 

SymbolixAU avatar Mar 03 '20 22:03 SymbolixAU