Skip to content

Commit

Permalink
improved logic of generate ahn point and renamed it to its name (get …
Browse files Browse the repository at this point in the history
…rectified grid)
  • Loading branch information
Jellest committed Apr 11, 2024
1 parent 962d818 commit 164b10c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
^\.vscode$
^\.github$
^Packaging$
^\.Rbuildignore$
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ena.Rproj.user
.Rproj.user
.Rhistory
.RData
.Ruserdata
Expand All @@ -19,3 +19,5 @@ deprecated/
.vscode
docs
.Rproj.user
builds/
.gitignore
2 changes: 1 addition & 1 deletion R/ahn_point.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ 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 <- generate_ahn_point(name = name_trim, X = X, Y = Y, LONLAT = LONLAT, resolution = my_resolution$res)
my_point <- get_rectified_grid(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)
Expand Down
35 changes: 21 additions & 14 deletions R/generate_ahn_point.R → R/get_rectified_grid.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#'@inheritParams ahn_area
#'@noRd
generate_ahn_point <- function(name = "", X, Y, LONLAT = FALSE, resolution) {
get_rectified_grid <- function(name = "", X, Y, LONLAT = FALSE, resolution) {
#RD new coordinaten systeem
my_point <- create_spatialpoint(X = X, Y = Y, LONLAT = LONLAT)

Expand All @@ -12,23 +12,27 @@ generate_ahn_point <- function(name = "", X, Y, LONLAT = FALSE, resolution) {
stop("Y coordinate out of range.")
}

##create 9 pixels bbox coordinates
## round number to whole number unless if decimal is 0.5
#round down if decimal digit is lower than 5 (0.5-)
#round up if decimal digit greater than 5 (0.5+)
#don't round if decimal digit = 0.5

#round number
#always round 0.5 to 1
rounding <- function(x, digits) {
posneg <- sign(x)
z <- abs(x) * 10^digits
z <- z + 0.5
z <- trunc(z)
z <- z / 10^digits
z * posneg
rounding <- function(x) {
rounded_value <- round(x, 1) # First round to 1 decimal digit
# Check if the rounded value is exactly 0.5
if (abs(rounded_value - floor(rounded_value)) == 0.5) {
z <- rounded_value # If it's exactly 0.5, retain the rounded value
} else {
z <- round(x) # Otherwise, round using the round() function
}
return(z)
}

#round
xround <- rounding(coords[1, "X"], digits = 0)
yround <- rounding(coords[1, "Y"], digits = 0)
#round to nearest x,y pixel bbox value to make middle pixel
xround <- rounding(coords[1, "X"])
yround <- rounding(coords[1, "Y"])

## create 8 pixel around middle pixel
if (resolution == 0.5) {
#x coordinate
if (coords[1, "X"] - xround > 0) {
Expand Down Expand Up @@ -62,6 +66,9 @@ generate_ahn_point <- function(name = "", X, Y, LONLAT = FALSE, resolution) {
} else {
stop("No correct WCS resolution is provided. Please try again.")
}

bbox <- data.frame("xmin" = my_xmin, "xmax" = my_xmax, "ymin" = my_ymin, "ymax" = my_ymax)
return(list("name" = name, "point" = my_point, "bbox" = bbox))
}

get_rectified_grid(X = 150000.6, Y = 450000.3, resolution = 0.5)

0 comments on commit 164b10c

Please sign in to comment.