Skip to content

st_as_sfc.SpatialPoints relatively slow #2060

Closed
@bart1

Description

@bart1

It seems this function is relatively slow compared to creating a sf object from a data.frame. It takes about 6 seconds to convert 300000 points. The conversion through a data frame is considerably faster. I think this could be easily resolved by using points_cpp here:

sf/R/sp.R

Line 94 in 4f550e5

lst = lapply(seq_len(nrow(cc)), function(x) st_point(cc[x,]))
. I don't seem to be able to label this issue as an enhancement but it is very much meant like that and not a bug. If desirable I can create a pull request.

require(sf)
#> Loading required package: sf
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
require(sp)
#> Loading required package: sp


a<-SpatialPoints(cbind(runif(300000),runif(300000)), proj4string =CRS( "+proj=longlat"))
bench::mark(st_as_sfc(a),
st_as_sfc(st_as_sf(as.data.frame(a), coords = 1:2, crs=st_crs(a))))[,1:3]
#> Warning: Some expressions had a GC in every iteration; so filtering is disabled.
#> # A tibble: 2 × 3
#>   expression                                                                min
#>   <bch:expr>                                                           <bch:tm>
#> 1 st_as_sfc(a)                                                            5.61s
#> 2 st_as_sfc(st_as_sf(as.data.frame(a), coords = 1:2, crs = st_crs(a)))  173.8ms
#> # … with 1 more variable: median <bch:tm>

Created on 2022-12-14 with reprex v2.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions