I am doing something similar to a previous post on stack exchange regarding exporting data from GEE to csv. Once I have the .geo column, how do I work with it in R? I would like to create a shapefile as the final output.

Considering an exported csv as per the previous link called "test" where I have deleted the system:index column that is automatically generated.

test<-{data.frame(B4=c(0.2043, 0.2043, 0.2080), B5=c(0.2573, 0.2573, 0.2653), .geo=c('{"type":"Point","coordinates":[2.6631005755365273,50.90550594166401]}','{"type":"Point","coordinates":[2.6631904070649393,50.90550594166401]}',

df = st_as_sf(test, wkt = ".geo", crs = 4326)

I get this error Error in CPL_sfc_from_wkt(x) : OGR error

I also tried this to modify the geometry structure

geonew= st_as_sfc(structure(as.character(test$.geo), class ="WKB"),EWKB=TRUE)

I got this error. Error in CPL_hex_to_raw(vapply(x, skip0x, USE.NAMES = FALSE, "")) : char2int: unrecognized character in hex string


These aren't WKT strings, they are GeoJSON strings.

I can't see a native converter in sf package, but you can do it with the geojsonsf package by adding a geom column and calling st_as_sf. You are left with both an sf geometry column and the source geoJSON column:

> library(geojsonsf)
> st_as_sf(data.frame(test, geom=geojson_sf(test$.geo)))
Simple feature collection with 3 features and 3 fields
geometry type:  POINT
dimension:      XY
bbox:           xmin: 2.663101 ymin: 50.90551 xmax: 2.66328 ymax: 50.90551
z_range:        zmin: NA zmax: NA
m_range:        mmin: NA mmax: NA
CRS:            EPSG:4326
      B4     B5
1 0.2043 0.2573
2 0.2043 0.2573
3 0.2080 0.2653
1 {"type":"Point","coordinates":[2.6631005755365273,50.90550594166401]}
2 {"type":"Point","coordinates":[2.6631904070649393,50.90550594166401]}
3  {"type":"Point","coordinates":[2.663280238593351,50.90550594166401]}
1 POINT (2.663101 50.90551)
2  POINT (2.66319 50.90551)
3  POINT (2.66328 50.90551)
  • Thank you @Spacedman ! it works perfectly – ie-con Dec 15 '20 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.