normalize_by_geometry.py
def normalize_by_polygon(
        raster_path, vector_path, percentile, clamp_range, workspace_dir,
        target_path):
    """Normalize a raster locally by regions defined by vector.

    Parameters:
        raster_path (str): path to base raster to aggregate over.
        vector_path (str): path to a vector that defines local regions to
            normalize over. Any pixels outside of these polygons will be set
            to nodata.
        percentile (float): a number in the range [0, 100] that is used to
            normalize the local regions defined by `vector_path`. This number
            will be used to calculate the percentile in each region separately
        clamp_range (list or tuple): a min/max range to clamp the normalized
            result by.
        workspace_dir (str): path to a workspace to create and keep
            intermediate files.

    Returns:
        None.

    """
    base_dir = os.path.dirname(target_path)
    for dir_path in [base_dir, workspace_dir]:
        try:
            os.makedirs(dir_path)
        except OSError:
            pass

    vector = ogr.Open(vector_path)
    layer = vector.GetLayer()
    fid_to_percentile_pickle_path = {}
    for feature in layer:
        # clip the original layer and then mask it
        fid = feature.GetFID()
        feature_mask_path = os.path.join(
            workspace_dir, '%d_mask.tif' % fid)
        mask_raster_task = TASK_GRAPH.add_task(
            func=clip_and_mask_raster,
            args=(raster_path, vector_path, fid, feature_mask_path),
            target_path_list=[feature_mask_path],
            task_name='mask feature %d' % fid)
        percentile_pickle_path = os.path.join(
            workspace_dir, '%d_%d.pickle' % (fid, percentile))
        _ = TASK_GRAPH.add_task(
            func=calculate_percentile,
            args=(feature_mask_path, [percentile], base_dir,
                  percentile_pickle_path),
            target_path_list=[percentile_pickle_path],
            dependent_task_list=[mask_raster_task],
            task_name='calculating %s' % percentile_pickle_path)
        fid_to_percentile_pickle_path[fid] = percentile_pickle_path
        feature = None

    local_vector_path = os.path.join(workspace_dir, 'local_vector.gpkg')
    gpkg_driver = ogr.GetDriverByName('GPKG')
    local_vector = gpkg_driver.CopyDataSource(vector, local_vector_path)
    vector = None
    layer = None
    local_layer = local_vector.GetLayer()
    local_layer.CreateField(ogr.FieldDefn('norm_val', ogr.OFTReal))

    global_norm_value_raster_path = os.path.join(
        workspace_dir, 'global_norm_values.tif')
    pygeoprocessing.new_raster_from_base(
        raster_path, global_norm_value_raster_path, gdal.GDT_Float32,
        [-1], raster_driver_creation_tuple=(
            'GTIFF', (
                'TILED=YES', 'BIGTIFF=YES', 'COMPRESS=ZSTD',
                'BLOCKXSIZE=256', 'BLOCKYSIZE=256', 'SPARSE_OK=TRUE')))

    TASK_GRAPH.join()
    for fid, pickle_path in fid_to_percentile_pickle_path.items():
        feature = local_layer.GetFeature(fid)
        with open(pickle_path, 'rb') as pickle_file:
            percentile_list = pickle.load(pickle_file)
        if len(percentile_list) > 0:
            feature.SetField('norm_val', percentile_list[0])
        else:
            feature.SetField('norm_val', -1.0)
        local_layer.SetFeature(feature)
        feature = None
    local_layer = None
    local_vector = None

    pygeoprocessing.rasterize(
        local_vector_path, global_norm_value_raster_path,
        option_list=['ATTRIBUTE=norm_val'])

    raster_nodata = pygeoprocessing.get_raster_info(raster_path)['nodata'][0]
    pygeoprocessing.raster_calculator(
        [(raster_path, 1), (global_norm_value_raster_path, 1),
         (clamp_range, 'raw'), (raster_nodata, 'raw'), (-1, 'raw'),
         (-1, 'raw')], divide_op,
        target_path, gdal.GDT_Float32, -1)
Normalize a raster locally by regions defined by vector.

    Parameters:
        raster_path (str): path to base raster to aggregate over.
        vector_path (str): path to a vector that defines local regions to
            normalize over. Any pixels outside of these polygons will be set
            to nodata.
        percentile (float): a number in the range [0, 100] that is used to
            normalize the local regions defined by `vector_path`. This number
            will be used to calculate the percentile in each region separately
        clamp_range (list or tuple): a min/max range to clamp the normalized
            result by.
        workspace_dir (str): path to a workspace to create and keep
            intermediate files.

    Returns:
        None.

    
Normalize a raster locally by regions defined by vector.
Normalize a raster locally by regions defined by vector .
def normalize_by_polygon(
        raster_path, vector_path, percentile, clamp_range, workspace_dir,
        target_path):
    base_dir = os.path.dirname(target_path)
    for dir_path in [base_dir, workspace_dir]:
        try:
            os.makedirs(dir_path)
        except OSError:
            pass
    vector = ogr.Open(vector_path)
    layer = vector.GetLayer()
    fid_to_percentile_pickle_path = {}
    for feature in layer:
        fid = feature.GetFID()
        feature_mask_path = os.path.join(
            workspace_dir, '%d_mask.tif' % fid)
        mask_raster_task = TASK_GRAPH.add_task(
            func=clip_and_mask_raster,
            args=(raster_path, vector_path, fid, feature_mask_path),
            target_path_list=[feature_mask_path],
            task_name='mask feature %d' % fid)
        percentile_pickle_path = os.path.join(
            workspace_dir, '%d_%d.pickle' % (fid, percentile))
        _ = TASK_GRAPH.add_task(
            func=calculate_percentile,
            args=(feature_mask_path, [percentile], base_dir,
                  percentile_pickle_path),
            target_path_list=[percentile_pickle_path],
            dependent_task_list=[mask_raster_task],
            task_name='calculating %s' % percentile_pickle_path)
        fid_to_percentile_pickle_path[fid] = percentile_pickle_path
        feature = None
    local_vector_path = os.path.join(workspace_dir, 'local_vector.gpkg')
    gpkg_driver = ogr.GetDriverByName('GPKG')
    local_vector = gpkg_driver.CopyDataSource(vector, local_vector_path)
    vector = None
    layer = None
    local_layer = local_vector.GetLayer()
    local_layer.CreateField(ogr.FieldDefn('norm_val', ogr.OFTReal))
    global_norm_value_raster_path = os.path.join(
        workspace_dir, 'global_norm_values.tif')
    pygeoprocessing.new_raster_from_base(
        raster_path, global_norm_value_raster_path, gdal.GDT_Float32,
        [-1], raster_driver_creation_tuple=(
            'GTIFF', (
                'TILED=YES', 'BIGTIFF=YES', 'COMPRESS=ZSTD',
                'BLOCKXSIZE=256', 'BLOCKYSIZE=256', 'SPARSE_OK=TRUE')))
    TASK_GRAPH.join()
    for fid, pickle_path in fid_to_percentile_pickle_path.items():
        feature = local_layer.GetFeature(fid)
        with open(pickle_path, 'rb') as pickle_file:
            percentile_list = pickle.load(pickle_file)
        if len(percentile_list) > 0:
            feature.SetField('norm_val', percentile_list[0])
        else:
            feature.SetField('norm_val', -1.0)
        local_layer.SetFeature(feature)
        feature = None
    local_layer = None
    local_vector = None
    pygeoprocessing.rasterize(
        local_vector_path, global_norm_value_raster_path,
        option_list=['ATTRIBUTE=norm_val'])
    raster_nodata = pygeoprocessing.get_raster_info(raster_path)['nodata'][0]
    pygeoprocessing.raster_calculator(
        [(raster_path, 1), (global_norm_value_raster_path, 1),
         (clamp_range, 'raw'), (raster_nodata, 'raw'), (-1, 'raw'),
         (-1, 'raw')], divide_op,
        target_path, gdal.GDT_Float32, -1)
