Created
May 24, 2016 12:34
-
-
Save vehrka/36f531b012caf8ddb1a993582915aec9 to your computer and use it in GitHub Desktop.
Print using Tilemill, Mapnik and Python
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# sudo add-apt-repository ppa:mapnik/nightly-2.3 | |
# sudo apt-get update | |
# sudo apt-get install libmapnik libmapnik-dev mapnik-utils python-mapnik | |
# sudo apt-get install mapnik-input-plugin-gdal mapnik-input-plugin-ogr mapnik-input-plugin-postgis mapnik-input-plugin-sqlite mapnik-input-plugin-osm | |
import psycopg2 | |
from mapnik import Map, load_map, Envelope, render_to_file | |
from invoke import run | |
from progressbar import ProgressBar | |
EXPORTDIR = 'mapas' | |
WIDTH = 1754 | |
HEIGHT = 1240 | |
run('mkdir -p {}'.format(EXPORTDIR)) | |
sqlpro = "SELECT cpro, st_xmin(enve)::integer, st_ymin(enve)::integer, st_xmax(enve)::integer, st_ymax(enve)::integer FROM ( SELECT cpro, st_transform(enve, 3857) enve FROM v_provhex ORDER BY 1 ) s1" | |
conn = psycopg2.connect("dbname=dbname host=host user=user password=password") | |
cur = conn.cursor() | |
cur.execute(sqlpro) | |
datpro = {cpro: [xmi, ymi, xma, yma] for cpro, xmi, ymi, xma, yma in cur} | |
lpro = list(datpro.keys()) | |
lpro.sort() | |
progress = ProgressBar(maxval=52).start() | |
for cpro in lpro: | |
run('cp master_info.xml p{0}_info.xml'.format(cpro)) | |
run("sed -i 's/XX/{0}/g' p{0}_info.xml".format(cpro)) | |
stylesheet = 'p{0}_info.xml'.format(cpro) | |
image = '{1}/p{0}.png'.format(cpro, EXPORTDIR) | |
m = Map(WIDTH, HEIGHT) | |
load_map(m, stylesheet) | |
lenv = datpro[cpro] | |
extent = (Envelope(lenv[0],lenv[1],lenv[2],lenv[3])) | |
m.zoom_to_box(extent) | |
render_to_file(m, 'tmp.png') | |
run('convert -bordercolor black -border 2 tmp.png {}'.format(image)) | |
run('rm {} tmp.png'.format(stylesheet)) | |
progress.update(int(cpro)) | |
progress.finish() | |
cur.close() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment