cumulative_density_function_per_country.py
def extract_feature_checked(
        align_lock, vector_path, field_name, field_value, base_raster_path,
        target_vector_path, target_raster_path):
    """Extract single feature into separate vector and check for no error.

    Do not do a transform since it's all wgs84.

    Parameters:
        align_lock (multiprocessing.Lock): lock to only allow one align at
            a time.
        vector_path (str): base vector in WGS84 coordinates.
        field_name (str): field to search for
        field_value (str): field value to isolate
        base_raster_path (str): path to raster to clip out.
        target_vector_path (str): path to new GPKG vector that will
            contain only that feature.
        target_raster_path (str): path to clipped out raster

    Returns:
        True if no error, False otherwise.

    """
    attempt_number = 0
    if not os.path.exists(base_raster_path):
        raise ValueError("%s does not exist" % base_raster_path)
    while True:
        try:
            LOGGER.debug('opening vector: %s', vector_path)
            base_vector = gdal.OpenEx(vector_path, gdal.OF_VECTOR)
            LOGGER.debug('getting layer')
            base_layer = base_vector.GetLayer()
            feature = None
            LOGGER.debug('iterating over features')
            for base_feature in base_layer:
                if base_feature.GetField(field_name) == field_value:
                    feature = base_feature
                    break
            LOGGER.debug('extracting feature %s', feature.GetField(field_name))

            geom = feature.GetGeometryRef()
            base_srs = base_layer.GetSpatialRef()

            base_raster_info = pygeoprocessing.get_raster_info(
                base_raster_path)

            base_layer = None
            base_vector = None

            # create a new shapefile
            if os.path.exists(target_vector_path):
                os.remove(target_vector_path)
            driver = ogr.GetDriverByName('GPKG')
            target_vector = driver.CreateDataSource(
                target_vector_path)
            target_layer = target_vector.CreateLayer(
                os.path.splitext(os.path.basename(target_vector_path))[0],
                base_srs, ogr.wkbMultiPolygon)
            layer_defn = target_layer.GetLayerDefn()
            feature_geometry = geom.Clone()
            base_feature = ogr.Feature(layer_defn)
            base_feature.SetGeometry(feature_geometry)
            target_layer.CreateFeature(base_feature)
            target_layer.SyncToDisk()
            geom = None
            feature_geometry = None
            base_feature = None
            target_layer = None
            target_vector = None

            with align_lock:
                pygeoprocessing.align_and_resize_raster_stack(
                    [base_raster_path], [target_raster_path], ['near'],
                    base_raster_info['pixel_size'], 'intersection',
                    base_vector_path_list=[target_vector_path],
                    vector_mask_options={
                        'mask_vector_path': target_vector_path,
                    })
            return True
        except Exception:
            LOGGER.exception('exception when extracting %s %s %s' % (
                field_name, field_value, vector_path))
            attempt_number += 1
            if attempt_number == 20:
                return False
            time.sleep(min(1, 0.1*2**attempt_number))
Extract single feature into separate vector and check for no error.

    Do not do a transform since it's all wgs84.

    Parameters:
        align_lock (multiprocessing.Lock): lock to only allow one align at
            a time.
        vector_path (str): base vector in WGS84 coordinates.
        field_name (str): field to search for
        field_value (str): field value to isolate
        base_raster_path (str): path to raster to clip out.
        target_vector_path (str): path to new GPKG vector that will
            contain only that feature.
        target_raster_path (str): path to clipped out raster

    Returns:
        True if no error, False otherwise.

    
Extract single feature into separate vector and check for no error.
Do not do a transform since it's all wgs84.
Extract single feature into separate vector and check for no error . Do not do a transform since it ' s all wgs84 .
def extract_feature_checked(
        align_lock, vector_path, field_name, field_value, base_raster_path,
        target_vector_path, target_raster_path):
    attempt_number = 0
    if not os.path.exists(base_raster_path):
        raise ValueError("%s does not exist" % base_raster_path)
    while True:
        try:
            LOGGER.debug('opening vector: %s', vector_path)
            base_vector = gdal.OpenEx(vector_path, gdal.OF_VECTOR)
            LOGGER.debug('getting layer')
            base_layer = base_vector.GetLayer()
            feature = None
            LOGGER.debug('iterating over features')
            for base_feature in base_layer:
                if base_feature.GetField(field_name) == field_value:
                    feature = base_feature
                    break
            LOGGER.debug('extracting feature %s', feature.GetField(field_name))
            geom = feature.GetGeometryRef()
            base_srs = base_layer.GetSpatialRef()
            base_raster_info = pygeoprocessing.get_raster_info(
                base_raster_path)
            base_layer = None
            base_vector = None
            if os.path.exists(target_vector_path):
                os.remove(target_vector_path)
            driver = ogr.GetDriverByName('GPKG')
            target_vector = driver.CreateDataSource(
                target_vector_path)
            target_layer = target_vector.CreateLayer(
                os.path.splitext(os.path.basename(target_vector_path))[0],
                base_srs, ogr.wkbMultiPolygon)
            layer_defn = target_layer.GetLayerDefn()
            feature_geometry = geom.Clone()
            base_feature = ogr.Feature(layer_defn)
            base_feature.SetGeometry(feature_geometry)
            target_layer.CreateFeature(base_feature)
            target_layer.SyncToDisk()
            geom = None
            feature_geometry = None
            base_feature = None
            target_layer = None
            target_vector = None
            with align_lock:
                pygeoprocessing.align_and_resize_raster_stack(
                    [base_raster_path], [target_raster_path], ['near'],
                    base_raster_info['pixel_size'], 'intersection',
                    base_vector_path_list=[target_vector_path],
                    vector_mask_options={
                        'mask_vector_path': target_vector_path,
                    })
            return True
        except Exception:
            LOGGER.exception('exception when extracting %s %s %s' % (
                field_name, field_value, vector_path))
            attempt_number += 1
            if attempt_number == 20:
                return False
            time.sleep(min(1, 0.1*2**attempt_number))
