cumulative_density_function_per_country.py
def stitch_raster(
        lock_map, cpu_semaphore, payload, raster_id_to_global_stitch_path_map):
    """Stitch incoming country rasters into global raster.

    Parameters:
        lock_map (dict): maps global raster paths to locks so we don't
        cpu_semaphore (BoundedSemaphore): release this when stitch is done
        payload (tuple): payloads come in as an alert that a sub raster
            is ready for stitching into the global raster. Payloads are of the
            form (bin_raster_path,(raster_id, aggregate_vector_id, nodataflag))
            is indexed into
            `raster_id_to_global_stitch_path_map[
                (raster_id, aggregate_id, nodata_flag)]`.
        raster_id_to_global_stitch_path_map (dict): dictionary indexed by
            raster id, aggregate id, and nodata flag tuple to the global
            raster.

    """
    local_tile_raster_path, raster_aggregate_nodata_id_tuple = payload
    global_stitch_raster_path = raster_id_to_global_stitch_path_map[
        raster_aggregate_nodata_id_tuple]

    # get ul of tile and figure out where it goes in global
    local_tile_info = pygeoprocessing.get_raster_info(
        local_tile_raster_path)
    global_stitch_info = pygeoprocessing.get_raster_info(
        global_stitch_raster_path)
    global_inv_gt = gdal.InvGeoTransform(
        global_stitch_info['geotransform'])
    local_gt = local_tile_info['geotransform']
    global_i, global_j = gdal.ApplyGeoTransform(
        global_inv_gt, local_gt[0], local_gt[3])

    for offsect_dict, local_array in pygeoprocessing.iterblocks(
            (local_tile_raster_path, 1)):
        valid_mask = ~numpy.isclose(
            local_array, local_tile_info['nodata'][0])
        if valid_mask.size > 0:
            with lock_map[global_stitch_raster_path]:
                global_raster = gdal.OpenEx(
                    global_stitch_raster_path, gdal.OF_RASTER | gdal.GA_Update)

                LOGGER.debug(
                    'stitching this %s into this %s',
                    payload, global_stitch_raster_path)
                global_band = global_raster.GetRasterBand(1)
                local_i = global_i+offsect_dict['xoff']
                local_j = global_j+offsect_dict['yoff']
                global_array = global_band.ReadAsArray(
                    xoff=local_i,
                    yoff=local_j,
                    win_xsize=local_array.shape[1],
                    win_ysize=local_array.shape[0])
                global_array[valid_mask] = local_array[valid_mask]
                win_ysize_write, win_xsize_write = global_array.shape
                if win_ysize_write == 0 or win_xsize_write == 0:
                    raise ValueError(
                        'got zeros on sizes: %d %d %s',
                        win_ysize_write, win_xsize_write, payload)
                if local_i + win_xsize_write >= global_band.XSize:
                    win_xsize_write = int(global_band.XSize - local_i)
                if local_j + win_ysize_write >= global_band.YSize:
                    win_ysize_write = int(global_band.YSize - local_j)

                global_band.WriteArray(
                    global_array[0:win_ysize_write, 0:win_xsize_write],
                    xoff=local_i, yoff=local_j)
                global_band.FlushCache()
                global_band = None
                global_raster = None

    # update the done database
    if raster_aggregate_nodata_id_tuple[2] == '':
        sql_string = '''
            UPDATE job_status
                SET
                  stitched_bin=1
                WHERE
                    bin_raster_path=? AND raster_id=? AND
                    aggregate_vector_id=?
            '''
    else:
        sql_string = '''
            UPDATE job_status
                SET
                  stitched_bin_nodata0=1
                WHERE
                    bin_nodata0_raster_path=? AND raster_id=? AND
                    aggregate_vector_id=?
            '''
    LOGGER.debug(
        'update the database with this command %s\nand these data:%s',
        sql_string, [
            local_tile_raster_path,
            raster_aggregate_nodata_id_tuple[0],
            raster_aggregate_nodata_id_tuple[1]])
    _execute_sqlite(
        sql_string, WORK_DATABASE_PATH, mode='modify',
        execute='execute', argument_list=[
            local_tile_raster_path,
            raster_aggregate_nodata_id_tuple[0],
            raster_aggregate_nodata_id_tuple[1]])
    cpu_semaphore.release()
Stitch incoming country rasters into global raster.

    Parameters:
        lock_map (dict): maps global raster paths to locks so we don't
        cpu_semaphore (BoundedSemaphore): release this when stitch is done
        payload (tuple): payloads come in as an alert that a sub raster
            is ready for stitching into the global raster. Payloads are of the
            form (bin_raster_path,(raster_id, aggregate_vector_id, nodataflag))
            is indexed into
            `raster_id_to_global_stitch_path_map[
                (raster_id, aggregate_id, nodata_flag)]`.
        raster_id_to_global_stitch_path_map (dict): dictionary indexed by
            raster id, aggregate id, and nodata flag tuple to the global
            raster.

    
