3

I would like to find the minimum distance between points and polygon boundary (all points lie inside the polygon). If that is possible, how can I extract the values?, so I can find a correlation between numbers of individuals and the distance from the border.

The polygon is on .SHP format and points on X/Y coordinates.

Any missing information please let me know! Your help is greatly appreciated!

3 Answers3

7

unit square polygon:

library(sp)
x = cbind(c(0,1,1,0,0),c(0,0,1,1,0))
pol = SpatialPolygons(list(Polygons(list(Polygon(x)), "ID")))

random points in unit square:

set.seed(131)
pts = SpatialPoints(cbind(runif(10), runif(10)))
plot(pol)
points(pts, col = 'red')

compute distances:

library(rgeos)
gDistance(pts, pol, byid = TRUE) # will be 0, all inside
gDistance(pts, as(pol, "SpatialLines"), byid = TRUE) # dist to line

add to plot:

text(coordinates(pts),
  as.character(
    round(as.vector(gDistance(pts, as(pol, "SpatialLines"), byid = TRUE)), 3)),
pos = 4)

read your polygon data from a shapefile into R by using readOGR in package rgdal

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
Edzer Pebesma
  • 3,814
  • 16
  • 26
4

The spatstat package has a function nncrossthat finds the nearest neighbour between two sets of point or one set of points and a set of segments.

It is relatively easy to load a set of x/y values to create a spatstat point pattern object: if X and Y are two vectors containing your coordinates, you can create a point pattern object with

library(spatstat)
p = ppp(x,y)

You need to convert your shp data to spatstat segment pattern object. To do so, you can load the shp file with commands from maptools and than convert into a spatstat object:

library(maptools)
shp = readShapeSpatial("yourdata.shp") #read shp file
shp = as.psp(shp) # convert to psp object

To calculate your nearest neighbour distance, you have to use nncross

nncross(p,shp)
xraynaud
  • 2,028
  • 19
  • 29
  • I had no problem creating the .ppp object, but when I tried to transform the .shp file to .psp shows a error that says: Error in as.psp.default(shp) : Unable to interpret x as a line segment pattern Any suggestion? – Renzo Ferreira Feb 07 '15 at 16:23
  • there is a doc [here](http://cran.r-project.org/web/packages/spatstat/vignettes/shapefiles.pdf) that might help you. There is probably a problem with how maptools recognizes your shp. What is the class() of your data after `readShapeSpatial()` ? – xraynaud Feb 07 '15 at 16:45
  • the class is [1] "SpatialPolygonsDataFrame" – Renzo Ferreira Feb 07 '15 at 17:22
  • To some degree this is not a correct answer. The minimum distance between a point and a polyline is not necessarily the one to one of the nodes of the polyline. But, nncross does exactly this: finding (only) the nearest node of the polyline. What if I want to know the minimum distance to any point on the polyline? – agoldev Mar 23 '16 at 13:35
  • 1
    Could you please develop ? I don't have this behaviour on my system. If I create a psp line segment using `line = psp(0.25,0.5,0.75,0.5,window=owin(c(0,1),c(0,1)))` and the set of points `pts = ppp((1:9)/10, rep(0.25,length.out=9))`, `nncross(pts,line)` returns 0.25 for all points that have 0.25>= x-value >= 0.75. The order of arguments is important: `nncross(line,pts)` returns the minimum distance between the nodes of the segment and one of the points. – xraynaud Mar 29 '16 at 08:58
3

Follow the steps of @xraynaud (slightly modified):

library(maptools)
shp = readShapeSpatial("yourdata.shp") #read shp file
W = as.owin(shp) # convert to owin object

library(spatstat)
p = ppp(x, y, window = W)

Now p is a point pattern containing the points bounded by the polygon. To compute the distance from each point to the bounding polygon (usually called the window in spatstat terminology):

d = bdist.points(p)

Now d is a vector of distances.

Ege Rubak
  • 4,347
  • 1
  • 10
  • 18