points_in_polygon: Filtering SpatialPoints* inside a SpatialPolygon*

Description Usage Arguments Value Examples

View source: R/points_in_polygon.R

Description

Filtering the points of a SpatialPoints* object that are inside a SpatialPolygon* object.

Usage

1

Arguments

points

SpatialPoints* object as defined in the package sp.

polygon

SpatialPolygon* object as defined in the package sp.

Value

A SpatialPoints* object of which all the points are inside the inputed SpatialPolygon* object.

Examples

 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
library(sf)

# SpatialPolygonsDataFrame
vn <- sf::as_Spatial(gadmVN::gadm(level = "country"))
# SpatialPoints
stations <- as(imhen::stations, "Spatial")
length(stations)


stations2 <- points_in_polygon(stations, vn)
length(stations2)
sp::plot(vn)
sp::plot(stations, add = TRUE, col = "red")
sp::plot(stations2, add = TRUE, col = "blue")
# The 2 stations that are excluded should obviously not be excluded.

# Let's add a 5-km buffer zone around the coast:
vnp <- sp::spTransform(vn, projVN)
vnp2 <- rgeos::gBuffer(vnp, width = 5000)
vn2 <- sp::spTransform(vnp2, proj0)
sp::plot(vn2, border = "green", add = TRUE)
# Let's now see how many stations are discarded:
stations3 <- points_in_polygon(stations, vn2)
length(stations3)


# Let's now see with a simplified version of the polygon:
vn3 <- largest_polygons(vn)
stations4 <- points_in_polygon(stations, vn3)
length(stations4)
sp::plot(vn3)
sp::plot(stations, add = TRUE, col = "red")
sp::plot(stations4, add = TRUE, col = "blue")

# With the 5-km buffer zone:
vn4 <- sp::spTransform(vn3, projVN)
vn4 <- rgeos::gBuffer(vn4, width = 5000)
vn4 <- sp::spTransform(vn4, crs(vn))

sp::plot(vn4, border = "green", add = TRUE)
stations5 <- points_in_polygon(stations, vn4)
length(stations5)
sp::plot(vn4)
sp::plot(stations, add = TRUE, col = "red")
sp::plot(stations5, add = TRUE, col = "blue")

choisy/sptools documentation built on Aug. 22, 2019, 12:57 p.m.