Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R ah nextract upgrade2024 #12

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
^\.github$
^Packaging$
^\.Rbuildignore$
^data-raw$
^builds$

3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ inst/doc
tests/
scratch/
AHN_output/
Rcode
my_output/
README_cache
Packaging/
Expand All @@ -20,4 +19,4 @@ deprecated/
docs
.Rproj.user
builds/
.gitignore
.gitignore
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Type: Package
Package: rAHNextract
Title: Extract elevation points or areas in the Netherlands from the AHN
Version: 0.98
Date: 11-04-2024
Date: 16-04-2024
Author: Jelle Stuurman
Maintainer: Jelle Stuurman <[email protected]>
Description: Extract elevation points or raster areas in the Netherlands
Expand Down
27 changes: 14 additions & 13 deletions R/ahn_area.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#'@title AHN raster area
#'@description Get AHN raster area of a specified area.
#'
#'AHN Area is retrieved through a circle (x, y and radius), BBOX (x, y and radius, OR bbox coordinates), or own geometry (polygon).
#'AHN Area is retrieved through a circle (x, y and radius), BBOX (x, y and radius, OR BBOX coordinates), or own geometry (polygon).
#'
#'AHN data is obtained from the AHN3 (default), AHN2 or AHN 1 from the available resolutions.
#'
#'Available for the Digital Surface Model (DSM) and Digital Terrain Model (DTM).
#'
#'You can download the AHN data using the WCS method (default, \code{sheets.method = FALSE}) returning a GeoTIFF format. The sheets method (\code{sheets.method = TRUE}) returns a regular raster GeoTIFF output file that is extracted from the PDOK sheets. The WCS method is recommended if only a few AHN elevation points need to be extracted. The sheets method always requires more data to be downloaded to the client but may be more efficient if many elevatiomns need to be retrieved from a small area. Choosing your method depends on speed and your desired output format. See documentation for all available parameters.
#'You can download the AHN data using the WCS method (default, \code{sheets.method = FALSE}) returning a GeoTIFF format. The sheets method (\code{sheets.method = TRUE}) returns a regular raster GeoTIFF output file that is extracted from the PDOK sheets. The WCS method is recommended if only a few AHN elevation points need to be extracted. The sheets method always requires more data to be downloaded to the client but may be more efficient if many elevations need to be retrieved from a small area. Choosing your method depends on speed and your desired output format. See documentation for all available parameters.
#'@param name Optional. Give a name of the specified area. This name will be used in the output file names.
#'@param X Required. X coordinate in RD New or WGS84 (LON).
#'@param Y Required. Y coordinate in RD New or WGS84 (LAT).
Expand All @@ -21,8 +21,8 @@
#'@param sheets Only required when only full AHN sheets need to be downloaded. To do so, fill the list() object with strings of the sheet numbers.
#'@param LONLAT Optional. Default FALSE. Set to TRUE if X and Y are in Longitude and Latitude format. Output will be in RD New format.
#'@param sheets.method Default FALSE. When TRUE, it downloads AHN area through the available GeoTIFF AHN sheets available on [PDOK](https://www.pdok.nl/atom-downloadservices/-/article/actueel-hoogtebestand-nederland-ahn).
#'@param sheets.dir Default is the 'AHN_sheets' directory in the working directory or \code{output.dir} (when defined). In this sheets directory all the AHN sheets are downloaded. If sheets already exists in this directory, they will be reused and no redownload will take place. Within this sheets directory, a directory structure will be created automatically: if `sheets.dir` is set to 'myFolder', all sheets will be downloaded in their apropriate AHN version and DEM folder. Eg. '\code{output.dir}/myFolder/AHN_sheets/AHN4/DSM'. You want to use this parameter if you want to have a central location where all the AHN sheets are stored so that they can be accessed elsewhere for other purposes. It is recommended to always use the their original file name after download. Due to the size of these GeoTiff sheets, it is not allowed to download this is the RAM memory (tempdir).
#'@param sheets.keep Default TRUE. Only applicable if \code{sheets.method} is set to TRUE and sheets were downloaded. Set to FALSE if you want to delete the downloaded sheet. It is recommended to keep the sheets if ahn elevation extarction will be followed.
#'@param sheets.dir Default is the 'AHN_sheets' directory in the working directory or \code{output.dir} (when defined). In this sheets directory all the AHN sheets are downloaded. If sheets already exists in this directory, they will be reused and no redownload will take place. Within this sheets directory, a directory structure will be created automatically: if `sheets.dir` is set to 'myFolder', all sheets will be downloaded in their appropriate AHN version and DEM folder. Eg. '\code{output.dir}/myFolder/AHN_sheets/AHN4/DSM'. You want to use this parameter if you want to have a central location where all the AHN sheets are stored so that they can be accessed elsewhere for other purposes. It is recommended to always use the their original file name after download. Due to the size of these GeoTiff sheets, it is not allowed to download this is the RAM memory (tempdir).
#'@param sheets.keep Default TRUE. Only applicable if \code{sheets.method} is set to TRUE and sheets were downloaded. Set to FALSE if you want to delete the downloaded sheet. It is recommended to keep the sheets if ahn elevation extraction will be followed.
#'@author Jelle Stuurman
#'@return GeoTIFF file of AHN area
#'@export
Expand All @@ -43,7 +43,7 @@ ahn_area <- function(name = "AHNarea", output.dir, X, Y, radius, bbox, polygon,
output.dir <- tempdir()
} else {
if (dir.exists(output.dir) == FALSE) {
dir.create(output.dir)
dir.create(output.dir, recursive = TRUE)
print(paste(output.dir, "directory was not found and was created.", sep = " "))
}
#print(output.dir)
Expand All @@ -69,7 +69,7 @@ ahn_area <- function(name = "AHNarea", output.dir, X, Y, radius, bbox, polygon,

if (length(sheets) == 0) {
#get and create area
ahn_area <- create_area(X = X, Y = Y, radius = radius, bbox = bbox, polygon = polygon, LONLAT = LONLAT, type = "raster")
ahn_area <- create_area(name = name_trim, X = X, Y = Y, resolution = my_resolution, radius = radius, bbox = bbox, polygon = polygon, LONLAT = LONLAT, type = "raster")
} else {
if (!is.list(sheets)) {
stop("No list is provided as the input for the 'sheets' parameter. Please use a list() with the needed sheet nrs.")
Expand All @@ -84,9 +84,10 @@ ahn_area <- function(name = "AHNarea", output.dir, X, Y, radius, bbox, polygon,
if (sheets.method == FALSE) {
#use WCS method and get data (fast)
wcs_source <- bladIndex.sf$wcs_url[1]
wcs_url <- create_wcs_url(bbox = ahn_area$bbox, type = "area", AHN = AHN, resolution = my_resolution, dem = dem, interpolate = interpolate, wcs = wcs_source)
wcs_url <- create_wcs_url(bbox = ahn_area$rectified_bbox, type = "area", AHN = AHN, resolution = my_resolution, dem = dem, interpolate = interpolate, wcs = wcs_source, LONLAT = LONLAT)
raster_data <- download_wcs_raster(wcsUrl = wcs_url, name = name_trim, AHN = AHN, dem = tolower(dem), radius = radius, resolution = my_resolution, interpolate = interpolate, output.dir = output.dir, type = "raster")
ahn_data <- terra::mask(x = raster_data$data, mask = ahn_area$area, filename = raster_data$fileName, overwrite = TRUE)
unlink(raster_data$data)
output <- ahn_data
if (output.dir == tempdir()) {
base::unlink(ahn_data)
Expand Down Expand Up @@ -114,21 +115,21 @@ ahn_area <- function(name = "AHNarea", output.dir, X, Y, radius, bbox, polygon,
if (dir.exists(ahn_sheets_directory) == FALSE) {
dir.create(ahn_sheets_directory, showWarnings = FALSE)
}
print(sprintf("The AHN sheets are loaded from or downloaded into: %s. If no AHN sheet(s) are found in this directory or if no correct name of AHN sheet(s) are found, the AHN sheet(s) will be downloaded.", ahn_sheets_directory))
print(sprintf("The AHN sheets are loaded from or downloaded into: %s. If no AHN sheet(s) are found in this directory or if no correct name of AHN sheet(s) are found, the AHN sheet(s) will be downloaded here.", ahn_sheets_directory))

#download AHN sheets and get data (slow)
sheets_df <- data.frame(kaartBlad = character(), filePath = character(), dwnld = logical(), stringsAsFactors = FALSE)
sheets_df <- data.frame(kaartBlad = character(), filePath = character(), downloaded = logical(), stringsAsFactors = FALSE)
if (length(sheets) == 0) {
#get ahn area from ahn sheets
bladnrs <- find_ahn_sheets(name = name_trim, area = ahn_area, type = "area", bladIndex = bladIndex.sf)
bladnrs <- find_ahn_sheets(name = name_trim, area = ahn_area$area, type = "area", bladIndex = bladIndex.sf)
#print(bladnrs)
for (b in bladnrs) {
url <- bladIndex.sf$atom_url[bladIndex.sf$kaartbladNr == b]
print(url)
path_sheet <- download_ahn_sheets(name = name_trim, AHN = AHN, dem = dem, url = url, output.dir = output.dir, sheets.dir = ahn_sheets_directory, sheets.keep = sheets.keep)
sheets_df <- rbind(sheets_df, path_sheet)
}
ahn_data <- merge_and_crop_ahn(name = name_trim, sheets = sheets_df, area = ahn_area, AHN = AHN, dem = tolower(dem), resolution = my_resolution$res_name, output.dir = output.dir)
ahn_data <- merge_and_crop_ahn(name = name_trim, sheets = sheets_df, area = ahn_area$area, AHN = AHN, dem = tolower(dem), resolution = my_resolution$res_name, output.dir = output.dir)

if (LONLAT == TRUE) {
warning("The input geometry was provided using Longitude and Latitude coordinates. The output is exported as a raster using the the RD New (epsg 28992) coordinate system.")
Expand All @@ -148,13 +149,13 @@ ahn_area <- function(name = "AHNarea", output.dir, X, Y, radius, bbox, polygon,
sheets_df <- rbind(sheets_df, path_sheet)
}
if (LONLAT == TRUE) {
warning("The input geometry was provided using Longitude and Latitude coordinates. The downloaded sheet(s) are geotif files with the RD New cordinate system.")
warning("The input geometry was provided using Longitude and Latitude coordinates. The downloaded sheet(s) are geotiff files with the RD New coordinate system.")
}
bladen <- data.frame(filePath = character(), stringsAsFactors = FALSE)
for (d in seq_along(sheets_df$kaartBlad)) {
bladen[d, "filePath"] <- sheets_df[d, "filePath"]
if (sheets_df[d, "downloadedNow"] == TRUE) {
print(paste("The AHN sheet", sheets_df[d, "kaartBlad"], "in", sheets_df[d, "filePath"], "is downloaded succesfully.", sep = " "))
print(paste("The AHN sheet", sheets_df[d, "kaartBlad"], "in", sheets_df[d, "filePath"], "is downloaded successfully.", sep = " "))
}
}
output <- bladen$filePath
Expand Down
18 changes: 9 additions & 9 deletions R/ahn_point.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#'
#'Available for the Digital Surface Model (DSM) and Digital Terrain Model (DTM).
#'
#'You can download the AHN data using the WCS method (default, sheets.method = FALSE) returning a GeoTIFF format. The sheets method (sheets.method = TRUE) returns a regular raster GeoTIFF output file that is extracted from the PDOK sheets. The WCS method is recommended if only a few AHN elevation points need to be extracted. The sheets method always requires more data to be downloaded to the client but may be more efficient if many elevatiomns need to be retrieved from a small area. Choosing your method depends on speed and your desired output format. See documentation for all available parameters.
#'You can download the AHN data using the WCS method (default, sheets.method = FALSE) returning a GeoTIFF format. The sheets method (sheets.method = TRUE) returns a regular raster GeoTIFF output file that is extracted from the PDOK sheets. The WCS method is recommended if only a few AHN elevation points need to be extracted. The sheets method always requires more data to be downloaded to the client but may be more efficient if many elevations need to be retrieved from a small area. Choosing your method depends on speed and your desired output format. See documentation for all available parameters.
#'@inheritParams ahn_area
#'@param decimals Default 2. Maximum Determines the number of decimal places in the output elevation.
#'@param extract.method Default 'simple'. Choose between 'simple' (nearest) and 'bilinear'. Intersection is done using [\code{extract()}](https://www.rdocumentation.org/packages/terra/versions/1.7-71/topics/extract) function from the \code{terra} package. See [this](https://gisgeography.com/raster-resampling/) article that explains the difference between the two methods.
Expand Down Expand Up @@ -42,15 +42,15 @@ ahn_point <- function(name = "AHNelevation", X, Y, AHN = "AHN", dem, resolution,
dem <- get_dem(AHN = AHN, resolution = my_resolution, dem = dem, interpolate = interpolate)

#get and create a point
my_point <- get_rectified_grid(name = name_trim, X = X, Y = Y, LONLAT = LONLAT, resolution = my_resolution$res)
my_point <- get_rectified_grid_for_point(name = name_trim, X = X, Y = Y, LONLAT = LONLAT, resolution = my_resolution$res)

#get AHN data
bladIndex.sf <- get_bladindex(AHN = AHN, dem = dem, resolution = my_resolution$res)
if (sheets.method == FALSE) {
##get elevation through WCS method (fast)
wcs_source <- bladIndex.sf$wcs_url[1]
wcs_url <- create_wcs_url(type = "point", bbox = my_point$bbox, AHN = AHN, dem = dem, resolution = my_resolution, interpolate = interpolate, wcs = wcs_source)
raster_data <- download_wcs_raster(wcsUrl = wcs_url, name = name_trim, area = ahn_area, AHN = AHN, dem = tolower(dem), resolution = my_resolution, output.dir = output.dir, interpolate = interpolate, type = "point")
wcs_url <- create_wcs_url(type = "point", bbox = my_point$rectified_bbox, AHN = AHN, dem = dem, resolution = my_resolution, interpolate = interpolate, wcs = wcs_source, LONLAT = LONLAT)
raster_data <- download_wcs_raster(wcsUrl = wcs_url, name = name_trim, AHN = AHN, dem = tolower(dem), resolution = my_resolution, output.dir = output.dir, interpolate = interpolate, type = "point")
my_elevation <- extract_elevation(raster_data$data, my_point$point, extract.method = extract.method)
} else if (sheets.method == TRUE) {
#download AHN sheets and get data (slow)
Expand Down Expand Up @@ -81,23 +81,23 @@ ahn_point <- function(name = "AHNelevation", X, Y, AHN = "AHN", dem, resolution,
print(sprintf("The AHN sheets are loaded from or downloaded in: %s. If no AHN sheet in the correct directory or if no correct name of AHN sheet is found, sheet will be downloaded. For first use it is recommended to use the default output directory.", ahn_sheets_directory))

#create area
ahn_area <- create_area(radius = "", bbox = my_point$bbox, LONLAT = LONLAT, type = "point")
ahn_area <- create_area(radius = "", bbox = my_point$rectified_bbox, resolution = my_resolution, LONLAT = LONLAT, type = "point")

#download AHN sheets and get data (slow)
bladnrs <- find_ahn_sheets(name, area = ahn_area, type = "point", bladIndex = bladIndex.sf)
bladnrs <- find_ahn_sheets(name, area = ahn_area$area, type = "point", bladIndex = bladIndex.sf)
#get AHN area
sheets_df <- data.frame(kaartBlad = character(), filePath = character(), dwnld = logical(), stringsAsFactors = FALSE)
sheets_df <- data.frame(kaartBlad = character(), filePath = character(), downloaded = logical(), stringsAsFactors = FALSE)
for (b in bladnrs) {
url <- bladIndex.sf$atom_url[bladIndex.sf$kaartbladNr == b]
path_sheet <- download_ahn_sheets(name = name_trim, AHN = AHN, dem = dem, url = url, output.dir = output.dir, sheets.dir = ahn_sheets_directory, sheets.keep = sheets.keep)
sheets_df <- rbind(sheets_df, path_sheet)
}
raster_data <- merge_and_crop_ahn(name = name_trim, sheets = sheets_df, area = ahn_area, AHN = AHN, dem = tolower(dem), resolution = my_resolution$res_name, output.dir = output.dir)
raster_data <- merge_and_crop_ahn(name = name_trim, sheets = sheets_df, area = ahn_area$area, AHN = AHN, dem = tolower(dem), resolution = my_resolution$res_name, output.dir = output.dir)

#get elevation
my_elevation <- extract_elevation(raster_data$data, my_point$point, extract.method = extract.method)
} else {
stop("Ncorrect value is provided for the sheets.method parameter. Please set it to 'TRUE' or 'FALSE'.")
stop("No correct value is provided for the sheets.method parameter. Please set it to 'TRUE' or 'FALSE'.")
}

if (is.na(my_elevation)) {
Expand Down
4 changes: 2 additions & 2 deletions R/ahn_sheets_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#'@author Jelle Stuurman
ahn_sheets_info <- function(AHN, dem, resolution, output.dir) {
if (missing(AHN) == TRUE) {
stop("No value provided for the AHN prameter. Please enter a valid AHN parameter.")
stop("No value provided for the AHN parameter. Please enter a valid AHN parameter.")
}
check_ahn_version(AHN)

Expand All @@ -15,7 +15,7 @@ ahn_sheets_info <- function(AHN, dem, resolution, output.dir) {
}

if (missing(dem) == TRUE) {
stop("No value provided for the dem prameter. Please enter a valid dem parameter.")
stop("No value provided for the dem parameter. Please enter a valid dem parameter.")
}

ahn_bi <- get_bladindex(AHN = AHN, dem = dem, resolution = my_resolution$res)
Expand Down
4 changes: 2 additions & 2 deletions R/check_ahn_version.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#'@description check the version of AHN that is called and whehter it is available within this package.
#'@description check the version of AHN that is called and whether it is available within this package.
#'@inheritParams ahn_area
#'@noRd
check_ahn_version <- function(AHN) {
Expand All @@ -18,7 +18,7 @@ check_ahn_version <- function(AHN) {
this_ahn <- paste0("AHN", ahn_version)

if (!toupper(this_ahn) %in% pdok_versions) {
stop(" The AHN version found is currently not supoorted at the moment with this package.")
stop(" The AHN version found is currently not supported at the moment with this package.")
} else {
AHN <- toupper(this_ahn)
if (note == TRUE) {
Expand Down
Loading