def extract_feature_checked ( align_lock , vector_path , field_name , field_value , base_raster_path , target_vector_path , target_raster_path ) : attempt_number = 0 if not os . path . exists ( base_raster_path ) : raise ValueError ( "%s does not exist" % base_raster_path ) while True : try : LOGGER . debug ( 'opening vector: %s' , vector_path ) base_vector = gdal . OpenEx ( vector_path , gdal . OF_VECTOR ) LOGGER . debug ( 'getting layer' ) base_layer = base_vector . GetLayer ( ) feature = None LOGGER . debug ( 'iterating over features' ) for base_feature in base_layer : if base_feature . GetField ( field_name ) == field_value : feature = base_feature break LOGGER . debug ( 'extracting feature %s' , feature . GetField ( field_name ) ) geom = feature . GetGeometryRef ( ) base_srs = base_layer . GetSpatialRef ( ) base_raster_info = pygeoprocessing . get_raster_info ( base_raster_path ) base_layer = None base_vector = None if os . path . exists ( target_vector_path ) : os . remove ( target_vector_path ) driver = ogr . GetDriverByName ( 'GPKG' ) target_vector = driver . CreateDataSource ( target_vector_path ) target_layer = target_vector . CreateLayer ( os . path . splitext ( os . path . basename ( target_vector_path ) ) [ 0 ] , base_srs , ogr . wkbMultiPolygon ) layer_defn = target_layer . GetLayerDefn ( ) feature_geometry = geom . Clone ( ) base_feature = ogr . Feature ( layer_defn ) base_feature . SetGeometry ( feature_geometry ) target_layer . CreateFeature ( base_feature ) target_layer . SyncToDisk ( ) geom = None feature_geometry = None base_feature = None target_layer = None target_vector = None with align_lock : pygeoprocessing . align_and_resize_raster_stack ( [ base_raster_path ] , [ target_raster_path ] , [ 'near' ] , base_raster_info [ 'pixel_size' ] , 'intersection' , base_vector_path_list = [ target_vector_path ] , vector_mask_options = { 'mask_vector_path' : target_vector_path , } ) return True except Exception : LOGGER . exception ( 'exception when extracting %s %s %s' % ( field_name , field_value , vector_path ) ) attempt_number += 1 if attempt_number == 20 : return False time . sleep ( min ( 1 , 0.1 * 2 ** attempt_number ) )
Extract single feature into separate vector and check for no error.
Extract single feature into separate vector and check for no error .
"""Extract single feature into separate vector and check for no error.

    Do not do a transform since it's all wgs84.

    Parameters:
        align_lock (multiprocessing.Lock): lock to only allow one align at
            a time.
        vector_path (str): base vector in WGS84 coordinates.
        field_name (str): field to search for
        field_value (str): field value to isolate
        base_raster_path (str): path to raster to clip out.
        target_vector_path (str): path to new GPKG vector that will
            contain only that feature.
        target_raster_path (str): path to clipped out raster

    Returns:
        True if no error, False otherwise.

    """ # create a new shapefile

True if no error, False otherwise.
True if no error False otherwise .
align_lock
lock to only allow one align at
a time.
lock to only allow one align at a time .
vector_path
base vector in WGS84 coordinates.
base vector in WGS84 coordinates .
field_name
field to search for
field to search for
field_value
field value to isolate
field value to isolate
base_raster_path
path to raster to clip out.
path to raster to clip out .
target_vector_path
path to new GPKG vector that will
contain only that feature.
path to new GPKG vector that will contain only that feature .
target_raster_path
path to clipped out raster
path to clipped out raster