-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.R
181 lines (153 loc) · 5.96 KB
/
map.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
# Map of Trafford councillors #
# Source: Trafford Council
# URL: https://democratic.trafford.gov.uk/mgMemberIndex.aspx
# Licence: OGL v3.0
library(tidyverse)
library(sf)
library(leaflet)
library(leaflet.extras)
library(htmlwidgets)
library(htmltools)
# read list of Trafford's councillors
df <- read_csv("councillors.csv")
# create HTML popup
councillors <- df %>%
mutate(popup = paste0("
<figure>
<img src='", Image, "' alt=''/>
<figcaption><a href='", Page, "' target='_blank' class='boldText'>", Name, "</a><br />", Party, ".</figcaption>
</figure>
<address>",
if_else(is.na(Email), "", str_c("<a href='mailto:", Email, "'>", Email, "</a><br />")),
if_else(is.na(Telephone), "", Telephone),
"</address>")) %>%
select(Ward, popup) %>%
group_by(Ward) %>%
mutate(id = 1:n()) %>%
pivot_wider(names_from = id, values_from = popup) %>%
mutate(`3` = replace_na(`3`, "")) %>%
unite(popup, 2:4, sep = "", remove = TRUE) %>%
mutate(popup = paste0("<h2>", Ward, "</h2>", popup))
# Load in the ward boundaries
wards <- st_read("https://www.trafforddatalab.io/spatial_data/ward/2023/trafford_ward_full_resolution.geojson")
# join councillor information to ward boundaries
sf <- left_join(wards, councillors, by = c("area_name" = "Ward"))
# build map
map <- leaflet(height = "100%", width = "100%") %>%
setView(-2.35533522781156, 53.419025498197, zoom = 12) %>%
addTiles(urlTemplate = "https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}{r}.png",
attribution = '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a> | <a href="https://www.ons.gov.uk/methodology/geography/licences">Contains OS data © Crown copyright and database right (2024)</a> | Source: <a href="https://democratic.trafford.gov.uk/mgMemberIndex.aspx?FN=WARD&VW=TABLE&PIC=1" target="_blank">Trafford Council</a>',
options = tileOptions(minZoom = 12, maxZoom = 17)) %>%
addPolygons(data = sf, fillColor = "#CCCCCC", weight = 0.8, opacity = 1, color = "#212121",
popup = ~popup,
highlight = highlightOptions(color = "#046DC3", weight = 3, bringToFront = TRUE)) %>%
addLabelOnlyMarkers(data = sf, lng = ~as.numeric(lon), lat = ~as.numeric(lat), label = ~as.character(area_name),
labelOptions = labelOptions(noHide = T, textOnly = T, direction = "center",
style = list("color" = "#212121",
"font-size" = "12px",
"text-shadow" = "-1px -1px #FFFFFF, 1px -1px #FFFFFF, -1px 1px #FFFFFF, 1px 1px #FFFFFF"))) %>%
addResetMapButton() %>%
addEasyButton(
easyButton(
position = "topleft",
icon = "fa-crosshairs",
title = "Locate Me",
onClick = JS(c("function(btn, map){map.locate({setView:true,enableHighAccuracy: true })}"))
)) %>%
addControl(paste0("<h1>Trafford councillors by ward</h1>"), position = 'topright', className = "map-title") %>%
onRender(paste0("function(el, x) {$('head').append(","\'<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, viewport-fit=cover\">\'",");}")) %>%
onRender(paste0("function(el, x) {$('head').append(","\'<link rel=\"stylesheet\" href=\"https://fonts.googleapis.com/css?family=Open+Sans%7CRoboto\"/>\'",");}"))
# add page title and CSS
browsable(
tagList(list(
tags$html(lang = "en-GB"),
tags$head(
tags$title("Trafford councillors by ward"),
tags$style(
"html, body {
height: 100%;
margin: 0;
font-family: 'Open Sans', sans-serif;
}
main {
display: block;
box-sizing: border-box;
width: 100%;
height: 100%;
}
h1, h2, h3 {
font-family: 'Roboto', sans-serif;
color: #707070;
}
figure, figcaption, address {
display: block;
margin: 0;
padding: 0;
}
figure {
margin-top: 1.5em;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.leaflet-bar a.leaflet-disabled {
color: #757575;
}
.leaflet-container a {
color: #046dc3;
}
.leaflet-bar a {
color: #212121;
}
.leaflet-popup {
position: absolute;
}
/* Override Leaflet style to increase contrast */
.leaflet-container a.leaflet-popup-close-button {
color: #555;
}
.leaflet-container a.leaflet-popup-close-button:hover {
color: #212121;
}
.leaflet-popup-content {
white-space: nowrap;
margin: 0 1.5em 1.5em 1.5em;
min-width: 100px !important;
max-height: 300px;
overflow: auto;
}
.leaflet-popup-content h2 {
text-align: center;
margin: 0.25em 0 0 0;
padding: 0;
border-bottom: 1px solid #ccc;
}
.leaflet-popup-content img {
box-shadow: 3px 3px 5px #ccc;
}
.leaflet-control.map-title {
background-color: transparent;
}
.leaflet-control.map-title h1 {
text-shadow: -1px -1px #FFFFFF, 1px -1px #FFFFFF, -1px 1px #FFFFFF, 1px 1px #FFFFFF;
padding: 0;
margin: 0;
}
.leaflet-control-attribution {
/* fixes text scaling issue when changing from portrait to landscape on mobile browsers */
-webkit-text-size-adjust: 100%;
-moz-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
.boldText {
font-weight: bold;
}"
)
)
),
tags$main(map)
)
)