-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfr_layer_prep.R
250 lines (214 loc) · 9.68 KB
/
fr_layer_prep.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
#######################################################
# Preparation Voronoi layers and aggregation France
######################################################
# Obj: From Municipalities centroids and generalized small scale Natural Earth layer,
# Create Voronoi Layer including all territories (buffer area for points falling out of Natural Eearth layer),
library(sf)
library(mapsf)
library(rnaturalearth)
library(rnaturalearthdata)
library(rmapshaper)
library(readxl)
#remotes::install_github("ropensci/rnaturalearthhires")
# 1 - Preparing Municipalities centroids ----
# Extracting centroids from municipal layer 2022 (too large file for github)
# com <- st_read("input/fr/COMMUNE.shp") # INPUT DATA (not saved) COMMUNE 2022 from IGN
# com <- st_transform(com, 2154)
# com$SUPERFICIE <- as.numeric(st_area(com) / 1000000)
# com <- st_centroid(com)
#st_write(com, "input/fr/COMMUNE.shp", delete_layer = TRUE)
com <- st_read("input/fr/COMMUNE.shp")
# Arrondissements for Paris, Lyon, Marseille
arr <- st_read("input/fr/ARRONDISSEMENT_MUNICIPAL.shp")
arr <- st_transform(arr, 2154)
arr$SUPERFICIE <- as.numeric(st_area(arr) / 1000000)
arr <- merge(arr, com[,c("INSEE_COM", "SIREN_EPCI"), drop = TRUE], by = "INSEE_COM", all.x = TRUE)
arr$INSEE_COM <- NULL
colnames(arr)[4] <- "INSEE_COM"
arr$INSEE_DEP <- substr(arr$INSEE_COM, 1, 2)
arr <- st_centroid(arr)
st_geometry(arr[arr$INSEE_COM == "75112", ]) <- st_sfc(st_point(c(655101.1, 6860266))) # Moving Point to municipal town-hall to have a single territory when aggregating in Paris
var <- c("INSEE_COM", "NOM", "POPULATION", "SUPERFICIE", "INSEE_DEP", "SIREN_EPCI")
com <- rbind(com[,var], arr[,var])
# Delete Paris, Lyon and Marseille from municipal layer
com <- com[!com$INSEE_COM %in% c("75056", "13055", "69123"),]
com <- com[order(com$INSEE_COM),]
# 2 - Country layer : Natural Earth, small scale, simplified ----
countries <- ne_countries(scale = 10, returnclass = "sf")
countries <- ms_simplify(countries, keep = .4)
countries <- countries[,c("adm0_a3", "name")]
# Extract French territories for mask-box purpose
fr <- countries[countries$adm0_a3 == "FRA",]
fr <- st_cast(fr, "POLYGON")
fr$id <- seq(from = 1, to = nrow(fr), by =1)
fr_df <- st_set_geometry(fr, NULL)
fr_df[1,c(1:2)] <- c("GUY", "Guyane")
fr_df[2,c(1:2)] <- c("MET", "France Métropolitaine")
fr_df[3,c(1:2)] <- c("MAR", "Martinique")
fr_df[4,c(1:2)] <- c("GUA", "Guadeloupe")
fr_df[5,c(1:2)] <- c("GUA", "Guadeloupe")
fr_df[6,c(1:2)] <- c("GUA", "Guadeloupe")
fr_df[7,c(1:2)] <- c("GUA", "Guadeloupe")
fr_df[8,c(1:2)] <- c("REU", "La Réunion")
fr_df[9,c(1:2)] <- c("MAY", "Mayotte")
fr_df[10,c(1:2)] <- c("MAY", "Mayotte")
fr_df[11,c(1:2)] <- c("MET", "France Métropolitaine")
fr_df[12,c(1:2)] <- c("MET", "France Métropolitaine")
fr_df[13,c(1:2)] <- c("MET", "France Métropolitaine")
fr_df[14,c(1:2)] <- c("MET", "France Métropolitaine")
fr_df[15,c(1:2)] <- c("MET", "France Métropolitaine")
fr_df[16,c(1:2)] <- c("MET", "France Métropolitaine")
fr_df[17,c(1:2)] <- c("MET", "France Métropolitaine")
fr_df[18,c(1:2)] <- c("MET", "France Métropolitaine")
fr <- merge(fr[,"id"], fr_df, by = "id")
fr <- aggregate(fr[,"adm0_a3"], by = list(fr$adm0_a3), FUN = head, 1)
fr <- st_transform(fr, 4326)
fr <- st_cast(fr, "MULTIPOLYGON")
st_write(fr, "input/fr/voronoi/mask.geojson")
# 3 - Create Voronoi ----
# When the point falls out of French boundaries, create a buffer around the point
# and join it to the original layer
voronoi <- function(x, var, proj, cent, buffer){
x <- st_transform(x, proj)
c <- st_transform(cent, proj)
st_agr(x) = "constant"
st_agr(c) = "constant"
out <- st_difference(c, x, sparse = FALSE)
st_agr(out) <- "contant"
if(nrow(out) > 0){
out <- st_buffer(out, buffer)
out <- st_union(x, out)
out <- aggregate(out, by = list(out[[var]]), FUN = head, 1)
}
else{
out <- x
}
box <- st_as_sfc(st_bbox(x, crs = proj))
v <- st_voronoi(st_union(c), box)
v <- st_as_sf(st_intersection(st_cast(v), out))
v <- st_join(st_sf(v), y = c, join = st_intersects)
v <- st_cast(v, "MULTIPOLYGON")
return(v)
}
# Voronoi creation for outermost territories and France
gua <- voronoi(x = fr[fr$adm0_a3 == "GUA",], cent = com[com$INSEE_DEP == "971",],
var = "adm0_a3", proj = 2154, buffer = 1000)
mar <- voronoi(x = fr[fr$adm0_a3 == "MAR",], cent = com[com$INSEE_DEP == "972",],
var = "adm0_a3", proj = 2154, buffer = 1000)
guy <- voronoi(x = fr[fr$adm0_a3 == "GUY",], cent = com[com$INSEE_DEP == "973",],
var = "adm0_a3", proj = 2154, buffer = 1000)
reu <- voronoi(x = fr[fr$adm0_a3 == "REU",], cent = com[com$INSEE_DEP == "974",],
var = "adm0_a3", proj = 2154, buffer = 1000)
may <- voronoi(x = fr[fr$adm0_a3 == "MAY",], cent = com[com$INSEE_DEP == "976",],
var = "adm0_a3", proj = 2154, buffer = 1000)
met <- voronoi(x = fr[fr$adm0_a3 == "MET",],
cent = com[!com$INSEE_DEP %in% c("971", "972", "973", "974", "976"),],
var = "adm0_a3", proj = 2154, buffer = 1000)
com <- rbind(met, gua, mar, guy, reu, may) # Attach all layers
com <- st_transform(com, 4326)
com$INSEE_DEP <- NULL
com$SIREN_EPCI <- NULL
st_write(com, dsn = "input/fr/voronoi/com_vor.geojson")
# 4 - Rebuild national boundaries with the union of previous layers, and build neighbouring countries layer
fra <- st_union(com, by_feature = FALSE)
inter <- countries[countries$adm0_a3 != 'FRA',]
inter <- st_difference(inter, fra) %>% st_collection_extract("POLYGON")
inter <- st_cast(inter, "MULTIPOLYGON")
fra <- st_as_sf(fra)
fra$adm0_a3 <- "FRA"
fra$name <- "France"
st_geometry(fra) <- "geometry"
countries <- rbind(inter, fra)
# Select countries to appear in the map template
sel <- c("BEL", "LUX", "DEU", "NLD", "CHE", "ITA", "ESP", "GBR", "JEY", "GGY", "IRL", "LIE",
"AUT", "CZE", "PRT", "SVN", "DMA", "LCA", "GUY", "SUR", "BRA", "FRA", "AND", "SMR")
sel <- countries[countries$adm0_a3 %in% sel,]
st_crs(sel) <- 4326
st_write(sel, "input/fr/voronoi/neighbors.shp", delete_layer = TRUE)
# Keep only relevant units according to template
bb <- c(xmin = -605000, ymin = 4288000, xmax = 2909000, ymax = 8008000)
mask <- st_as_sfc(st_bbox(bb, crs = 2154))
head(sel)
# Make country layer pretty
countries <- st_cast(sel, "POLYGON")
countries <- st_transform(countries, 2154)
inter <- st_intersects(countries, mask, sparse = FALSE)
countries <- countries[inter,]
head(countries)
countries <- aggregate(countries, by = list(countries$adm0_a3),
FUN = head, 1)
countries <- st_cast(countries, "MULTIPOLYGON")
borders <- getBorders(countries)
countries <- st_transform(countries, 4326)
st_crs(borders) <- 2154
borders <- st_transform(borders, 4326)
countries <- countries[,-1]
st_write(countries, "output/france/countries.shp", delete_layer = TRUE) # export geojson loose
st_write(borders, "output/france/borders.geojson")
# 5 - Aggregate EPCI, ZEMP, UU, REG, EPCI, DEP ----
zon <- data.frame(read_xlsx("input/fr/table-appartenance-geo-communes-22_v2022-09-27.xlsx",
skip = 5, sheet = "COM"))
zon2 <- data.frame(read_xlsx("input/fr/table-appartenance-geo-communes-22_v2022-09-27.xlsx",
skip = 5, sheet = "ARM"))
sel <- c("CODGEO", "DEP", "REG", "EPCI", "ZE2020", "AAV2020", "TAAV2017")
zon <- rbind(zon[,sel], zon2[,sel])
zon_name <- data.frame(read_xlsx("input/fr/table-appartenance-geo-communes-22_v2022-09-27.xlsx",
skip = 5, sheet = "Zones_supra_communales"))
com <- merge(com, zon, by.x = "INSEE_COM", by.y = "CODGEO", all.x = TRUE)
# Départements
dep <- aggregate(com[,c("POPULATION", "SUPERFICIE")],
by = list(com$DEP),
FUN = sum)
colnames(dep)[1] <- "CODGEO"
zon <- zon_name[zon_name$NIVGEO == "DEP",]
dep <- merge(dep, zon[,c("CODGEO", "LIBGEO")], by = "CODGEO",
all.x = TRUE)
dep <- st_cast(dep, "MULTIPOLYGON")
st_write(dep, dsn = "input/fr/voronoi/dep.geojson")
# Régions
reg <- aggregate(com[,c("POPULATION", "SUPERFICIE")],
by = list(com$REG),
FUN = sum)
colnames(reg)[1] <- "CODGEO"
zon <- zon_name[zon_name$NIVGEO == "REG",]
reg <- merge(reg, zon[,c("CODGEO", "LIBGEO")], by = "CODGEO",
all.x = TRUE)
reg <- st_cast(reg, "MULTIPOLYGON")
st_write(reg, dsn = "input/fr/voronoi/reg.geojson")
# EPCI
epci <- aggregate(com[,c("POPULATION", "SUPERFICIE")],
by = list(com$EPCI),
FUN = sum)
colnames(epci)[1] <- "CODGEO"
zon <- zon_name[zon_name$NIVGEO == "EPCI",]
epci <- merge(epci, zon[,c("CODGEO", "LIBGEO")], by = "CODGEO",
all.x = TRUE)
epci <- st_cast(epci, "MULTIPOLYGON")
st_write(epci, dsn = "input/fr/voronoi/epci.geojson")
# ZEMP
zemp <- aggregate(com[,c("POPULATION", "SUPERFICIE")],
by = list(com$ZE2020),
FUN = sum)
colnames(zemp)[1] <- "CODGEO"
zon <- zon_name[zon_name$NIVGEO == "ZE2020",]
zemp <- merge(zemp, zon[,c("CODGEO", "LIBGEO")], by = "CODGEO",
all.x = TRUE)
zemp <- st_cast(zemp, "MULTIPOLYGON")
st_write(zemp, dsn = "input/fr/voronoi/zemp.geojson")
# AAV
com_urb <- com[com$AAV2020 != "000",]
aav <- aggregate(com_urb[,c("POPULATION", "SUPERFICIE")],
by = list(com_urb$AAV2020),
FUN = sum)
colnames(aav)[1] <- "CODGEO"
zon <- zon_name[zon_name$NIVGEO == "AAV2020",]
aav <- merge(aav, zon[,c("CODGEO", "LIBGEO")], by = "CODGEO",
all.x = TRUE)
aav <- st_cast(aav, "MULTIPOLYGON")
zon <- aggregate(com_urb[,c("AAV2020", "TAAV2017")],
by = list(com_urb$AAV2020),
FUN = head, 1)
zon <- st_set_geometry(zon, NULL)
aav <- merge(aav, zon[,c("AAV2020", "TAAV2017")], by.x = "CODGEO", by.y = "AAV2020",
all.x = TRUE)
st_write(aav, dsn = "input/fr/voronoi/aav.geojson")