From cef5f75d6fbdd880d7ced9e8f0a230324d86da6e Mon Sep 17 00:00:00 2001 From: Mike Mahoney Date: Tue, 20 Oct 2020 22:17:59 -0400 Subject: [PATCH] Run styler --- R/classes.R | 6 ++- R/get_tiles.R | 56 +++++++++++++-------- R/merge_rasters.R | 32 +++++++----- R/raster_to_raw_tiles.R | 25 +++++---- tests/testthat/test-1-raster_to_raw_tiles.R | 7 +-- tests/testthat/test-classes.R | 6 ++- 6 files changed, 81 insertions(+), 51 deletions(-) diff --git a/R/classes.R b/R/classes.R index 1da70bf..33b458e 100644 --- a/R/classes.R +++ b/R/classes.R @@ -188,8 +188,10 @@ terrainr_bounding_box <- function(bl, tr, coord_units = "degrees") { bl <- tr tr <- swap } else { - stop("The given coordinates don't seem to be ", - "the bottom left and upper right corners.") + stop( + "The given coordinates don't seem to be ", + "the bottom left and upper right corners." + ) } methods::new("terrainr_bounding_box", bl = bl, diff --git a/R/get_tiles.R b/R/get_tiles.R index 798ca87..041cb30 100644 --- a/R/get_tiles.R +++ b/R/get_tiles.R @@ -119,7 +119,7 @@ get_tiles <- function(bbox, replacements <- which(services %in% names(list_of_services)) services[replacements] <- as.vector( list_of_services[services[replacements]] - ) + ) } services <- unique(services) @@ -145,9 +145,11 @@ get_tiles <- function(bbox, tl <- terrainr::terrainr_coordinate_pair(c(bbox@tr@lat, bbox@bl@lng)) img_width <- round(terrainr::calc_haversine_distance(tl, bbox@tr), - digits = 0) + digits = 0 + ) img_height <- round(terrainr::calc_haversine_distance(tl, bbox@bl), - digits = 0) + digits = 0 + ) x_tiles <- ceiling(img_width / side_length) y_tiles <- ceiling(img_height / side_length) @@ -159,31 +161,43 @@ get_tiles <- function(bbox, for (i in 1:x_tiles) { if (i == x_tiles) { - left_lng <- terrainr::point_from_distance(bbox@bl, - side_length * (i - 1), - 90)@lng + left_lng <- terrainr::point_from_distance( + bbox@bl, + side_length * (i - 1), + 90 + )@lng right_lng <- bbox@tr@lng } else { - left_lng <- terrainr::point_from_distance(bbox@bl, - side_length * (i - 1), - 90)@lng - right_lng <- terrainr::point_from_distance(bbox@bl, - side_length * i, - 90)@lng + left_lng <- terrainr::point_from_distance( + bbox@bl, + side_length * (i - 1), + 90 + )@lng + right_lng <- terrainr::point_from_distance( + bbox@bl, + side_length * i, + 90 + )@lng } for (j in 1:y_tiles) { if (j == y_tiles) { - top_lat <- terrainr::point_from_distance(bbox@tr, - side_length * (j - 1), - 180)@lat + top_lat <- terrainr::point_from_distance( + bbox@tr, + side_length * (j - 1), + 180 + )@lat bot_lat <- bbox@bl@lat } else { - top_lat <- terrainr::point_from_distance(bbox@tr, - side_length * (j - 1), - 180)@lat - bot_lat <- terrainr::point_from_distance(bbox@tr, - side_length * j, - 180)@lat + top_lat <- terrainr::point_from_distance( + bbox@tr, + side_length * (j - 1), + 180 + )@lat + bot_lat <- terrainr::point_from_distance( + bbox@tr, + side_length * j, + 180 + )@lat } tile_boxes[[i]][[j]] <- list( diff --git a/R/merge_rasters.R b/R/merge_rasters.R index 2bf942f..0272871 100644 --- a/R/merge_rasters.R +++ b/R/merge_rasters.R @@ -109,18 +109,26 @@ merge_rasters <- function(input_rasters, } total_extent <- raster::raster(raster::extent( - min(vapply(input_raster_objects, - function(x) raster::extent(x)@xmin, - numeric(1))), - max(vapply(input_raster_objects, - function(x) raster::extent(x)@xmax, - numeric(1))), - min(vapply(input_raster_objects, - function(x) raster::extent(x)@ymin, - numeric(1))), - max(vapply(input_raster_objects, - function(x) raster::extent(x)@ymax, - numeric(1))) + min(vapply( + input_raster_objects, + function(x) raster::extent(x)@xmin, + numeric(1) + )), + max(vapply( + input_raster_objects, + function(x) raster::extent(x)@xmax, + numeric(1) + )), + min(vapply( + input_raster_objects, + function(x) raster::extent(x)@ymin, + numeric(1) + )), + max(vapply( + input_raster_objects, + function(x) raster::extent(x)@ymax, + numeric(1) + )) )) raster::projection(total_extent) <- raster::projection(input_raster_objects[[1]]) # nolint diff --git a/R/raster_to_raw_tiles.R b/R/raster_to_raw_tiles.R index 685fe82..0981409 100644 --- a/R/raster_to_raw_tiles.R +++ b/R/raster_to_raw_tiles.R @@ -35,7 +35,6 @@ raster_to_raw_tiles <- function(input_file, output_prefix, side_length = 4097, raw = TRUE) { - input_raster <- raster::raster(input_file) max_raster <- raster::cellStats(input_raster, "max") @@ -66,18 +65,22 @@ raster_to_raw_tiles <- function(input_file, for (i in seq_along(x_tiles)) { for (j in seq_along(y_tiles)) { if (any(grepl("progressr", utils::installed.packages()))) { - p(message = sprintf("Cropping tile (%d,%d)", - x_tiles[[i]], - y_tiles[[j]])) + p(message = sprintf( + "Cropping tile (%d,%d)", + x_tiles[[i]], + y_tiles[[j]] + )) } gdalUtils::gdal_translate(input_file, temptiffs[[counter]], - srcwin = paste0(x_tiles[[i]], - ", ", - y_tiles[[j]], - ", ", - side_length, - ", ", - side_length) + srcwin = paste0( + x_tiles[[i]], + ", ", + y_tiles[[j]], + ", ", + side_length, + ", ", + side_length + ) ) names(temptiffs)[[counter]] <- paste0( output_prefix, diff --git a/tests/testthat/test-1-raster_to_raw_tiles.R b/tests/testthat/test-1-raster_to_raw_tiles.R index d1e5f39..9a31954 100644 --- a/tests/testthat/test-1-raster_to_raw_tiles.R +++ b/tests/testthat/test-1-raster_to_raw_tiles.R @@ -1,5 +1,4 @@ test_that("raster_to_raw runs without errors", { - outputs <- raster_to_raw_tiles( input_file = "testdata/merge_rasters_test.tif", output_prefix = tempfile(), @@ -7,8 +6,10 @@ test_that("raster_to_raw runs without errors", { raw = FALSE ) - expect_equal(png::readPNG(outputs[[1]]), - png::readPNG("testdata/raster_to_raw_1.png")) + expect_equal( + png::readPNG(outputs[[1]]), + png::readPNG("testdata/raster_to_raw_1.png") + ) expect_error( raster_to_raw_tiles( diff --git a/tests/testthat/test-classes.R b/tests/testthat/test-classes.R index dc9d2ea..463c37a 100644 --- a/tests/testthat/test-classes.R +++ b/tests/testthat/test-classes.R @@ -2,8 +2,10 @@ test_that("classes are stable", { expect_warning(terrainr_coordinate_pair(c(44.04905, -74.01188))) expect_equal( terrainr_coordinate_pair(c(lat = 44.04905, lng = -74.01188)), - terrainr_coordinate_pair(deg_to_rad(c(lat = 44.04905, lng = -74.01188)), - "radians") + terrainr_coordinate_pair( + deg_to_rad(c(lat = 44.04905, lng = -74.01188)), + "radians" + ) ) expect_equal(