R package for downloading OpenStreetMap data
1
2# hex sticker script (issue 185)
3
4## Get a background map, here of Kichijoji in Tokyo
5
6```{r}
7library (osmdata)
8library (osmplotr)
9bb <- getbb ("kichijoji japan")
10bb [2, 1] <- bb [2, 1] - 0.2 * diff (bb [2, ])
11bb [2, 2] <- mean (bb [2, ])
12hw <- opq (bb) |>
13 add_osm_feature (key = "highway") |>
14 osmdata_sf (quiet = FALSE) |>
15 osm_poly2line () |>
16 extract2 ("osm_lines")
17b <- opq (bb) |>
18 add_osm_feature (key = "building") |>
19 osmdata_sf (quiet = FALSE) |>
20 extract2 ("osm_polygons")
21g1 <- opq (bb) |>
22 add_osm_feature (key = "landuse", value = "grass") |>
23 osmdata_sf (quiet = FALSE) |>
24 extract2 ("osm_polygons")
25g2 <- opq (bb) |>
26 add_osm_feature (key = "leisure", value = "park") |>
27 osmdata_sf (quiet = FALSE) |>
28 extract2 ("osm_polygons")
29g <- sf::st_sf (osm_id = c (g1$osm_id, g2$osm_id),
30 geometry = c (g1$geometry, g2$geometry))
31w <- opq (bb) |>
32 add_osm_feature (key = "natural", value = "water") |>
33 osmdata_sf (quiet = FALSE) |>
34 extract2 ("osm_polygons")
35
36#osm_structures (col_scheme = "light")
37map <- osm_basemap (bbox = bb, bg = "gray95") |>
38 add_osm_objects (hw, col = "#969696FF") |>
39 add_osm_objects (b, col = "#C8C8C8FF") |>
40 add_osm_objects (g, col = "#C8FFC8FF") |>
41 add_osm_objects (w, col = "#C8C8DCFF")
42#print_osm_map (map, filename = "kichijoji.png")
43saveRDS (map, file = "map.Rds")
44```
45
46Reduce image to square:
47```{r}
48map <- readRDS ("map.Rds")
49f <- "kichijoji.png"
50osmplotr::print_osm_map (map, filename = f)
51magick::image_read (f) |>
52 magick::image_trim () |>
53 magick::image_write (f)
54i <- magick::image_read (f) |>
55 magick::image_info ()
56chop <- floor (i$width - i$height) / 2
57chop <- paste0 (i$height, "x", i$height, "+", chop, "+0")
58magick::image_read (f) |>
59 magick::image_crop (chop) |>
60 magick::image_write (f)
61```
62
63
64
65# crop image to hex
66
67image is 1260 h X 2100 w
68
69```{r}
70f <- "kichijoji.png"
71img <- png::readPNG (f)
72# define hexagon and set all outer pixels to 1
73s3 <- sqrt (3) / 2
74border <- data.frame (x = 1 + c (rep (-s3, 2), 0, rep (s3, 2), 0, -s3),
75 y = 1 + c (0.5, -0.5, -1, -0.5, 0.5, 1, 0.5))
76border$x <- round (dim (img) [2] * border$x / 2)
77border$y <- round (dim (img) [1] * border$y / 2)
78
79#h <- 7
80#w <- h * diff (range (border$x)) / diff (range (border$y))
81#x11 (width = w, height = h)
82#plot (border, type = "l")
83border
84
85library (sp)
86library (raster)
87p <- Polygon (border)
88p <- SpatialPolygons (list (Polygons (list (p), "p")))
89
90n1 <- dim (img) [1]
91n2 <- dim (img) [2]
92r <- raster (nrows = n1, ncols = n2, xmn = 1, xmx = n2, ymn = 1, ymx = n1,
93 vals = TRUE)
94r [mask (r, p)] <- FALSE
95r <- !r
96#plot (r)
97r <- as.matrix (r)
98index <- which (!r)
99index_not <- which (r)
100
101for (i in 1:3) {
102 i1 <- img [, , i]
103 i1 [index] <- 0
104 img [, , i] <- i1
105}
106mmand::display (img)
107# Then add a 4th channel for alpha values
108img4 <- array (dim = c (dim (img) [1:2], 4))
109for (i in 1:3) {
110 i1 <- img [, , i]
111 img4 [, , i] <- i1
112 i1 [index] <- 0
113 i1 [index_not] <- 1
114 img4 [, , 4] <- i1
115}
116png::writePNG (img4, f)
117```
118
119## make the hex logo
120
121```{r}
122library (ggplot2)
123# trace outline of hexagon from centre bottom point in anti-clockwise direction
124s3 <- sqrt (3) / 2
125border <- data.frame (x = 1 + c (rep (-s3, 2), 0, rep (s3, 2), 0, -s3),
126 y = 1 + c (0.5, -0.5, -1, -0.5, 0.5, 1, 0.5))
127asp <- diff (range (border$x)) / diff (range (border$y)) # aspect ratio for image
128
129f <- "kichijoji.png"
130d <- data.frame(x = 1, y = 1, image = f)
131size <- 1.0
132hex <- ggplot() +
133 ggimage::geom_image (aes_ (x = ~x, y = ~y, image = ~image), d,
134 size = 1.05, asp = asp) +
135 geom_polygon (aes_ (x = ~x, y = ~y), data = border,
136 size = 5, fill = NA, color = "#555555")
137
138#extrafont::loadfonts ()
139lab_dat <- data.frame (x = 1 - 0.0001,
140 y = 1 + 0.0001,
141 lab = 'osmdata')
142aes <- ggplot2::aes (x, y, label = lab)
143fs <- 30 # font size
144hex <- hex + ggplot2::geom_text (dat = lab_dat,
145 mapping = aes,
146 size = fs,
147 colour = 'gray80',
148 family = 'SF Alien Encounters',
149 fontface = 1,
150 nudge_y = 0.0001,
151 nudge_x = 0.0001)
152hex <- hex + ggplot2::geom_text (dat = lab_dat,
153 mapping = aes,
154 size = fs,
155 colour = 'black',
156 fontface = 1,
157 family = 'SF Alien Encounters')
158
159th <- theme_minimal ()
160th$panel.background <- element_rect (fill = "transparent", size = 0)
161th$line <- element_blank ()
162th$axis.text <- element_blank ()
163th$axis.title <- element_blank ()
164th$plot.margin <- margin (rep (unit (0, 'null'), 4))
165#th$plot.margin <- margin (rep (unit (-0.5, 'line'), 4))
166th$legend.position <- 'none'
167th$axis.ticks.length <- unit (0, 'null')
168
169hex <- hex + th
170
171print (hex)
172```
173```{r}
174asp <- 1
175fname <- file.path (here::here (), "man", "figures", "logo.png")
176ggsave (hex, filename = fname, width = 7, height = 7 * asp)
177```
178
179it is then necessary to read the png back in and re-convert the border pixels
180to alpha = 0
181```{r}
182fname <- file.path (here::here (), "man", "figures", "logo.png")
183img <- png::readPNG (fname)
184img4 <- array (1, dim = c (dim (img) [1:2], 4))
185index_out <- which (img [, , 1] == 1 & img [, , 2] == 1 & img [, , 3] == 1)
186index_in <- which (!seq (img [, , 1]) %in% index_out)
187for (i in 1:3) {
188 i1 <- img [, , i]
189 img4 [, , i] <- i1
190 i1 [index_in] <- 1
191 i1 [index_out] <- 0
192 img4 [, , 4] <- i1
193}
194fname <- file.path (here::here (), "man", "figures", "logo.png")
195png::writePNG (img4, fname)
196```