Skip to content

Instantly share code, notes, and snippets.

@jebyrnes
Created June 10, 2014 17:50
Show Gist options
  • Save jebyrnes/c3733b4d0208cf88012e to your computer and use it in GitHub Desktop.
Save jebyrnes/c3733b4d0208cf88012e to your computer and use it in GitHub Desktop.
Script to get Spalding's Marine Ecoregions of the World in R from lat/long data and a GIS file.
############################################################################################
#
# Script to turn lat/long data into
# marine ecoregions using Spalding's Marine Ecoregions
# of the World (MEOW).
#
# To run, first acquire the appropriate GIS files from
# http://maps.tnc.org/gis_data.html and unzip them into
# a directory (MEOW-TNC) - then go from there.
#
# getRegionalInfo returns Realms, Provinces, and Ecoregions
# for a single lat/long. I'm sure this can be improved, as this is
# my first attempt at doing anything GIS-y in R!
#
# - Jarrett Byrnes
#
# Last Updated 6/10/2014
############################################################################################
library(rgdal)
library(raster)
# for shapefiles, first argument of the read/write/info functions is the
# directory location, and the second is the file name without suffix
# optionally report shapefile details
ogrInfo("../MEOW-TNC", "meow_ecos")
regions <- readOGR("../MEOW-TNC", "meow_ecos")
#let's see the map
#plot(regions, axes=TRUE, border="gray")
getRegionalInfo <- function(lat1, long1){
#lat1 <- c(50.09444)
#long1 <- c(-127.5589)
#first, extract the co-ordinates (x,y - i.e., Longitude, Latitude)
coords <- cbind(long1, lat1)
FB.sp <- SpatialPointsDataFrame(coords,data.frame(value = c(4)))
proj4string(FB.sp) <- CRS("+proj=longlat +datum=WGS84 +no_defs")
# plot(regions)
# plot(FB.sp, add=T)
dsdat <- over(regions, FB.sp, add=T, fn = mean)
ret <- data.frame(ECOREGION = regions$ECOREGION[which(dsdat$value==4)],
PROVICE = regions$PROVINCE[which(dsdat$value==4)],
REALM = regions$REALM[which(dsdat$value==4)])
if(nrow(ret)==0) ret <- data.frame(ECOREGION = NA,
PROVICE = NA,
REALM = NA)
return(ret)
}
#Not Run Demos
#getRegionalInfo(50.00806, -127.4342)
@camilarebarreto
Copy link

Hi,
I'm trying to apply the getRegionalInfo function to a dataframe containing lat and long. My data frame has more than 1 million pair of coordinates, and R has been running for about 8 hours. Do you know if it's normal to take that long?

Thanks!

Camila.

@jebyrnes
Copy link
Author

That..... seems long. FYI, you might want to look at the mregions package. It's what I use now. And/or maybe doing this with sf would be more efficient? I wrote this a looooong time ago. 1 million coords, though - hrm, yeah, I don't know.

@camilarebarreto
Copy link

Thank you for the answer!
I managed to solve it. I decreased data by removing duplicate coordinates. In the end I got 120 thousand. And the code, made available by you, ran for about 5 hours.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment