Browse Source

Préparation données séparée

geojson
U-U001PRD\u992093 7 years ago
parent
commit
532d635b7d
7 changed files with 65 additions and 6022 deletions
  1. +1
    -23
      app.R
  2. BIN
      communes_byCP/communes_byCP.dbf
  3. +0
    -5998
      communes_byCP/communes_byCP.json
  4. +0
    -1
      communes_byCP/communes_byCP.prj
  5. BIN
      communes_byCP/communes_byCP.shp
  6. BIN
      communes_byCP/communes_byCP.shx
  7. +64
    -0
      preparefiles.R

+ 1
- 23
app.R View File

@@ -1,10 +1,6 @@
library(tidyverse)
library(shiny)
library(leaflet)
library(RColorBrewer)
library(sp)
library(rgdal)
library(plotly)
library(stringr)
library(lubridate)
library(jsonlite)
@@ -26,26 +22,8 @@ names(racine) <- racines$codlib
racine <- racine[racine %in% GHM$GHM]
rm(racines)

# # Lecture codes postaux <-> codes insee et sauvegarde carte pour simplification avec mapshaper
# read_csv2("insee.csv", col_types = cols(INSEE = col_character())) %>%
# select(Codepos, insee = INSEE) %>%
# mutate(insee = insee %>% str_pad(5, "left", "0")) -> insee
# # Lecture carte et join avec codes postaux ----
# readOGR("communes", "communes-20150101-100m") -> communes
# communes@data %>%
# left_join(insee) -> communes@data
# rm(insee)
# communes <- communes[!is.na(communes$Codepos),]
# writeOGR(communes, "communes_byCP", "communes_byCP", driver = "ESRI Shapefile")

# Lecture carte ----
readOGR("communes_byCP", "communes_byCP") -> communes
fromJSON("communes_byCP/communes_byCP.json") -> geojson

# Filtre Codes existants dans la base
communes <- communes[communes$Codepos %in% GHM$Codepos,]
communes@data$Codepos <- communes@data$Codepos %>% as.character

fromJSON("communes.json") -> geojson

# long/lat de chaque commune ----



BIN
communes_byCP/communes_byCP.dbf View File


+ 0
- 5998
communes_byCP/communes_byCP.json
File diff suppressed because it is too large
View File


+ 0
- 1
communes_byCP/communes_byCP.prj View File

@@ -1 +0,0 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

BIN
communes_byCP/communes_byCP.shp View File


BIN
communes_byCP/communes_byCP.shx View File


+ 64
- 0
preparefiles.R View File

@@ -0,0 +1,64 @@
library(sp)
library(rgdal)
library(stringr)
library(tidyverse)
library(jsonlite)
library(maptools)
library(geojsonio)

# Lecture codes postaux <-> codes insee (! padding)
# https://raw.githubusercontent.com/Rudloff/french-postal-codes-api/master/insee.csv
read_csv2("insee.csv") %>%
select(Codepos, insee = INSEE) %>%
mutate(insee = insee %>% str_pad(5, "left", "0")) %>%
distinct -> insee

# Lecture carte et join avec codes postaux (! Corse)
# http://osm13.openstreetmap.fr/~cquest/openfla/export/communes-20150101-100m-shp.zip
readOGR("communes", "communes-20150101-100m") -> communes
communes@data %>%
mutate(insee = insee %>% str_replace("2(A|B)", "20")) %>%
left_join(insee) -> communes@data
rm(insee)

# Écriture shapefile avec codes postaux
communes <- communes[!is.na(communes$Codepos),]
#writeOGR(communes, "communes_byCP", "communes_byCP", driver = "ESRI Shapefile")

communes <- unionSpatialPolygons(communes, communes$Codepos)

# Conversion geojson
communes %>% geojson_list -> geojson

geojson_write(geojson, file = "communes.json")

# Ajout centroides
geojson$features$geometry$coordinates %>%
map(function(feature)
{
if (!is.list(feature))
feature <- list(feature)
feature %>%
map(function(polygon)
{
if(dim(polygon)[1] > 1)
polygon %>% apply(2, mean)
else
polygon[1,,] %>% apply(2, mean)
}) %>%
{
c(map_dbl(.,1) %>% mean,
map_dbl(.,2) %>% mean)
}
}) -> geojson$features$properties$centroids

# Ajout id
geojson$features$id <- geojson$features$properties$Codepos

geojson$features <- lapply(geojson$features, function(feat)
{
feat$id <- feat$properties$Codepos
feat
})

Loading…
Cancel
Save