def get_attribute(self, path_to_metadata): ''' Method that overrides the usual behavior of getting an attribute, this class has two dictionaries of metadata, if an attribute is requested, both should be inspected. ''' attribute = BaseParser.get_attribute(self, path_to_metadata) if not attribute: attribute = _get_attribute(path_to_metadata, self.usgs_metadata) return attribute
def default_options_for_create_raster_from_reference(reference_metadata): ''' This method will extract the metadata from a given reference file to be used in the creation of a new file. ''' geotransform = _get_attribute(GEOTRANSFORM, reference_metadata) projection = _get_attribute(PROJECTION, reference_metadata) options = { 'features_of_image_for_create': None, 'gdal_create_options': None, 'dataset': None } options['features_of_image_for_create'] = { 'projection': None, 'geotransform': None, 'create_using_geotransform_from_gcps': None, 'raster_stacked': None } put_in_dictionary(options, CREATE_WITH_PROJECTION, projection) put_in_dictionary(options, CREATE_WITH_GEOTRANSFORM, geotransform) put_in_dictionary(options, CREATE_WITH_GEOTRANSFORM_FROM_GCPS, False) put_in_dictionary(options, CREATE_STACKING, False) put_in_dictionary(options, GDAL_CREATE_OPTIONS, []) return options
def test_get_attribute(self): ''' Tests the get attribute function. This tries several scenarios. ''' dictionary = {'1': {'2': {'3': '4'}, '5': {'6': '7'}, '8': {'9': '0'}}} self.assertEqual(_get_attribute(['1','5','6'], dictionary), '7') self.assertEqual(_get_attribute(['1','2','3'], dictionary), '4') self.assertEqual(_get_attribute(['9','NoneSense'], dictionary), None) self.assertEqual(_get_attribute([], dictionary), None) self.assertEqual(_get_attribute(None, dictionary), None) self.assertEqual(_get_attribute(123, dictionary), None)
def test_get_attribute(self): ''' Tests the get attribute function. This tries several scenarios. ''' dictionary = {'1': {'2': {'3': '4'}, '5': {'6': '7'}, '8': {'9': '0'}}} self.assertEqual(_get_attribute(['1', '5', '6'], dictionary), '7') self.assertEqual(_get_attribute(['1', '2', '3'], dictionary), '4') self.assertEqual(_get_attribute(['9', 'NoneSense'], dictionary), None) self.assertEqual(_get_attribute([], dictionary), None) self.assertEqual(_get_attribute(None, dictionary), None) self.assertEqual(_get_attribute(123, dictionary), None)
def create_raster_tiff_from_reference(reference_metadata, output_file, array=None, options={}, data_type=gdal.GDT_Float32): ''' This method creates a raster tif from a given tif file to be used as a reference. From the reference file, data such as , the projection, and the geotransform will be extracted and used in the new file. ''' if not options: options = default_options_for_create_raster_from_reference( reference_metadata) if output_file == '': LOGGER.info('Creating raster in memory') driver = gdal.GetDriverByName(str(MEMORY)) else: if _get_attribute(CREATE_STACKING, options) is False: LOGGER.info('Creating raster tif from reference in %s' % output_file) else: LOGGER.info('Stacking in file %s' % output_file) driver = gdal.GetDriverByName(str(GDAL_TIFF)) if array is None and _get_attribute(DATA_SHAPE, options) is None: LOGGER.info( 'Error in creating raster, at least one of array or DATA_SHAPE attribute needs to be defined when calling this function' ) return None else: if array is not None: shape = array.shape if len(shape) == 2: bands = 1 width = shape[1] height = shape[0] else: bands = shape[0] width = shape[2] height = shape[1] else: if _get_attribute(DATA_SHAPE, options) is not None: width, height, bands = _get_attribute(DATA_SHAPE, options) gdal_options = _get_attribute(GDAL_CREATE_OPTIONS, options) if _get_attribute(CREATE_STACKING, options) is True: width, height, bands = _get_attribute(DATA_SHAPE, options) projection = _get_attribute(CREATE_WITH_PROJECTION, options) if _get_attribute(CREATE_WITH_GEOTRANSFORM_FROM_GCPS, options) is True: geotransform = _get_attribute(GEOTRANSFORM_FROM_GCPS, reference_metadata) else: geotransform = _get_attribute(CREATE_WITH_GEOTRANSFORM, options) if _get_attribute(DATASET, options) is None: data = driver.Create(output_file, width, height, bands, data_type, gdal_options) if projection: data.SetProjection(projection) if geotransform: data.SetGeoTransform(geotransform) else: data = _get_attribute(DATASET, options) if bands != 1 and array != None: if _get_attribute(CREATE_STACKING, options) is False: for band in range(bands): data.GetRasterBand(band + 1).WriteArray(array[band, :, :]) LOGGER.info('Created raster in %s' % output_file) else: data.GetRasterBand(_get_attribute(STACK_OFFSET, options) + 1).WriteArray(array) LOGGER.info( 'Writing offset %s in %s' % (_get_attribute(STACK_OFFSET, options), output_file)) else: if array != None: #data.SetNoDataValue(-9999) data.GetRasterBand(1).WriteArray(array) LOGGER.info('Created raster in %s' % output_file) else: if _get_attribute(DATASET, options) is None: LOGGER.info( 'Returning dataset %s from function create raster tiff from reference' % data) return data
def get_attribute(self, path_to_attribute): ''' Returns the attribute that is found in the given path. ''' return _get_attribute(path_to_attribute, self.metadata)
def get_attribute(self, path_to_attribute): ''' Returns the attribute that is found in the given path. ''' return _get_attribute(path_to_attribute, self.harmonized_extents)