-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
195 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
|
||
#' get_all_overture_types | ||
#' | ||
#' This function returns all of the overturemaps theme types. | ||
#' | ||
#' @return Character vector. All overturemaps theme types. | ||
#' | ||
#' @note The theme types are important for fetching data from the S3 bucket, | ||
#' as they indicate if you are fetching places, buildings, admin, etc. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' # Example usage | ||
#' types <- get_all_overture_types() | ||
#' print(types) | ||
#' } | ||
get_all_overture_types <- function() { | ||
return(names(all_type_theme_map())) | ||
} | ||
|
||
|
||
#' dataset_path | ||
#' | ||
#' This function returns the S3 path for the specified Overture dataset type. | ||
#' | ||
#' @param overture_type Character. Required. The type of feature to select. | ||
#' Examples include 'building', 'place', etc. To learn more, run \code{get_all_overture_types()}. | ||
#' | ||
#' @return Character. The S3 path to the bucket where the data is stored. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' # Example usage | ||
#' path <- dataset_path('place') | ||
#' print(path) | ||
#' } | ||
dataset_path <- function(overture_type) { | ||
|
||
# Get the theme based on the overture_type | ||
type_theme_map = all_type_theme_map() | ||
theme = type_theme_map[[overture_type]] | ||
|
||
# Return the S3 path | ||
return(paste0("s3://overturemaps-us-west-2/release/2024-05-16-beta.0/theme=",theme, "?region=us-west-2")) | ||
} | ||
|
||
|
||
|
||
#' record_batch_reader | ||
#' | ||
#' This function retrieves a filtered dataset from the specified Overture dataset type, | ||
#' optionally within a bounding box, and converts it to an `sf` object. | ||
#' | ||
#' @param overture_type Character. Required. The type of feature to select. Examples include 'building', 'place', etc. | ||
#' To learn more, run \code{get_all_overture_types()}. | ||
#' @param bbox Numeric vector. Optional. A bounding box specified as c(xmin, ymin, xmax, ymax). | ||
#' It is recommended to use a bounding box to limit the dataset size and processing time. | ||
#' Without a bounding box, processing the entire dataset (e.g., buildings over 2 billion) can be time-consuming. | ||
#' | ||
#' @return An `sf` object containing the filtered dataset based on the bounding box. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' # Example usage with a bounding box | ||
#' sf_bbox <- c(-122.5, 37.7, -122.3, 37.8) | ||
#' result <- record_batch_reader(overture_type = 'place', bbox = sf_bbox) | ||
#' print(result) | ||
#' } | ||
#' | ||
#' @importFrom arrow open_dataset | ||
#' @importFrom sf st_as_sf | ||
#' @importFrom dplyr %>% filter collect | ||
record_batch_reader <- function(overture_type, bbox = NULL){ | ||
|
||
# Open the dataset based on the overture_type | ||
dataset <- open_dataset(dataset_path(overture_type)) | ||
|
||
if(!is.null(bbox)){ | ||
filtered_df <- open_dataset(dataset_path(overture_type)) %>% | ||
filter(bbox$xmin > bbox[1], | ||
bbox$ymin > bbox[2], | ||
bbox$xmax < bbox[3], | ||
bbox$ymax < bbox[4]) %>% | ||
collect() %>% | ||
st_as_sf(crs = 4326) | ||
} else { | ||
# collect the entire dataset if bbox is not provided (not recommended) | ||
filtered_df <- dataset %>% | ||
collect() | ||
} | ||
|
||
return(filtered_df) | ||
} | ||
|
||
# Example usage (uncomment to test) | ||
# sf_bbox <- c(-122.5, 37.7, -122.3, 37.8) | ||
# result <- record_batch_reader(overture_type = 'place', bbox = sf_bbox) | ||
# print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
|
||
|
||
test_that("get_all_overture_types returns all overture types", { | ||
expected_types <- names(all_type_theme_map()) | ||
result <- get_all_overture_types() | ||
|
||
expect_type(result, "character") | ||
expect_equal(sort(result), sort(expected_types)) | ||
expect_length(result, length(expected_types)) | ||
}) | ||
|
||
test_that("dataset_path returns correct S3 path for a given type", { | ||
test_type <- "building" | ||
expected_path <- paste0("s3://overturemaps-us-west-2/release/2024-05-16-beta.0/theme=buildings?region=us-west-2") | ||
|
||
result <- dataset_path(test_type) | ||
expect_type(result, "character") | ||
expect_equal(result, expected_path) | ||
}) | ||
|