-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
106 lines (91 loc) · 3.95 KB
/
server.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
library(shiny)
library(dplyr)
library(reshape2)
library(RJSONIO)
library(rMaps)
shinyServer(function(input, output) {
loadGeoJSON <- reactive({
# Loading the administratie boundaries of Belgium
filename <- 'data/BEL_adm4.geojson'
geoJSON <- readChar(filename, file.info(filename)$size)
geo.df <- fromJSON(geoJSON)
# Removing unnecessary fields in properties
geo.df$features <- lapply(geo.df$features, function(feature) {
feature$properties <- feature$properties[names(feature$properties) == "NAME_4"]
names(feature$properties) <- c("Municipality")
feature
})
geo.df
})
loadPopulationNumbers <- reactive({
# Postal codes
municipalities <-
rbind(
read.csv('data/postalcodes/Antwerpen.csv', sep = ';', stringsAsFactors = FALSE),
read.csv('data/postalcodes/Brussel.csv', sep = ';', stringsAsFactors = FALSE),
read.csv('data/postalcodes/Henegouwen.csv', sep = ';', stringsAsFactors = FALSE),
read.csv('data/postalcodes/Limburg.csv', sep = ';', stringsAsFactors = FALSE),
read.csv('data/postalcodes/Luik.csv', sep = ';', stringsAsFactors = FALSE),
read.csv('data/postalcodes/Luxemburg.csv', sep = ';', stringsAsFactors = FALSE),
read.csv('data/postalcodes/Namen.csv', sep = ';', stringsAsFactors = FALSE),
read.csv('data/postalcodes/Oost-Vlaanderen.csv', sep = ';', stringsAsFactors = FALSE),
read.csv('data/postalcodes/Vlaams-Brabant.csv', sep = ';', stringsAsFactors = FALSE),
read.csv('data/postalcodes/Waals-Brabant.csv', sep = ';', stringsAsFactors = FALSE),
read.csv('data/postalcodes/West-Vlaanderen.csv', sep = ';', stringsAsFactors = FALSE)
)
municipalities <- municipalities %>% group_by(Postal.Code) %>%
summarise(Municipality = max(Municipality)) %>% ungroup()
# Population number
population <- read.csv('data/population.csv', sep = ';', stringsAsFactors = FALSE)
population <- population %>% mutate(Municipality = GEMEENTE, Number = TOTAAL.BEVOLKING) %>%
select(Municipality, Number) %>% filter(!is.na(Number))
# Combine
postal.population <- left_join(municipalities, population)
postal.population
})
## MAP
output$map <- renderMap({
# Loading GeoJSON data
geo.df <- loadGeoJSON()
# Loading population data
population <- loadPopulationNumbers()
geo.df$features <- lapply(geo.df$features, function(feature) {
municipality <- feature$properties["Municipality"]
p <- population %>% filter(Municipality == toupper(municipality))
feature$properties[["Population"]] <- max(p$Number, 0)
feature$properties[["Density"]] <- rnorm(1, mean = max(p$Number, 0) / 2, sd = max(p$Number, 0) / 16) / max(p$Number, 0)
feature
})
# Creating the map
map <- Leaflet$new()
map$setView(c(50.5, 4.6), zoom = 8)
map$tileLayer("http://{s}.www.toolserver.org/tiles/bw-mapnik/{z}/{x}/{y}.png")
# Adding GeoJSON to the map
map$geoJson(geo.df,
style = "#!
function(feature) {
if(feature.properties.Density < 0.5)
return {
'color': '#cc3333',
'fillColor': '#cc3333',
'opacity': 0.8,
'fillOpacity': 0.8,
'weight': 2
};
else
return {
'color': '#33cc33',
'fillColor': '#33cc33',
'opacity': 0.8,
'fillOpacity': 0.8,
'weight': 2
};
} !#",
onEachFeature = "#!
function(feature, layer) {
L.polygon(feature.geometry.coordinates).addTo(map);
}
!#")
map
})
})