From a2cbc0e63636026bea4a05c964502b3a01e3736f Mon Sep 17 00:00:00 2001 From: Rob Marty Date: Tue, 15 Oct 2024 09:18:13 +0300 Subject: [PATCH] pricing --- DESCRIPTION | 2 +- R/gt_html_to_raster.R | 48 ++++++++++++++++++++++++++++ R/gt_make_png.R | 41 ++++++++++++++++++++---- README.md | 4 +-- make_readme_figures/testing.R | 4 +-- make_readme_figures/troubleshoot.R | 44 +++++++++++++++---------- vignettes/googletraffic-vignette.Rmd | 2 +- 7 files changed, 115 insertions(+), 30 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7dbca43..925fba2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: googletraffic Title: Google Traffic -Version: 0.1.6 +Version: 0.1.7 Authors@R: person("Robert", "Marty", , "rmarty@worldbank.org", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-3164-3813")) diff --git a/R/gt_html_to_raster.R b/R/gt_html_to_raster.R index 305a997..49521c7 100644 --- a/R/gt_html_to_raster.R +++ b/R/gt_html_to_raster.R @@ -53,6 +53,7 @@ gt_html_to_raster <- function(filename, dir.create(filename_dir) file.create(file.path(filename_dir, paste0(filename_only,".png")) %>% str_replace_all("\\\\", "/")) + #### Approach 1 suppressWarnings({ webshot2::webshot(url = filename, file = file.path(filename_dir, paste0(filename_only,".png")) %>% str_replace_all("\\\\", "/"), @@ -63,9 +64,56 @@ gt_html_to_raster <- function(filename, zoom = webshot_zoom) # change }) + #### Approach 2 + ## Check if the PNG is blank + img <- png::readPNG(file.path(filename_dir, paste0(filename_only,".png"))) + + if (all(img == 1)) { + + chrome_path <- Sys.which("chrome") + if (chrome_path != "") { + system2(chrome_path, + args = c("--headless", "--disable-gpu", "--screenshot", + paste0("--window-size=", width, ",", height), + "--default-background-color=0", + filename_html), + stdout = TRUE, + stderr = TRUE) + + # Move the screenshot to the desired location + file.rename("screenshot.png", out_filename) + + } + } + #### Load as raster and image png_filename <- file.path(filename_dir, paste0(filename_only, ".png")) %>% str_replace_all("\\\\", "/") + #### Approach 2 + ## Check if the PNG is blank + img <- png::readPNG(png_filename) + + a <<- img + + if (all(img == 1)) { + + chrome_path <- Sys.which("chrome") + if (chrome_path != "") { + system2(chrome_path, + args = c("--headless", "--disable-gpu", "--screenshot", + paste0("--window-size=", width, ",", height), + "--default-background-color=0", + filename_html), + stdout = TRUE, + stderr = TRUE) + + # Move the screenshot to the desired location + file.rename("screenshot.png", out_filename) + + } + } + + #### PNG to Raster r <- gt_load_png_as_traffic_raster(filename = png_filename, location = c(latitude, longitude), height = height, diff --git a/R/gt_make_png.R b/R/gt_make_png.R index 19a4f35..44bf26b 100644 --- a/R/gt_make_png.R +++ b/R/gt_make_png.R @@ -66,17 +66,44 @@ gt_make_png <- function(location, stringr::str_replace_all(filename_only, "") %>% stringr::str_sub(end = -2) + message("test123") + if(print_progress){ message(paste0("Pausing for ", webshot_delay, " seconds to allow traffic data to render")) } - webshot2::webshot(url = filename_html, - file = file.path(filename_dir, paste0(filename_only,".png")), - vheight = height, - vwidth = width, - cliprect = "viewport", - delay = webshot_delay, - zoom = webshot_zoom) + #### Approach 1 + suppressWarnings({ + webshot2::webshot(url = filename_html, + file = file.path(filename_dir, paste0(filename_only,".png")), + vheight = height, + vwidth = width, + cliprect = "viewport", + delay = webshot_delay, + zoom = webshot_zoom) + }) + + #### Approach 2 + ## Check if the PNG is blank + img <- png::readPNG(file.path(filename_dir, paste0(filename_only,".png"))) + + if (all(img == 1)) { + + chrome_path <- Sys.which("chrome") + if (chrome_path != "") { + system2(chrome_path, + args = c("--headless", "--disable-gpu", "--screenshot", + paste0("--window-size=", width, ",", height), + "--default-background-color=0", + filename_html), + stdout = TRUE, + stderr = TRUE) + + # Move the screenshot to the desired location + file.rename("screenshot.png", file.path(filename_dir, paste0(filename_only,".png"))) + + } + } ## Read/Write png to file img <- png::readPNG(file.path(filename_dir, paste0(filename_only,".png"))) diff --git a/README.md b/README.md index 30b7a09..378d99a 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Create Georeferenced Traffic Data from the [Google Maps Javascript API](https:// Google Maps [displays information about traffic conditions](https://www.google.com/maps/@38.909083,-77.0249335,12.07z/data=!5m1!1e1) across an area. This package provides functions to produce georeferenced rasters from real-time Google Maps traffic information. Having Google traffic information in a georeferenced data format facilitates analysis of traffic information (e.g., spatially merging traffic information with other data sources). -This package was inspired by (1) existing research that has used Google traffic information, including in [New York City](https://www.sciencedirect.com/science/article/pii/S0048969721034070) and [Dar es Salaam](https://openknowledge.worldbank.org/handle/10986/33445), and (2) similar algorithms implemented in [JavaScript](https://ieeexplore.ieee.org/abstract/document/8326831) and in a [C shell script](https://arxiv.org/abs/2105.12235). +This package was inspired by (1) existing research that has used Google traffic information, including in [New York City](https://doi.org/10.1016/j.scitotenv.2021.148336) and [Dar es Salaam](https://openknowledge.worldbank.org/handle/10986/33445), and (2) similar algorithms implemented in [JavaScript](https://ieeexplore.ieee.org/abstract/document/8326831) and in a [C shell script](https://arxiv.org/abs/2105.12235).