Created
November 27, 2022 04:57
-
-
Save dakvid/0a18c5e2dbfc5b7e880a7b864d339634 to your computer and use it in GitHub Desktop.
#30DayMapChallenge 2022 - Day 27 - Music
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Take the geocoded album covers from Audioculture and plonk them on a map :-) | |
# Don't obviously see options to control the size of the images :-/ | |
library(curl) | |
library(jsonlite) | |
library(stringr) | |
library(purrr) | |
library(dplyr) | |
library(sf) | |
library(rmapshaper) | |
library(ggplot2) | |
library(ggimage) | |
# https://datafinder.stats.govt.nz | |
nz <- | |
st_read("2021/statsnz/regional-council-2021-clipped-generalised.gpkg") |> | |
filter(REGC2021_V1_00 != "99") |> | |
count() |> | |
ms_simplify(keep = 0.02) | |
crs_nz <- st_crs(nz) | |
# https://www.audioculture.co.nz/articles/album-cover-location-map | |
albums <- | |
read_json("Day_27_Music/album_covers.json") | |
get_album_details <- | |
function(list_from_json) { | |
tibble(lat = list_from_json$location$lat, | |
long = list_from_json$location$lon, | |
name = list_from_json$text$headline, | |
cover_url = list_from_json$media$url) | |
} | |
album_data <- | |
albums$storymap$slide |> | |
map_df(get_album_details) |> | |
filter(!is.na(lat), !is.na(long)) |> | |
st_as_sf(coords = c("long", "lat"), | |
crs = 4326L) |> | |
st_transform(crs = crs_nz) |> | |
mutate(cover_image = paste0("Day_27_Music/covers/", | |
cover_url |> str_extract("[^/]+$"))) | |
album_data <- | |
bind_cols( | |
album_data, | |
album_data |> st_coordinates() |> as_tibble() | |
) | |
pwalk(album_data |> | |
st_drop_geometry() |> | |
select(url = cover_url, | |
destfile = cover_image), | |
curl_download) | |
ggplot() + | |
geom_sf(data = nz, | |
fill = "light green", colour = NA) + | |
geom_image(data = album_data, | |
aes(image = cover_image, | |
x = X, y = Y)) + | |
theme_void() + | |
theme(panel.background = element_rect(fill = "light blue", colour = "light blue"), | |
plot.background = element_rect(fill = "light blue", colour = "light blue")) | |
ggsave("Day_27_Music/day_27_nz_albums.png", | |
width = 2400, height = 3600, units = "px", | |
background = "light blue") | |
auckland_bbox <- | |
st_read("2021/statsnz/regional-council-2021-clipped-generalised.gpkg") |> | |
filter(REGC2021_V1_00 == "02") |> | |
st_bbox() | |
# xmin 1704081 xmax 1828993 | |
# ymin 5871395 ymax 6025464 | |
ggplot() + | |
geom_sf(data = nz, | |
fill = "light green", colour = NA) + | |
geom_image(data = album_data, | |
aes(image = cover_image, | |
x = X, y = Y)) + | |
# xlim(c(auckland_bbox$xmin, auckland_bbox$xmax)) + | |
# ylim(c(auckland_bbox$ymin, auckland_bbox$ymax)) + | |
xlim(c(1725000, 1770000)) + | |
ylim(c(5910000, 5930000)) + | |
theme_void() + | |
theme(panel.background = element_rect(fill = "light blue", colour = "light blue"), | |
plot.background = element_rect(fill = "light blue", colour = "light blue")) | |
ggsave("Day_27_Music/day_27_nz_albums_auckland.png", | |
width = 4500, height = 2000, units = "px", | |
background = "black") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment