Changeset 9149


Ignore:
Timestamp:
May 5, 2011, 9:34:53 AM (14 years ago)
Author:
rick
Message:

Make sure that we generate a slightly bigger image, to ensure the borders are
properly set. Next chop/crop the image to match is proper size.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/django_gheat/website/tile.py

    r9148 r9149  
    1111import logging
    1212import numpy as np
    13 import settings
     13import pygame
    1414import sys
    15 import pygame
    1615import tempfile
    1716
     
    2322  def __init__(self, method, size):
    2423    self.surf = pygame.Surface(size,flags=pygame.SRCALPHA)
     24
     25  def center_crop(self,size):
     26    """ Resize to make centered rectange from image """
     27    new_surf = pygame.Surface(size, flags=pygame.SRCALPHA)
     28    curr_size = self.surf.get_size()
     29    new_surf.blit(self.surf,(0,0),
     30      ((curr_size[0] - size[0]) / 2, (curr_size[1] - size[1]) / 2, size[0], size[1]))
     31    self.surf = new_surf
    2532
    2633  def write(self, fh,format='png'):
     
    3441  def add_circle(self, center, radius, colour=(255,0,0)):
    3542    # Quirky hack to generate lineair gradient circles and merge them with the parent.
    36     new_surf = pygame.Surface((250,250),flags=pygame.SRCALPHA)
     43    new_surf = pygame.Surface(self.surf.get_size(),flags=pygame.SRCALPHA)
    3744    for r in range(radius,1,-1):
    3845      pygame.draw.circle(new_surf,colour + (255 - (r * (float(255)/radius)),),center,r,0)
     
    123130def make_tile(x,y,z):
    124131  """ Crude attempt to generate tiles, by placing a gradient circle on a
    125   coordinate point.
     132  coordinate point.  Generate a larger tile  and make sure to plot related
     133  points first and then crop it to the required size (250x250).
    126134 
    127135  Many stuff NOT implemented yet, like:
    128136  - Caching Images
    129   - Generate a larger tile (300x300) at first and then crop it to the required
    130     size (250x250).
    131137  """
     138  SIZE = 250
     139 
    132140  nw_deg,se_deg = boundbox_deg(x,y,z)
     141
    133142 
    134143  Picture = PyGamePicture
    135   resolution_deg = nw_deg.deg_per_pixel(se_deg, 250)
    136   im = Picture("RGBA",(250,250))
     144  resolution_deg = nw_deg.deg_per_pixel(se_deg, SIZE)
     145  # Converting LatLon to Meters is discussed here:
     146  #  http://stackoverflow.com/questions/3024404/transform-longitude-latitude-into-meters
     147  tile_height = float(40008000) / (2 ** z)
     148  meters_per_pixel = float(tile_height) / SIZE
     149 
     150  # Worst case scenario could a circle with 100% 'outside' our 250x250 range
     151  # also add data to the picture as circles are used
     152  border_pixels = 100 / meters_per_pixel / 2
     153
     154  im = Picture("RGBA", (SIZE + border_pixels * 2,) * 2)
     155
     156  nw_deg.lat += resolution_deg.lat * border_pixels
     157  nw_deg.lon -= resolution_deg.lon * border_pixels
     158  se_deg.lat -= resolution_deg.lat * border_pixels
     159  se_deg.lon += resolution_deg.lon * border_pixels
    137160 
    138161  lat_min = 999
     
    147170    return max(x,y) - min(x,y)
    148171 
    149   # Converting LatLon to Meters is discussed here:
    150   #  http://stackoverflow.com/questions/3024404/transform-longitude-latitude-into-meters
    151   tile_height = float(40008000) / (2 ** z)
    152   meters_per_pixel = float(tile_height) / 250
    153172  for meting in metingen:
    154173    lat_min = min(lat_min, meting.latitude)
     
    175194  log.info("Metingen Count: %s" % metingen.count())
    176195
     196  im.center_crop((SIZE,SIZE))
    177197  return im
    178198
Note: See TracChangeset for help on using the changeset viewer.