1

I would like to create a sf() object from the streamstats function delineateWatershed() without first needing to save as shapefiles of jsons. I can delineate the polygon, then save as a shape, and finally import back in as a shapefile. This leaves me with a version of the shape file in a nested folder, which will quickly take up space. Once I have my sf() polygons I plan to merge multiple together and save them as a single shapefile. In the end I will have two copies of the polygons. How can I skip the writeShapefile() then st_read step?

setwd('~/R/GIS/data/') #I need to define the working directory for writeShapefile()
ws <- delineateWatershed(xlocation = -123.9637, ylocation = 40.06269, crs = 4326)

##writeShapefile(watershed = ws, layer = "name of shape file", "name of folder in dir", what='boundary')
writeShapefile(watershed = ws, layer = "shed", "data", what = 'boundary')

ws_sf <- st_read("~/R/GIS/data/shed.shp") 

This is a screen shot of what ws looks like in the source viewer:

  • what type of object is `ws` ? – SymbolixAU Feb 11 '21 at 23:44
  • I think geojson, but in all honesty, not perfectly sure. There are functions within the package to export to geojson and shapfile. Below is an example from the delineateWatershed() report. I think this what 'ws' is, geojson "C:\Users\nmack\AppData\Local\Temp\RtmpKagCLS\file47ac34673f50.geojson" I do not know how to predict the file path for the temp. file, so I am reliant on the R functions. – Nathan Mack Feb 12 '21 at 04:33
  • `st_read()` can read geojson directly, for example `sf::st_read('{"type":"Point","coordinates":[0,0]}')`, so if your `ws` object is geojson, you should be able to simply do `sf::st_read( ws )` – SymbolixAU Feb 12 '21 at 04:51
  • I added a screen shot. I think the geojson is nested, but I can not tell where. @SymbolixAU, I am trying things like: 'st_read(ws[[featurecollection]][[2]])' 'st_read(ws$featurecollection$globalwatershed)' – Nathan Mack Feb 12 '21 at 16:23
  • Did you try `st_read( ws )` directly? – SymbolixAU Feb 12 '21 at 20:39
  • I did. `> st_read(ws) Error in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, : Expecting a single value: [extent=3]. In addition: Warning message: In if (nchar(dsn) < 1) { : the condition has length > 1 and only the first element will be used` I am sure it would have worked if I used `st_read("C:\Users\nmack\AppData\Local\Temp\RtmpKagCLS\file47ac34673f50.geojson")` I am writing this into a loop and I have no way of knowing where the file will be stored prior to its download. – Nathan Mack Feb 12 '21 at 21:30

1 Answers1

0

After some some guess and check I came up with a solution, but I would love to see a more elegant one without a loop. I found the list of coords for the polygon. Made a data frame from the list, then used st_polygon. Props to @Gilles for the last part

    ws1 <- ws$featurecollection[[2]]$feature$features[[1]]$geometry$coordinates[[1]]
length(ws1)

for (i in 1:length(ws1)){
ws2[i,] <- data.frame(ws1[[i]])   
}
ws3 <- st_sf(st_sfc(st_polygon(list(as.matrix(ws2)))), crs = 4326)
mapview(ws3)