def _write_hazard_map_geotiff(self, path, haz_map_data): """ Write out a hazard map geotiff using the input hazard map data. :param path: path to output file, including file name :param haz_map_data: list of hazard map data to be serialized to the geotiff :type haz_map_data: list of tuples of (shapes.Site object, hazard map data dict) """ try: cpt_path = os.path.join(self.params['BASE_PATH'], self.params['HAZARD_MAP_CPT']) cpt_reader = cpt.CPTReader(cpt_path) colormap = cpt_reader.get_colormap() except (IOError, KeyError): LOG.info("Unable to read hazard map CPT file from job config." " Using default colormap.") colormap = geotiff.HazardMapGeoTiffFile.DEFAULT_COLORMAP iml_min_max = None if 'HAZARD_MAP_IML_MIN' in self.params and \ 'HAZARD_MAP_IML_MAX' in self.params: iml_min_max = (float(self.params['HAZARD_MAP_IML_MIN']), float(self.params['HAZARD_MAP_IML_MAX'])) hm_writer = geotiff.HazardMapGeoTiffFile(path, self.region.grid, colormap=colormap, iml_min_max=iml_min_max, html_wrapper=True) # write the hazard map and close the file return hm_writer.serialize(haz_map_data)
def _test_hazard_map_relative_scaling(region, hm_data): path = helpers.get_output_path( 'TEST_HAZARD_MAP_relative_scaling.tiff') # expected colors for each pixel in the map: exp_red_vals = numpy.array([[49, 186, 255, 138, 186, 24], [0, 208, 67, 0, 255, 24], [143, 255, 123, 186, 143, 0], [186, 0, 255, 186, 238, 143], [255, 186, 0, 186, 12, 205], [255, 143, 208, 238, 97, 0]]) exp_green_vals = numpy.array([[190, 197, 255, 236, 197, 175], [39, 216, 202, 39, 255, 175], [161, 255, 235, 197, 161, 39], [197, 39, 255, 197, 79, 161], [255, 197, 39, 197, 129, 255], [255, 161, 216, 79, 122, 39]]) exp_blue_vals = numpy.array([[255, 247, 255, 174, 247, 255], [224, 251, 255, 224, 255, 255], [241, 255, 200, 247, 241, 224], [247, 224, 255, 247, 77, 241], [255, 247, 224, 247, 248, 162], [255, 241, 251, 77, 236, 224]]) hm_writer = geotiff.HazardMapGeoTiffFile(path, small_region.grid, html_wrapper=True) hm_writer.serialize(hm_data) self._assert_image_rgb_is_correct(path, exp_red_vals, exp_green_vals, exp_blue_vals)
def test_hazard_map_geotiff_scaling_fixed(self): """ Scaling type for a HazardMapGeoTiffFile is 'fixed' if the iml_min_max is specified in the constructor. This test ensures the scaling type is properly set to 'fixed'. """ # Nothing is actually written to this file in this test test_file_path = "test.tiff" test_region = shapes.Region.from_coordinates(TEST_REGION_SMALL) test_iml_min_max = ('0.0', '4.8') # test 'fixed' color scaling hm_writer = geotiff.HazardMapGeoTiffFile(test_file_path, test_region.grid, TEST_COLORMAP, iml_min_max=test_iml_min_max) self.assertEqual('fixed', hm_writer.scaling)
def test_hazard_map_geotiff_scaling_relative(self): """ Scaling type for a HazardMapGeoTiffFile is 'relative' if the iml_min_max is not specified in the constructor. Instead, the min and max values are derived from the lowest and highest existing values in the hazard map raster (the min and max values are calculated later when the raw raster values are normalized; see the HazardMapGeoTiffFile class doc for more info). This test ensures that the scaling type is properly set to 'relative'. """ # Nothing is actually written to this file in this test test_file_path = "test.tiff" test_region = shapes.Region.from_coordinates(TEST_REGION_SMALL) # test 'relative' color scaling hm_writer = geotiff.HazardMapGeoTiffFile(test_file_path, test_region.grid, TEST_COLORMAP) self.assertEqual('relative', hm_writer.scaling)
def _test_hazard_map_fixed_scaling(region, hm_data): path = helpers.get_output_path( 'TEST_HAZARD_MAP_fixed_scaling.tiff') # expected colors for each pixel in the map: exp_red_vals = numpy.array([[238, 255, 247, 238, 255, 238], [238, 255, 238, 238, 247, 238], [244, 247, 238, 255, 244, 238], [255, 238, 247, 244, 238, 244], [247, 255, 238, 255, 238, 238], [247, 244, 255, 238, 238, 238]]) exp_green_vals = numpy.array([[79, 160, 215, 79, 160, 79], [79, 160, 79, 79, 215, 79], [116, 215, 79, 160, 116, 79], [160, 79, 215, 116, 79, 116], [215, 160, 79, 160, 79, 79], [215, 116, 189, 79, 79, 79]]) exp_blue_vals = numpy.array([[77, 68, 103, 77, 68, 77], [77, 68, 77, 77, 103, 77], [74, 103, 77, 68, 74, 77], [68, 77, 103, 74, 77, 74], [103, 68, 77, 68, 77, 77], [103, 74, 86, 77, 77, 77]]) iml_min = 0.0 iml_max = 0.3 hm_writer = geotiff.HazardMapGeoTiffFile(path, small_region.grid, html_wrapper=True, iml_min_max=(iml_min, iml_max)) hm_writer.serialize(hm_data) self._assert_image_rgb_is_correct(path, exp_red_vals, exp_green_vals, exp_blue_vals)