sl.intersect: Diagnose Intersections of Line Segments

View source: R/sl.intersect.R

sl.intersectR Documentation

Diagnose Intersections of Line Segments

Description

Diagnose if and where line segments intersect, either within one set of line segments or across two sets, where the sets can be single or multiple concatenated line segments, including polygons.

Usage

sl.intersect(lon1,lat1,lon2=NULL,lat2=NULL,close1=FALSE,close2=FALSE,use.dist=TRUE,dist1=NULL,dist2=NULL,distmat=NULL,return.on.firsthit=FALSE)

Arguments

lon1

a numeric vector of length N1 > 1 specifying the longitudes of the points constituting the first set of line segments.

lat1

a numeric vector of length N1 > 1 specifying the latitudes of the points constituting the first set of line segments.

lon2

an optional numeric vector of length N2 > 1 specifying the longitudes of the points constituting the second set of line segments. If NULL (default), line segment crossings within the first set are diagnosed.

lat2

an optional numeric vector of length N2 > 1 specifying the latitudes of the points constituting the second set of line segments. Only used if lon2 != NULL.

close1

a logical value specifying whether or not to consider the first set as a polygon, that is, to connect the last and first points. Default is FALSE.

close2

a logical value specifying whether or not to consider the second set as a polygon, that is, to connect the last and first points. Default is FALSE.

use.dist

a logical value specifying whether or not to use segment lengths and point distances to exclude segments for which no intersection is possible a priori based on the criterion (first segment length + second segment length) / 2 <= min(point distance)). Default is TRUE.

dist1

a numeric vector of length N1-1 (or N1 if close1=TRUE) specifying the sequential distances (in radians) between consecutive pairs of points in the first set. Used only if use.dist=TRUE. If NULL, the distances are computed using sl.gc.dist.

dist2

a numeric vector of length N2-1 (or N2 if close2=TRUE) specifying the sequential distances (in radians) between consecutive pairs of points in the second set. Used only if use.dist=TRUE. If NULL, the distances are computed using sl.gc.dist.

distmat

an optional numeric N1xN2 (or N1xN1 if lon2=NULL) matrix specifying the distances (in radians) between all pairs of points between the two sets (or within the single set). Used only if use.dist=TRUE. If NULL, the distances are computed using sl.finddist.

return.on.firsthit

a logical value specifying whether or not to abort the function and return only the first intersection as soon as it occurs. Default is FALSE.

Details

This function uses sl.line.line.intersect to diagnose intersection points between pairs of line segments. However, if use.dist=TRUE (default), before invoking sl.line.line.intersect, the segment lengths and point distances are used to exclude segments for which no intersection is possible a priori based on the criterion (first segment length + second segment length) / 2 <= min(point distance)). This may speed up the algorithm considerably, in particular when distances are already available and provided as input. In contrast, if return.on.firsthit and no distances are available beforehand, use.dist=FALSE can be faster.

This functions considers only those line segments that constitute the shortest great-circle connection between two consecutive points. To obtain great-circle intersection points located on the longer great-circle of either segment, and/or x-y-z coordinates of intersection points directly, consider using sl.line.line.intersect or sl.line.polygon.intersect.

Value

A list with the following elements:

anylines.intersect

a logical value indicating whether any pair of line segments intersects.

lines.intersect

a numeric Mx2 matrix providing for all M intersecting pairs of line segments the corresponding indices for the first and second (or first and first if lon2=NULL) set, where index i corresponds to the segment connecting points i and i+1 (or i and 1 if i=NX and closeX=TRUE) within set X. NULL if anylines.intersect=FALSE.

lon

a numeric vector of length M providing the longitudes of the intersection points corresponding to each row of lines.intersect. NULL if anylines.intersect=FALSE.

lat

a numeric vector of length M providing the latitudes of the intersection points corresponding to each row of lines.intersect. NULL if anylines.intersect=FALSE.

Author(s)

Helge Goessling

See Also

sl.line.line.intersect, sl.line.polygon.intersect

Examples

lon1 = c(0,0,10,10)
lat1 = c(0,10,0,10)
lon2 = seq(3,11,2)
lat2 = c(5,0,1,2,5)

plot(lon1,lat1,xlim=c(0,11),ylim=c(0,10),type="b",xlab="longitude",ylab="latitude")
lines(lon2,lat2,type="b",col="red")

res = sl.intersect(lon1,lat1,lon2,lat2)
str(res)
## Should return:
## List of 4
##  $ anylines.intersect: logi TRUE
##  $ lines.intersect   : int [1, 1:2] 2 3
##  $ lon               : num 8.35
##  $ lat               : num 1.68

points(res$lon,res$lat,col="blue")

res.close2 = sl.intersect(lon1,lat1,lon2,lat2,close2=TRUE)
str(res.close2)
## Should return:
## List of 4
##  $ anylines.intersect: logi TRUE
##  $ lines.intersect   : int [1:4, 1:2] 2 2 3 3 3 5 4 5
##  $ lon               : num [1:4] 8.35 5.05 10 10
##  $ lat               : num [1:4] 1.68 5.01 3.5 5.01

lines(lon2[c(1,length(lon2))],lat2[c(1,length(lon2))],col="red",lty=2)
points(res.close2$lon,res.close2$lat,col="blue",pch=4)


FESOM/spheRlab documentation built on April 6, 2024, 6:52 p.m.