Stitch incoming country rasters into global raster.
Stitch incoming country rasters into global raster .
def stitch_raster(
        lock_map, cpu_semaphore, payload, raster_id_to_global_stitch_path_map):
    local_tile_raster_path, raster_aggregate_nodata_id_tuple = payload
    global_stitch_raster_path = raster_id_to_global_stitch_path_map[
        raster_aggregate_nodata_id_tuple]
    local_tile_info = pygeoprocessing.get_raster_info(
        local_tile_raster_path)
    global_stitch_info = pygeoprocessing.get_raster_info(
        global_stitch_raster_path)
    global_inv_gt = gdal.InvGeoTransform(
        global_stitch_info['geotransform'])
    local_gt = local_tile_info['geotransform']
    global_i, global_j = gdal.ApplyGeoTransform(
        global_inv_gt, local_gt[0], local_gt[3])
    for offsect_dict, local_array in pygeoprocessing.iterblocks(
            (local_tile_raster_path, 1)):
        valid_mask = ~numpy.isclose(
            local_array, local_tile_info['nodata'][0])
        if valid_mask.size > 0:
            with lock_map[global_stitch_raster_path]:
                global_raster = gdal.OpenEx(
                    global_stitch_raster_path, gdal.OF_RASTER | gdal.GA_Update)
                LOGGER.debug(
                    'stitching this %s into this %s',
                    payload, global_stitch_raster_path)
                global_band = global_raster.GetRasterBand(1)
                local_i = global_i+offsect_dict['xoff']
                local_j = global_j+offsect_dict['yoff']
                global_array = global_band.ReadAsArray(
                    xoff=local_i,
                    yoff=local_j,
                    win_xsize=local_array.shape[1],
                    win_ysize=local_array.shape[0])
                global_array[valid_mask] = local_array[valid_mask]
                win_ysize_write, win_xsize_write = global_array.shape
                if win_ysize_write == 0 or win_xsize_write == 0:
                    raise ValueError(
                        'got zeros on sizes: %d %d %s',
                        win_ysize_write, win_xsize_write, payload)
                if local_i + win_xsize_write >= global_band.XSize:
                    win_xsize_write = int(global_band.XSize - local_i)
                if local_j + win_ysize_write >= global_band.YSize:
                    win_ysize_write = int(global_band.YSize - local_j)
                global_band.WriteArray(
                    global_array[0:win_ysize_write, 0:win_xsize_write],
                    xoff=local_i, yoff=local_j)
                global_band.FlushCache()
                global_band = None
                global_raster = None
    if raster_aggregate_nodata_id_tuple[2] == '':
        sql_string = '''
            UPDATE job_status
                SET
                  stitched_bin=1
                WHERE
                    bin_raster_path=? AND raster_id=? AND
                    aggregate_vector_id=?
            '''
    else:
        sql_string = '''
            UPDATE job_status
                SET
                  stitched_bin_nodata0=1
                WHERE
                    bin_nodata0_raster_path=? AND raster_id=? AND
                    aggregate_vector_id=?
            '''
    LOGGER.debug(
        'update the database with this command %s\nand these data:%s',
        sql_string, [
            local_tile_raster_path,
            raster_aggregate_nodata_id_tuple[0],
            raster_aggregate_nodata_id_tuple[1]])
    _execute_sqlite(
        sql_string, WORK_DATABASE_PATH, mode='modify',
        execute='execute', argument_list=[
            local_tile_raster_path,
            raster_aggregate_nodata_id_tuple[0],
            raster_aggregate_nodata_id_tuple[1]])
    cpu_semaphore.release()
