Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Created September 6, 2011 13:59
Show Gist options
  • Save springmeyer/1197587 to your computer and use it in GitHub Desktop.
Save springmeyer/1197587 to your computer and use it in GitHub Desktop.
Index: generate_tiles.py
===================================================================
--- generate_tiles.py (revision 26571)
+++ generate_tiles.py (working copy)
@@ -1,15 +1,15 @@
-#!/usr/bin/env python
+#!/usr/bin/python
from math import pi,cos,sin,log,exp,atan
from subprocess import call
import sys, os
from Queue import Queue
-
+import mapnik
import threading
+import random
+import argparse
-try:
- import mapnik2 as mapnik
-except:
- import mapnik
+custom_fonts_dir = '/usr/share/fonts/'
+mapnik.register_fonts(custom_fonts_dir)
DEG_TO_RAD = pi/180
RAD_TO_DEG = 180/pi
@@ -17,7 +17,6 @@
# Default number of rendering threads to spawn, should be roughly equal to number of CPU cores available
NUM_THREADS = 4
-
def minmax (a,b,c):
a = max(a,b)
a = min(a,c)
@@ -37,14 +36,14 @@
self.zc.append((e,e))
self.Ac.append(c)
c *= 2
-
+
def fromLLtoPixel(self,ll,zoom):
- d = self.zc[zoom]
- e = round(d[0] + ll[0] * self.Bc[zoom])
- f = minmax(sin(DEG_TO_RAD * ll[1]),-0.9999,0.9999)
+ d = self.zc[int(zoom)]
+ e = round(d[0] + float(ll[0]) * self.Bc[zoom])
+ f = minmax(sin(DEG_TO_RAD * float(ll[1])),-0.9999,0.9999)
g = round(d[1] + 0.5*log((1+f)/(1-f))*-self.Cc[zoom])
return (e,g)
-
+
def fromPixelToLL(self,px,zoom):
e = self.zc[zoom]
f = (px[0] - e[0])/self.Bc[zoom]
@@ -61,15 +60,14 @@
self.m = mapnik.Map(256, 256)
self.printLock = printLock
# Load style XML
- mapnik.load_map(self.m, mapfile, True)
- # Obtain <Map> projection
+ mapnik.load_map(self.m, mapfile)
+ # Obtain projection
self.prj = mapnik.Projection(self.m.srs)
# Projects between tile pixel co-ordinates and LatLong (EPSG:4326)
self.tileproj = GoogleProjection(maxZoom+1)
def render_tile(self, tile_uri, x, y, z):
-
# Calculate pixel positions of bottom-left & top-right
p0 = (x * 256, (y + 1) * 256)
p1 = ((x + 1) * 256, y * 256)
@@ -124,8 +122,8 @@
-def render_tiles(bbox, mapfile, tile_dir, minZoom=1,maxZoom=18, name="unknown", num_threads=NUM_THREADS, tms_scheme=False):
- print "render_tiles(",bbox, mapfile, tile_dir, minZoom,maxZoom, name,")"
+def render_tiles(bbox, mapfile, tile_dir, minZoom=1,maxZoom=18, name="unknown", num_threads=NUM_THREADS):
+ print "render_tiles(",bbox, mapfile, tile_dir, minZoom, maxZoom, name,")"
# Launch rendering threads
queue = Queue(32)
@@ -141,7 +139,7 @@
if not os.path.isdir(tile_dir):
os.mkdir(tile_dir)
- gprj = GoogleProjection(maxZoom+1)
+ gprj = GoogleProjection(maxZoom+1)
ll0 = (bbox[0],bbox[3])
ll1 = (bbox[2],bbox[1])
@@ -149,6 +147,7 @@
for z in range(minZoom,maxZoom + 1):
px0 = gprj.fromLLtoPixel(ll0,z)
px1 = gprj.fromLLtoPixel(ll1,z)
+ print "fromlattolon"
# check if we have directories in place
zoom = "%s" % z
@@ -156,7 +155,7 @@
os.mkdir(tile_dir + zoom)
for x in range(int(px0[0]/256.0),int(px1[0]/256.0)+1):
# Validate x co-ordinate
- if (x < 0) or (x >= 2**z):
+ if (x = 2**z):
continue
# check if we have directories in place
str_x = "%s" % x
@@ -164,20 +163,13 @@
os.mkdir(tile_dir + zoom + '/' + str_x)
for y in range(int(px0[1]/256.0),int(px1[1]/256.0)+1):
# Validate x co-ordinate
- if (y < 0) or (y >= 2**z):
+ if (y = 2**z):
continue
- # flip y to match OSGEO TMS spec
- if tms_scheme:
- str_y = "%s" % ((2**z-1) - y)
- else:
- str_y = "%s" % y
+ str_y = "%s" % y
tile_uri = tile_dir + zoom + '/' + str_x + '/' + str_y + '.png'
# Submit tile to be rendered into the queue
t = (name, tile_uri, x, y, z)
- try:
- queue.put(t)
- except KeyboardInterrupt:
- raise SystemExit("Ctrl-c detected, exiting...")
+ queue.put(t)
# Signal render threads to exit by sending empty request to queue
for i in range(num_threads):
@@ -190,66 +182,13 @@
if __name__ == "__main__":
- home = os.environ['HOME']
- try:
- mapfile = os.environ['MAPNIK_MAP_FILE']
- except KeyError:
- mapfile = home + "/svn.openstreetmap.org/applications/rendering/mapnik/osm-local.xml"
- try:
- tile_dir = os.environ['MAPNIK_TILE_DIR']
- except KeyError:
- tile_dir = home + "/osm/tiles/"
-
- if not tile_dir.endswith('/'):
- tile_dir = tile_dir + '/'
-
- #-------------------------------------------------------------------------
- #
- # Change the following for different bounding boxes and zoom levels
- #
- # Start with an overview
- # World
- bbox = (-180.0,-90.0, 180.0,90.0)
-
- render_tiles(bbox, mapfile, tile_dir, 0, 5, "World")
-
- minZoom = 10
- maxZoom = 16
- bbox = (-2, 50.0,1.0,52.0)
- render_tiles(bbox, mapfile, tile_dir, minZoom, maxZoom)
-
- # Muenchen
- bbox = (11.4,48.07, 11.7,48.22)
- render_tiles(bbox, mapfile, tile_dir, 1, 12 , "Muenchen")
-
- # Muenchen+
- bbox = (11.3,48.01, 12.15,48.44)
- render_tiles(bbox, mapfile, tile_dir, 7, 12 , "Muenchen+")
-
- # Muenchen++
- bbox = (10.92,47.7, 12.24,48.61)
- render_tiles(bbox, mapfile, tile_dir, 7, 12 , "Muenchen++")
-
- # Nuernberg
- bbox=(10.903198,49.560441,49.633534,11.038085)
- render_tiles(bbox, mapfile, tile_dir, 10, 16, "Nuernberg")
-
- # Karlsruhe
- bbox=(8.179113,48.933617,8.489252,49.081707)
- render_tiles(bbox, mapfile, tile_dir, 10, 16, "Karlsruhe")
-
- # Karlsruhe+
- bbox = (8.3,48.95,8.5,49.05)
- render_tiles(bbox, mapfile, tile_dir, 1, 16, "Karlsruhe+")
-
- # Augsburg
- bbox = (8.3,48.95,8.5,49.05)
- render_tiles(bbox, mapfile, tile_dir, 1, 16, "Augsburg")
-
- # Augsburg+
- bbox=(10.773251,48.369594,10.883834,48.438577)
- render_tiles(bbox, mapfile, tile_dir, 10, 14, "Augsburg+")
-
- # Europe+
- bbox = (1.0,10.0, 20.6,50.0)
- render_tiles(bbox, mapfile, tile_dir, 1, 11 , "Europe+")
+ MIN_LON = '29.5732';
+ MAX_LON = '35.0360';
+ MIN_LAT = '-1.4840';
+ MAX_LAT = '4.2144';
+ bbox = ( MIN_LON, MIN_LAT,MAX_LON, MAX_LAT)
+ style_file="/home/mossplix/projects/Dev/mapit/map/static/tilemill/uganda_districts.xml"
+ tile_dir="/home/mossplix/projects/UnicefDev/mapit/map/static/tiles/"
+ min_zoom=7
+ max_zoom=14
+ render_tiles(bbox, style_file, tile_dir, min_zoom, max_zoom)
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment