TransWikia.com

Working with .geo of GEE in R

Geographic Information Systems Asked by ie-con on February 22, 2021

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]}',
'{"type":"Point","coordinates":[2.663280238593351,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

One Answer

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
                                                                   .geo
1 {"type":"Point","coordinates":[2.6631005755365273,50.90550594166401]}
2 {"type":"Point","coordinates":[2.6631904070649393,50.90550594166401]}
3  {"type":"Point","coordinates":[2.663280238593351,50.90550594166401]}
                   geometry
1 POINT (2.663101 50.90551)
2  POINT (2.66319 50.90551)
3  POINT (2.66328 50.90551)

Correct answer by Spacedman on February 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP