This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.2 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tmap)
library(sf)
## Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(tidycensus)
library(jsonlite)
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:purrr':
##
## flatten
library(httr)
library(jsonlite)
library(reshape2)
##
## Attaching package: 'reshape2'
##
## The following object is masked from 'package:tidyr':
##
## smiths
library(here)
## here() starts at C:/Users/akaamah3/Documents/SCaRP Course Materials_Fall2025/Into_to_Urban_Analytics/CP8883_working_with_R
library(knitr)
COLLECTING POI DATA USING GOOGLE PLACES API
Choose two place types for the “includedTypes” parameter in the Google Places Nearby Search API. Refer to the API documentation for options.
Select a U.S. city. Use the “tigris” package to obtain an “sf” object of the city boundary. Avoid large cities (e.g., Atlanta) to prevent excessive API calls.
Divide the city into small areas using one of the following methods and prepare “locationRestriction” parameter values (longitude, latitude, and radius) for each area: Method 1: Census Block Groups (BGs) that intersect the city polygon Method 2: Fishnet grid cells based on the city polygon
Verify coverage to ensure there are no uncovered areas by visualizing your locationRestriction parameters on a map: Create a POINT “sf” object using longitude, latitude, and sf::st_as_sf(). Create buffers using radius and sf::st_buffer(). Build an interactive map displaying (1) the city boundary and (2) the buffer objects with the tmap package.
Collect POI data using the Nearby Search in the Google Places API. Include two place types in the includedTypes parameter. For FieldMask, include these 8 fields: places.id; places.displayName; places.formattedAddress; places.location; places.types; places.priceLevel; places.rating; places.userRatingCount You may also add other fields of interest, but these 8 must be included. Parse the API response into a data frame. Export the POI dataset into an RData or RDS file.
Map the collected POIs Convert the POI dataset into an sf object using sf::st_as_sf(). Create an interactive map showing both the POIs and the city boundary.
Answer the following questions at the end of your script: What city did you choose? Which two place types did you select? How many rows does your dataset contain? Upon visual inspection, do you notice any spatial patterns in how POIs are distributed across the city? (Optional) Did you observe any other interesting findings?
Working with R Markdown
You may copy and paste the instructions above into your script. Keep your document readable, well-formatted, and concise: Avoid making the document unnecessarily lengthy by printing or plotting everything. Add comments before and/or inside each code chunk (using #) to explain what your code is doing. Remember that every time you click Knit, the entire script runs. Be cautious, as this will directly impact your API usage.
Submission Guidelines Write your report and R code in an R Markdown (.Rmd) script. Use the Knit button in RStudio to render the document as HTML. Publish the rendered HTML to RPubs. Submit the URL of your RPubs document through Canvas by 9/19/2025 Friday 11:59 PM:
# Get block groups
bg <- suppressMessages(
tidycensus::get_acs(geography = "block group", # or "block", "tract", "county", "state" etc.
state = "GA",
county = c("Clarke County"),
variables = c(hhincome = 'B19013_001'),
year = 2023,
survey = "acs5", # we typically use American Community Survey 5-year estimates
geometry = TRUE, # we need an sf object
output = "wide") # wide vs. long
)
## | | | 0% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |=== | 4% | |=== | 5% | |==== | 5% | |==== | 6% | |===== | 7% | |===== | 8% | |====== | 9% | |======= | 10% | |======== | 11% | |======== | 12% | |========= | 12% | |========= | 13% | |========== | 14% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============ | 16% | |============ | 17% | |============= | 18% | |============= | 19% | |============== | 20% | |============== | 21% | |=============== | 21% | |=============== | 22% | |================ | 22% | |================ | 23% | |================= | 24% | |================= | 25% | |================== | 25% | |================== | 26% | |=================== | 27% | |=================== | 28% | |==================== | 28% | |==================== | 29% | |===================== | 29% | |===================== | 30% | |====================== | 31% | |====================== | 32% | |======================= | 32% | |======================= | 33% | |======================== | 34% | |======================== | 35% | |========================= | 35% | |========================= | 36% | |========================== | 37% | |========================== | 38% | |=========================== | 38% | |=========================== | 39% | |============================ | 40% | |============================ | 41% | |============================= | 41% | |============================= | 42% | |============================== | 42% | |============================== | 43% | |=============================== | 44% | |=============================== | 45% | |================================ | 45% | |================================ | 46% | |================================= | 47% | |================================= | 48% | |================================== | 48% | |================================== | 49% | |=================================== | 50% | |=================================== | 51% | |==================================== | 51% | |==================================== | 52% | |===================================== | 52% | |===================================== | 53% | |====================================== | 54% | |====================================== | 55% | |======================================= | 55% | |======================================= | 56% | |======================================== | 57% | |======================================== | 58% | |========================================= | 58% | |========================================= | 59% | |========================================== | 60% | |========================================== | 61% | |=========================================== | 61% | |=========================================== | 62% | |============================================ | 63% | |============================================= | 64% | |============================================= | 65% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 67% | |=============================================== | 68% | |================================================ | 68% | |================================================ | 69% | |================================================= | 70% | |================================================= | 71% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 72% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |====================================================== | 78% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 80% | |======================================================== | 81% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 82% | |========================================================== | 83% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 87% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 89% | |=============================================================== | 90% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 93% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 97% | |==================================================================== | 98% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 100%
# City of Winterville boundary
Winterville <- tigris::places('GA') %>% filter(NAME == 'Winterville')
## Retrieving data for the year 2024
## | | | 0% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |=== | 4% | |=== | 5% | |==== | 5% | |==== | 6% | |===== | 7% | |===== | 8% | |====== | 8% | |====== | 9% | |======= | 9% | |======= | 10% | |======= | 11% | |======== | 11% | |======== | 12% | |========= | 12% | |========= | 13% | |========== | 14% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============ | 17% | |============ | 18% | |============= | 18% | |============= | 19% | |============== | 19% | |============== | 20% | |=============== | 21% | |=============== | 22% | |================ | 22% | |================ | 23% | |================= | 24% | |================= | 25% | |================== | 25% | |================== | 26% | |=================== | 27% | |=================== | 28% | |==================== | 28% | |==================== | 29% | |===================== | 30% | |===================== | 31% | |====================== | 31% | |====================== | 32% | |======================= | 32% | |======================= | 33% | |======================== | 34% | |======================== | 35% | |========================= | 35% | |========================= | 36% | |========================== | 36% | |========================== | 37% | |========================== | 38% | |=========================== | 38% | |=========================== | 39% | |============================ | 39% | |============================ | 40% | |============================ | 41% | |============================= | 41% | |============================= | 42% | |============================== | 42% | |============================== | 43% | |============================== | 44% | |=============================== | 44% | |=============================== | 45% | |================================ | 45% | |================================ | 46% | |================================= | 47% | |================================= | 48% | |================================== | 48% | |================================== | 49% | |=================================== | 50% | |=================================== | 51% | |==================================== | 51% | |==================================== | 52% | |===================================== | 52% | |===================================== | 53% | |====================================== | 54% | |====================================== | 55% | |======================================= | 55% | |======================================= | 56% | |======================================== | 57% | |======================================== | 58% | |========================================= | 58% | |========================================= | 59% | |========================================== | 59% | |========================================== | 60% | |=========================================== | 61% | |=========================================== | 62% | |============================================ | 62% | |============================================ | 63% | |============================================= | 64% | |============================================= | 65% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 66% | |=============================================== | 67% | |=============================================== | 68% | |================================================ | 68% | |================================================ | 69% | |================================================= | 69% | |================================================= | 70% | |================================================= | 71% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 72% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |====================================================== | 78% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 79% | |======================================================== | 80% | |======================================================== | 81% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 82% | |========================================================== | 83% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 87% | |============================================================= | 88% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 89% | |=============================================================== | 90% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 92% | |================================================================= | 93% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 97% | |==================================================================== | 98% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 99% | |======================================================================| 100%
# Get BGs intersecting with the City of Winterville boundary
bg_Winterville <- bg[Winterville,]
Method 1: Census Block Groups (BGs) that intersect the city polygon Method 2: Fishnet grid cells based on the city polygon Verify coverage to ensure there are no uncovered areas by visualizing your locationRestriction parameters on a map:
# Function: Get XY coordinates and radius
getXYRadius <- function(polygon, gcs_id, pcs_id){
# Transform the CRS to PCS.
if (st_crs(polygon) != st_crs(pcs_id)){
polygon <- polygon %>% st_transform(pcs_id)
}
# Get bounding box of a given polygon
bb <- st_bbox(polygon)
# Get XY coordinates of any one corner of the bounding box.
bb_corner <- st_point(c(bb[1], bb[2])) %>% st_sfc(crs = pcs_id)
# Get centroid of the bb
bb_center <- bb %>% st_as_sfc() %>% st_centroid()
# Get the distance between bb_center and bb_corner
r <- st_distance(bb_corner, bb_center)
# Convert the CRS of centroid to GCS.
bb_center <- bb_center %>% st_transform(gcs_id)
# Get longitude and latitude
xy <- bb_center %>% st_coordinates() %>% as.vector()
lon_lat_radius <- data.frame(x = xy[1],
y = xy[2],
r = r)
return(lon_lat_radius)
}
We can apply this function to each BG.
# Define EPSG codes for GCS (WGS 84) and PCS (WGS 84 / UTM zone 16N)
gcs_id <- 4326
pcs_id <- 32616
# Pre-allocate a data frame. Results will fill this data frame
bg_Winterville_xyr <- data.frame(x = numeric(nrow(bg_Winterville)),
y = NA,
r = NA)
# Do a for-loop
for (i in 1:nrow(bg_Winterville)){
bg_Winterville_xyr[i,] <- bg_Winterville[i, ] %>%
getXYRadius(gcs_id = gcs_id,
pcs_id = pcs_id)
}
tmap_mode('view')
## ℹ tmap mode set to "view".
bg_Winterville_xyr %>%
# Convert the data frame into an sf object
st_as_sf(coords = c("x", "y"), crs = st_crs(bg_Winterville)) %>%
# Draw a buffer centered at the centroid of BG polygons.
# The buffer distance is the radius we just calculated
st_buffer(dist = .$r) %>%
# Display this buffer in red
tm_shape(.) + tm_polygons(alpha = 0.3, col = 'red') +
# Display the original polygon in blue
tm_shape(bg_Winterville) + tm_borders(col= 'blue')
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_polygons()`: use 'fill' for the fill color of polygons/symbols
## (instead of 'col'), and 'col' for the outlines (instead of 'border.col').[v3->v4] `tm_polygons()`: use `fill_alpha` instead of `alpha`.
# Define EPSG codes for GCS (WGS 84) and PCS (WGS 84 / UTM zone 16N)
#gcs_id <- ????
#pcs_id <- ????
# Get bbox for Winterville
Winterville_bb <- Winterville %>% st_transform(pcs_id) %>% st_bbox()
# Find coordinates for the four sides of the bbox
west <- Winterville_bb[1]
east <- Winterville_bb[3]
south <- Winterville_bb[2]
north <- Winterville_bb[4]
# Split the bbox into a grid
fishnet_n <- 4
dist <- abs(east - west)/fishnet_n
# Fishnet points
fish_x <- seq(from = west, to = east, by = dist)
fish_y <- seq(from = south, to = north, by = dist)
fishnet <- expand.grid(fish_x, fish_y) %>%
rename(x = Var1, y = Var2) %>%
st_as_sf(coords = c('x', 'y'), crs = pcs_id)
fishnet$x <- fishnet %>% st_transform(gcs_id) %>% st_coordinates() %>% .[,1]
fishnet$y <- fishnet %>% st_transform(gcs_id) %>% st_coordinates() %>% .[,2]
fishnet$r <- dist*(2^(1/2)/2)
# Visualize it
fishnet %>%
st_buffer(dist = .$r) %>%
tm_shape() +
tm_polygons(alpha = 0.1, col = 'red') +
tm_shape(Winterville) + tm_borders(lwd = 3)
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_polygons()`: use `fill_alpha` instead of `alpha`.
library(httr)
google_api_key <- Sys.getenv("GOOGLE_API")
lat <- 33.966700 # Winterville
lon <- -83.281700 # Winterville
radius <- 500.0 # 1 mile in meters
endpoint <- "https://places.googleapis.com/v1/places:searchNearby" # Nearby Search endpoint
body <- list(
includedTypes = list("bank", "school"),
locationRestriction = list(
circle = list(
center = list(latitude = lat, longitude = lon),
radius = radius
)
)
)
resp <- POST(
endpoint,
add_headers(
"Content-Type" = "application/json",
"X-Goog-Api-Key" = google_api_key,
"X-Goog-FieldMask" = "places.displayName,places.formattedAddress,places.types"),
body = body,
encode = "json"
)
print(resp) # metadata + body
## Response [https://places.googleapis.com/v1/places:searchNearby]
## Date: 2025-09-20 02:35
## Status: 200
## Content-Type: application/json; charset=UTF-8
## Size: 369 B
## {
## "places": [
## {
## "types": [
## "preschool",
## "child_care_agency",
## "school",
## "point_of_interest",
## "establishment"
## ],
## ...
# Extract content from a request
data <- content(resp, as="text")
# Parse JSON into a list and turn it into a data frame
data <- jsonlite::fromJSON(data, flatten = T) %>% as.data.frame()
print(data)
## places.types
## 1 preschool, child_care_agency, school, point_of_interest, establishment
## places.formattedAddress
## 1 305 N Church St, Winterville, GA 30683, USA
## places.displayName.text places.displayName.languageCode
## 1 Winterville First Baptist Preschool en
nearbySearch <- function(lat, lon, radius, types_vec, fieldmask_vec, google_api_key){
endpoint <- "https://places.googleapis.com/v1/places:searchNearby" # Nearby Search endpoint
body <- list(
includedTypes = as.list(types_vec),
locationRestriction = list(
circle = list(
center = list(latitude = lat, longitude = lon),
radius = radius
)
),
rankPreference = "DISTANCE"
)
resp <- POST(
endpoint,
add_headers(
"Content-Type" = "application/json",
"X-Goog-Api-Key" = google_api_key,
"X-Goog-FieldMask" = paste(fieldmask_vec, collapse = ",")),
body = body,
encode = "json"
)
data <- content(resp, as="text") %>%
jsonlite::fromJSON(flatten = T) %>%
as.data.frame()
if (nrow(data) == 20){
print("WARNING: The response has 20 rows! Consider using a smaller spatial unit.")
}
return(data)
}
nearbySearch(lat = 33.966700,
lon = -83.281700,
radius = radius,
types_vec = c("bank", "school"),
fieldmask_vec = c("places.id",
"places.displayName",
"places.formattedAddress",
"places.location",
"places.types",
"places.priceLevel",
"places.rating",
"places.userRatingCount"),
google_api_key = Sys.getenv("GOOGLE_API"))
Now that we have the Nearby Search API function, we can repeat this
process for all the Census BGs using the bg_Winterville_xyr
object we created earlier.
# pre-allocate list
data_list <- vector("list", nrow(bg_Winterville_xyr))
for (i in seq_len(nrow(bg_Winterville_xyr))) {
data_list[[i]] <- nearbySearch(
lat = bg_Winterville_xyr$y[i],
lon = bg_Winterville_xyr$x[i],
radius = bg_Winterville_xyr$r[i],
types_vec = c("bank", "school"), # banks and schools
fieldmask_vec = c("places.id",
"places.displayName",
"places.formattedAddress",
"places.location",
"places.types",
"places.priceLevel",
"places.rating",
"places.userRatingCount"),
google_api_key = Sys.getenv("GOOGLE_API")
)
Sys.sleep(1)
}
# Combine all data frames
data_all <- dplyr::bind_rows(data_list)
saveRDS(data_all, here('mini_assignment_google_poi_data.rds'))
# Convert the data to an sf object using XY coordinates
data_all_sf <- data_all %>%
rename(x = places.location.longitude, y = places.location.latitude) %>%
filter(!is.na(x) & !is.na(y)) %>%
st_as_sf(coords = c("x", "y"), crs = 4326)
# Map
tm_shape(data_all_sf) +
tm_dots(col = "places.rating",
size = "places.userRatingCount",
palette = "magma",
popup.vars = c("Name" = "places.displayName.text",
"Rating" = "places.rating",
"Rating Count" = "places.userRatingCount")) +
tm_shape(Winterville) +
tm_borders()
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_tm_dots()`: migrate the argument(s) related to the scale of the
## visual variable `fill` namely 'palette' (rename to 'values') to fill.scale =
## tm_scale(<HERE>).
Answer: I chose a city called Winterville. It is located in Clarke County in the State of Georgia, USA.
Answer: The place types selected were banks and schools
Answer: the dataset contains one row
Answer: There are high ratings in terms of place ratings and place user rating counts in the northern part of the city compared to the southern part. Again, the central part of the city does not show any form of rating in the city, using banks and schools as a reference point. The distribution of the POIs in the city could be highly affected when there are missing values to report from the Nearby search.
Answer: Nearby search also showed place ratings and place user ratings of a location that is well beyond the boundary of the study location (Winterville).