I have a data frame in R, with some coordinates in EPSG 25832 system. I have tried some different things to convert the class to a spatial point, but I can't get it to work.
| Geometri_EPSG_25832 | Var1 | Var2 |
|---|---|---|
| POINT (549611.77 6189284.79) | 1.99 | 739 |
| POINT (700990.83 6223807.37) | 12 | 129 |
I have then tried something from another post: Error "OGR: Unsupported geometry type" when using st_as_sfc from sf package in R
# Remove "POINT" and parentheses
Total_df$EPSG <- gsub("POINT|\\(|\\)", "", Total_df$Geometri_EPSG_25832)
Total_df$EPSG <- sf::st_as_sfc(Total_df$EPSG,CRS("+init=epsg:25832"))
# Convert to numeric values
Total_df$x_coord <- as.numeric(stringr::str_extract(Total_df$EPSG, "[^ ]+"))
Total_df$y_coord <- as.numeric(stringr::str_extract(Total_df$EPSG, "(?<=\\s)[^\\s]+"))
Total_df$Coor <- paste0(Total_df$x_coord,", ", Total_df$y_coord)
Total_df$geos_points <- sapply(Total_df$Coor, function(x) st_multipoint(matrix(eval(str2lang(x)), ncol = 2)), USE.NAMES = FALSE)
But then get an: Error in parse(text = x) : :1:10: unexpected ',' 1: 549611.77, ^
Your dataframe seems to have a geometry in Well-known text format (WKT).
sfis supporting that format.I just saw the answer of margusl. I think that we can do everything in the same
st_as_sfcall.Created on 2024-03-20 with reprex v2.1.0