[OSM-talk] [OSM-dev] Bug Bounties
Robert (Jamie) Munro
rjmunro at arjam.net
Tue Apr 24 13:06:02 BST 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Roozbeh Pournader wrote:
> On 4/23/07, SteveC <steve at asklater.com> wrote:
>> This is an experiment to see if any bugs get closed more quickly. On
>> receipt of an email to this list with the first working patch for each
>> bug you'll not only get a warm glow in your tummy but a bounty for
>> completion. [...]
>> python:
>> £10 get ugly tiles code nice http://trac.openstreetmap.org/ticket/415
>
> Patch attached for that. Needed to learn ruby for that, but anyway it
> was a nice experience. Please donate the GBP 20 to EFF.
>
> Roozbeh
I thought the idea of that bug was to replace the whole thing with a
single script like this (untested as I don't have a tile server):
#!/usr/bin/python
import fileinput
from math import pi,cos,sin,log,exp,atan
from subprocess import Popen
DEG_TO_RAD = pi/180
RAD_TO_DEG = 180/pi
def minmax (a,b,c):
a = max(a,b)
a = min(a,c)
return a
class GoogleProjection:
def __init__(self,levels=18):
self.Bc = []
self.Cc = []
self.zc = []
self.Ac = []
c = 256
for d in range(0,levels):
e = c/2;
self.Bc.append(c/360.0)
self.Cc.append(c/(2 * pi))
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)
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]
g = (px[1] - e[1])/-self.Cc[zoom]
h = RAD_TO_DEG * ( 2 * atan(exp(g)) - 0.5 * pi)
return (f,h)
import os
from PIL.Image import fromstring, new
from PIL.ImageDraw import Draw
from StringIO import StringIO
from mapnik import *
mapfile = "/home/steve/osm.xml"
tile_dir = '/tmp/tiles/'
maxZoom = 18
gprj = GoogleProjection(maxZoom+1)
m = Map(2 * 256,2 * 256)
load_map(m,mapfile)
prj = Projection("+proj=merc +datum=WGS84")
def insert_tile(x,y,z,filename):
print "tile %s,%s,%s size is %s" % (x,y,z,File.size(filename))
if os.path.getsize(filename) == 158: # its blank
cursor.execute("delete from tiles where x=? and y=? and z=?;",(x,y,z))
else:
print 'inserting tile'
file = open(filename, "rb")
data = file.read()
file.close()
cursor.execute("update tiles set data=?, created_at=NOW(),
dirty_t='false' where x=? and y=? and z=?;",(data,x,y,z))
os.remove(filename)
def dotile(x,y,z):
x_str = "%s" % x
y_str = "%s" % y
z_str = "%s" % z
p0 = gprj.fromPixelToLL((x * 256, (y+1) * 256),z)
p1 = gprj.fromPixelToLL(((x+1) * 256, y* 256),z)
print z,x,y,p0,p1
# render a new tile and store it on filesystem
c0 = prj.forward(Coord(p0[0],p0[1]))
c1 = prj.forward(Coord(p1[0],p1[1]))
bbox = Envelope(c0.x,c0.y,c1.x,c1.y)
bbox.width(bbox.width() * 2)
bbox.height(bbox.height() * 2)
m.zoom_to_box(bbox)
if not os.path.isdir(tile_dir + z_str):
os.mkdir(tile_dir + z_str)
if not os.path.isdir(tile_dir + z_str + '/' + x_str):
os.mkdir(tile_dir + z_str + '/' + x_str)
tile_uri = tile_dir + z_str + '-' + x_str + '-' + y_str + '.png'
im = Image(512, 512)
render(m, im)
im = fromstring('RGBA', (512, 512), rawdata(im))
im = im.crop((128,128,512-128,512-128))
fh = open(tile_uri,'w+b')
im.save(fh, 'PNG', quality=100)
command = "convert -colors 255 %s %s" % (tile_uri,tile_uri)
p = Popen(command, shell=True)
sts = os.waitpid(p.pid, 0)
insert_tile(x_str, y_str, z_str, tile_uri)
# Main program starts here
# check for pidfile
pidfile = '/tmp/tilegen.pid'
try:
pf = file(pidfile,'r')
oldpid = int(pf.read().strip())
pf.close()
# exit if the lockfile is there
print "Already running with pid %s" % oldpid
sys.exit()
except IOError:
pass # File not found
# Write our pidfile
pid = str(os.getpid())
file(pidfile,'w+').write("%s\n" % pid)
import MySQLdb
db = MySQLdb.connect(host="localhost", user="tile", passwd="tile",db="tile")
tile_cursor = db.cursor()
tile_query = "select x,y,z from tiles where dirty_t='true' limit 10000"
tile_cursor.execute(tile_query)
for tile_data in tile_cursor:
dotile(tile_data[0], tile_data[1], tile_data[2])
# Remove pidfile when we finish
os.remove(pidfile)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFGLfKoz+aYVHdncI0RAoxkAKCEAeMytFMh+DfiTMBDssjdbnY2SgCeOcOm
rHuzYx+t8X1sFk4ezbr8PZg=
=HT+r
-----END PGP SIGNATURE-----
More information about the talk
mailing list