Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculating minimum distance between a point and the coast

Tags:

r

gis

spatial

I am trying to get the minimum distance between a given point and the coast. My example is the distance of Madrid to the coast:

library(rgeos)
library(maptools)
coast <- readShapeLines("Natural_Earth_quick_start/10m_physical/ne_10m_coastline.shp")
MAD = readWKT("POINT(-3.716667 40.383333)")
gDistance(MAD,coast)
[1] 3.021808

I am having trouble understanding what gDistance() returns. The docs say it's in the units of the projection. Does that mean it is in latlong degrees? How can I convert that to kilometers?

like image 660
user525602 Avatar asked Jan 22 '14 22:01

user525602


People also ask

What is the formula for minimum distance?

The shortest distance between two points is a straight line. This distance can be calculated by using the distance formula. The distance between two points ( x 1 , y 1 ) and ( x 2 , y 2 ) can be defined as d = ( x 2 − x 1 ) 2 + ( y 2 − y 1 ) 2 .

How do you find the minimum distance between a point and a plane?

To find the shortest distance between point and plane, we use the formula d = |Axo + Byo + Czo + D |/√(A2 + B2 + C2), where (xo, yo, zo) is the given point and Ax + By + Cz + D = 0 is the equation of the given plane.

How do you find the minimum distance of a curve?

Since distance is positive and the square root function is increasing, it suffices to find the smallest value the squared distance between (x,y) on the curve and the point (2,0) can take. This is L(x)=(x−2)2+(y−0)2=(x−2)2+y2=x2−4x+4+16x2+5x+16=17x2+x+20.


1 Answers

gDistance(...) returns the minimum Cartesian (Euclidean) distance between the point and feature set provided as arguments. Since your map is in long/lat coordinates, you get distance in "degrees", e.g.

d = sqrt { (long1 - long2)2 + (lat1 - lat2)2 }

where long and lat are in decimal degrees. As was pointed out, this doesn't mean much because converting to planar distance (say, km) depends on where you are. So we need to transform your data into a CRS which is approximately planar in the region of interest. It turns out that the appropriate CRS for Spain is EPSG-2062. The projection string for EPSG-2062 is:

+proj=lcc +lat_1=40 +lat_0=40 +lon_0=0 +k_0=0.9988085293 +x_0=600000 +y_0=600000 +a=6378298.3 +b=6356657.142669561 +pm=madrid +units=m +no_defs 

which has +units=m (meters). So we need to reproject both the point (MAD) and the borders to EPSG-2062.

library(rgeos)
library(maptools)

epsg.2062 <- "+proj=lcc +lat_1=40 +lat_0=40 +lon_0=0 +k_0=0.9988085293 +x_0=600000 +y_0=600000 +a=6378298.3 +b=6356657.142669561 +pm=madrid +units=m +no_defs"
wgs.84    <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

coast <- readShapeLines("ne_10m_coastline",CRS(wgs.84))
MAD   <- readWKT("POINT(-3.716667 40.383333)",p4s=CRS(wgs.84))
gDistance(MAD,coast)            # WGS-84 (long/lat) projection, units in "degrees"
# [1] 3.021808

coast.proj <- spTransform(coast,CRS(epsg.2062))
MAD.proj   <- spTransform(MAD,CRS(epsg.2062))
gDistance(MAD.proj,coast.proj)  #EPSG-2062 projection, units are in meters.
# [1] 305171.2

So the minimum distance is ~305.2km.

Finally, note that your coastline file has all the coastlines of the world, so this is the minimum distance to some coastline, not necessarily the Spanish coast (although in this case it does turn out to be on the northern coast of Spain). If your reference point was very near the border with Portugal, the the nearest coastal point would be to the western coast of Portugal.

like image 115
jlhoward Avatar answered Oct 18 '22 19:10

jlhoward