def normalize_by_polygon ( raster_path , vector_path , percentile , clamp_range , workspace_dir , target_path ) : base_dir = os . path . dirname ( target_path ) for dir_path in [ base_dir , workspace_dir ] : try : os . makedirs ( dir_path ) except OSError : pass vector = ogr . Open ( vector_path ) layer = vector . GetLayer ( ) fid_to_percentile_pickle_path = { } for feature in layer : fid = feature . GetFID ( ) feature_mask_path = os . path . join ( workspace_dir , '%d_mask.tif' % fid ) mask_raster_task = TASK_GRAPH . add_task ( func = clip_and_mask_raster , args = ( raster_path , vector_path , fid , feature_mask_path ) , target_path_list = [ feature_mask_path ] , task_name = 'mask feature %d' % fid ) percentile_pickle_path = os . path . join ( workspace_dir , '%d_%d.pickle' % ( fid , percentile ) ) _ = TASK_GRAPH . add_task ( func = calculate_percentile , args = ( feature_mask_path , [ percentile ] , base_dir , percentile_pickle_path ) , target_path_list = [ percentile_pickle_path ] , dependent_task_list = [ mask_raster_task ] , task_name = 'calculating %s' % percentile_pickle_path ) fid_to_percentile_pickle_path [ fid ] = percentile_pickle_path feature = None local_vector_path = os . path . join ( workspace_dir , 'local_vector.gpkg' ) gpkg_driver = ogr . GetDriverByName ( 'GPKG' ) local_vector = gpkg_driver . CopyDataSource ( vector , local_vector_path ) vector = None layer = None local_layer = local_vector . GetLayer ( ) local_layer . CreateField ( ogr . FieldDefn ( 'norm_val' , ogr . OFTReal ) ) global_norm_value_raster_path = os . path . join ( workspace_dir , 'global_norm_values.tif' ) pygeoprocessing . new_raster_from_base ( raster_path , global_norm_value_raster_path , gdal . GDT_Float32 , [ - 1 ] , raster_driver_creation_tuple = ( 'GTIFF' , ( 'TILED=YES' , 'BIGTIFF=YES' , 'COMPRESS=ZSTD' , 'BLOCKXSIZE=256' , 'BLOCKYSIZE=256' , 'SPARSE_OK=TRUE' ) ) ) TASK_GRAPH . join ( ) for fid , pickle_path in fid_to_percentile_pickle_path . items ( ) : feature = local_layer . GetFeature ( fid ) with open ( pickle_path , 'rb' ) as pickle_file : percentile_list = pickle . load ( pickle_file ) if len ( percentile_list ) > 0 : feature . SetField ( 'norm_val' , percentile_list [ 0 ] ) else : feature . SetField ( 'norm_val' , - 1.0 ) local_layer . SetFeature ( feature ) feature = None local_layer = None local_vector = None pygeoprocessing . rasterize ( local_vector_path , global_norm_value_raster_path , option_list = [ 'ATTRIBUTE=norm_val' ] ) raster_nodata = pygeoprocessing . get_raster_info ( raster_path ) [ 'nodata' ] [ 0 ] pygeoprocessing . raster_calculator ( [ ( raster_path , 1 ) , ( global_norm_value_raster_path , 1 ) , ( clamp_range , 'raw' ) , ( raster_nodata , 'raw' ) , ( - 1 , 'raw' ) , ( - 1 , 'raw' ) ] , divide_op , target_path , gdal . GDT_Float32 , - 1 )
Normalize a raster locally by regions defined by vector.
Normalize a raster locally by regions defined by vector .
"""Normalize a raster locally by regions defined by vector.

    Parameters:
        raster_path (str): path to base raster to aggregate over.
        vector_path (str): path to a vector that defines local regions to
            normalize over. Any pixels outside of these polygons will be set
            to nodata.
        percentile (float): a number in the range [0, 100] that is used to
            normalize the local regions defined by `vector_path`. This number
            will be used to calculate the percentile in each region separately
        clamp_range (list or tuple): a min/max range to clamp the normalized
            result by.
        workspace_dir (str): path to a workspace to create and keep
            intermediate files.

    Returns:
        None.

    """ # clip the original layer and then mask it

None
raster_path
path to base raster to aggregate over.
path to base raster to aggregate over .
vector_path
path to a vector that defines local regions to
normalize over. Any pixels outside of these polygons will be set
to nodata.
path to a vector that defines local regions to normalize over . Any pixels outside of these polygons will be set to nodata .
percentile
a number in the range [0, 100] that is used to
normalize the local regions defined by `vector_path`. This number
will be used to calculate the percentile in each region separately
a number in the range [ 0 100 ] that is used to normalize the local regions defined by ` vector_path ` . This number will be used to calculate the percentile in each region separately
clamp_range
a min/max range to clamp the normalized
result by.
a min / max range to clamp the normalized result by .
workspace_dir
path to a workspace to create and keep
intermediate files.
path to a workspace to create and keep intermediate files .
target_path