Skip to content

Instantly share code, notes, and snippets.

@z3tt
Created April 7, 2020 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z3tt/84d06b3d66adca38b2795ebed449f2bc to your computer and use it in GitHub Desktop.
Save z3tt/84d06b3d66adca38b2795ebed449f2bc to your computer and use it in GitHub Desktop.
``` r
library(tidyverse)
library(raster)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
oceans <-
rnaturalearth::ne_download(scale = 10,
category = "physical",
type = "ocean",
returnclass = "sf")
#> OGR data source with driver: ESRI Shapefile
#> Source: "C:\Users\Cedric\AppData\Local\Temp\RtmpKQR6db", layer: "ne_10m_ocean"
#> with 1 features
#> It has 3 fields
ggplot(oceans) + geom_sf(fill = "dodgerblue")
```
![](https://i.imgur.com/wjDgmYB.png)
``` r
bbox <- as(extent(4, 16, 39, 51), 'SpatialPolygons')
crs(bbox) <- crs(oceans)
oceans_med <- oceans %>%
st_crop(., bbox)
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries
ggplot(oceans_med) + geom_sf(fill = "dodgerblue")
```
![](https://i.imgur.com/UqjkqtG.png)
``` r
oceans_fix <- st_difference(oceans_med, st_as_sfc(st_bbox(oceans_med)))
#> although coordinates are longitude/latitude, st_difference assumes that they are planar
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries
ggplot(oceans_fix) + geom_sf(fill = "dodgerblue")
```
![](https://i.imgur.com/zJsJB75.png)
<sup>Created on 2020-04-07 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment