forked from aaronschiff/nz-small-town-maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nz-small-town-maps.R
311 lines (256 loc) · 11.7 KB
/
nz-small-town-maps.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Make maps of NZ's smaller urban areas using LINZ primary parcels data
# -----------------------------------------------------------------------------
# Setup
rm(list = ls())
library(parallel)
library(magrittr)
library(tidyverse)
library(sf)
library(png)
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Configuration
max_area_size <- 25 # Square kilometres, the maximum size of urban area to map
plot_resolution <- 4 # Metres per pixel, the output image resolution
bbox_expansion_factor <- 1.25 # Ratio to increase bounding boxes used for mapping, to fit edges more nicely
urban_area_buffer <- 25 # Metres of buffer around urban area boundary, to ensure features on the boundary are not clipped
min_plot_parcel_size <- 200 # Square metres, the minimum area of parcels to map
max_plot_parcel_size <- 2000 # Square metres, the maximum area of parcels to map
# Map colours and settings
background_colour <- "black"
parcel_colour <- "white"
parcel_lwd <- 2
overview_map_width <- 150
overview_map_height <- 200
# Utility function to expand a bounding box by the bbox_expansion_factor
expand_bbox <- function(b) {
b_width_delta <- as.numeric((b["xmax"] - b["xmin"]) * (bbox_expansion_factor - 1) / 2)
b_height_delta <- as.numeric((b["ymax"] - b["ymin"]) * (bbox_expansion_factor - 1) / 2)
b_new <- b + c(-b_width_delta, -b_height_delta, b_width_delta, b_height_delta)
return(b_new)
}
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Load data
print("Loading data")
# Urban areas
urban_areas <- read_sf("data/2017 Digital Boundaries Generalised Clipped/UA2017_GV_Clipped.shp")
names(urban_areas) <- tolower(names(urban_areas))
# Primary parcels
parcels_1 <- read_sf("data/nz-primary-parcels/nz-primary-parcels-1.shp") %>%
select(id, parcel_int, geometry)
parcels_2 <- read_sf("data/nz-primary-parcels/nz-primary-parcels-2.shp") %>%
select(id, parcel_int, geometry)
parcels_3 <- read_sf("data/nz-primary-parcels/nz-primary-parcels-3.shp") %>%
select(id, parcel_int, geometry)
parcels <- rbind(parcels_1, parcels_2, parcels_3)
rm(parcels_1, parcels_2, parcels_3)
# Coastline and lakes for outline maps
coastline <- read_sf("data/nz-coastlines-topo-1500k/nz-coastlines-topo-1500k.shp") %>%
st_cast("POLYGON")
lakes <- read_sf("data/nz-lake-polygons-topo-1500k/nz-lake-polygons-topo-1500k.shp")
lakes$area <- as.numeric(st_area(lakes) / 1000000)
lakes %<>% filter(area > 100)
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Filter and process data
print("Filtering areas and processing data")
# Filter out non-urban areas and remove unncessary columns from urban_areas
urban_areas %<>%
filter(!ua2017_nam %in% c("Rural Centre",
"Inland Water not in Urban Area",
"Inlet-in TA but not in Urban Area",
"Rural (Incl.some Off Shore Islands)")) %>%
select(-area_sq_km, -land_sq_km)
# Combine central Auckland urban areas into one
auckland_areas <- c("Northern Auckland Zone",
"Western Auckland Zone",
"Central Auckland Zone",
"Southern Auckland Zone")
auckland_geometry <- urban_areas %>%
filter(ua2017_nam %in% auckland_areas) %>%
st_union()
auckland <- st_as_sf(tibble(ua2017 = "999",
ua2017_nam = "Auckland",
geometry = auckland_geometry))
urban_areas %<>%
filter(!ua2017_nam %in% auckland_areas)
urban_areas %<>% rbind(auckland)
rm(auckland_geometry, auckland)
# Find areas smaller than size limit
urban_areas$area <- as.numeric(st_area(urban_areas) / 1000000)
small_urban_areas <- filter(urban_areas, area < max_area_size)
small_urban_areas$centroid <- st_centroid(small_urban_areas)
# Buffer small urban areas so that features on the edges will be included properly
small_urban_areas_buffered <- st_buffer(small_urban_areas, urban_area_buffer)
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Intersect urban areas with other geographic data
small_urban_area_names <- small_urban_areas$ua2017_nam
# Function to find the boundary of a named area, optionally returning the bounding box
find_area_boundary <- function(area_name, use_bbox = FALSE) {
area_boundary <- small_urban_areas_buffered %>%
filter(ua2017_nam == area_name)
if (use_bbox) {
area_boundary <- st_as_sfc(expand_bbox(st_bbox(area_boundary)))
}
return(area_boundary)
}
# Parcels
# Note just using two cores here due to memory requirements for parcels data
print("Intersecting parcels")
plot_parcels <- mclapply(small_urban_area_names,
function(i) {
st_intersection(parcels, find_area_boundary(i, use_bbox = FALSE))
},
mc.cores = 2)
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Plotting
print("Plotting")
# Function to plot a little overview map for an area
plot_overview_map <- function(i, output_directory) {
area_name <- small_urban_area_names[i]
area_shape <- small_urban_areas_buffered[i, ]
area_centroid <- st_centroid(area_shape)
# Plotting setup
png(paste0(output_directory, "/map-", area_name, ".png"),
width = overview_map_width,
height = overview_map_height)
par(mar = c(0, 0, 0, 0),
bg = background_colour)
plot(st_geometry(coastline),
col = parcel_colour,
border = NA,
xaxs = "i",
yaxs = "i")
plot(st_geometry(lakes),
col = background_colour,
border = NA,
add = TRUE)
plot(st_geometry(st_centroid(area_shape)),
pch = 21,
cex = 2.5,
col = rgb(255/255, 255/255, 255/255),
bg = rgb(255/255, 0/255, 0/255),
lwd = 2,
add = TRUE)
dev.off()
}
# Function to plot an urban area
plot_area <- function(i, output_directory) {
# Gather data for the selected area
area_name <- small_urban_area_names[i]
area_shape <- small_urban_areas_buffered[i, ]
area_parcels <- plot_parcels[[i]]
# Set up output fule
area_bbox <- expand_bbox(st_bbox(area_shape))
output_width <- as.numeric(round((area_bbox["xmax"] - area_bbox["xmin"]) / plot_resolution))
output_height <- as.numeric(round((area_bbox["ymax"] - area_bbox["ymin"]) / plot_resolution))
# Plotting setup
png(paste0(output_directory, "/", area_name, ".png"), width = output_width, height = output_height)
par(mar = c(0, 0, 0, 0),
bg = background_colour)
plot(st_as_sfc(area_bbox), col = NA, border = NA, xaxs = "i", yaxs = "i")
# Plot property parcels
area_parcels_property <- filter(area_parcels, !(parcel_int %in% c("Road",
"Road Strata",
"Hydro",
"Streambed",
"Riverbed")))
area_parcels_property$area <- as.numeric(st_area(area_parcels_property))
area_parcels_residential <- filter(area_parcels_property,
area < max_plot_parcel_size,
area > min_plot_parcel_size)
plot(st_geometry(area_parcels_residential),
col = parcel_colour,
border = background_colour,
lwd = parcel_lwd,
add = TRUE)
# Plot finished
dev.off()
# Plot summary map
plot_overview_map(i, output_directory)
# Done
return(c(name = area_name, width = output_width, height = output_height))
}
# For some reason, need to call plot_area on one area before plotting all with parallel processing
# I don't know why this makes it work properly
plot_area(1, "outputs")
plot_results <- mclapply(1:length(small_urban_area_names),
plot_area,
output_directory = "outputs",
mc.cores = detectCores() - 2)
plot_info <- as_tibble(matrix(unlist(plot_results), nrow = length(plot_results), ncol = 3, byrow = TRUE))
names(plot_info) <- c("area_name", "width", "height")
plot_info$width <- as.numeric(plot_info$width)
plot_info$height <- as.numeric(plot_info$height)
plot_info %<>% arrange(area_name)
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Web gallery
output_html <- function(content, output_filename) {
# Load HTML template parts
template_header <- read_file("header.html")
template_footer <- read_file("footer.html")
output <- paste(template_header, content, template_footer, sep = "\n")
write_file(output, output_filename)
}
wrap_html_tag <- function(x, tag, params = NULL) {
output <- paste0("<", tag)
if (!is.null(params)) output <- paste(output, paste(params, collapse = " "))
output <- paste0(output, ">", x, "</", tag, ">")
return(output)
}
build_content <- function(current = NULL, new) {
return(paste(current, new, sep = "\n"))
}
output_area <- function(i, output_directory) {
area_name <- plot_info[i, "area_name"]
area_content <- wrap_html_tag(area_name, "h2")
area_content %<>% build_content(
paste0("<div><img src='maps/map-", area_name, ".png' ",
"width = '", round(overview_map_width / 2), "' ",
"data-original-width = '", round(overview_map_width / 2), "' ",
"height = '", round(overview_map_height / 2), "' ",
"data-original-height = '", round(overview_map_height / 2), "' ",
"class = 'overview-map-img'",
"/></div>")
)
image <- readPNG(paste0(output_directory, "/", area_name, ".png"), info = TRUE)
area_content %<>% build_content(
paste0("<div><img src='", area_name, ".png' ",
"width = '", round(attr(image, "dim")[2] / 2), "' ",
"data-original-width = '", round(attr(image, "dim")[2] / 2), "' ",
"height = '", round(attr(image, "dim")[1] / 2), "' ",
"data-original-height = '", round(attr(image, "dim")[1] / 2), "' ",
"class = 'town-img'",
"/></div>")
)
area_content %<>% wrap_html_tag("div", "class = 'town-div'")
return(area_content)
}
generate_gallery <- function(output_directory) {
# Title
content <- wrap_html_tag("Small towns of Aotearoa", "h1")
content %<>% build_content(
wrap_html_tag(
paste("<p>Made by <a href='http://schiff.co.nz'>Aaron Schiff</a> with data from LINZ and Statistics New Zealand.</p>",
"<p>All images are licensed <a href='https://creativecommons.org/licenses/by/4.0/'>CC BY 4.0</a>.</p>",
"<p>I wrote a bit more about this <a href='https://medium.com/aaron-schiffs-blog/approximate-maps-of-the-small-towns-of-aotearoa-9c2e241606b9'>here</a>. The R code is <a href='https://github.com/aaronschiff/nz-small-town-maps'>here</a>.</p>",
sep = "\n"),
"div",
"id = 'info'"
)
)
# Loop through images
for (i in 1:nrow(plot_info)) {
content %<>% build_content(output_area(i, output_directory))
}
# End
output_html(content, paste0(output_directory, "/index.html"))
file.copy(from = "style.css",
to = paste0(output_directory, "/style.css"),
overwrite = TRUE)
}
generate_gallery("outputs-edited-cropped")