-
Notifications
You must be signed in to change notification settings - Fork 87
/
geojson2svg.Rmd
76 lines (66 loc) · 2.66 KB
/
geojson2svg.Rmd
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
---
title: "GeoJSON to SVG"
author: "André Ourednik"
---
# Info
This is an R utility for batch converting geoJSON files to SVG.
# Libraries
```{r}
# installation of sf needs gdal. Enventually, you need to run system("hombrew install gdal") before.
require(sf) # Combines sp, rgeos and rgdal functionality
# require(cartography)
require(ggplot2)
require(ggrepel)
require(stringr)
require(magrittr)
```
# COnvert new GeoJSON files to SVG
This takes all geojson more recent than the most recent svg file and converts them
```{r}
basedir <- dirname(rstudioapi::getActiveDocumentContext()$path)
geodatas <- list.files(file.path(basedir,"geojson"),pattern="world_.*geojson")
geodatas_fullpath <- list.files(file.path(basedir,"geojson"),pattern="world_.*geojson",full.names = TRUE)
geodatas_mtime = file.mtime(geodatas_fullpath)
# i <- 14
for (i in 1:length(geodatas)) {
geofile <- geodatas[i]
geofilePure <- str_extract(geofile,"[^\\.]*")
svg_files <- list.files(file.path(basedir,"svg"),pattern=geofilePure,full.names = TRUE)
svg_mtime <- ifelse(length(svg_files) > 0, file.mtime(svg_files[1]), as.Date("2021-09-01") %>% as.POSIXct())
if (geodatas_mtime[i] >= svg_mtime) { # update only SVGs more ancient than the current GeoJSON file
year <- str_extract(geofilePure,"[^\\_]*$")
geodata <- st_read(file.path(basedir,"geojson",geofile), quiet=TRUE)
if(!"SUBJECTO" %in% colnames(geodata)) {
geodata$SUBJECTO <- geodata$NAME
}
geodata <- st_transform(geodata, "+proj=natearth2")
# plot(st_geometry(geodata))
# plot(geodata["NAME"])
ggplot(data = geodata) +
geom_sf(aes(fill = SUBJECTO)) +
# geom_sf_text(aes(label=NAME),size=0.5) +
ggrepel::geom_text_repel(
data = geodata,
aes(label = NAME, geometry = geometry),
stat = "sf_coordinates",
min.segment.length = 0.2, # skip drawing segments shorter than this
max.overlaps = 30,
arrow = arrow(length = unit(0.05, 'cm'), type = 'closed'),
segment.color = 'grey',
size = 2
) +
theme(legend.position="none") +
labs(
title=paste0("The World in ",year),
subtitle= "Historical Boundaries of World Countries and Cultural Regions. (work in progress) https://github.com/aourednik/historical-basemaps",
x = element_blank(),
y = element_blank(),
caption = "André Ourednik & GitHub contributors, 2021"
)
ggsave(file.path(basedir,"svg",paste0(geofilePure,".svg")),width = 30, height=20)
}
}
```
# Documentation
* [cartographie avec R](https://rcarto.github.io/carto_avec_r/)
* [projections avec PROJ](https://proj.org/operations/projections/)