[GIS] Find intersections between spatial lines and points using R

intersectionlinepoint-of-interestrsp

This question assumes that I'm using R with the sp and rgdal packages installed. Now that I have a set of spatial segments of class SpatialLinesDataFrame (called 'segmentData') and a set of spatial points of class SpatialPointsDataFrame (called 'poi'). Suppose that the segments and the points intersect each other as illustrated in the example figure below where line segments are in red and POIs are in blue. I'm trying to query using the function 'over' of the sp package in order to find out which lines cross over a given point. I'm having the following 2 problems:

(1) Whenever I try to query using over(poi, segmentData) or over(segmentData, poi), the returned result is always a list of NA's. But apparently, they do intersect each other, as illustrated in the example. How do I get a valid returned list?

(2) The 'over' method only returns one line id that intersects a given POI (if any), but apparently given one POI there could be several segments that cross over it (e.g., POI located at intersection of 2 road segments). How do I query so that for a given POI, I wish to find all the segments that cross over it?

Illustrating example

Best Answer

(1) Your plot shows the points as small circles. In the computation, they are considered as real, zero-area points, and the lines are considered as zero-width lines. If the plot suggests they intersect, this may be because the lines have a certain width and points a certain area, but that their mathematical representations do not intersect. You could compute a small buffer around the points with rgeos::gBuffer to replace the points with small circles.

(2) You can use rgeos::gIntersects with argument byid = TRUE to find all intersections.