Ejemplo n.º 1
0
    def hook_rebuild(self):
        self.img  = Image.new('RGBA', (SIZE,SIZE), (255,255,255,0))
        draw = ImageDraw.Draw(self.img)
        
        if len(self.colorScheme) > 0:
            colors = self.colorScheme.keys()
            for color in colors:
                objIDs = self.colorScheme[color]
                if self.shapeType == "POINT":
                    pts = []
                    for i in objIDs:
                        x,y = self.shapeObjects[i]
                        x, y = gmerc.ll2px(y,x, self.zoom)
                        x = x - self.x1 -self.pad# account for tile offset relative to 
                        y = y - self.y1 -self.pad#  overall map
                        pts.append((x,y))
                    pts = list(set(pts))
                    for x,y in pts:
                        draw.ellipse( (x-2,y-2,x+2,y+2), fill=color,outline="black") 
                elif self.shapeType == "MULTIPOLYGON":
                    for i in objIDs:
                        poly = self.shapeObjects[i]
                        _poly = []
                        for pt in poly:
                            x, y = gmerc.ll2px(pt[1],pt[0], self.zoom)
                            x = x - self.x1 -self.pad# account for tile offset relative to 
                            y = y - self.y1 -self.pad#  overall map
                            _poly.append((x,y))
                        draw.polygon(_poly, outline="black", fill=color)
            return self.img
 

        if self.shapeType == "POINT":
            pts = []
            for x,y in self.shapeObjects:
                x, y = gmerc.ll2px(y,x, self.zoom)
                x = x - self.x1 -self.pad# account for tile offset relative to 
                y = y - self.y1 -self.pad#  overall map
                pts.append((x,y))
            pts = list(set(pts))
            for x,y in pts:
                draw.ellipse( (x-2,y-2,x+2,y+2), fill="red",outline="black") 

        elif self.shapeType == "POLYGON":
            for obj in self.shapeObjects:
                for poly in obj.parts:
                    _poly = []
                    for pt in poly:
                        x, y = gmerc.ll2px(pt[1],pt[0], self.zoom)
                        x = x - self.x1 -self.pad# account for tile offset relative to 
                        y = y - self.y1 -self.pad#  overall map
                        _poly.append((x,y))
                    draw.polygon(_poly, outline="black", fill="blue")
                    
        return self.img
Ejemplo n.º 2
0
  def __init__(self, layer, zoom, x, y):
    log.info("Initializing tile");
    self.layer = layer
    self.zoom = zoom
    self.x = x
    self.y = y
    self.color_scheme = color_scheme.cyan_red

    # attempt to get a cached object
    self.tile_dump = self.__get_cached_image()
    if not self.tile_dump:
      log.info("No tile was found, creating one from the input data")

      # Get the bounds of this tile
      self.width, self.height = gmerc.ll2px(-90, 180, self.zoom)
      self.numcols = int(math.ceil(self.width / 256.0))
      self.numrows = int(math.ceil(self.height / 256.0))
      self.zoom_step = [ 180. / self.numrows, 360. / self.numcols ]
      self.georange = ( min(90, max(-90, 180. / self.numrows * y - 90)), min(180, max(-180, 360. / self.numcols * x - 180 )))

      # Extra info for provider.get_data
      extras = {"lat_north": self.georange[0],
                "lng_west": self.georange[1],
                "range_lat": self.zoom_step[0],
                "range_lng": self.zoom_step[1]}

      # Get the points and start plotting data
      log.info("Getting the tile points from the provider")
      data = provider.get_data(self.zoom, self.layer, **extras)

      log.info("Plotting the tile")
      self.tile_img = self.plot_image(data)
Ejemplo n.º 3
0
    def center2corners(self, center):
        """Gets the lat/lng value of four corners of the image file
        """

        center_px = ll2px(center["lat"], center["lng"], ZOOM_LEVEL)
        west_north_corner = px2ll(center_px[0] - SIZE / 2,
                                  center_px[1] - SIZE / 2, ZOOM_LEVEL)
        east_north_corner = px2ll(center_px[0] + SIZE / 2,
                                  center_px[1] - SIZE / 2, ZOOM_LEVEL)
        west_south_corner = px2ll(center_px[0] - SIZE / 2,
                                  center_px[1] + SIZE / 2, ZOOM_LEVEL)
        east_south_corner = px2ll(center_px[0] + SIZE / 2,
                                  center_px[1] + SIZE / 2, ZOOM_LEVEL)
        return [{
            "lat": west_north_corner[0],
            "lng": west_north_corner[1]
        }, {
            "lat": east_north_corner[0],
            "lng": east_north_corner[1]
        }, {
            "lat": west_south_corner[0],
            "lng": west_south_corner[1]
        }, {
            "lat": east_south_corner[0],
            "lng": east_south_corner[1]
        }]
Ejemplo n.º 4
0
 def points():
     """Yield x,y pixel coords within this tile, top-left of dot.
     """
     for point in q.all():
         x, y = gmerc.ll2px(point.latitude, point.longitude, self.zoom)
         x = x - self.x1 # account for tile offset relative to 
         y = y - self.y1 #  overall map
         yield x-self.pad,y-self.pad,point.magnitude
