Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.
Another frequent question of my students is how to obtain a polygon map of the seas and oceans, rather than the land polygons (countries, etc.) that are commonly imported with R spatial data packages. You can mostly just use the land polygons and do the opposite operation as you would do for terrestrial features — e.g., to colour the sea in a map of land polygons, just use the background
instead of the col
argument; to mask a raster map to the sea, just use the land polygons for a terra::mask()
with inverse=TRUE
; to select or crop features that overlap the sea, just do instead terra::erase()
with the land polygons. But anyway, you may just prefer or need to have a marine polygon.
There are marine polygon maps available for download, e.g. at Marineregions.org. But if you don’t need the separation into particular seas or oceans or EEZs, you can easily create a global marine polygon from a countries map in R:
# import a world countries map: countries <- geodata::world(path = tempdir()) terra::plot(countries, col = "tan") # make a polygon map delimiting the entire extent of the Earth: earth <- terra::vect(terra::ext(), crs = "EPSG:4326") terra::plot(earth, col = "lightblue") terra::plot(countries, col = "tan", add = TRUE) # erase the countries (land parts) to get just the marine polygon: marine <- terra::erase(earth, countries) terra::plot(marine, col = "lightblue")
That’s it! See also terra::symdif()
, or terra::mask(inverse=TRUE)
. You can then crop the marine polygon with your own other polygon or desired extent, e.g. terra::crop(marine, terra::ext(-20, 60, -40, 40))
; and/or you can use the marine polygon to crop/mask other maps to the marine regions, e.g.:
# import a global bathymetry map: bathy_source <- "/vsicurl/https://gebco2023.s3.valeria.science/gebco_2023_land_cog.tif" # from https://gist.github.com/mdsumner/aaa6f1d2c1ed107fbdd7e83f509a7cf3 bathy <- terra::rast(bathy_source) # terra::plot(bathy) # slow # crop bathymetry to a given extent: bathy_crop <- terra::crop(bathy, terra::ext(110, 180, -50, 0)) terra::plot(bathy_crop, main = "Pixel values everywhere") terra::plot(countries, add = TRUE) # crop and mask bathymetry to keep values only on the marine polygon: bathy_marine <- terra::crop(bathy_crop, marine, mask = TRUE) terra::plot(bathy_marine, main = "Marine pixel values only")
See also this previous post for how to further crop/mask to the near-shore raster values, including for particular continents or islands.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you’re looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.
Continue reading: Getting marine polygon maps in R
Understanding and Creating Marine Polygons in R
The original article outlines how to use R’s spatial data packages to create a polygon map of the seas and oceans. It offers clear instructions and practical advice on how to import a map of world countries and then modify it to generate a map focused on marine elements. It also provides guidance on manipulating marine polygons, entertaining the possibility of isolating certain features by using additional functionalities present in R.
“But anyway, you may just prefer or need to have a marine polygon… There are marine polygon maps available for download, e.g. at Marineregions.org.”
Long-Term Implications and Future Developments
Understanding how to create and manipulate marine polygons in R can open up new research opportunities, particularly in marine science, environmental studies, and geography. The ability to isolate features – such as specific seas, oceans, or countries – allows detailed investigations into these regions, potentially informing policy makers on issues such as climate change effects on different marine ecosystems. Moreover, these techniques can contribute to the development of educational resources for teaching geospatial analysis.
Actionable Advices
For users dealing with marine polygons, it’s recommended to explore further functionalities of R, especially those related to geospatial data handling. Considering the current growth of spatial data availability, developing comfort with such manipulations is increasingly important.
- Always keep your R packages updated. Improved functions and new methods are regularly added that simplify and optimize geospatial data processing.
- Learn and practice frequently. Geospatial data analysis requires practice to master. Online communities such as R-bloggers offer valuable resources for learning and staying updated with the latest developments in the field.
- Share your knowledge and findings. The R community greatly benefits from its members sharing their insights and techniques. By publishing your methods and results, you contribute to this open-source ecosystem.
In conclusion, R provides powerful tools for working with geospatial data. By understanding how to create and manipulate maps and features, including marine polygons, users can unleash the full potential of these tools, opening new avenues for research, education, and policy development.