I'd like to run some spatial algorithms where it's necessary to "mask out" areas of land. I can make a map showing land:water as black:white with the
lonRange <- c( 100, 150 )
latRange <- c( 10, 50 )
mapObject <- maps::map( database = "world", xlim = lonRange, ylim = latRange, fill = TRUE )
land = 0
water = 1
map <- array( data = NA, dim = c( 10, 10, 3 ) )
map[,,1] <- c( rep( 1, 42 ), rep( 0, 58 ) )
map[,,2] <- c( rep( seq.int( from = latRange, to = latRange, length.out = 10 ), 10 ) )
map[,,3] <- sort( c( rep( seq.int( from = lonRange, to = lonRange, length.out = 10 ), 10 ) ) )
 1 50 100
"the x and y vectors have coordinates of successive polygons"
Note: As @hrbrmstr mentioned, there are better methods.
rgeos::gContains() judges whether a point (defined by longitude/latitude) is in
SpatialPolygon... (see: Check if point is in spatial object which consists of multiple polygons/holes)(caution: it is a bad idea to handle a lot of ponits in this way). In your case, True means on land. (2) You can make
SpatialPolygons from coordinates
mapObject has. (Note:
NA as a division, so coordinates between
NA correspond to one
library(rgeos); library(maps); library(dplyr) coords.mat <- matrix(c(mapObject$x, mapObject$y), ncol=2) # get lon-lat coordinates div <- c(0, which(is.na(mapObject$x)), length(mapObject$x)+1) # get divisions information ## separate coordinates by divisions(NA) coords.list <- sapply(1:(length(div)-1), function(x) coords.mat[(div[x]+1):(div[x+1]-1),]) ## change sets of coordinates into SpatialPolygons map.sp <- coords.list %>% sapply(function(x) Polygon(x)) %>% Polygons(ID = "a") %>% list() %>% SpatialPolygons() ## judge contain.judge <- apply(map, c(1,2), function(x) gContains(map.sp, SpatialPoints(matrix(c(x, x), ncol=2)))) ## chage into binary data and combine map[,,1] <- as.numeric(contain.judge) ## plot(coords.mat, pch=".", xlim = lonRange, ylim = latRange) points(c(map[,,3]), c(map[,,2]), col=c(2,4)[as.factor(c(map[,,1]))], pch = 19)
# bonus: 100x100x3 array version. It takes a few minutes to judge all points.