Ejemplo n.º 5
0
 def points():
     """Yield x,y pixel coords within this tile, top-left of dot.
     """
     for point in _points:
         x, y = gmerc.ll2px(point['lat'], point['lng'], self.zoom)
         x = x - self.x1 # account for tile offset relative to 
         y = y - self.y1 #  overall map
         yield x-self.pad,y-self.pad
Ejemplo n.º 6
0
 def get(self):
     for metro_id, metro in metros.metro_list.items():
         s = metro['lat']['start'] - .1
         w = metro['lng']['start'] - .1
         n = metro['lat']['end'] + .1
         e = metro['lng']['end'] + .1       
         for zoom in [13, 14]:
             x1, y1 = gmerc.ll2px(n, w, zoom)
             x2, y2 = gmerc.ll2px(s, e, zoom)
             startx = int(math.ceil(x1 / 256.0))
             starty = int(math.ceil(y1 / 256.0))
             endx = int(math.ceil(x2 / 256.0))
             endy = int(math.ceil(y2 / 256.0))
             for x in [startx, endx]:
                 for y in [starty, endy]:
                     genTile(zoom, x, y, metro_id)
     self.response.out.write('Starting at: %d, %d, ending at: %d, %d for zoom: %d\n' % (startx, starty, endx, endy, zoom))
Ejemplo n.º 7
0
 def get(self):
     for metro_id, metro in metros.metro_list.items():
         s = metro['lat']['start'] - .1
         w = metro['lng']['start'] - .1
         n = metro['lat']['end'] + .1
         e = metro['lng']['end'] + .1
         for zoom in [13, 14]:
             x1, y1 = gmerc.ll2px(n, w, zoom)
             x2, y2 = gmerc.ll2px(s, e, zoom)
             startx = int(math.ceil(x1 / 256.0))
             starty = int(math.ceil(y1 / 256.0))
             endx = int(math.ceil(x2 / 256.0))
             endy = int(math.ceil(y2 / 256.0))
             for x in [startx, endx]:
                 for y in [starty, endy]:
                     genTile(zoom, x, y, metro_id)
     self.response.out.write(
         'Starting at: %d, %d, ending at: %d, %d for zoom: %d\n' %
         (startx, starty, endx, endy, zoom))
Ejemplo n.º 8
0
 def points():
     """Yield x,y pixel coords within this tile, top-left of dot.
     """
     for point in _points:
         x, y = gmerc.ll2px(point['loc'][1], point['loc'][0], self.zoom)
         x = x - self.x1 # account for tile offset relative to 
         y = y - self.y1 #  overall map
         if point["type"] in self.pad:
             yield { 
                 "loc" : [x-self.pad[point["type"]], y-self.pad[point["type"]]],
                 "category" : point["type"]
             }
Ejemplo n.º 9
0
    def hook_rebuild(self, shp_objects, shp_type):
        self.img  = Image.new('RGBA', (SIZE,SIZE), (255,255,255,0))
        draw = ImageDraw.Draw(self.img)

        if shp_type == pysal.cg.Point:
            for x,y in shp_objects:
                x, y = gmerc.ll2px(y,x, self.zoom)
                x = x - self.x1 # account for tile offset relative to 
                y = y - self.y1 #  overall map
                draw.ellipse( (x-2,y-2,x+2,y+2), fill="red",outline="black") 

        elif shp_type == pysal.cg.Polygon:
            for obj in shp_objects:
                for poly in obj.parts:
                    _poly = []
                    for pt in poly:
                        x, y = gmerc.ll2px(pt[1],pt[0], self.zoom)
                        x = x - self.x1 # account for tile offset relative to 
                        y = y - self.y1 #  overall map
                        _poly.append((x,y))
                    draw.polygon(_poly, outline="black", fill="blue")
                    
        return self.img
Ejemplo n.º 10
0
  def __init__(self, user, zoom, lat_north, lng_west, offset_x_px, offset_y_px):
    self.zoom = zoom
    self.decay = 0.5
    #dot_radius = int(math.ceil(len(dot[self.zoom]) / 2))
    dot_radius = int(math.ceil((self.zoom + 1) * DOT_MULT)) #TODO double check that this is + 1 - because range started from 1 in old dot array?!

    # convert to pixel first so we can factor in the dot radius and get the tile bounds
    northwest_px = gmerc.ll2px(lat_north, lng_west, zoom)

    self.northwest_ll_buffered = gmerc.px2ll(northwest_px[0] + offset_x_px       - dot_radius, northwest_px[1] + offset_y_px       - dot_radius, zoom)
    self.northwest_ll          = gmerc.px2ll(northwest_px[0] + offset_x_px                   , northwest_px[1] + offset_y_px                   , zoom)

    self.southeast_ll_buffered = gmerc.px2ll(northwest_px[0] + offset_x_px + 256 + dot_radius, northwest_px[1] + offset_y_px + 256 + dot_radius, zoom)
    self.southeast_ll          = gmerc.px2ll(northwest_px[0] + offset_x_px + 256             , northwest_px[1] + offset_y_px + 256             , zoom) # THIS IS IMPORTANT TO PROPERLY CALC latlng_diff

    self.latlng_diff_buffered = [ self.southeast_ll_buffered[0] - self.northwest_ll_buffered[0], self.southeast_ll_buffered[1] - self.northwest_ll_buffered[1]]
    self.latlng_diff          = [ self.southeast_ll[0]          - self.northwest_ll[0]         , self.southeast_ll[1]          - self.northwest_ll[1]]

    BasicTile.__init__(self, user, self.northwest_ll_buffered[0], self.northwest_ll_buffered[1], self.latlng_diff_buffered[0], self.latlng_diff_buffered[1])
