I am still trying to create a map of the Vermeille Coast in order to calculate the distance between two sampling points with the condition that the path between the two points is not crossing the land.
I am not familiar with the sf package but thanks to stack I already :
1/ bind two shapefile together (R cran: sf Sew two MULTILINESTRING/LINESTRING)
2/ created à box around to draw a polygon (Sf package: Close a polygon fom complex shape, R cran sf create a polygon from two sewed shapefiles)
3/ I am trying now to rasterize the polygon in order to apply this method (https://www.r-bloggers.com/2020/02/three-ways-to-calculate-distances-in-r/)
(data available here : https://www.dropbox.com/sh/hzsdklnmvjg4hsz/AAATHLV0pkJXDvSqyRIBlVl_a?dl=0)
To rasterize, I used the following codes:
https://cran.r-project.org/web/packages/fasterize/vignettes/using-fasterize.html https://rdrr.io/cran/fasterize/man/fasterize.html
library(sf)
library(fasterize)
library(raster)
library(dplyr)
library(devtools)
library(stars)
setwd("~/Dropbox/Data")
frenchCoast_CoteBanyuls <- st_read("coasts_subnational_France/coasts_subnational.shp")
spainCoast_CoteBanyuls <- st_read("coasts_subnational_Spain/coasts_subnational.shp")
bbox_frenchCoast_CoteBanyuls <- st_bbox(frenchCoast_CoteBanyuls) %>%
st_as_sfc()
polygon_frenchCoast_CoteBanyuls <- bbox_frenchCoast_CoteBanyuls %>%
lwgeom::st_split(frenchCoast_CoteBanyuls) %>%
st_collection_extract("POLYGON")
bbox_spainCoast_CoteBanyuls <- st_bbox(spainCoast_CoteBanyuls) %>%
st_as_sfc()
polygon_spainCoast_CoteBanyuls <- bbox_spainCoast_CoteBanyuls %>%
lwgeom::st_split(spainCoast_CoteBanyuls) %>%
st_collection_extract("POLYGON")
polyCombin <- st_union(polygon_frenchCoast_CoteBanyuls[1], polygon_spainCoast_CoteBanyuls[3])
plot(polyCombin, col = 'steelblue')
r <- raster(polyCombin, res = 1)
r <- fasterize(polyCombin, r, field = "value", fun="sum")
plot(r)
However I get the following error:
r <- raster(polyCombin, res = 1) Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘raster’
for signature ‘"sfc_POLYGON"’
r <- fasterize(polyCombin, r, field = "value", fun="sum") Error in fasterize(polyCombin, r, field = "value", fun = "sum") : Expecting
a single string value: [type=NULL; extent=0].
I then realized that in the example (https://rdrr.io/cran/fasterize/man/fasterize.html), the object to rasterize was different that mine:
library(raster)
library(fasterize)
library(sf)
p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
p1 <- list(p1, hole)
p2 <- list(rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0)))
p3 <- list(rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0)))
st_sf <- st_sf(value = c(1,2,3),
geometry = st_sfc(lapply(list(p1, p2, p3), st_polygon)))
class(st_sf)
[1] "sf" "data.frame"
in my case, I did:
class(polyCombin) [1] "sfc_POLYGON" "sfc"
So I tried to convert sfc_polygon into data.frame:
raster(as.data.frame(polyCombin), res = 1)
Error in .local(x, ...) : unused argument (res = 1)
it did not work. And I did not find any solution. I even tried :D
st_write(polyCombin, "polyCombin.shp")
polyCombin <- st_read("polyCombin.shp")
r <- raster(polyCombin, res = 1)
r <- fasterize(polyCombin, r)
plot(r)
Booh :D But it did not work.
Does someone have an idea? Thanks a lot in advance !
Charlotte