[R] Midpoint between coordinates

Mafalda Viana vianam at tcd.ie
Fri Jul 23 16:13:03 CEST 2010


Dear David and R users,

Thank you very much for the help. There is a function in the suggested
package called "midPoint" which does what I need.

df<- data.frame(lon1=c(-4.568,-4.3980), lat1=c(59.235,56.369),
lon2=c(-5.123,-4.698), lat2=c(60.258,59.197) )

library(geosphere)

 p1 <- matrix(c(df$lon1,df$lat1), ncol=2  )
 p2 <- matrix(c(df$lon2,df$lat2), ncol=2  )

midpoint <- as.data.frame(midPoint(p1,p2) )

For short distances the results are indeed very similar to the arithmetic mean.

Thank you again.
Mafalda


On 23 July 2010 14:12, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Jul 23, 2010, at 8:58 AM, Mafalda Viana wrote:
>
>> The arithmetic mean was my first approach and to nearby points it
>> doesn't make much difference. However, when the distance between the 2
>> points gets bigger this is no longer accurate enough. So yes, I was
>> thinking on spherical geometry, midpoint considering the great circle
>> distance or similar.
>
> Are you "up" for some searching?
>
> require(sos)
>> ???"distance spherical"
> found 65 matches;  retrieving 4 pages
> 2 3 4
>
> On first page of hits the geosphere package says it handles spherical
> geometry. Perhaps the gcIntermediate function:
>
> gcIntermediate {geosphere}
> Get intermediate points on a Great Circle inbetween the two points used to
> define the Great Circle
> Usage
> gcIntermediate(p1, p2, n=50)
> Arguments
> p1  Longitude/latitude of a single point, in degrees; can be a vector of two
> numbers, a matrix of 2 columns (first one is longitude, second is latitude)
> or a SpatialPoints* object
> p2  As above
> n  The requested number of points on the Great Circle
> ---
>
> There is also a geospatial Task View and SIG mailing list that is very
> active.
> --
> David
>>
>> Thank you
>> Mafalda
>>
>> On 23 July 2010 13:30, David Winsemius <dwinsemius at comcast.net> wrote:
>>>
>>> On Jul 23, 2010, at 7:13 AM, Mafalda Viana wrote:
>>>
>>>> Dear R users,
>>>>
>>>> I need to find the coordinates for the point (midpoint) located half
>>>> way between two pairs of coordinates (lon1,lat1 and lon2,lat2)
>>>> assuming a straight line between them. What would be the best way? I
>>>> tried to find an answer in the help archives but without success. I
>>>> would greatly appreciate any help.
>>>>
>>>> df<- data.frame(lon1=c(-4.568,-4.3980), lat1=c(59.235,56.369),
>>>> lon2=c(-5.123,-4.698), lat2=c(60.258,59.197) )
>>>
>>> Wouldn't that just be the arithmetic average of the values? Or to you
>>> have
>>> some need for a more accurate calculation based on spherical geometry?
>>>  Or
>>> some thing that will handle some other coordinate weirdness?
>>>
>>>> df$midlong <- apply(df[,c(1,3)], 1, mean)
>>>> df$midlat <- apply(df[,c(2,4)], 1, mean)
>>>> df
>>>
>>>   lon1   lat1   lon2   lat2 midlong  midlat
>>> 1 -4.568 59.235 -5.123 60.258 -4.8455 59.7465
>>> 2 -4.398 56.369 -4.698 59.197 -4.5480 57.7830
>>>>
>>> --
>>>
>>> David Winsemius, MD
>>> West Hartford, CT
>>>
>>>
>>>
>>
>>
>>
>> --
>> Mafalda Viana
>> Department of Zoology
>> School of Natural Sciences
>> Trinity College Dublin
>> Dublin 2
>> Ireland
>>
>> (+353) (0) 872829850
>>
>> vianam at tcd.ie
>>
>> http://www.tcd.ie/Zoology/research/research/theoretical
>
> David Winsemius, MD
> West Hartford, CT
>
>
>



-- 
Mafalda Viana
Department of Zoology
School of Natural Sciences
Trinity College Dublin
Dublin 2
Ireland

(+353) (0) 872829850

vianam at tcd.ie

http://www.tcd.ie/Zoology/research/research/theoretical



More information about the R-help mailing list