0

I'd like to resample three images into the same scale as the rainfall data in GEE. So far the code below works for Sentinel but not for the soils data which is still in 30 meters.

var rainfall = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY')
                  .filter(ee.Filter.date('2019-01-01','2019-06-30'))
                  .filterBounds(Drakensberg3)

var proj_var = rainfall.first()
                .projection();
                
                
 
// Define variables
var aoi = ee.FeatureCollection(Drakensberg3)

var START_DATE = '2019-04-01'
var END_DATE = '2019-06-30'
var CLOUD_FILTER = 100
var CLD_PRB_THRESH = 20
var NIR_DRK_THRESH = 0.15
var CLD_PRJ_DIST = 1
var BUFFER = 50               
                
             
function get_s2_sr_cld_col(aoi, start_date, end_date) {
    // # Import and filter S2 SR.
    var s2_sr_col = (ee.ImageCollection('COPERNICUS/S2_SR')
        .filterBounds(aoi)
        .filterDate(start_date, end_date)
        .filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE', CLOUD_FILTER)))

    // # Import and filter s2cloudless.
    var s2_cloudless_col = (ee.ImageCollection('COPERNICUS/S2_CLOUD_PROBABILITY')
        .filterBounds(aoi)
        .filterDate(start_date, end_date))

    // # Join the filtered s2cloudless collection to the SR collection by the 'system:index' property.
    return ee.ImageCollection(ee.Join.saveFirst('s2cloudless').apply({
        'primary': s2_sr_col,
        'secondary': s2_cloudless_col,
        'condition': ee.Filter.equals({
            'leftField': 'system:index',
            'rightField': 'system:index'
        })
    }))
}


function add_cloud_bands(img) {
    // # Get s2cloudless image, subset the probability band.
    var cld_prb = ee.Image(img.get('s2cloudless')).select('probability')

    // # Condition s2cloudless by the probability threshold value.
    var is_cloud = cld_prb.gt(CLD_PRB_THRESH).rename('clouds')

    // # Add the cloud probability layer and cloud mask as image bands.
    return img.addBands(ee.Image([cld_prb, is_cloud]))
    
}


function add_shadow_bands(img) {
    // # Identify water pixels from the SCL band.
    var not_water = img.select('SCL').neq(6)

    // # Identify dark NIR pixels that are not water (potential cloud shadow pixels).
    var SR_BAND_SCALE = 1e4
    var dark_pixels = img.select('B8').lt(NIR_DRK_THRESH*SR_BAND_SCALE).multiply(not_water).rename('dark_pixels')

    // # Determine the direction to project cloud shadow from clouds (assumes UTM projection).
    var shadow_azimuth = ee.Number(90).subtract(ee.Number(img.get('MEAN_SOLAR_AZIMUTH_ANGLE')));

    // # Project shadows from clouds for the distance specified by the CLD_PRJ_DIST input.
    var cld_proj = (img.select('clouds').directionalDistanceTransform(shadow_azimuth, CLD_PRJ_DIST*10)
        .reproject({'crs': img.select(0).projection(), 'scale': 100})
        .select('distance')
        .mask()
        .rename('cloud_transform'))

    // # Identify the intersection of dark pixels with cloud shadow projection.
    var shadows = cld_proj.multiply(dark_pixels).rename('shadows')

    // # Add dark pixels, cloud projection, and identified shadows as image bands.
    return img.addBands(ee.Image([dark_pixels, cld_proj, shadows]))
}


function add_cld_shdw_mask(img) {
    // # Add cloud component bands.
    var img_cloud = add_cloud_bands(img)

    // # Add cloud shadow component bands.
    var img_cloud_shadow = add_shadow_bands(img_cloud)

    // # Combine cloud and shadow mask, set cloud and shadow as value 1, else 0.
    var is_cld_shdw = img_cloud_shadow.select('clouds').add(img_cloud_shadow.select('shadows')).gt(0)

    // # Remove small cloud-shadow patches and dilate remaining pixels by BUFFER input.
    // # 20 m scale is for speed, and assumes clouds don't require 10 m precision.
    is_cld_shdw = (is_cld_shdw.focal_min(2).focal_max(BUFFER*2/20)
        .reproject({'crs': img.select([0]).projection(), 'scale': 20})
        .rename('cloudmask'))

    // # Add the final cloud-shadow mask to the image.
    return img_cloud_shadow.addBands(is_cld_shdw)
}



function apply_cld_shdw_mask(img) {
    // # Subset the cloudmask band and invert it so clouds/shadow are 0, else 1.
    var not_cld_shdw = img.select('cloudmask').not()

    // # Subset reflectance bands and update their masks, return the result.
    return img.select('B.*').updateMask(not_cld_shdw)
}


function resample(s2_sr_col) { 
    var s2_sr_col_res = s2_sr_col.resample('bilinear').reproject(proj_var);
    return s2_sr_col_res;
}

var s2_sr_cld_col = get_s2_sr_cld_col(aoi, START_DATE, END_DATE)

//get median processes sentinel image with indeces as additional bands
var s2_sr_median = (s2_sr_cld_col.map(add_cld_shdw_mask))
                             .map(apply_cld_shdw_mask)
                              .filterBounds(aoi)
                              .median()


var visPaaramsTrue = {bands: ['B4','B3','B2'], min:0, max:3000, gamma: 1.4};
var s2_sr_medianClip = s2_sr_median.clip(Drakensberg3);
Map.addLayer(s2_sr_medianClip, visPaaramsTrue, 'Sentinel');
Map.centerObject(Drakensberg3, 8);

function resample(Soil_texture_top) { 
    var Soil_texture_top_res = Soil_texture_top.resample('bilinear').reproject(proj_var);
    return Soil_texture_top_res;
}


var Soil_texture_top = ee.Image('ISDASOIL/Africa/v1/texture_class')
.select ('texture_0_20')
.rename('soil1');

//Color pallette and map both soil layers 
var vizParams = {
min: 0,
max: 2000,
palette: ['001137', '0aab1e', 'e7eb05', 'ff4a2d', 'e90000'],
};

var Soil_texture_top_clip = Soil_texture_top.clipToCollection(Drakensberg3)
Map.addLayer(Soil_texture_top_clip, vizParams, 'Soil_Texture1');


var mosaic = rainfall.mosaic();
print(mosaic.projection());
print('Scale in meters:', mosaic.projection().nominalScale());

print('Scale in meters:', s2_sr_medianClip.projection().nominalScale());
print('Scale in meters:', Soil_texture_top_clip.projection().nominalScale());

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.