I'm using gDistance from the rgeos package for R to find the nearest point to a polygon. The projection of both points and the polygon is +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0. My code looks something like this:

min(gDistance(MyPolygon, MyPoints, byid=TRUE))

Running this gives a result and the following 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

My understanding of this is that gDistance isn't happy with my projection (or lack thereof). The actual magnitude of the result isn't important, just that the point is indeed the closest point to the polygon. My question is will this result change if I change the projection? That is, regardless of the actual numeric value of the distance between the polygon and point, will the same point be identified as being closest if I changed the projection?

Further details

My points are data that has a global coverage over the oceans at a 1 degree resolution. My polygons are regions of the ocean typically a few degrees in length, but generally quite narrow. Nevertheless, since the points and polygons originate from different sources, the two do not necessarily line up – which is why I need to find points closest to the polygons. That said, there will always be a point within 1 degree of a polygon. My results don't need to be very accurate (i.e., ballpark estimates).

  • 1
    Absolutely! some projections don't really support distances (mercator, geographic) while others significantly affect the actual distance you will get (Albers vs Lamberts vs UTM). Each projection is a model of the earths' surface and has its own limitations and benefits.. if you want the true distance you need to use geodetic distance using an ellipsoid. Commented May 27, 2015 at 22:47
  • 1
    (I created an edit from a useful comment you posted). With respect to that, it begins to sound like you are not attempting to find nearest points in any geographic sense, but rather in a database sense of identifying cells in an underlying grid that is defined in lat-lon coordinates. If that's the case, you don't have a geographic problem at all, so the solution is to ignore projection issues totally. Equivalently, you could lie about the projection: pretend it is practically any projection you like; find the desired points; and then continue on your way. Is this a fair interpretation?
    – whuber
    Commented May 28, 2015 at 13:04
  • @whuber Yep, that sounds about right. In that case, I won't worry too much about the projection issues. Thanks to all for the helpful comments & discussion.
    – Dan
    Commented May 28, 2015 at 16:27

2 Answers 2


Instead of gDistance (for planar coordinates) You can use dist2Line (for angular coordinates), from the geosphere package. Although it is called dist2Line, the function also works for (Spatial) Polygons*.


It doesn't want you to use unprojected data because polygons are not properly represented unless you use an equal area projection.

Take a look at Greenland vs Africa on a GCS map and it tells you Greenland is bigger than Africa - not even close.

So, in order to get proper measurements ensure you apply an equal area projection to your data that suits the area of the Earth and scale you are working in.

  • Could you elaborate on what "OK" means? At all points not near the earth's Equator, the two methods can produce profoundly different results even on a large scale (where distances are on the order of meters). There's even a problem using projected coordinates when distances are large.
    – whuber
    Commented May 27, 2015 at 22:25
  • They can produce differences relative to each other yes, but if you're comparing apples to apples (GCS WGS84 to GCS WGS84) and evaluating distances using great circle routes, you will get proper distance comparisons between 2 points. I wouldn't go and calculate areas on the other hand.
    – ToddBlan
    Commented May 27, 2015 at 22:31
  • 1
    @ToddBlan How do I change my projection to suit gDistance (i.e., to planar coordinates)?
    – Dan
    Commented May 27, 2015 at 22:32
  • I might be misunderstanding what you're saying, but I do know that the answer to the question "will the same point be identified as being closest if I changed the projection?" is an emphatic no. I'm trying to reconcile this sad mathematical fact with what you have answered, which seems to say the opposite.
    – whuber
    Commented May 27, 2015 at 22:33
  • I'm going to state now that I obviously did NOT read the question well and thought he was comparing points to points. I did not see polygons. So yes, as stated above, you will get measurement errors on areal features at different latitudes.
    – ToddBlan
    Commented May 27, 2015 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.