def draw_debug(self, draw): # draw outlines of original OSM tiles in GREEN for dy in OSM_TILES_DELTAS: for dx in OSM_TILES_DELTAS: box1 = vmul(OSM_TILE_SIZE, (vadd((dx, dy), OSM_TILES_OFFSETS))) box2 = vmul(OSM_TILE_SIZE, (vadd((dx, dy), vadd(OSM_TILES_OFFSETS, (1, 1))))) box = box1+box2 draw.rectangle(box, outline='#00ff00') # Draw dot at QRA in BLUE (px, py) = self.lonlat_xy_in_composite_osm_tile(poi.lonlat) #print "qra (px,py) = (%d, %d)" % (px,py) Tiles.drawCircle(draw, px, py, 2, '#0000ff')
def draw_debug(self, draw): poi_xy = composite_tile.lonlat_xy_in_composite_osm_tile(poi.lonlat) lcd_tile_center = (LCD_TILE_WIDTH / 2, LCD_TILE_HEIGHT / 2) # draw debugging info on image and save as foo.png # draw outlines of LCD tile crop in RED for dy in LCD_TILES_DELTAS: for dx in LCD_TILES_DELTAS: xy = vadd(poi_xy, vmul(LCD_TILE_SIZE, (dx, dy))) box = vsub(xy, lcd_tile_center) + vadd(xy, lcd_tile_center) draw.rectangle(box, outline='#ff0000')
def retrieve(self): if self.composite_image == None: self.composite_image = Image.new("RGB", (OSM_TILE_WIDTH * OSM_TILES_SQUARE, OSM_TILE_HEIGHT * OSM_TILES_SQUARE), (0, 0, 0)) for dy in OSM_TILES_DELTAS: for dx in OSM_TILES_DELTAS: dxdy = (dx, dy) osm_tile = OSM_Tile(Tiles.rose(OSM_TILES_OFFSETS, dx, dy), vadd((self.x, self.y), dxdy), dxdy, zoom) self.osm_tiles_by_xytile[osm_tile.xytile] = osm_tile if DEBUG: print "Adding self.osm_tiles_by_dxdy[%s] = osm_tile" % (dxdy,) self.osm_tiles_by_dxdy[dxdy] = osm_tile osm_tile_image = osm_tile.retrieve() box = (vmul(OSM_TILE_SIZE, vadd((dx, dy), OSM_TILES_OFFSETS)) + vmul(OSM_TILE_SIZE, vadd((dx, dy), vadd(OSM_TILES_OFFSETS, (1, 1))))) self.composite_image.paste(osm_tile_image, box) fn = "%s-%s-%dx%dx%d.png" % (SERVER_TYPE, qra, OSM_TILE_WIDTH, OSM_TILE_HEIGHT, OSM_TILES_SQUARE) if DEBUG: print("Saving composite map image to " + fn) self.composite_image.save(fn) return self.composite_image
def clip_lcd_tiles(self): description_fn = "%s-%s-%dx%d.txt" % (SERVER_TYPE, qra, LCD_TILE_WIDTH, LCD_TILE_HEIGHT) try: os.remove(description_fn) except: pass poi_xy = composite_tile.lonlat_xy_in_composite_osm_tile(poi.lonlat) if DEBUG: print "poi_xy %d %d in composite_osm_tile" % poi_xy center_lcd_tile_center_xy = (LCD_TILE_WIDTH / 2, LCD_TILE_HEIGHT / 2) if DEBUG: print "center_lcd_tile_center_xy = %s %s" % (center_lcd_tile_center_xy) for dy in LCD_TILES_DELTAS: for dx in LCD_TILES_DELTAS: if DEBUG: print "Creating LCD_Tile %s (%d %d) " % (Tiles.rose(LCD_TILES_OFFSETS, dx, dy), dx, dy) lcd_tile_dxy = vadd(poi_xy, vmul(LCD_TILE_SIZE, (dx, dy))) composite_box_top_left_xy = vsub(lcd_tile_dxy, center_lcd_tile_center_xy) composite_box_bottom_right_xy = vadd(lcd_tile_dxy, center_lcd_tile_center_xy) box = composite_box_top_left_xy + composite_box_bottom_right_xy lcd_image = self.composite_tile.composite_image.crop(box) osm_tl_dxdy = vsub(vidiv(composite_box_top_left_xy, OSM_TILE_SIZE), OSM_TILES_OFFSETS) osm_br_dxdy = vsub(vidiv(composite_box_bottom_right_xy, OSM_TILE_SIZE), OSM_TILES_OFFSETS) if DEBUG: print " LCD_Tile composite_box_top_left_xy = %d %d" % (composite_box_top_left_xy) print " LCD_Tile osm_tl_dxdy = %d %s upper left is at this location in composite_box: %d %d" % (osm_tl_dxdy + composite_box_top_left_xy) print " LCD_Tile osm_br_dxdy = %d %s bottom right is at this location in composite_box: %d %d" % (osm_br_dxdy + composite_box_top_left_xy) print "osm_tl_dxdy = %s osm_br_dxdy = %s" % (osm_tl_dxdy, osm_br_dxdy) osm_tile_tl = composite_tile.osm_tiles_by_dxdy[osm_tl_dxdy] osm_tile_br = composite_tile.osm_tiles_by_dxdy[osm_br_dxdy] if DEBUG: print " OSM_TILE pixels_per_degree %f %f, %f %f" % (osm_tile_tl.pixels_per_degree+osm_tile_br.pixels_per_degree) print " OSM_Tile %s top_left_lonlat %f %f" % (osm_tile_tl.name, osm_tile_tl.top_left_lon, osm_tile_tl.top_left_lat) lcd_tile_top_left_in_osm_tile = vmod(composite_box_top_left_xy, OSM_TILE_SIZE) lcd_tile_bottom_right_in_osm_tile = vmod(composite_box_bottom_right_xy, OSM_TILE_SIZE) lcd_tile_top_left_lonlat = osm_tile_tl.xy_lonlat(lcd_tile_top_left_in_osm_tile) lcd_tile_bottom_right_lonlat = osm_tile_br.xy_lonlat(lcd_tile_bottom_right_in_osm_tile) #print " LCD_Tile %s (%d,%d) lcd_tile_top_left_in_osm_tile: %d %d" % ((Tiles.rose(LCD_TILES_OFFSETS, dx, dy), dx, dy) + lcd_tile_top_left_in_osm_tile) #print " LCD_Tile %s (%d,%d) top left lon lat %f %f" % ((Tiles.rose(LCD_TILES_OFFSETS, dx, dy), dx, dy) + lcd_tile_top_left_lonlat) if False: self.lcd_tiles[(dx,dy)] = LCD_Tile(Tiles.rose(LCD_TILES_OFFSETS, dx, dy), (dx,dy), lcd_tile_top_left_lonlat, lcd_tile_bottom_right_lonlat, lcd_image) png_fn = self.calculate_lcd_tile_fn(dx, dy) bmp_fn = self.calculate_lcd_bmp_fn(dx, dy) tl_latlon = Tiles.vswap(lcd_tile_top_left_lonlat) br_latlon = Tiles.vswap(lcd_tile_bottom_right_lonlat) lcd_image.save(png_fn, "PNG") lcd_image.save(bmp_fn, "BMP") description = "%d,%d %d,%d\n" % tuple([round(x*1e6) for x in (tl_latlon[0],tl_latlon[1], br_latlon[0],br_latlon[1])]) self.lat_degrees_per_pixel += (br_latlon[0] - tl_latlon[0]) self.lon_degrees_per_pixel += (br_latlon[1] - tl_latlon[1]) if dx == 0 and dy == 0: poi_lcd_tile_tl_latlon = tl_latlon #print "Adding LCD map four corners to %s " % (description_fn) with open(description_fn, 'a') as f: f.write(Tiles.rose(LCD_TILES_OFFSETS, dx,dy)) f.write(" ") f.write(description) self.save_pixels_file(poi_lcd_tile_tl_latlon)
def lonlat_xy_in_composite_osm_tile(self, lonlat): osm_tile = self.osm_tiles_by_xytile[Tiles.lonlat_tile(lonlat[0], lonlat[1], self.zoom)] #print "OSM tile is '%s' tile_delta %d %d" % ((osm_tile.name,)+ osm_tile.tile_delta) top_left_pixel_xy = vmul(OSM_TILE_SIZE, vadd(osm_tile.tile_delta, OSM_TILES_OFFSETS)) #print "top_left_pixel_xy is %d %d" % top_left_pixel_xy return vadd(top_left_pixel_xy, osm_tile.lonlat_xy(lonlat[0], lonlat[1]))
def lonlat_xy(self, lon, lat): # lon increases left->right, but lat increases bottom->top #print "osm_tile %s self.top_left_lon lat %f %f finding lonlat %f %f" % (self.name, self.top_left_lon, self.top_left_lat, lon, lat) delta_degrees = vsub((lon, self.top_left_lat), (self.top_left_lon, lat)) return vint(vmul(delta_degrees, self.pixels_per_degree))