Created
May 16, 2017 20:57
Star
You must be signed in to star a gist
Script population proximité centrale nucléaire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Auteur: Lucas Gautheron | |
# Données population : https://www.insee.fr/fr/statistiques/2520034 | |
import sys | |
import re | |
from lxml import etree | |
import dbf | |
from pyproj import Proj, transform | |
from geopy.distance import vincenty, great_circle | |
inCoords = Proj(proj='latlong',datum='WGS84') | |
outCoords = Proj(init='epsg:3035') | |
thresholds = [ 10, 20, 25, 30, 50, 75, 100, 150, 200, 100000 ] | |
populations = {} | |
for t in thresholds: | |
populations[t] = 0 | |
def distance(a, b): | |
return great_circle(a,b) | |
#import reverse_geocoder as rg | |
def parse_plants(): | |
plants = [] | |
tree = etree.parse("plants.xml") | |
for coordinates in tree.xpath("//coordinates" if int(sys.argv[1]) == 0 else "//coordinates[not(@french='1')]"): | |
m = re.search('((?<![a-zA-Z:])[-+]?\d*\.?\d+),((?<![a-zA-Z:])[-+]?\d*\.?\d+)?,0', coordinates.text) | |
lat = float(m.group(2)) | |
long = float(m.group(1)) | |
x, y = transform(inCoords,outCoords,lat,long) | |
plants.append({'lat': lat, 'long': long, 'x': x, 'y': y}) | |
return plants | |
plants = parse_plants() | |
table = dbf.Table("car_m.dbf") | |
table.open() | |
count = 0 | |
for rec in table: | |
m = re.search('CRS3035RES200mN(-?[0-9]+)E(-?[0-9]+)', rec.idinspire) | |
x = m.group(2) | |
y = m.group(1) | |
population = rec.ind_c | |
longitude, latitude = transform(outCoords, inCoords, x, y) | |
#print (latitude, longitude, rec.id, rec.idinspire, rec.idk, rec.ind_c) | |
min_distance = 1e100 | |
for p in plants: | |
d = distance((latitude, longitude), (p['lat'], p['long'])) | |
if d.kilometers < min_distance: | |
min_distance = d.kilometers | |
for t in thresholds: | |
if min_distance < t: | |
populations[t] = populations[t] + population | |
count = count + 1 | |
if count % 10000 == 0: | |
print (count) | |
print(populations) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment