# 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:

- Visualize marine regions alone
- Visualize marine regions with associated data paired with analysis
- Use marine region geospatial boundaries to query data providers (e.g., OBIS (https://www.obis.org))
- Geocode - get geolocation data from place names
- Reverse Geocode - get place names from geolocation data

## 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)
    #> [1] "Town"                      "Arrondissement"
    #> [3] "Department"                "Province (administrative)"
    #> [5] "Country"                   "Continent"

## Get Marineregions records by place type

    res1 <- mr_records_by_type(type = "EEZ")
    head(res1)
    #>   MRGID
    #> 1  3293
    #> 2  5668
    #> 3  5669
    #> 4  5670
    #> 5  5672
    #> 6  5673
    #>                                                                                                                                                                                                            gazetteerSource
    #> 1 Flanders Marine Institute (2019). Maritime Boundaries Geodatabase: Maritime Boundaries and Exclusive Economic Zones (200NM), version 11. Available online at https://www.marineregions.org/. https://doi.org/10.14284/386
    #> 2 Flanders Marine Institute (2019). Maritime Boundaries Geodatabase: Maritime Boundaries and Exclusive Economic Zones (200NM), version 11. Available online at https://www.marineregions.org/. https://doi.org/10.14284/386
    #> 3 Flanders Marine Institute (2019). Maritime Boundaries Geodatabase: Maritime Boundaries and Exclusive Economic Zones (200NM), version 11. Available online at https://www.marineregions.org/. https://doi.org/10.14284/386
    #> 4 Flanders Marine Institute (2019). Maritime Boundaries Geodatabase: Maritime Boundaries and Exclusive Economic Zones (200NM), version 11. Available online at https://www.marineregions.org/. https://doi.org/10.14284/386
    #> 5 Flanders Marine Institute (2019). Maritime Boundaries Geodatabase: Maritime Boundaries and Exclusive Economic Zones (200NM), version 11. Available online at https://www.marineregions.org/. https://doi.org/10.14284/386
    #> 6 Flanders Marine Institute (2019). Maritime Boundaries Geodatabase: Maritime Boundaries and Exclusive Economic Zones (200NM), version 11. Available online at https://www.marineregions.org/. https://doi.org/10.14284/386
    #>   placeType latitude longitude minLatitude minLongitude maxLatitude
    #> 1       EEZ 51.46545   2.70504    51.08931      2.23833    51.87611
    #> 2       EEZ 53.61208   4.18654    51.03976      2.53933    55.76500
    #> 3       EEZ 54.55354   8.39440    52.87112      3.35000    55.91928
    #> 4       EEZ 40.92708  19.11300    39.64039     18.32149    42.01120
    #> 5       EEZ 42.93396  29.20165    41.97819     27.44371    43.73847
    #> 6       EEZ 43.43006  15.65218    41.62970     13.00833    45.56495
    #>   maxLongitude precision            preferredGazetteerName
    #> 1      3.37040  58302.49   Belgian Exclusive Economic Zone
    #> 2      7.21028 294046.10     Dutch Exclusive Economic Zone
    #> 3     14.75000 395845.50    German Exclusive Economic Zone
    #> 4     20.02083 139751.70  Albanian Exclusive Economic Zone
    #> 5     31.33082 186792.50 Bulgarian Exclusive Economic Zone
    #> 6     18.54925 313990.30  Croatian Exclusive Economic Zone
    #>   preferredGazetteerNameLang   status accepted
    #> 1                    English standard     3293
    #> 2                    English standard     5668
    #> 3                    English standard     5669
    #> 4                    English standard     5670
    #> 5                    English standard     5672
    #> 6                    English standard     5673

## 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")
    #> # A tibble: 7 x 6
    #>   layer            name_first    name_second id     name                   mrgid
    #>   <chr>            <chr>         <chr>       <chr>  <chr>                  <chr>
    #> 1 MarineRegions:i~ MarineRegions iho         iho.1  Rio de La Plata        4325
    #> 2 MarineRegions:i~ MarineRegions iho         iho.7  Timor Sea              4344
    #> 3 MarineRegions:i~ MarineRegions iho         iho.18 Gulf of California     4314
    #> 4 MarineRegions:i~ MarineRegions iho         iho.40 Ionian Sea             3351
    #> 5 MarineRegions:i~ MarineRegions iho         iho.53 Seto Naikai or Inland~ 4306
    #> 6 MarineRegions:i~ MarineRegions iho         iho.75 Gulf of Mexico         4288
    #> 7 MarineRegions:i~ MarineRegions iho         iho.93 Sea of Okhotsk         4309

or don’t (but then `mr_names_search()` call takes longer)

    mr_names_search("iho", q = "Sea")
    #> # A tibble: 73 x 6
    #>    layer             name_first    name_second id     name                 mrgid
    #>    <chr>             <chr>         <chr>       <chr>  <chr>                <chr>
    #>  1 MarineRegions:iho MarineRegions iho         iho.3  Great Australian Bi~ 4276
    #>  2 MarineRegions:iho MarineRegions iho         iho.4  Tasman Sea           4365
    #>  3 MarineRegions:iho MarineRegions iho         iho.6  Savu Sea             4343
    #>  4 MarineRegions:iho MarineRegions iho         iho.7  Timor Sea            4344
    #>  5 MarineRegions:iho MarineRegions iho         iho.8  Bali Sea             4340
    #>  6 MarineRegions:iho MarineRegions iho         iho.9  Coral Sea            4364
    #>  7 MarineRegions:iho MarineRegions iho         iho.10 Flores Sea           4341
    #>  8 MarineRegions:iho MarineRegions iho         iho.11 Solomon Sea          4361
    #>  9 MarineRegions:iho MarineRegions iho         iho.12 Arafura Sea          4347
    #> 10 MarineRegions:iho MarineRegions iho         iho.14 Java Sea             4338
    #> # ... with 63 more rows

## Get a region - geojson

    res3 <- mr_geojson(key = "Morocco:dam")
    class(res3)
    #> [1] "mr_geojson"
    names(res3)
    #> [1] "type"          "totalFeatures" "features"      "crs"

## Get a region - shp

    res4 <- mr_shp(key = "Morocco:dam")
    class(res4)
    #> [1] "sf"         "tbl_df"     "tbl"        "data.frame"

## 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