knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE,
  warning = FALSE,
  message = FALSE
)

mregions introduction

mregions is useful to a wide diversity of R users because you get access to all of the data MarineRegions has, which can help in a variety of use cases:

Install

Stable version

install.packages("mregions")

Dev version

devtools::install_github("ropensci/mregions")
install.packages("leaflet")
library("mregions")

Get list of place types

res <- mr_place_types()
head(res$type)

Get Marineregions records by place type

res1 <- mr_records_by_type(type = "EEZ")
head(res1)

Get a data.frame of region names

rnames <- mr_names("MarineRegions:iho")

Search region names

Either pass output of mr_names()

mr_names_search(rnames, "IHO")

or don't (but then mr_names_search() call takes longer)

mr_names_search("iho", q = "Sea")

Get a region - geojson

res3 <- mr_geojson(key = "Morocco:dam")
class(res3)
names(res3)

Get a region - shp

res4 <- mr_shp(key = "Morocco:dam")
class(res4)

Convert to WKT

From geojson or shp. Here, geojson

res7 <- mr_geojson(key = "Morocco:dam")
mr_as_wkt(res7, fmt = 5)
#> [1] "MULTIPOLYGON (((41.573732 -1.659444, 45.891882 ... cutoff

Dealing with bigger WKT

What if you're WKT string is super long? It's often a problem because some online species occurrence databases that accept WKT to search by geometry bork due to limitations on length of URLs if your WKT string is too long (about 8000 characters, including remainder of URL). One way to deal with it is to reduce detail - simplify.

install.packages("rmapshaper")

Using rmapshaper we can simplify a spatial object, then search with that.

shp <- mr_shp(key = "MarineRegions:eez_iho_union_v2", maxFeatures = 5)

Visualize

library(leaflet)
leaflet() %>%
  addTiles() %>%
  addPolygons(data = shp)

map2

Simplify

library("rmapshaper")
shp <- ms_simplify(shp)

It's simplified:

library(leaflet)
leaflet() %>%
  addTiles() %>%
  addPolygons(data = shp)

map3



Try the mregions package in your browser

Any scripts or data that you put into this service are public.

mregions documentation built on April 12, 2022, 1:05 a.m.