def test_equality(self): proj1 = projections.FlatEarthProjection(self.bounding_box, self.image_size) proj2 = projections.FlatEarthProjection(self.bounding_box, self.image_size) print proj1 print proj2 assert proj1 == proj2 assert not proj1 != proj2
def test_not_equal(self): proj1 = projections.FlatEarthProjection(self.bounding_box, self.image_size) proj2 = projections.FlatEarthProjection(self.bounding_box, self.image_size) proj2.image_size = (500, 510) proj2.set_scale(self.bounding_box) assert proj1 != proj2 assert not proj1 == proj2
def __init__(self, map_filename=None, output_dir='./', image_size=(800, 600), projection=None, viewport=None, map_BB=None, land_polygons=None, draw_back_to_fore=True, draw_map_bounds=False, draw_spillable_area=False, formats=['png', 'gif'], draw_ontop='forecast', cache=None, output_timestep=None, output_zero_step=True, output_last_step=True, output_start_time=None, on=True, timestamp_attrib={}, **kwargs): """ Init the image renderer. :param str map_filename=None: name of file for basemap (BNA) :type map_filename: str :param str output_dir='./': directory to output the images :param 2-tuple image_size=(800, 600): size of images to output :param projection=None: projection instance to use: If None, set to projections.FlatEarthProjection() :type projection: a gnome.utilities.projection.Projection instance :param viewport: viewport of map -- what gets drawn and on what scale. Default is full globe: (((-180, -90), (180, 90))) If not specifies, it will be set to the map's bounds. :type viewport: pair of (lon, lat) tuples ( lower_left, upper right ) :param map_BB=None: bounding box of map if None, it will use the bounding box of the mapfile. :param draw_back_to_fore=True: draw the background (map) to the foregound image when outputting the images each time step. :type draw_back_to_fore: boolean :param formats=['gif']: list of formats to output. :type formats: string or list of strings. Options are: ['bmp', 'jpg', 'jpeg', 'gif', 'png'] :param draw_ontop: draw 'forecast' or 'uncertain' LEs on top. Default is to draw 'forecast' LEs, which are in black on top :type draw_ontop: str Following args are passed to base class Outputter's init: :param cache: sets the cache object from which to read prop. The model will automatically set this param :param output_timestep: default is None in which case everytime the write_output is called, output is written. If set, then output is written every output_timestep starting from model_start_time. :type output_timestep: timedelta object :param output_zero_step: default is True. If True then output for initial step (showing initial release conditions) is written regardless of output_timestep :type output_zero_step: boolean :param output_last_step: default is True. If True then output for final step is written regardless of output_timestep :type output_last_step: boolean Remaining kwargs are passed onto baseclass's __init__ with a direct call: Outputter.__init__(..) """ projection = (projections.FlatEarthProjection() if projection is None else projection) # set up the canvas self.map_filename = map_filename self.output_dir = output_dir if map_filename is not None and land_polygons is None: self.land_polygons = haz_files.ReadBNA(map_filename, 'PolygonSet') elif land_polygons is not None: self.land_polygons = land_polygons else: self.land_polygons = [] # empty list so we can loop thru it self.last_filename = '' self.draw_ontop = draw_ontop self.draw_back_to_fore = draw_back_to_fore Outputter.__init__(self, cache, on, output_timestep, output_zero_step, output_last_step, output_start_time, output_dir, **kwargs) if map_BB is None: if not self.land_polygons: map_BB = ((-180, -90), (180, 90)) else: map_BB = self.land_polygons.bounding_box self.map_BB = map_BB viewport = self.map_BB if viewport is None else viewport MapCanvas.__init__(self, image_size, projection=projection, viewport=viewport) # assorted rendering flags: self.draw_map_bounds = draw_map_bounds self.draw_spillable_area = draw_spillable_area self.raster_map = None self.raster_map_fill = True self.raster_map_outline = False # initilize the images: self.add_colors(self.map_colors) self.background_color = 'background' if self.map_filename is not None: file_prefix = os.path.splitext(self.map_filename)[0] sep = '_' else: file_prefix = sep = '' fn = '{}{}anim.gif'.format(file_prefix, sep) self.anim_filename = os.path.join(output_dir, fn) self.formats = formats self.delay = 50 self.repeat = True self.timestamp_attribs = {} self.set_timestamp_attrib(**timestamp_attrib) self.grids = [] self.props = []
class Test_FlatEarthProjection: # bb with 60 degrees in the center: ( cos(60 deg) == 0.5 ) bounding_box = ((20, 50.0), (40, 70.0)) image_size = (500, 500) proj = projections.FlatEarthProjection(bounding_box, image_size) def test_bounds(self): # corners of the BB: (sqare in lat-long) coords = ((20.0, 50.0, 0.0), (20.0, 70.0, 0.0), (40.0, 50.0, 0.0), (40.0, 70.0, 0.0)) proj_coords = self.proj.to_pixel(coords, asint=True) print proj_coords assert np.array_equal(proj_coords, [[124, 500], [124, 0], [375, 500], [375, 0]]) def test_middle(self): # middle of the BB coords = ((30, 60.0, 0.0), ) proj_coords = self.proj.to_pixel(coords, asint=True) print proj_coords assert np.array_equal(proj_coords, [[250, 250]]) def test_outside(self): """ points outside the BB should still come back, but the pixel coords will be outside the image shape """ # just outside the bitmap coords = ((9.9999999, 49.99999999, 0.0), (50.0000001, 70.000000001, 0.0)) print 'scale:', self.proj.scale proj_coords = self.proj.to_pixel(coords, asint=True) print proj_coords assert np.array_equal(proj_coords, [[-1, 500], [500, -1]]) def test_reverse(self): # middle of the BB # some non-round numbers # outside the bitmap coords = np.array( ((-7.5, 28.0, 0.0), (-8.123, 25.345, 0.0), (-2.500001, 33.00000001, 0.0), (-4.2345, 32.123, 0.0))) # first test without rounding proj_coords = self.proj.to_pixel(coords, asint=False) back_coords = self.proj.to_lonlat(proj_coords) print coords print proj_coords print repr(back_coords) assert np.allclose(coords[:, :2], back_coords) # now with the pixel rounding proj_coords = self.proj.to_pixel(coords, asint=True) back_coords = self.proj.to_lonlat(proj_coords) print coords print proj_coords print repr(back_coords) # tolerence set according to the scale: tol = 1. / self.proj.scale[0] / 1.5 # should be about 1/2 pixel # rtol tiny so it really doesn't matter assert np.allclose(coords[:, :2], back_coords, rtol=1e-100, atol=tol) def test_equality(self): proj1 = projections.FlatEarthProjection(self.bounding_box, self.image_size) proj2 = projections.FlatEarthProjection(self.bounding_box, self.image_size) print proj1 print proj2 assert proj1 == proj2 assert not proj1 != proj2 def test_not_equal(self): proj1 = projections.FlatEarthProjection(self.bounding_box, self.image_size) proj2 = projections.FlatEarthProjection(self.bounding_box, self.image_size) proj2.image_size = (500, 510) proj2.set_scale(self.bounding_box) assert proj1 != proj2 assert not proj1 == proj2