Skip to content

Instantly share code, notes, and snippets.

Last active June 21, 2018 14:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
"""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 :
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
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
Arg2 (optionnel): nom de la carte html en sortie qui contient le SVG
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) }
<img width=1024>
TEMPLATE_BEGIN_2 ="\n" + (" " * 160) + """
preserveAspectRatio="xMidYMid meet"
<g fill="red" stroke="blue" stroke-width="0" >
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
for i,polygon in enumerate(coord):
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
%s />""" % (dec, l["Code INSEE"], i, points, data)
boundary = dict( ymin = lower.imag, xmin=lower.real, width = (upper - lower).real, height=(upper - lower).imag)
out = open(map_name, "rb+") TEMPLATE_BEGIN_1,encoding="utf8")))
tow = """ transform="scale(1,-1)"
ViewBox="%(xmin)f %(ymin)f %(width)f %(height)f"\n""" % boundary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment