简体   繁体   中英

How to create a geometry column for a sf object in R

My data looks like:

example <- tibble(geometry = "POINT (-3.8258 43.4109)")

I would like to transform the example from a tibble to a sf object I have the following solution but I was wondering if there is a simpler way (because I will have polygons and lines also)

left.border <- "POINT ("
right.border <- ")"
example %>% 
  mutate(coord =str_match(geometry, paste(left.border, '(.+)', right.border, sep=''))[,2]) %>% 
  separate( coord, c("X","Y"), sep = " ") %>% 
  mutate(X = str_sub(X, 2)) %>% 
  mutate(Y = str_sub(Y,1,nchar(Y)-2)) %>%
  mutate(X= as.numeric(X),Y= as.numeric(Y) ) %>%
  sf::st_as_sf(coords = c("X","Y"))

If all your geometries are in well known text format, you can use:

example = sf::st_as_sf(example, wkt = "geometry")

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