Search code examples
rpostgresqlcsvpostgisr-sf

Import CSV with postgres geometry column as SF


I have a csv file with a postgis geometry column. I want to import the csv in to R and export the file in to a postgres database. I'm trying to make the geometry column in to the proper postgis geometry data type. However, I keep getting this warning message:

"Warning message: "no simple feature geometries present: returning a data.frame or tbl_df""

I've used:

file <- st_read("name.csv", stringsAsFactors=F, geometry_column=geom) and

file <- fread("name.csv", headers=True)
file <- st_as_sf(file)

Any help would be appreciated!


Solution

  • I found an answer thanks to the r-sig-geo listserv

    #1. convert the geometry strings to sf spatial objects:
    newGeom = st_as_sfc(structure(as.character(file$geom), class = "WKB"),EWKB=TRUE)
    
    #2. create a new spatial data frame with the new spatial objects as geometry
    sdf = st_set_geometry(file, newGeom)
    
    #3. (optional) drop the character format column
    sdf$geom=NULL