Ejemplo n.º 11
0
  def __init__(self, user, zoom, lat_north, lng_west, offset_x_px, offset_y_px):
    self.zoom = zoom
    self.decay = 0.5
    #dot_radius = int(math.ceil(len(dot[self.zoom]) / 2))
    dot_radius = int(math.ceil((self.zoom + 1) * DOT_MULT)) #TODO double check that this is + 1 - because range started from 1 in old dot array?!

    # convert to pixel first so we can factor in the dot radius and get the tile bounds
    northwest_px = gmerc.ll2px(lat_north, lng_west, zoom)

    self.northwest_ll_buffered = gmerc.px2ll(northwest_px[0] + offset_x_px       - dot_radius, northwest_px[1] + offset_y_px       - dot_radius, zoom)
    self.northwest_ll          = gmerc.px2ll(northwest_px[0] + offset_x_px                   , northwest_px[1] + offset_y_px                   , zoom)

    self.southeast_ll_buffered = gmerc.px2ll(northwest_px[0] + offset_x_px + 256 + dot_radius, northwest_px[1] + offset_y_px + 256 + dot_radius, zoom)
    self.southeast_ll          = gmerc.px2ll(northwest_px[0] + offset_x_px + 256             , northwest_px[1] + offset_y_px + 256             , zoom) # THIS IS IMPORTANT TO PROPERLY CALC latlng_diff

    self.latlng_diff_buffered = [ self.southeast_ll_buffered[0] - self.northwest_ll_buffered[0], self.southeast_ll_buffered[1] - self.northwest_ll_buffered[1]]
    self.latlng_diff          = [ self.southeast_ll[0]          - self.northwest_ll[0]         , self.southeast_ll[1]          - self.northwest_ll[1]]

    BasicTile.__init__(self, user, self.northwest_ll_buffered[0], self.northwest_ll_buffered[1], self.latlng_diff_buffered[0], self.latlng_diff_buffered[1])
Ejemplo n.º 12
0
  def __init__(self, layer, zoom, x, y):
    self.layer = layer
    self.zoom = zoom
    self.x = x
    self.y = y
    self.color_scheme = color_scheme.cyan_red
    self.decay = 0.5

    # attempt to get a cached object
    self.tile_dump = self.__get_cached_image()
    if not self.tile_dump:
      # Get the bounds of this tile
      self.width, self.height = gmerc.ll2px(-90, 180, self.zoom)
      self.numcols = int(math.ceil(self.width / 256.0))
      self.numrows = int(math.ceil(self.height / 256.0))
      self.zoom_step = [ 180. / self.numrows, 360. / self.numcols ]
      self.georange = ( min(90, max(-90, 180. / self.numrows * y - 90)), min(180, max(-180, 360. / self.numcols * x - 180 )))

      # Get the points and start plotting data
      self.tile_img = self.plot_image(
          provider.get_data(self.zoom, self.layer, 
                            self.georange[0], self.georange[1], 
                            self.zoom_step[0], self.zoom_step[1]))
Ejemplo n.º 13
0
    if not fname.endswith('.png'):
        continue
    name = os.path.splitext(fname)[0]
    fspath = os.path.join(_color_schemes_dir, fname)
    color_schemes[name] = backend.ColorScheme(name, fspath)


def load_dots(backend):
    """Given a backend module, return a mapping of zoom level to Dot object.
    """
    return dict([(zoom, backend.Dot(zoom)) for zoom in range(20)])
dots = load_dots(backend) # factored for easier use from scripts


for zoom in [0,1,2,3,4]:
    width, height = ll2px(-90, 180, zoom)
    numcols = int(math.ceil(width / 256.0))
    numrows = int(math.ceil(height / 256.0))
    cs_name = 'classic'
    color_scheme = color_schemes[cs_name]
    for x in range(numcols):
        for y in range(numrows):
            fspath = join( aspen.paths.root
                         , cs_name
                         , str(zoom)
                         , "%d,%d" % (x, y)
                          ) + '.png'
            tile = backend.Tile(color_scheme, dots, zoom, x, y, fspath)
            sys.stdout.write('tile %s\n' % fspath); sys.stdout.flush()
            if tile.is_empty():
                sys.stdout.write('skipping empty tile %s\n' % fspath)