topcat
topcat

Reputation: 584

Get distances between boundaries from an SpatialPolygon and raster pixels

I am trying to calculate the distance from several pixels of a raster to a boundary of some polygons in R as it is showed in the image. I have two problems:

  1. How to correctly define a boundary from a SpatialPolygon object. I currently have two options, but I can not decide for which is the best one. First, I can coerce the SpatialPolygon as a SpatialLines object and then to SpatialPoints. Second, I can raster::rasterize the SpatialPolygon. I suppose each option has its pros and cons.

  2. I only want make the distance calculation from one of the sides of the border. In the example, I only want the distance to the border from the pixels inside Colombia to the border between both countries. I tried to "filter" the border using a latitude parameter but the real border is quite irregular, so using a strict cut-off value can lead to error.

The following code can be used to get the an example SpatialPolygonsDataFrame:

#Get the world map and select two countries [Colombia and Venezuela]
library(maptools) #To get the polygon data 
data(wrld_simpl)
colven <- c("Colombia", "Venezuela")
colven_map <- wrld_simpl[wrld_simpl$NAME %in% colven, ]

Raster data can be created with this code:

library(raster)
raster <- raster(colven_map, nrow=100, ncol=100)
raster[] <- 1:length(raster)
raster_colven <- mask(raster, colven_map)

Example

Thanks in advance

Upvotes: 1

Views: 1677

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47071

#example data
library(maptools) #To get the polygon data 
data(wrld_simpl)
colven <- wrld_simpl[wrld_simpl$NAME %in% c("Colombia", "Venezuela"), ]

library(raster)
raster <- raster(colven, nrow=100, ncol=100)
raster[] <- 1:length(raster)
raster <- mask(raster, colven)

I create some random cells of interest

set.seed(33)
cells <- sample(ncell(raster), 10)
xy <- xyFromCell(raster, cells)
sp <- SpatialPoints(xy, proj4string=crs(colven))

And then use rgeos

library(rgeos)
# inside Colombia only
col <- colven[colven$NAME == "Colombia", ]
sp <- gIntersection(sp, col)

# get the border between Venezuela and Colombia
ven <- colven[colven$NAME == "Venezuela", ]
border <- gIntersection(col, ven)

# get the distance
# this fails for me, that seems to be a bug in rgeos for these data
gDistance(sp, border, byid=TRUE)  

In this example the coordinate reference system is angular (longitude/latitude) and rgeos does not deal with that (it only works well for planar coordinates). But here you can use a function from geosphere:

library(geosphere)
dist2Line(xy, border)

If you want the distance form all grid cell of Colombia to the border you can also do:

rborder <- rasterize(border,  raster)
dborder <- distance(rborder)
dbcol <- mask(dborder, col)

See functions in the gdistance package if you want the distance from a place within Colombia to the border with Venezuela, while only travelling within Colombia.

Upvotes: 1

Related Questions