简体   繁体   中英

transform dataframe to sf object using an already formated geometry column

I have a dataframe (dat) like this, geom_transect is a factor so far. I would like to transform my dataframe to an sf object by specifying that geom_transect is a geometry column but without success. here is my table

rn   geom_transect
1   c(2.59301435154815, 51.0850974058788)
2   c(2.65908408179987, 51.120810848601)
3   c(3.33344625797791, 51.3620269560137)
4   c(4.36158333330571, 51.2919925240015)
5   c(4.11514955241717, 51.1308060725143)

I tried:

st_as_sf(as.numeric(as.character(dat$geom_transect)))

i got the following error message:

Error in UseMethod("st_as_sf") : 
  no applicable method for 'st_as_sf' applied to an object of class "c('double', 'numeric')"
In addition: Warning message:
In st_as_sf(as.numeric(as.character(dat$geom_transect))) :
  NAs introduced by coercion

I also tried :

st_point(is.numeric(dat$geom_transect))

Error in st_point(is.numeric(dat$geom_transect)) : 
  is.numeric(x) is not TRUE

any help is welcome, thanks in advance!

you indeed put me on the track, thanks for that! I thought there would be an easier way and I do agree that I should have store the geometry table differently but the data extraction was long and I do not want to re-run that part of the code.

this is what I finally did to retrieve the lon/lat coordinates:

dat$geom_transect <- as.character(dat$geom_transect)
dat$lon <- as.numeric(sapply(strsplit(dat$geom_transect, '[(,)]'), "[[", 2))
dat$lat <- as.numeric(sapply(strsplit(dat$geom_transect, '[(,)]'), "[[", 3))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM