R/spClipPoint.R
spClipPoint.Rd
Wrapper for sf::st_intersection, to clip (intersect) point vector layer with a polygon vector layer.
spClipPoint(
xyplt,
xyplt_dsn = NULL,
uniqueid = "PLT_CN",
clippolyv,
clippolyv_dsn = NULL,
clippolyv.filter = NULL,
buffdist = NULL,
validate = FALSE,
showext = FALSE,
keepNA = FALSE,
returnsp = TRUE,
othertabnms = NULL,
stopifnotin = TRUE,
savedata = FALSE,
exportsp = FALSE,
spMakeSpatial_opts = NULL,
savedata_opts = NULL
)
sf R object or String. Point data to clip. Can be a spatial points object, full pathname to a shapefile, or name of a layer within a database.
String. Data source name (dsn; e.g., sqlite or shapefile pathname) of layer to clip. The dsn varies by driver. See gdal OGR vector formats (https://www.gdal.org/ogr_formats.html).
String.* Unique identifier of xyplt rows.
sf R object or String. Name of clipping polygon spatial polygon object, full path to shapefile, or name of a layer within a database.
String. Data source name (dsn; e.g., sqlite or shapefile pathname) of clipping polygon. The dsn varies by driver. See gdal OGR vector formats (https://www.gdal.org/ogr_formats.html).
String. Filter to subset clippolyv spatial layer.
Number. The distance to buffer the polygon before clipping. Uses sf::st_buffer. The distance is based on units of polygon, st_crs(x)$units.
Logical. If TRUE, validates polyv and clippolyv before clipping. Uses sf::st_make_valid with default parameters (geos_method='valid_structure', geos_keep_collapsed=FALSE).
Logical. If TRUE, layer extents are displayed in plot window.
Logical. If TRUE, keep NA values after data intersection.
Logical. If TRUE, returns sf object of points. If FALSE, returns data frame of points (i.e., drops sf geometry).
String vector. Name(s) of R objects, comma-delimited files, or database layers to subset. Must include quotes (e.g., othertabnms=c("tree", "cond")).
Logical. If TRUE, stops if boundaries do not overlap. If FALSE, returns NULL.
Logical. If TRUE, save data to outfolder.
Logical. If TRUE, the clipped spatial point data are exported.
List. See help(spMakeSpatial_options()) for a list of options. Use to convert X/Y values to simple feature (sf) coordinates.
List. See help(savedata_options()) for a list of options for saving data. If out_layer = NULL, default = 'pntclip'.
A list of the following objects:
sf object. The input xyplt, clipped to polygon boundary layer. The projection will be same as clippolyv projection.
String. Unique identifier of clip_xy.
SpatialPolygonsDataFrame. The polygon boundary layer used for clipping.
Data frame(s). Other tables in intabs clipped to boundary.
If exportsp=TRUE, the sf object will be written to out_dsn (See note).
The sf::st_intersection function is used to clip points.
If the projection of clippolyv is not the same as the xyplt, the xyplt layer layer will be reprojected to the same projection as the clippoly before intersection.
On-the-fly projection conversion
The spTransform (sf) method is
used for on-the-fly map projection conversion and datum transformation using
PROJ.4 arguments. Datum transformation only occurs if the +datum tag is
present in the both the from and to PROJ.4 strings. The +towgs84 tag is used
when no datum transformation is needed. PROJ.4 transformations assume NAD83
and WGS84 are identical unless other transformation parameters are
specified. Be aware, providing inaccurate or incomplete CRS information may
lead to erroneous data shifts when reprojecting. See spTransform help
documentation for more details.
ESRI Shapefile Driver
If exportsp=TRUE:
The st_write (sf) function
is called. If out_fmt="shp", the ESRI Shapefile driver truncates variable
names to 10 characters or less. Variable names are changed before export
using an internal function (trunc10shp). If sf object has more than 1
record, it will be returned but not exported.
# Get point data from WYplt data in FIESTA
WYplt <- FIESTA::WYplt
# Get polygon vector layer from FIESTA external data
WYbhdistfn <- system.file("extdata",
"sp_data/WYbighorn_districtbnd.shp",
package = "FIESTA")
# Extract points from polygon vector layer
xyext <- spClipPoint(xyplt = WYplt,
clippolyv = WYbhdistfn,
clippolyv.filter = "DISTRICTNU == '03'",
uniqueid = "CN",
spMakeSpatial_opts = list(xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
xy.crs = 4269))
#> DISTRICTNU == '03'
#> filter removed 2 records: DISTRICTNU == '03'
#> crs.default must be in a projected CRS... using US albers
#> +proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +type=crs
#> reprojecting layer...
#> from: EPSG:4269
#> to: +proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +type=crs
names(xyext)
#> [1] "clip_xyplt" "uniqueid" "clip_polyv"
xyplt <- xyext$clip_xyplt
polyv <- xyext$clip_polyv
# Plot extracted values of national forest district
plot(sf::st_geometry(polyv))
plot(sf::st_geometry(xyplt), add = TRUE)