Skip to content

Commit

Permalink
testing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
denironyx committed Jun 16, 2024
1 parent 453fc31 commit 5a0cb5c
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 56 deletions.
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@

export(dataset_path)
export(get_all_overture_types)
export(record_batch_reader)
importFrom(arrow,open_dataset)
importFrom(dplyr,"%>%")
importFrom(dplyr,collect)
importFrom(dplyr,filter)
importFrom(sf,st_as_sf)
101 changes: 101 additions & 0 deletions R/core.R
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)
54 changes: 4 additions & 50 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,44 +1,7 @@
#' Title
#'
#' @param overture_type
#'
#' @return
#' @export
#'
#' @examples
dataset_path <- function(overture_type) {
type_theme_map = list(
"locality" = "admins",
"locality_area" = "admins",
"administrative_boundary" = "admins",
"building" = "buildings",
"building_part" = "buildings",
"division" = "divisions",
"division_area" = "divisions",
"place" = "places",
"segment" = "transportation",
"connector" = "transportation",
"infrastructure" = "base",
"land" = "base",
"land_cover" = "base",
"land_use" = "base",
"water" = "base"
)

theme = type_theme_map[[overture_type]]
return(paste0("s3://overturemaps-us-west-2/release/2024-05-16-beta.0/theme=",theme, "?region=us-west-2"))
}


# Define the mapping of overture types to their themes

#' get_all_overture_types
#'
#' @return
#' @export
#'
#' @examples
get_all_overture_types <- function() {
type_theme_map = list(
all_type_theme_map <- function(){
type_theme_map = list(
"locality" = "admins",
"locality_area" = "admins",
"administrative_boundary" = "admins",
Expand All @@ -55,14 +18,5 @@ get_all_overture_types <- function() {
"land_use" = "base",
"water" = "base"
)

return(names(type_theme_map))
}


record_batch_reader <- function(overture_type, bbox = NULL) {
path <- dataset_path(overture_type)

filter_ex
return(type_theme_map)
}

19 changes: 15 additions & 4 deletions man/dataset_path.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions man/get_all_overture_types.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions man/record_batch_reader.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/testthat/test-1-core.R
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)
})

0 comments on commit 5a0cb5c

Please sign in to comment.