Skip to content

Commit e90acaa

Browse files
authored
Merge pull request nationalparkservice#88 from RobLBaker/master
add function validate_coord_list
2 parents 0ea5a16 + 9b72b55 commit e90acaa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+311
-236
lines changed

DESCRIPTION

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: QCkit
22
Type: Package
33
Title: NPS Inventory and Monitoring Quality Control Toolkit
4-
Version: 0.1.5
4+
Version: 0.1.6
55
Authors@R: c(
66
person(given = "Robert",
77
family = "Baker",
@@ -20,6 +20,9 @@ Authors@R: c(
2020
family = "Quevedo",
2121
role = "aut",
2222
comment = c(ORCID = "0000-0003-0129-981X")),
23+
person(given = "Sarah",
24+
family = "Kelson",
25+
role = "ctb"),
2326
person(given = "Amy",
2427
family = "Sherman",
2528
role = "ctb",

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ export(long2UTM)
2121
export(order_cols)
2222
export(replace_blanks)
2323
export(te_check)
24-
export(test_coord)
2524
export(utm_to_ll)
2625
export(validate_coord)
26+
export(validate_coord_list)
2727
importFrom(lifecycle,deprecated)
2828
importFrom(magrittr,"%>%")
2929
importFrom(stats,sd)

NEWS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
# QCkit v0.1.6
12
2024-03-07
23
* Update error warning in `check_te()` to not reference VPN since NPS no longer uses VPN.
4+
* add private function `.get_unit_bondaries()`: hits ArcGIS API to pull more precise park unit boundaries than `get_park_polgyon()`
5+
* add `validate_coord_list()` function that takes advantage of improved precision of `.get_unit_boundaries()` and is vectorized, enabling users to input multiple coordinate combinations and park units directly from a data frame.
36

47
# QCkit v0.1.5
58
2024-02-09
69
* This version adds the DRR template, example files, and associated documentation to the QCkit package.
7-
* Bugfix in `get_custom_flag()`: it was counting both A (accepted) and AE (Accepted, estiamted) as Accepted. Fixed the regex such that it Accepted will include all cells that start with A followed by nothing or by any character except AE such that flags can have explanation codes added to them (e.g. A_jenkins if "Jenkins" flagged the data as accepted)
10+
* Bugfix in `get_custom_flag()`: it was counting both A (accepted) and AE (Accepted, estimated) as Accepted. Fixed the regex such that it Accepted will include all cells that start with A followed by nothing or by any character except AE such that flags can have explanation codes added to them (e.g. A_jenkins if "Jenkins" flagged the data as accepted)
811

912
# QCkit v0.1.4
1013
2024-01-23

R/geography.R

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,58 @@
1-
21
#' Test whether decimal GPS coordinates are inside a park unit
32
#'
4-
#' @param lat numeric. An individual or vector of numeric values representing the decimal degree latitude of a coordinate
5-
#' @param lon numeric. An individual or vector of numeric values representing the decimal degree longitude of a coordinate
6-
#' @param park_unit String. Or list of strings each containin the four-letter park unit designation
3+
#' This function can take a list of coordinates and park units as input. In
4+
#' adddition to being vectorized, depending on the park borders, it can be a
5+
#' major improvement on `validate_coord()`.
6+
#'
7+
#' @param lat numeric. An individual or vector of numeric values representing
8+
#' the decimal degree latitude of a coordinate
9+
#' @param lon numeric. An individual or vector of numeric values representing
10+
#' the decimal degree longitude of a coordinate
11+
#' @param park_units String. Or list of strings each containing the four letter
12+
#' park unit designation
713
#'
8-
#' @return dataframe
14+
#' @return logical
915
#' @export
1016
#'
1117
#' @examples
1218
#' \dontrun{
13-
#' test_coord(105.555, -47.4332, "DRTO")
19+
#' x <- validate_coord_list(lat = 105.555, long = -47.4332, park_units = "DRTO")
20+
#'
21+
#' # or a dataframe with many coordinates and potentially many park units:
22+
#' x <- validate_coord_list(lat = df$decimalLatitutde,
23+
#' lon = df$decimalLongitude,
24+
#' park_units = df$park_units)
25+
#' # you can then merge it back in to the original dataframe:
26+
#' df$test_GPS_coord <- x
1427
#' }
15-
test_coord <- function(lat, lon, park_unit) {
16-
17-
# get geography from NPS Rest Services
18-
park <- .get_unit_boundary(park_unit)
28+
validate_coord_list <- function(lat, lon, park_units) {
29+
# get geography from ArcGIS Rest Services
30+
park <- .get_unit_boundary(park_units)
1931

32+
#create a multipolygon sf object
33+
valid_park <- sf::st_make_valid(park)
2034
# create sf dataframe from coordinates
2135
points_df <- data.frame(lon = lon, lat = lat)
22-
points <- sf::st_as_sf(points_df, coords = c("lon", "lat"), crs = 4326, na.fail = FALSE)
23-
36+
points <- sf::st_as_sf(points_df,
37+
coords = c("lon", "lat"),
38+
crs = 4326,
39+
na.fail = FALSE)
2440
# Test whether the coordinates provided are within the polygon spatial feature
25-
results <- sf::st_covers(park, points, sparse = FALSE)
26-
27-
# convert dataframe from wide to long format
28-
results_long <- tidyr::gather(results)
29-
30-
# drop key column
31-
results_long <- dplyr::select(results_long, -key)
32-
33-
# combine coordinates and results into a dataframe
34-
result_df <- data.frame(lon = lon, lat = lat, in_polygon = results_long)
35-
36-
37-
return(invisible(result_df))
41+
results <- as.data.frame(sf::st_covers(valid_park,
42+
points,
43+
sparse = FALSE))
44+
# Turn data frame into a list of logicals
45+
colnames(results) <- park_units
46+
rownames(results) <- unique(park_units)
47+
in_park <- NULL
48+
for (i in seq(ncol(results))) {
49+
for (j in seq(nrow(results))) {
50+
if (colnames(results)[i] == rownames(results)[j]) {
51+
in_park <- append(in_park, results[j,i])
52+
}
53+
}
54+
}
55+
return(in_park)
3856
}
3957

4058
#' Gets NPS unit boundaries from Arc GIS
@@ -57,14 +75,17 @@ test_coord <- function(lat, lon, park_unit) {
5775

5876
for (locality in unique_localities) {
5977
# Request feature in WGS84 spatial reference (outSR=4326)
60-
feature_service_path <- paste0('query?where=UNIT_CODE+%3D+%27', locality, '%27&outFields=*&returnGeometry=true&outSR=4326&f=pjson')
78+
feature_service_path <- paste0(
79+
"query?where=UNIT_CODE+%3D+%27",
80+
locality,
81+
"%27&outFields=*&returnGeometry=true&outSR=4326&f=pjson")
6182
feature_service_request <- paste(feature_service_url,
6283
feature_service_path,
6384
sep = "/")
6485
geo_json_feature <- jsonlite::fromJSON(feature_service_request)
6586

6687
# Have to save to temp file
67-
jsonFeature <- utils::download.file(feature_service_request,
88+
json_feature <- utils::download.file(feature_service_request,
6889
temp_output,
6990
mode = "w",
7091
quiet = TRUE)
@@ -74,14 +95,11 @@ test_coord <- function(lat, lon, park_unit) {
7495

7596
all_localities <- rbind(all_localities, feature_polygon)
7697
}
77-
file.remove("temp.geojson")
98+
suppressWarnings(file.remove("temp.geojson"))
7899
#featurePoly <- readOGR(dsn = tempOutput, layer = "OGRGeoJSON")
79100
return(all_localities)
80-
81101
}
82102

83-
84-
85103
#' Retrieve the polygon information for the park unit from NPS REST services
86104
#'
87105
#' @description `get_park_polygon()` retrieves a geoJSON string for a polygon of
@@ -133,7 +151,6 @@ validate_coord <- function(unit_code, lat, lon) {
133151

134152
# Test whether the coordinates provided are within the polygon spatial feature
135153
result <- sf::st_covers(park, point, sparse = FALSE)[, 1]
136-
137154
return(result)
138155
}
139156

docs/404.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/LICENSE-text.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/LICENSE.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/DRR_Purpose_and_Scope.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/Starting-a-DRR.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/Using-the-DRR-Template.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/index.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/authors.html

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/news/index.html

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ articles:
55
DRR_Purpose_and_Scope: DRR_Purpose_and_Scope.html
66
Starting-a-DRR: Starting-a-DRR.html
77
Using-the-DRR-Template: Using-the-DRR-Template.html
8-
last_built: 2024-03-21T19:40Z
8+
last_built: 2024-03-25T20:40Z
99

docs/reference/DC_col_check.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/QCkit-package.html

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/check_dc_cols.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/check_te.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/convert_datetime_format.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/convert_long_to_utm.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/convert_utm_to_ll.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/create_datastore_script.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)