Last active
June 21, 2018 14:09
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
"""Licence : Julien Tayon (2018) | |
If you find me a job in GIS or BigData I will change this license to a full | |
2 clauses BSD | |
Licence proprietaire sur un fichier a code source ouvert | |
ca me laissera le plaisir de poursuivre ceux qui le reutilisent sans mon accord | |
Proprietary licence with opened source. This a bait for all the newbies | |
that copy paste from the internet without taking attention of the licenses. | |
This code is BSD licensed only for : | |
Wikimedia | |
Wikipedia | |
all the BSD projects | |
Ligue des Droits de L'Homme | |
Public highschools as long it is not reused out of the schools usage | |
PS bug probable autour du seek/write du aux IO de windows, symptome en tete magane autour de la balise svg/ViewBox | |
""" | |
### STDLIB ONLY | |
from sys import argv | |
from csv import DictReader | |
from sys import exit | |
from json import loads, dumps | |
from functools import reduce | |
from time import sleep | |
if len(argv) < 2: | |
print("""Generateur de carte SVG depuis donnees INSEE | |
Arg1 : donnees issue de https://data.opendatasoft.com/explore/dataset/code-postal-code-insee-2015@public/ | |
Arg2 (optionnel): nom de la carte html en sortie qui contient le SVG | |
""") | |
exit(0) | |
filename = argv[1] | |
map_name = len(argv) > 2 and argv[2] or "carte_insee.html" | |
f = open(filename, encoding="utf8") | |
out = open(map_name, "w" , encoding="utf8", newline="\n") | |
csv = DictReader(f, delimiter=";") | |
initial = "Code INSEE;Code Postal;Commune;Département;Région;Statut;Altitude Moyenne;Superficie;Population;geo_point_2d;ID Geofla;Code Commune;Code Canton;Code Arrondissement;Code Département;Code Région".split(";") | |
dest = "insee;code_postal;commune;departement;region;statut;altitude;superficie;population;geo_point_2d;geo_flaid;code_commune;code_canton;code_arrondissement;code_departement;code_region".split(";") | |
translate = { s:d for s,d in zip(initial, dest) } | |
TEMPLATE_BEGIN_1 = """ | |
<html> | |
<header> | |
<style> | |
</style> | |
</header> | |
<body> | |
<img width=1024> | |
<svg | |
""" | |
TEMPLATE_BEGIN_2 ="\n" + (" " * 160) + """ | |
preserveAspectRatio="xMidYMid meet" | |
> | |
<g fill="red" stroke="blue" stroke-width="0" > | |
""" | |
TEMPLATE_BEGIN = TEMPLATE_BEGIN_1 + TEMPLATE_BEGIN_2 | |
TEMPLATE_END = """ | |
</g> | |
</svg> | |
</img> | |
</body> | |
</html> | |
""" | |
minc = lambda a,b: complex(min(a.real, b.real), min(b.imag, a.imag)) | |
maxc = lambda a,b: complex(max(a.real, b.real), max(b.imag, a.imag)) | |
lower = complex(4.504753386804683,48.8154004451761454) | |
upper = complex(4.504753386804683,48.8154004451761454) | |
dec="\n" + " "*4 * 3 | |
decp="\n" + " "*4 * 4 | |
out.write(TEMPLATE_BEGIN ) | |
for l in csv: | |
coord = loads(l["geo_shape"])["coordinates"] | |
if l["Code Département"] == "97": | |
#carte france metropolitaine | |
# pour les DOMTOMs, faite l'oppose | |
continue | |
for i,polygon in enumerate(coord): | |
try: | |
coords= list(map(lambda x:complex(*x), polygon)) | |
except TypeError: | |
### Lol Multipolygon | |
coords= list(map(lambda x:complex(*x), polygon[0])) | |
lower = minc(reduce(minc, coords), lower) | |
upper = maxc(reduce(maxc, coords), upper) | |
data = decp.join( ["%s=\"%s\"" % (translate[k], l[k]) for k in initial ]) | |
#just for fun | |
data += decp + "style='fill:rgb(%d,%d,0)'" % ( | |
min(256,int(float(l["Altitude Moyenne"])/5)), | |
min(256,int(float(l["Population"])* 10)), | |
) | |
points = " ".join(["%.4f,%.4f" % (c.real,c.imag) for c in coords ]) | |
el = """%s<polygon | |
id="%s_%d" | |
points="%s" | |
%s />""" % (dec, l["Code INSEE"], i, points, data) | |
out.write(el) | |
boundary = dict( ymin = lower.imag, xmin=lower.real, width = (upper - lower).real, height=(upper - lower).imag) | |
out.write(TEMPLATE_END) | |
out.close() | |
out = open(map_name, "rb+") | |
out.seek(len(bytearray( TEMPLATE_BEGIN_1,encoding="utf8"))) | |
tow = """ transform="scale(1,-1)" | |
ViewBox="%(xmin)f %(ymin)f %(width)f %(height)f"\n""" % boundary | |
out.write(bytearray(tow,encoding="utf8")) | |
out.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment