Does there already exist a function in R to identify intersection between pairs of points?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Does there already exist a function in R to identify intersection between pairs of points?

sharx
This post has NOT been accepted by the mailing list yet.
Hello,

I am looking to find if two lat/lon points collected from a radio collared animal intersect a road.  The current data I am working with consists of
1) road points converted from road layers using fortify() function in library
(ggplot2) and
2) GPS radio lat/lon locations.

Given two pairs of points, is there a predefined function in R that will tell if the straight lines connecting each pair intersect? I have written my own function for this purpose and was wondering if it would be worth it to develop this into a package.

For example, say I want to know if the line segments defined by

(a1,b1) = (-119, 34) and (a2, b2) = (-120, 35)

intersect with:
pair 1: (x1,y1) = (-119.5, 33), (x2,y2) =(-119.1, 36)
or
pair 2: (x1,y1) = (-120.5, 33), (x2,y2) =(-119.1, 32)

The function should return TRUE for pair 1 and FALSE for pair 2. Or even better, it could return the intersection points for those coordinates that intersect.

Thanks for your help! Much appreciated.

Best,
Sharon
Reply | Threaded
Open this post in threaded view
|

Re: Does there already exist a function in R to identify intersection between pairs of points?

JLong
Sharon,
You are likely best to leave the road data as lines (SpatialLines) and then convert your radio collar to SpatialLines connecting consecutive fixes. see: http://stackoverflow.com/questions/24284356/convert-spatialpointsdataframe-to-spatiallinesdataframe-in-r, keep in mind the ordering of the points will matter.

Then intersecting the two SpatialLines can be done using the rgeos package and gIntersection function. The output will be the SpatialPoints (Coordinates) where the collar fix segments and roads intersect (representing presumably road crossing areas?), which you can map.

Cheers,
Jed
Jed Long
Lecturer in GeoInformatics
Department of Geography & Sustainable Development
University of St Andrews, UK