Skip to content

Commit

Permalink
pricing
Browse files Browse the repository at this point in the history
  • Loading branch information
ramarty committed Oct 15, 2024
1 parent 0070d7b commit a2cbc0e
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 30 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: googletraffic
Title: Google Traffic
Version: 0.1.6
Version: 0.1.7
Authors@R:
person("Robert", "Marty", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-3164-3813"))
Expand Down
48 changes: 48 additions & 0 deletions R/gt_html_to_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -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("\\\\", "/"),
Expand 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,
Expand Down
41 changes: 34 additions & 7 deletions R/gt_make_png.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

<!--- The below image shows an example raster produced using the package showing [traffic within Washington, DC.](https://www.google.com/maps/@38.9098813,-77.0406205,15.01z/data=!5m1!1e1)
Expand Down Expand Up @@ -55,7 +55,7 @@ devtools::install_github("dime-worldbank/googletraffic")

## API Key <a name="apikey"></a>

Querying Google traffic information requires a Google API key with the [Maps Javascript API](https://developers.google.com/maps/documentation/javascript/overview) enabled. To create a Google API key, [follow these instructions](https://developers.google.com/maps/get-started#create-project).
Querying Google traffic information requires a Google API key with the [Maps Javascript API](https://developers.google.com/maps/documentation/javascript/overview) enabled. To create a Google API key, [follow these instructions](https://developers.google.com/maps/get-started#create-project). For information on Google Maps API pricing, see [here](https://mapsplatform.google.com/pricing/); 1,000 queries to the Maps JavaScript API (for Dynamic Maps) costs \$7. Google Maps provides a \$200 monthly credit; consequently, 28,571 queries can be made within the \$200 credit amount each month.

## Quickstart <a name="quickstart"></a>

Expand Down
4 changes: 2 additions & 2 deletions make_readme_figures/testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ gt_make_png(location = c(40.717437418183884, -73.99145764250052),
google_key = google_key)

r <- gt_make_raster(location = c(40.717437418183884, -73.99145764250052),
height = 200,
width = 200,
height = 600,
width = 600,
zoom = 16,
google_key = google_key)

Expand Down
44 changes: 27 additions & 17 deletions make_readme_figures/troubleshoot.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@
#library(googletraffic)

# Setup ------------------------------------------------------------------------
library(dplyr)
library(googleway)
library(htmlwidgets)
library(plotwidgets)
library(png)
library(sf)
library(sp)
library(stringr)
library(webshot2)
library(raster)
library(ColorNameR)
library(schemr)

git_dir <- "~/Documents/Github/googletraffic/R/"
source(file.path(git_dir, "gt_mosaic.R"))
Expand All @@ -27,20 +38,19 @@ google_key_df <- api_keys_df |>
google_key <- google_key_df$Key

# Make raster ------------------------------------------------------------------
r <- googletraffic::gt_make_raster(location = c(40.712778, -74.006111),
height = 800,
width = 800,
zoom = 16,
#webshot_delay = 20,
google_key = google_key)
r

# Make png ---------------------------------------------------------------------
gt_make_png(location = c(40.712778, -74.006111),
height = 1000,
width = 1000,
zoom = 16,
out_filename = "~/Desktop/new_folder/hello/google_traffic.png",
google_key = google_key)


r <- gt_make_raster(location = c(40.712778, -74.006111),
height = 600,
width = 600,
zoom = 15,
google_key = google_key)
# r
#
# # Make png ---------------------------------------------------------------------
# gt_make_png(location = c(40.712778, -74.006111),
# height = 1000,
# width = 1000,
# zoom = 16,
# out_filename = "~/Desktop/new_folder/hello/google_traffic.png",
# google_key = google_key)
#
#
2 changes: 1 addition & 1 deletion vignettes/googletraffic-vignette.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ knitr::opts_chunk$set(

Google Maps displays information about traffic conditions 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).

The below image shows an example raster produced using the package showing [traffic within Washington, DC.](https://www.google.com/maps/@38.9098813,-77.0406205,15.01z/data=!5m1!1e1)

Expand Down

0 comments on commit a2cbc0e

Please sign in to comment.