SHARE
TWEET

Untitled

a guest Mar 26th, 2020 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Mar 25 22:04:48 2020
  5.  
  6. @author: florian
  7. """
  8. import geopandas as gpd
  9. import pandas as pd
  10. import numpy as np
  11.  
  12. # %% data spatiales
  13. #  https://www.data.gouv.fr/fr/datasets/r/ab21d892-aa39-466a-a3ed-dc25ff076b78
  14. shapefile = gpd.read_file("/home/florian/Downloads/dpts/departements-20140306-5m.shp")
  15.  
  16. # %% data train
  17. #  https://www.data.gouv.fr/fr/datasets/frequentation-en-gares/
  18. gareFile = "/home/florian/Downloads/frequentation-gares.csv"
  19. gares = pd.read_csv(gareFile, delimiter=";")
  20. # sum by dpt
  21. freqGares = np.zeros(shapefile.shape[0])
  22.  
  23. # %% data pop/age/sex
  24. #  https://www.insee.fr/fr/statistiques/1893198
  25. ageFile = '/home/florian/Downloads/estim-pop-dep-sexe-gca-1975-2020.xls'
  26. ages = pd.read_excel(ageFile)
  27.  
  28. # %% sort and sum by dpt data train
  29. for i in range(gares.shape[0]):
  30.     gare = gares.loc[i]
  31.     cp = str(gare["Code postal"])
  32.     if len(cp) < 5:
  33.         cp = "0" + cp
  34.     dpt = int(cp[:2]) - 1  # parceque les dpts commencent à 01 et les arrays a 0
  35.     freq = gare["Total Voyageurs 2018"]
  36.     freqGares[dpt] += freq
  37.  
  38. # %% create dict
  39. franceGeom = {}
  40. dptID = 0
  41. for dpt in range(shapefile.shape[0]):
  42.     dptID += 1
  43.     dptGeom = shapefile["geometry"][dpt]
  44.     franceGeom[shapefile["nom"][dpt]] = {}
  45.     franceGeom[shapefile["nom"][dpt]]["neighbours"] = {}
  46.     franceGeom[shapefile["nom"][dpt]]["length"] = {}
  47.     franceGeom[shapefile["nom"][dpt]]["freqGare"] = freqGares[dpt]
  48.     franceGeom[shapefile["nom"][dpt]]["agesSex"] = ages.loc[dpt]
  49.     print("\n\n", shapefile["nom"][dpt], " centroid is ", dptGeom.centroid.coords[0])
  50.     print("\n\n", shapefile["nom"][dpt], " ages/sex : ", franceGeom[shapefile["nom"][dpt]]["agesSex"])
  51.  
  52.     franceGeom[shapefile["nom"][dpt]]["centroid"] = dptGeom.centroid.coords[0]
  53.     # look for neighbours
  54.     neighList = []
  55.     lenList = []
  56.  
  57.     for nb in range(shapefile.shape[0] - 1):
  58.         if nb != dpt:
  59.             neighbour = shapefile["geometry"][nb]
  60.             if dptGeom.touches(neighbour):
  61.                 neighList.append(shapefile["nom"][nb])
  62.                 l = dptGeom.boundary.intersection(neighbour.boundary)
  63.                 l = l.length
  64.                 lenList.append(l)
  65.  
  66.                 print(
  67.                     "\t",
  68.                     shapefile["nom"][dpt],
  69.                     " touches ",
  70.                     shapefile["nom"][nb],
  71.                     " on ",
  72.                     l,
  73.                     " m",
  74.                 )
  75.  
  76.     franceGeom[shapefile["nom"][dpt]]["neighbours"] = neighList
  77.     franceGeom[shapefile["nom"][dpt]]["length"] = lenList
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top