How do you import sentinel - 2 data in Earth engine - google-earth-engine

This is the code i have so far.
var roi = ee.FeatureCollection(polygon);
Map.addLayer(roi, {}, 'Turkey');
//Load Sentinel data
var image = ee.ImageCollection("COPERNICUS/S2_SR")
filterDate('2019-04-01', '2019-04-10')
//Remove cloud contamination
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
.filterBounds(roi)
//Visualise data
var visParamsTrue = {bands: ['B4', 'B3', 'B2'], min:0, max:2500 ,gamma:1.1};
Map.addLayer(image.clip(roi), visParamsTrue, 'Sentinel 2019');
Map.centerObject(roi, 20);
I want to import sentinel - 2 data within the study region using Google Earth Engine.
This is just an example of the expected result -
Sentinel data is imported within the vector points
This is what i get -
Sentinel data is not imported
How do i fix this?
The code above is taken from the youtube video : https://www.youtube.com/watch?v=N_yJDMgRfik

Related

How do I merge a collection of imagines into a single image then export it

I would like to merge a collection of images (unknown amount) of a polygon into a single image and then export it to google drive for analysis in qgis. This is the code I have tried to us
// Define the AOI
var aoi = XXX;
Map.centerObject(aoi);
Map.setOptions('SATELLITE');
var dataset = ee.ImageCollection('UQ/murray/Intertidal/v1_1/global_intertidal');
var visualization = {
bands: ['classification'],
min: 0.0,
max: 1.0,
palette: ['0000FF']
};
Map.addLayer(dataset, visualization, 'Intertidal areas');
I have tried the following code to export the image, but obviously because I am viewing a collection of images, I cannot export a single image of the mosaic
// Export to base Google Drive
Export.image.toDrive({ image: FraserRiver , description: 'exportToDrive', fileNamePrefix: ' FraserRiver', scale: 30, region: aoi, maxPixels: 800000000000 });
This code simply extracts all bands within an image collection and put them in one single image (got the idea from https://www.nature.com/articles/s41597-021-00827-9):
// Function to merge bands
var mergeBands = function(image, previous) {
return ee.Image(previous).addBands(image);
};
// Merge bands
var image = ee.Image(mycollection.iterate(mergeBands, ee.Image([])))

Google Earth Engine NBR daily difference(δNBR)

I calculated the NBR for a Landsat 8 imagecollection and mapped it over. I now want to calculate daily difference of the index as NBR[i] - NBR[i-1] for each day ,for each pixel in the image collection anything I have tried so far has failed.
var landsat8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate("2015-01-1","2017-12-31").filterBounds(geometry);
var NBR=function(image){
var nbr=image.normalizedDifference(["B5","B7"]);
return image.addBands(nbr.rename("nbr"));
}
var imNBR=landsat8.map(NBR);
print(imNBR);

image.filter is not a function in google earth engine

As a newbie to the google earth engine, I have been trying something (https://code.earthengine.google.com/6f45059a59b75757c88ce2d3869fc9fd) following a NASA tutorial (https://www.youtube.com/watch?v=JFvxudueT_k&ab_channel=NASAVideo). My last line (line 60) shows image.filter is not a function, while the one in the tutorial (line 34) is working. I am not sure what happened and how to sort this out?
//creating a new variable 'image' from the L8 collection data imported
var image = ee.Image (L8_tier1 //the details in the data will represent that the band resolution is 30m
//the details in the data will represent that the band resolution is 30m
//.filterDate ("2019-07-01","2021-10-03") //for a specific date range. maybe good to remove it for the function.
//the details in the data will represent that the band resolution is 30m
//the details in the data will represent that the band resolution is 30m
//.filterDate ("2019-07-01","2021-10-03") //for a specific date range. maybe good to remove it for the function.
.filterBounds (ROI) //for the region of interest we are interested in
//.sort ("COLUD_COVER") //for sorting the data between the range with a cloud cover, the metadata property we are interested in. Other way to do this is using the function below.
//.first() //this will make the image choose the first image with the least amount of cloud cover for the area. Other way to do this is using the function below.
);
//print ("Hague and Rotterdam", image); //printing the image in the console
//console on the right hand side will explain everything from the data
//id will show the image deatils and date of the image, for this case 29th July 2019
//under the properties tab cloud cover can be found, this is the least we can get for this area during this period
// //vizualisation of the data in the map with true color rendering
// var trueColour = {
// bands:["SR_B4","SR_B3","SR_B2"],
// min: 5000,
// max: 12000
// };
// Map.centerObject (ROI, 12); //for the centering the area in the center of the map with required zoom level
// Map.addLayer (image, trueColour, "Hague and Rotterdam"); //for adding the image with the variable of bands we made and naming the image
//Alternate way
//Function to cloud mask from the qa_pixel band of Landsat 8 SR data. In this case bits 3 and 4 are clouds and cloud shadow respectively. This can be different for different image sets.
function maskL8sr(image) {
var cloudsBitMask = 1 << 3; //remember to check this with the source
var cloudshadowBitMask = 1 << 4; //remember to check this with the source
var qa = image.select ('qa_pixel'); //creating the new variable from the band of the source image
var mask = qa.bitwiseAnd(cloudsBitMask).eq(0) //making the cloud equal to zero to mask them out
.and(qa.bitwiseAnd(cloudshadowBitMask).eq(0)); //making the cloud shadow equal to zero to mask them out
return image.updateMask(mask).divide(10000)
.select("SR_B[0-9]*")
.copyProperties(image, ["system:time_start"]);
}
// print ("Hague and Rotterdam", image);// look into the console now. How many images the code have downloaded!!!
//filtering imagery for 2015 to 2021 summer date ranges
//creating joint filter and applying to image collection
var sum21 = ee.Filter.date ('2021-06-01','2021-09-30');
var sum20 = ee.Filter.date ('2020-06-01','2020-09-30');
var sum19 = ee.Filter.date ('2019-06-01','2019-09-30');
var sum18 = ee.Filter.date ('2018-06-01','2018-09-30');
var sum17 = ee.Filter.date ('2017-06-01','2017-09-30');
var sum16 = ee.Filter.date ('2016-06-01','2016-09-30');
var sum15 = ee.Filter.date ('2015-06-01','2015-09-30');
var SumFilter = ee.Filter.or(sum21, sum20, sum19, sum18, sum17, sum16, sum15);
var allsum = image.filter(SumFilter);
Filtering is an operation you can do on ImageCollections, not individual Images, because all filtering does is choose a subset of the images. Then, in your script, you have (with the comments removed):
var image = ee.Image (L8_tier1
.filterBounds (ROI)
);
The result of l8_tier1.filterBounds(ROI) is indeed an ImageCollection. But in this case, you have told the Earth Engine client that it should be treated as an Image, and it believed you. So, then, the last line
var allsum = image.filter(SumFilter);
fails with the error you saw because there is no filter() on ee.Image.
The script will successfully run if you change ee.Image(...) to ee.ImageCollection(...), or even better, remove the cast because it's not necessary — that is,
var image = L8_tier1.filterBounds(ROI);
You should probably also change the name of var image too, since it is confusing to call an ImageCollection by the name image. Naming things accurately helps avoid mistakes, while you are working on the code and also when others try to read it or build on it.

Google Earth Engine video export error (a.element.map is not a function)

I'm trying to export a time-lapse here but got a weird error:
Error Creating or Submitting Task
a.element.map is not a function
I want to keep the visParams on my exported video by visualize() which I'm not sure is the right way to do so or not. do you have any suggestions for it?
var l8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_TOA"),
region = ee.Geometry.Polygon(
[[[44.76385083079123, 38.28074335406828],
[44.76385083079123, 37.1334667575582],
[46.08221020579123, 37.1334667575582],
[46.08221020579123, 38.28074335406828]]], null, false),
params = {"opacity":1,"bands":["B4","B3","B2"],"min":0.07630298537191671,"max":0.3954072752450793,"gamma":1.356};
var collection = l8.filterBounds(region)
.filterMetadata('CLOUD_COVER', 'LESS_THAN', 30);
.filterDate('1999-01-01', '2020-01-01');
var l8med = collection.median();
Map.addLayer(collection, params, 'Layer');
print(collection.size());
var newimg = l8med.visualize(params);
Export.video.toDrive({
collection: newimg,
description: 'a1',
dimensions: 720,
framesPerSecond: 12,
folder: "GEE",
maxFrames: 100000,
region: region
});
You made a single image out of the collection using .median() and then tried to export that, so it can't work — there's no time series to make a video out of, after that.
You do need .visualize() but you need to do it for each image:
Export.video.toDrive({
collection: collection.map(function (image) { return image.visualize(params); }),
...

How to increase resolution of exported image in Google Earth Engine?

I have some code to export an LS8 image to Drive using GEE. Somehow, the resolution of the image seems to be lower (larger pixels) than what I am able to see on the browser. How can I increase the resolution? This is the code I´ve been using, with two different options.
I attempted to use .resample() as a solution but it produced a single band image that did not look good.
geometry2 = /* color: #57d64b */ee.Geometry.Polygon(
[[[-78.2812086867645, 2.3386717366200585],
[-77.56984394067075, 2.3729749945579948],
[-77.72227924340513, 2.776314152654142],
[-78.20842426293638, 2.725560942159387]]]);
function maskL8sr(image)
{
// Bits 3 and 5 are cloud shadow and cloud, respectively.
var cloudShadowBitMask = ee.Number(2).pow(3).int();
var cloudsBitMask = ee.Number(2).pow(5).int();
// Get the pixel QA band.
var qa = image.select('pixel_qa');
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
.and(qa.bitwiseAnd(cloudsBitMask).eq(0));
// Return the masked image, scaled to TOA reflectance, without the QA bands.
return image.updateMask(mask).divide(10000)
.select("B[0-9]*")
.copyProperties(image, ["system:time_start"]);
}
// Map the function over one year of data.
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterDate('2016-04-01', '2018-7-31')
.map(maskL8sr)
var composite = collection.median();
var VIS = {bands: ['B5', 'B6', 'B4'], min: 0, max: 0.47};
// Display the results.
Map.addLayer(composite,VIS ,"compt2");
var params ={
crs: 'EPSG:4326',
maxPixels: 1e12,
region:geometry2
}
Export.image(composite,'Guapi',params);
Export.image.toDrive({
image: composite,
description: 'Guapi',
scale: 30,
region: geometry2,
maxPixels: 1000000000
});

Resources