def test_basemap(dump): polygons = haz_files.ReadBNA(input_file, 'PolygonSet') gmap = map_canvas.MapCanvas((400, 800), polygons) gmap.draw_background() gmap.save_background(os.path.join(dump, 'background1.png')) assert True
class Test_bna_polygonset: polys = haz_files.ReadBNA(test_bna, 'PolygonSet') def test_length(self): assert len(self.polys) == 6 def test_type(self): print self.polys[0] assert self.polys[0].metadata[0] == 'polygon' assert self.polys[3].metadata[0] == 'polyline' assert self.polys[4].metadata[0] == 'point' def test_name(self): assert self.polys[0].metadata[1] == 'Another Name' assert self.polys[1].metadata[1] == "A third 'name'" assert self.polys[2].metadata[1] == 'A name with, a comma' def test_sname(self): print self.polys[0] assert self.polys[0].metadata[2] == 'Another Type' assert self.polys[1].metadata[2] == '6' assert self.polys[2].metadata[2] == '1' def test_dtype(self): polys = haz_files.ReadBNA(test_bna, polytype='PolygonSet', dtype=np.float32) for p in polys: assert p.dtype == np.float32
def test_basemap_wide(dump_folder): polygons = haz_files.ReadBNA(input_file, 'PolygonSet') gmap = map_canvas.MapCanvas((800, 400), polygons) gmap.draw_background() gmap.save_background(os.path.join(dump_folder, 'background2.png')) assert True
def test_without_filescanner(): state = haz_files.FILESCANNER haz_files.FILESCANNER = False polys = haz_files.ReadBNA(test_bna, 'PolygonSet') haz_files.FILESCANNER = state # reset, 'cause global state assert len(polys) == 6 assert polys[1].metadata[0] == 'polygon' assert polys[1].metadata[1] == "A third 'name'" assert polys[1].metadata[2] == '6'
class Test_bna_list: polys = haz_files.ReadBNA(test_bna) def test_length(self): assert len(self.polys) == 6 def test_type(self): assert self.polys[0][1] == 'polygon' assert self.polys[1][1] == 'polygon' assert self.polys[2][1] == 'polygon' assert self.polys[3][1] == 'polyline' assert self.polys[4][1] == 'point' def test_name(self): assert self.polys[0][2] == 'Another Name' assert self.polys[1][2] == "A third 'name'" assert self.polys[2][2] == 'A name with, a comma' def test_sname(self): print self.polys[0] assert self.polys[0][3] == 'Another Type' assert self.polys[1][3] == '6' assert self.polys[2][3] == '1' def test_points(self): assert np.array_equal( self.polys[0][0], np.array(( (-81.531753540039, 31.134635925293), (-81.531150817871, 31.134529113769), (-81.530662536621, 31.134353637695), (-81.530502319336, 31.134126663208), (-81.530685424805, 31.133970260620), (-81.531112670898, 31.134040832519), ))) def test_end_points(self): for p in self.polys: if p[1] == 'polygon' or p[1] == 'polyline': assert not np.array_equal(p[0][0], p[0][-1]) def test_end_points_point(self): for p in self.polys: if p[1] == 'point': assert p[0].shape == (1, 2) def test_dtype(self): polys = haz_files.ReadBNA(test_bna, polytype='list', dtype=np.float32) for p in polys: assert p[0].dtype == np.float32
def test_render_BW(): ''' Test the creation of a black and white map with an island inset. note: it is rendered "almost black" on black... ''' polygons = haz_files.ReadBNA(input_file, 'PolygonSet') m = map_canvas.BW_MapCanvas((500, 500), land_polygons=polygons) m.draw_background() # Write the result to the present working directory as a PNG image file. # m.save_background('BW_LandMap.png') assert True
def test_draw_raster_map(dump): """ tests drawing the raster map """ import gnome polygons = haz_files.ReadBNA(input_file, 'PolygonSet') gmap = map_canvas.MapCanvas((1000, 1000), polygons) gmap.viewport = ((-127.47,48.10),(-127.22, 48.24)) gmap.draw_background() # make a raster map out of the BNA: raster_map = gnome.map.MapFromBNA(input_file, raster_size=10000) gmap.draw_raster_map(raster_map, outline=True) gmap.save_background(os.path.join(dump, 'raster.png'))
def make_map(bna_filename, png_filename, image_size=(500, 500)): """ utility function to draw a PNG map from a BNA file param: bna_filename -- file name of BNA file to draw map from param: png_filename -- file name of PNG file to write out param: image_size=(500,500) -- size of image (width, height) tuple param: format='RGB' -- format of image. Options are: 'RGB','palette','B&W' """ # print "Reading input BNA" polygons = haz_files.ReadBNA(bna_filename, 'PolygonSet') canvas = MapCanvas(image_size, land_polygons=polygons) canvas.draw_background() canvas.save_background(png_filename, 'PNG')
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 = []
def test_dtype(self): polys = haz_files.ReadBNA(test_bna, polytype='PolygonSet', dtype=np.float32) for p in polys: assert p.dtype == np.float32
def test_dtype(self): polys = haz_files.ReadBNA(test_bna, polytype='list', dtype=np.float32) for p in polys: assert p[0].dtype == np.float32
def __init__(self, filename, raster_size=1024 * 1024, **kwargs): """ Creates a GnomeMap (specifically a RasterMap) from a bna file. It is expected that you will get the spillable area and map bounds from the BNA -- if they exist Required arguments: :param bna_file: full path to a bna file :param refloat_halflife: the half-life (in hours) for the re-floating. :param raster_size: the total number of pixels (bytes) to make the raster -- the actual size will match the aspect ratio of the bounding box of the land Optional arguments (kwargs): :param map_bounds: The polygon bounding the map -- could be larger or smaller than the land raster :param spillable_area: The polygon bounding the spillable_area :param id: unique ID of the object. Using UUID as a string. This is only used when loading object from save file. :type id: string """ self.filename = filename polygons = haz_files.ReadBNA(filename, 'PolygonSet') map_bounds = None self.name = kwargs.pop('name', os.path.split(filename)[1]) # find the spillable area and map bounds: # and create a new polygonset without them # fixme -- adding a "pop" method to PolygonSet might be better # or a gnome_map_data object... just_land = PolygonSet() # and lakes.... spillable_area = PolygonSet() for p in polygons: if p.metadata[1].lower() == 'spillablearea': spillable_area.append(p) elif p.metadata[1].lower() == 'map bounds': map_bounds = p else: just_land.append(p) # now draw the raster map with a map_canvas: # determine the size: BB = just_land.bounding_box # create spillable area and bounds if they weren't in the BNA if map_bounds is None: map_bounds = BB.AsPoly() if len(spillable_area) == 0: spillable_area.append(map_bounds) # user defined spillable_area, map_bounds overrides data obtained # from polygons # todo: should there be a check between spillable_area read from BNA # versus what the user entered. if this is within spillable_area for # BNA, then include it? else ignore #spillable_area = kwargs.pop('spillable_area', spillable_area) spillable_area = kwargs.pop('spillable_area', spillable_area) map_bounds = kwargs.pop('map_bounds', map_bounds) # stretch the bounding box, to get approximate aspect ratio in # projected coords. aspect_ratio = (np.cos(BB.Center[1] * np.pi / 180) * (BB.Width / BB.Height)) w = int(np.sqrt(raster_size * aspect_ratio)) h = int(raster_size / w) canvas = BW_MapCanvas((w, h), land_polygons=just_land) canvas.draw_background() # canvas.save_background("raster_map_test.png") # # get the bitmap as a numpy array: bitmap_array = canvas.as_array() # __init__ the RasterMap # hours RasterMap.__init__(self, bitmap_array, canvas.projection, map_bounds=map_bounds, spillable_area=spillable_area, **kwargs) return None
def __init__(self, filename=None, images_dir='./', image_size=(800, 600), cache=None, output_timestep=None, output_zero_step=True, output_last_step=True, draw_ontop='forecast', draw_back_to_fore=True, **kwargs): """ Init the image renderer. Following args are passed to base class Outputter's init: :param filename: the name of the image file :param images_dir: the folder in which to write the image :param image_size: the width and height of the image :param cache: sets the cache object from which to read data. 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 :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 :param draw_back_to_fore=True: draw the background (map) to the foregound image when drawing Elements. :type draw_ontop: boolean Remaining kwargs are passed onto baseclass's __init__ with a direct call: MapCanvas.__init__(..) Optional parameters (kwargs) :param projection_class: gnome.utilities.projections class to use. Default is gnome.utilities.projections.FlatEarthProjection :param map_BB: map bounding box. Default is to use land_polygons.bounding_box. If land_polygons is None, then this is the whole world, defined by ((-180,-90),(180, 90)) :param viewport: viewport of map -- what gets drawn and on what scale. Default is to set viewport = map_BB :param image_mode: Image mode ('P' for palette or 'L' for Black and White image). BW_MapCanvas inherits from MapCanvas and sets the mode to 'L'. Default image_mode is 'P'. """ # set up the canvas self._filename = filename if filename is not None: polygons = haz_files.ReadBNA(filename, 'PolygonSet') else: polygons = None self.images_dir = images_dir self.last_filename = '' self.draw_ontop = draw_ontop self.draw_back_to_fore = draw_back_to_fore Outputter.__init__(self, cache, kwargs.pop('on', True), output_timestep, output_zero_step, output_last_step, kwargs.pop('name', None)) MapCanvas.__init__(self, image_size, land_polygons=polygons, **kwargs)