as.points,GRaster-method | R Documentation |
as.points()
converts a GRaster
, or a lines or polygons GVector
to a points GVector
.
For GRasters
, the points have the coordinates of cell centers and are assigned the cells' values. Only non-NA
cells will be converted to points.
For GVectors
, each point will have the attributes of the line or polygon to which it belonged. Points are extracted from each vertex.
## S4 method for signature 'GRaster'
as.points(x, values = TRUE)
## S4 method for signature 'GVector'
as.points(x)
x |
A |
values |
Logical: If |
A points
GVector
.
crds()
, as.lines()
, as.polygons()
, terra::as.points()
, and modules v.to.points
and r.to.vect
in GRASS
if (grassStarted()) {
# Setup
library(sf)
library(terra)
# Elevation raster, outline of a part of Madagascar, and rivers vector:
madElev <- fastData("madElev")
madCoast0 <- fastData("madCoast0")
madRivers <- fastData("madRivers")
# Convert to GRaster and GVectors:
elev <- fast(madElev)
coast <- fast(madCoast0)
rivers <- fast(madRivers)
# Extract points from raster. To make this fast, we will first crop the raster
# to a small extent defined by a single river.
river <- rivers[1]
elevCrop <- crop(elev, river)
elevPoints <- as.points(elevCrop)
elevPoints
plot(elevCrop)
plot(elevPoints, pch = '.', add = TRUE)
# Extract points from vectors:
coastPoints <- as.points(coast)
riversPoints <- as.points(rivers)
plot(coast)
plot(coastPoints, add = TRUE)
plot(rivers, col = "blue", add = TRUE)
plot(riversPoints, col = "blue", add = TRUE)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.