Skip to content

Instantly share code, notes, and snippets.

@pifantastic
Created November 5, 2009 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pifantastic/227509 to your computer and use it in GitHub Desktop.
Save pifantastic/227509 to your computer and use it in GitHub Desktop.
def draw_tile(x, y, z):
# Create a blank, transparent tile
tile = Image.new("RGBA", (gmap.TILE_SIZE, gmap.TILE_SIZE), (0, 0, 0, 0))
# Query for Zipcodess within bounding box
rect = gmap.getTileRect(x, y, z)
bbox = Polygon.from_bbox((rect.x, rect.y, (rect.x + rect.width), (rect.y + rect.height)))
zipcodes = Zipcode.objects.filter(geom__intersects=bbox)
# Draw polygons on tile
for zipcode in zipcodes:
# Set the color for this tile
color = zipcode.color
draw = ImageDraw.Draw(tile)
for poly in zipcode.geom.coords[0]:
def pixel_coords(coords):
point = gmap.toZoomedPixelCoords(coords[1], coords[0], z)
point.x = point.x - (gmap.TILE_SIZE * x)
point.y = point.y - (gmap.TILE_SIZE * y)
return (int(point.x), int(point.y))
poly = map(pixel_coords, poly)
draw.polygon(poly, fill=color, outline=settings.TILE_OUTLINE)
del draw
# Set opacity
alpha = tile.split()[0]
alpha = ImageEnhance.Brightness(alpha).enhance(settings.TILE_OPACITY)
tile.putalpha(alpha)
# Save file
filename = "%d-%d-%d.png" % (x, y, z)
filename = os.path.join(settings.WEB_ROOT, "tile", filename)
tile.save(filename, "PNG")
# Save tile to database
tile = Tile(filename=filename)
tile.save()
if len(zipcodes) > 0:
tile.zipcodes.add(list(zipcodes))
tile.save()
return filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment