potential_pollination.py
def create_radial_convolution_mask(
        pixel_size_degree, radius_meters, kernel_filepath):
    """Create a radial mask to sample pixels in convolution filter.

    Parameters:
        pixel_size_degree (float): size of pixel in degrees.
        radius_meters (float): desired size of radial mask in meters.

    Returns:
        A 2D numpy array that can be used in a convolution to aggregate a
        raster while accounting for partial coverage of the circle on the
        edges of the pixel.

    """
    degree_len_0 = 110574  # length at 0 degrees
    degree_len_60 = 111412  # length at 60 degrees
    pixel_size_m = pixel_size_degree * (degree_len_0 + degree_len_60) / 2.0
    pixel_radius = numpy.ceil(radius_meters / pixel_size_m)
    n_pixels = (int(pixel_radius) * 2 + 1)
    sample_pixels = 200
    mask = numpy.ones((sample_pixels * n_pixels, sample_pixels * n_pixels))
    mask[mask.shape[0]//2, mask.shape[0]//2] = 0
    distance_transform = scipy.ndimage.morphology.distance_transform_edt(mask)
    mask = None
    stratified_distance = distance_transform * pixel_size_m / sample_pixels
    distance_transform = None
    in_circle = numpy.where(stratified_distance <= 2000.0, 1.0, 0.0)
    stratified_distance = None
    reshaped = in_circle.reshape(
        in_circle.shape[0] // sample_pixels, sample_pixels,
        in_circle.shape[1] // sample_pixels, sample_pixels)
    kernel_array = numpy.sum(reshaped, axis=(1, 3)) / sample_pixels**2
    normalized_kernel_array = kernel_array / numpy.sum(kernel_array)
    reshaped = None

    driver = gdal.GetDriverByName('GTiff')
    kernel_raster = driver.Create(
        kernel_filepath.encode('utf-8'), n_pixels, n_pixels, 1,
        gdal.GDT_Float32, options=[
            'BIGTIFF=IF_SAFER', 'TILED=YES', 'BLOCKXSIZE=256',
            'BLOCKYSIZE=256'])

    # Make some kind of geotransform, it doesn't matter what but
    # will make GIS libraries behave better if it's all defined
    kernel_raster.SetGeoTransform([-180, 1, 0, 90, 0, -1])
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(4326)
    kernel_raster.SetProjection(srs.ExportToWkt())
    kernel_band = kernel_raster.GetRasterBand(1)
    kernel_band.SetNoDataValue(TARGET_NODATA)
    kernel_band.WriteArray(normalized_kernel_array)
Create a radial mask to sample pixels in convolution filter.

    Parameters:
        pixel_size_degree (float): size of pixel in degrees.
        radius_meters (float): desired size of radial mask in meters.

    Returns:
        A 2D numpy array that can be used in a convolution to aggregate a
        raster while accounting for partial coverage of the circle on the
        edges of the pixel.

    
Create a radial mask to sample pixels in convolution filter.
Create a radial mask to sample pixels in convolution filter .
def create_radial_convolution_mask(
        pixel_size_degree, radius_meters, kernel_filepath):
    degree_len_0 = 110574  
    degree_len_60 = 111412  
    pixel_size_m = pixel_size_degree * (degree_len_0 + degree_len_60) / 2.0
    pixel_radius = numpy.ceil(radius_meters / pixel_size_m)
    n_pixels = (int(pixel_radius) * 2 + 1)
    sample_pixels = 200
    mask = numpy.ones((sample_pixels * n_pixels, sample_pixels * n_pixels))
    mask[mask.shape[0]//2, mask.shape[0]//2] = 0
    distance_transform = scipy.ndimage.morphology.distance_transform_edt(mask)
    mask = None
    stratified_distance = distance_transform * pixel_size_m / sample_pixels
    distance_transform = None
    in_circle = numpy.where(stratified_distance <= 2000.0, 1.0, 0.0)
    stratified_distance = None
    reshaped = in_circle.reshape(
        in_circle.shape[0] // sample_pixels, sample_pixels,
        in_circle.shape[1] // sample_pixels, sample_pixels)
    kernel_array = numpy.sum(reshaped, axis=(1, 3)) / sample_pixels**2
    normalized_kernel_array = kernel_array / numpy.sum(kernel_array)
    reshaped = None
    driver = gdal.GetDriverByName('GTiff')
    kernel_raster = driver.Create(
        kernel_filepath.encode('utf-8'), n_pixels, n_pixels, 1,
        gdal.GDT_Float32, options=[
            'BIGTIFF=IF_SAFER', 'TILED=YES', 'BLOCKXSIZE=256',
            'BLOCKYSIZE=256'])
    kernel_raster.SetGeoTransform([-180, 1, 0, 90, 0, -1])
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(4326)
    kernel_raster.SetProjection(srs.ExportToWkt())
    kernel_band = kernel_raster.GetRasterBand(1)
    kernel_band.SetNoDataValue(TARGET_NODATA)
    kernel_band.WriteArray(normalized_kernel_array)
def create_radial_convolution_mask ( pixel_size_degree , radius_meters , kernel_filepath ) : degree_len_0 = 110574 degree_len_60 = 111412 pixel_size_m = pixel_size_degree * ( degree_len_0 + degree_len_60 ) / 2.0 pixel_radius = numpy . ceil ( radius_meters / pixel_size_m ) n_pixels = ( int ( pixel_radius ) * 2 + 1 ) sample_pixels = 200 mask = numpy . ones ( ( sample_pixels * n_pixels , sample_pixels * n_pixels ) ) mask [ mask . shape [ 0 ] // 2 , mask . shape [ 0 ] // 2 ] = 0 distance_transform = scipy . ndimage . morphology . distance_transform_edt ( mask ) mask = None stratified_distance = distance_transform * pixel_size_m / sample_pixels distance_transform = None in_circle = numpy . where ( stratified_distance <= 2000.0 , 1.0 , 0.0 ) stratified_distance = None reshaped = in_circle . reshape ( in_circle . shape [ 0 ] // sample_pixels , sample_pixels , in_circle . shape [ 1 ] // sample_pixels , sample_pixels ) kernel_array = numpy . sum ( reshaped , axis = ( 1 , 3 ) ) / sample_pixels ** 2 normalized_kernel_array = kernel_array / numpy . sum ( kernel_array ) reshaped = None driver = gdal . GetDriverByName ( 'GTiff' ) kernel_raster = driver . Create ( kernel_filepath . encode ( 'utf-8' ) , n_pixels , n_pixels , 1 , gdal . GDT_Float32 , options = [ 'BIGTIFF=IF_SAFER' , 'TILED=YES' , 'BLOCKXSIZE=256' , 'BLOCKYSIZE=256' ] ) kernel_raster . SetGeoTransform ( [ - 180 , 1 , 0 , 90 , 0 , - 1 ] ) srs = osr . SpatialReference ( ) srs . ImportFromEPSG ( 4326 ) kernel_raster . SetProjection ( srs . ExportToWkt ( ) ) kernel_band = kernel_raster . GetRasterBand ( 1 ) kernel_band . SetNoDataValue ( TARGET_NODATA ) kernel_band . WriteArray ( normalized_kernel_array )
Create a radial mask to sample pixels in convolution filter.
Create a radial mask to sample pixels in convolution filter .
"""Create a radial mask to sample pixels in convolution filter.

    Parameters:
        pixel_size_degree (float): size of pixel in degrees.
        radius_meters (float): desired size of radial mask in meters.

    Returns:
        A 2D numpy array that can be used in a convolution to aggregate a
        raster while accounting for partial coverage of the circle on the
        edges of the pixel.

    """ # length at 0 degrees
 # length at 60 degrees
 # Make some kind of geotransform, it doesn't matter what but
 # will make GIS libraries behave better if it's all defined

A 2D numpy array that can be used in a convolution to aggregate a
raster while accounting for partial coverage of the circle on the
edges of the pixel.
A 2D numpy array that can be used in a convolution to aggregate a raster while accounting for partial coverage of the circle on the edges of the pixel .
pixel_size_degree
size of pixel in degrees.
size of pixel in degrees .
radius_meters
desired size of radial mask in meters.
desired size of radial mask in meters .
kernel_filepath