Learn R Programming

spatialEco (version 1.3-7)

extract.vertices: Extract vertices for polygons or lines

Description

Extracts [x,y] vertices from an sp line or polygon object

Usage

extract.vertices(x, as.sp = FALSE, rm.duplicates = FALSE, join = FALSE)

Value

A SpatialPointsDataFrame or data.frame with id, x, y and merged attributes

Arguments

x

An sp class SpatialPolygonsDataFrame, SpatialPolygons, SpatialLinesDataFrame or SpatialLines object

as.sp

(FALSE/TRUE) Output as sp SpatialPointsDataFrame

rm.duplicates

(FALSE/TRUE) remove duplicate (x,y) coordinates

join

(FALSE/TRUE) Joint attributes from original object

Author

Jeffrey S. Evans <jeffrey_evans@tnc.org>

Examples

Run this code
library(sp)
library(raster)
library(sf)

# For polygons
r <- raster(xmn=-11.69, xmx=2988.31, ymn=-749.97, ymx=1650.03,
            resolution=c(100,100))
  r[] <- runif(ncell(r))
    names(r) <- "random_process"
  
polys <- as(r, "SpatialPolygonsDataFrame")
  polys <- polys[sample(1:nrow(polys),10),]

extract.vertices(polys, join=TRUE, rm.duplicates=TRUE)

v <- extract.vertices(polys, as.sp=TRUE, join=TRUE)
  head(v@data)
  
  plot(polys)
    points(v, pch=20, cex=2, col="red")

# For lines
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))
  nc <- sf::st_cast(sf::st_cast(nc, "POLYGON"), "LINESTRING")
    nc <- as(nc, "Spatial")
 
extract.vertices(nc)
extract.vertices(nc, join=TRUE, rm.duplicates=TRUE)

v <- extract.vertices(nc, as.sp=TRUE, join=TRUE)
  head(v@data)

  plot(nc)
    points(v, pch=20, cex=2, col="red")

Run the code above in your browser using DataLab