[R-sig-Geo] calculating shortest distance from a set of SpatialPoints to a polygon

O'Hanlon, Simon J simon.ohanlon at imperial.ac.uk
Wed Jun 13 15:14:36 CEST 2012


Hi Malcom,
I forgot one step (and as.ppp is from spatstat not maptools package).

as.psp is expecting a SpatialLines object not a SpatialPolygons object. You can change your SpatialPolygons to SpatialLines easily enough, so try this:

library(spatstat)
sea.l <- as(sea, "SpatialLines")
sea.psp <- as.psp(sea.l)
locations.ppp <- as.ppp(locations)
dists <- nncross(locations.ppp, sea.psp)

HTH,

Simon

-----Original Message-----
From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Malcolm Fairbrother
Sent: 13 June 2012 13:46
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] calculating shortest distance from a set of SpatialPoints to a polygon

Dear list,

I have a large set of locations scattered over a land area covering about 200,000 km². That area borders the sea, and I'd like to find the shortest distance from each point location to the sea/coast. Point locations are in lat/long.

I've turned the locations into a SpatialPoints object, with a proj4string. And I have a SpatialPolygons object consisting of a single polygon, defining the sea areas (same proj4string as for the SpatialPoints object).

My problem is I'm not clear on the next step. I tried using "gDistance" from the "rgeos" package:

dists <- gDistance(locations, sea)

but got two warnings:

Warning messages:
1: In RGEOSDistanceFunc(spgeom1, spgeom2, byid, "rgeos_distance") :
  Spatial object 1 is not projected; GEOS expects planar coordinates
2: In RGEOSDistanceFunc(spgeom1, spgeom2, byid, "rgeos_distance") :
  Spatial object 2 is not projected; GEOS expects planar coordinates

So should I set the proj4string to NA? And/or is gDistance even the right tool for this job? Maybe it's designed for instances where distances are small, not in the hundreds of kilometres?

Any advice on how to do this appropriately would be much appreciated.

- Malcolm

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list