-
Notifications
You must be signed in to change notification settings - Fork 0
/
intersectPoly.R
54 lines (48 loc) · 1.63 KB
/
intersectPoly.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#' @title Intersect of polygons
#'
#' @description Creates intersected polygons of catchment against Voronoi polygons
#'
#' @param voronoi Voronoi/Thiessen polygon
#' @param catchment Catchment shapefile
#' @param coords Coordinates of rain gauges set as a polygon shapefile
#'
#' @return The calculated intersections of a polygon
#' @export
#'
#' @importFrom sf st_cast
#' @importFrom sf st_intersection
#' @importFrom sf st_sf
#' @importFrom sf st_join
#' @importFrom sf st_nearest_feature
#'
#' @examples
#' library(riskyData)
#' data(crowle); data(bickley); data(barnhurst); data(hollies); data(ledbury);
#' data(bettwsYCrwyn); data(bewdCatch)
#'
#' # Obtain gauge coordinates
#' gcs <- getCoords(crowle,
#' bickley,
#' barnhurst,
#' hollies,
#' ledbury,
#' bettwsYCrwyn)
#'
#' bewdTeeSun <- teeSun(gaugeCoords = gcs, catchment = bewdCatch)
#' int <- intersectPoly(coords = gcs,
#' voronoi = bewdTeeSun,
#' catchment = bewdCatch)
#' int
intersectPoly <- function(voronoi, catchment, coords){
## Simplify geometry
cast <- sf::st_cast(voronoi)
## Return all non-empty intersections between voronoi and catchment polygon
intersect <- sf::st_intersection(cast, catchment)
## Split multipolygons using the first feature
intersect <- sf::st_cast(intersect, "POLYGON", do_split = FALSE)
## Convert to sf
intersect_sf <- sf::st_sf(intersect, crs = sf::st_crs(27700))
## Join between intersected polygons and raingauge coordinates
join <- sf::st_join(intersect_sf, coords, join = st_nearest_feature)
return(join)
}