def stitch_raster ( lock_map , cpu_semaphore , payload , raster_id_to_global_stitch_path_map ) : local_tile_raster_path , raster_aggregate_nodata_id_tuple = payload global_stitch_raster_path = raster_id_to_global_stitch_path_map [ raster_aggregate_nodata_id_tuple ] local_tile_info = pygeoprocessing . get_raster_info ( local_tile_raster_path ) global_stitch_info = pygeoprocessing . get_raster_info ( global_stitch_raster_path ) global_inv_gt = gdal . InvGeoTransform ( global_stitch_info [ 'geotransform' ] ) local_gt = local_tile_info [ 'geotransform' ] global_i , global_j = gdal . ApplyGeoTransform ( global_inv_gt , local_gt [ 0 ] , local_gt [ 3 ] ) for offsect_dict , local_array in pygeoprocessing . iterblocks ( ( local_tile_raster_path , 1 ) ) : valid_mask = ~ numpy . isclose ( local_array , local_tile_info [ 'nodata' ] [ 0 ] ) if valid_mask . size > 0 : with lock_map [ global_stitch_raster_path ] : global_raster = gdal . OpenEx ( global_stitch_raster_path , gdal . OF_RASTER | gdal . GA_Update ) LOGGER . debug ( 'stitching this %s into this %s' , payload , global_stitch_raster_path ) global_band = global_raster . GetRasterBand ( 1 ) local_i = global_i + offsect_dict [ 'xoff' ] local_j = global_j + offsect_dict [ 'yoff' ] global_array = global_band . ReadAsArray ( xoff = local_i , yoff = local_j , win_xsize = local_array . shape [ 1 ] , win_ysize = local_array . shape [ 0 ] ) global_array [ valid_mask ] = local_array [ valid_mask ] win_ysize_write , win_xsize_write = global_array . shape if win_ysize_write == 0 or win_xsize_write == 0 : raise ValueError ( 'got zeros on sizes: %d %d %s' , win_ysize_write , win_xsize_write , payload ) if local_i + win_xsize_write >= global_band . XSize : win_xsize_write = int ( global_band . XSize - local_i ) if local_j + win_ysize_write >= global_band . YSize : win_ysize_write = int ( global_band . YSize - local_j ) global_band . WriteArray ( global_array [ 0 : win_ysize_write , 0 : win_xsize_write ] , xoff = local_i , yoff = local_j ) global_band . FlushCache ( ) global_band = None global_raster = None if raster_aggregate_nodata_id_tuple [ 2 ] == '' : sql_string = '''
            UPDATE job_status
                SET
                  stitched_bin=1
                WHERE
                    bin_raster_path=? AND raster_id=? AND
                    aggregate_vector_id=?
            ''' else : sql_string = '''
            UPDATE job_status
                SET
                  stitched_bin_nodata0=1
                WHERE
                    bin_nodata0_raster_path=? AND raster_id=? AND
                    aggregate_vector_id=?
            ''' LOGGER . debug ( 'update the database with this command %s\nand these data:%s' , sql_string , [ local_tile_raster_path , raster_aggregate_nodata_id_tuple [ 0 ] , raster_aggregate_nodata_id_tuple [ 1 ] ] ) _execute_sqlite ( sql_string , WORK_DATABASE_PATH , mode = 'modify' , execute = 'execute' , argument_list = [ local_tile_raster_path , raster_aggregate_nodata_id_tuple [ 0 ] , raster_aggregate_nodata_id_tuple [ 1 ] ] ) cpu_semaphore . release ( )
Stitch incoming country rasters into global raster.
Stitch incoming country rasters into global raster .
"""Stitch incoming country rasters into global raster.

    Parameters:
        lock_map (dict): maps global raster paths to locks so we don't
        cpu_semaphore (BoundedSemaphore): release this when stitch is done
        payload (tuple): payloads come in as an alert that a sub raster
            is ready for stitching into the global raster. Payloads are of the
            form (bin_raster_path,(raster_id, aggregate_vector_id, nodataflag))
            is indexed into
            `raster_id_to_global_stitch_path_map[
                (raster_id, aggregate_id, nodata_flag)]`.
        raster_id_to_global_stitch_path_map (dict): dictionary indexed by
            raster id, aggregate id, and nodata flag tuple to the global
            raster.

    """ # get ul of tile and figure out where it goes in global
 # update the done database

lock_map
maps global raster paths to locks so we don't
maps global raster paths to locks so we don ' t
cpu_semaphore
release this when stitch is done
release this when stitch is done
payload
payloads come in as an alert that a sub raster
is ready for stitching into the global raster. Payloads are of the
form (bin_raster_path,(raster_id, aggregate_vector_id, nodataflag))
is indexed into
`raster_id_to_global_stitch_path_map[
(raster_id, aggregate_id, nodata_flag)]`.
payloads come in as an alert that a sub raster is ready for stitching into the global raster . Payloads are of the form ( bin_raster_path ( raster_id aggregate_vector_id nodataflag )) is indexed into ` raster_id_to_global_stitch_path_map [ ( raster_id aggregate_id nodata_flag ) ] ` .
raster_id_to_global_stitch_path_map
dictionary indexed by
raster id, aggregate id, and nodata flag tuple to the global
raster.
dictionary indexed by raster id aggregate id and nodata flag tuple to the global raster .