Sf: helper function to convert from sfc to sf?

Created on 2 Mar 2017  路  3Comments  路  Source: r-spatial/sf

Hi,

Below is the code I use when I need to create sf class object for polygons (and other types)

d <- data.frame(matrix(, nrow=length(mydata), ncol=0)) 
st_geometry(d) <- mydata
my_sf <- st_as_sf(d)

I'm not sure if what I'm doing is the best approach, is there any helper function I'm not utilizing so that I can just pipe the sfc object and get a sf object immediately and add properties later on.

Suggestions are welcomed.

Most helpful comment

Why not use

st_sf(mydata)

?

All 3 comments

Why not use

st_sf(mydata)

?

omg it's that simple

I guess I'm having this question because I didn't see such conversion example in package vignette.
The closest example is

st_sf(a = 15:16, geometry = geom)

I'm closing this 'non-issue'

The above doesn't actually complete the job. If, like me, you are still having issues, try this:

library(sf)
library(tidyverse)
sf_object = sfc_object %>%
st_sf %>%
st_cast

This is particularly useful when performing unions and intersection before outputting in leaflet.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

angela-li picture angela-li  路  4Comments

gregmacfarlane picture gregmacfarlane  路  4Comments

duleise picture duleise  路  3Comments

adrfantini picture adrfantini  路  4Comments

tiernanmartin picture tiernanmartin  路  3Comments