# gDistance to determine closest point to polygon: does changing projection change results qualitatively?

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).

• 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. May 27, 2015 at 22:47
• (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? 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
May 28, 2015 at 16:27

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. 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. May 27, 2015 at 22:31
• @ToddBlan How do I change my projection to suit gDistance (i.e., to planar coordinates)?
– Dan
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. 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. May 27, 2015 at 22:33