Learn R Programming

elevatr (version 0.99.0)

get_elev_point: Get Point Elevation

Description

This function provides access to point elevations using either the USGS Elevation Point Query Service (US Only) or by extracting point elevations from the AWS Terrain Tiles. The function accepts a data.frame of x (long) and y (lat) or a sf POINT or MULTIPOINT object as input. A sf POINT or MULTIPOINT object is returned with elevation and elevation units as an added data.frame.

Usage

get_elev_point(
  locations,
  prj = NULL,
  src = c("epqs", "aws"),
  overwrite = FALSE,
  ...
)

Value

Function returns an sf object in the projection specified by the prj argument.

Arguments

locations

Either a data.frame with x (e.g. longitude) as the first column and y (e.g. latitude) as the second column, a SpatialPoints/SpatialPointsDataFrame, or a sf POINT or MULTIPOINT object. Elevation for these points will be returned in the originally supplied class.

prj

A valid input to st_crs. This argument is required for a data.frame of locations and optional for sf locations.

src

A character indicating which API to use, either "epqs" or "aws" accepted. The "epqs" source is relatively slow for larger numbers of points (e.g. > 500). The "aws" source may be quicker in these cases provided the points are in a similar geographic area. The "aws" source downloads a DEM using get_elev_raster and then extracts the elevation for each point.

overwrite

A logical indicating that existing elevation and elev_units columns should be overwritten. Default is FALSE and get_elev_point will error if these columns already exist.

...

Additional arguments passed to get_epqs or get_aws_points. When using "aws" as the source, pay attention to the `z` argument. A defualt of 5 is used, but this uses a raster with a large ~4-5 km pixel. Additionally, the source data changes as zoom levels increase. Read https://github.com/tilezen/joerd/blob/master/docs/data-sources.md#what-is-the-ground-resolution for details.

Examples

Run this code
if (FALSE) {
library(elevatr)
library(sf)
library(terra)

mts <- data.frame(x = c(-71.3036, -72.8145), 
                  y = c(44.2700, 44.5438), 
                  names = c("Mt. Washington", "Mt. Mansfield"))
ll_prj <- 4326
mts_sf <- st_as_sf(x = mts, coords = c("x", "y"), crs = ll_prj)
#Empty Raster
mts_raster <- rast(mts_sf, nrow = 5, ncol = 5)
# Raster with cells for each location
mts_raster_loc <- terra::rasterize(mts_sf, rast(mts_sf, nrow = 10, ncol = 10))

get_elev_point(locations = mts, prj = ll_prj)
get_elev_point(locations = mts, units="feet", prj = ll_prj)
get_elev_point(locations = mts_sf)
get_elev_point(locations = mts_raster)
get_elev_point(locations = mts_raster_loc)


# Code to split into a loop and grab point at a time.
# This is usually faster for points that are spread apart 
 
library(dplyr)

elev <- vector("numeric", length = nrow(mts))
for(i in seq_along(mts)){
elev[i]<-get_elev_point(locations = mts[i,], prj = ll_prj, src = "aws", 
                        z = 10)$elevation}
mts_elev <- cbind(mts, elev)
mts_elev
}

Run the code above in your browser using DataLab