Global Geographic Distance Raster

I am wondering if someone has plotted a raster of the world's continents where each cell is equal to the distance from that cell to the nearest coast. This map will highlight the land parcels that are most isolated within the country.

I would guess it is just a rasterize

shapefile of global bounds and then calculates the distances.

+2


source to share


1 answer


You can do this with a raster::distance

which calculates the distance from each cell NA

to the nearest cell NA

. You just need to create a bitmap NA

for earth pixels and a different value for unearthly pixels.

enter image description here

Here's how:



library(raster)
library(maptools)
data(wrld_simpl)

# Create a raster template for rasterizing the polys. 
# (set the desired grid resolution with res)
r <- raster(xmn=-180, xmx=180, ymn=-90, ymx=90, res=1)

# Rasterize and set land pixels to NA
r2 <- rasterize(wrld_simpl, r, 1)
r3 <- mask(is.na(r2), r2, maskvalue=1, updatevalue=NA)

# Calculate distance to nearest non-NA pixel
d <- distance(r3)

# Optionally set non-land pixels to NA (otherwise values are "distance to non-land")
d <- d*r2

      

To create the graph above (I like rasterVis

for plotting, but you can use plot(r)

):

library(rasterVis)
levelplot(d/1000, margin=FALSE, at=seq(0, maxValue(d)/1000, length=100),
          colorkey=list(height=0.6), main='Distance to coast')

      

+6


source







All Articles