Google Earth Engine: Image intersection and inverse intersection - google-earth-engine

I am new to Google Earth Enginge and I struggle to bring together two images in Google Earth Engine to get the areas which are in both images and the areas which are only part of one image to show forest cover change (loss, gain, no change).
My code so far which seems to at least display what I want by stacking the images above each other:
var treeCanopyCoverVis = {
min: 0.0,
max: 100.0,
palette: ['ffffff', 'afce56', '5f9c00', '0e6a00', '003800'],
};
var forest2000 = ee.ImageCollection('NASA/MEASURES/GFCC/TC/v3')
.filterDate('2000-01-01', '2000-12-31')
.select('tree_canopy_cover')
.reduce(ee.Reducer.mean());
var forest2000_ab60 = forest2000.gt(60).selfMask();
Map.addLayer(forest2000_ab60, {palette: '#d80078'}, 'Loss');
var forest2015 = ee.ImageCollection('NASA/MEASURES/GFCC/TC/v3')
.filterDate('2015-01-01', '2015-12-31')
.select('tree_canopy_cover')
.reduce(ee.Reducer.mean());
var forest2015_ab60 = forest2015.gt(60).selfMask();
Map.addLayer(forest2015_ab60, {palette: '#ebb13a'}, 'Gain');
// var loss = forest2015_ab60.intersection(forest2000_ab60);
print(forest2015_ab60);
print(forest2000_ab60);
var remain = forest2015_ab60.and(forest2000_ab60);
Map.addLayer(remain, {palette: '#746d75'}, 'Remain');
With this code the gain still includes the remain part and the loss part also still includes the remain part. I need kind of the subtraction. All functions I now tried result in errors. I appreciate any help!
How my current result looks:

I could somehow solve it with try and error.
The code:
var forest2000 = ee.ImageCollection('NASA/MEASURES/GFCC/TC/v3')
.filterDate('2000-01-01', '2000-12-31')
.select('tree_canopy_cover')
.reduce(ee.Reducer.mean());
var forest2015 = ee.ImageCollection('NASA/MEASURES/GFCC/TC/v3')
.filterDate('2015-01-01', '2015-12-31')
.select('tree_canopy_cover')
.reduce(ee.Reducer.median());
var gain = forest2000.lt(60).and(forest2015.gt(60));
Map.addLayer(gain.selfMask(), {palette: '#ebb13a'}, 'gain');
var loss = forest2000.gt(60).and(forest2015.lt(60));
Map.addLayer(loss.selfMask(), {palette: '#d80078'}, 'loss');
var nochange = forest2000.gt(60).and(forest2015.gt(60));
Map.addLayer(nochange.selfMask(), {palette: '#746d75'}, 'no change');

Related

How to filter a Google Earth Engine Image Collection by crs?

I have a script to download Sentinel-1 images from Google Earth Engine, which works perfectly over UK regions and other parts of Europe. However, when I try to run it for a region of Norway, the image returned is blurred. I think this is because within the ee.imagecollection some of the images have a different crs projection.
Hence, my question is how do I filter the images to remove images with the other crs? Here is an example of how it looks in Google Earth Engine:
Sentinel-1 image of area of Norway in Google Earth Engine
and here is how a print out of the image collection looks like in Google Earth Engine showing the two projections (see features 0 and 3 showing EPSG: 32632 and EPSG 32633):
Print out in Google Earth Engine of Norway image collection
My Google Earth Engine Script is included below. To replicate the problem replace the Norway geometry with a drawn polygon.
var year = 2021;
var region = 9;
var mth = 'October';
var mthno1 = 10;
var mthno2 = 11;
var endday1 = 18;
var endday2 = 18;
var geometry = ee.FeatureCollection("users/nfigbfr/Norway");
var s1c = ee.ImageCollection('COPERNICUS/S1_GRD')
.filterBounds(geometry)
.filterDate(year+'-'+mthno1+'-'+endday1,year+'-'+mthno2+'-'+endday2)
.filter(ee.Filter.eq('transmitterReceiverPolarisation', ['VV','VH']))
.filter(ee.Filter.eq('instrumentMode', 'IW'))
.map(function(image) {
var edge = image.lt(-30.0);
var maskedImage = image.mask().and(edge.not());
return image.updateMask(maskedImage);
});
print(s1c)
var img = s1c.mean();
print(img)
var img = img.addBands(img.select('VV').subtract(img.select('VH')).rename('Ratio'));
var img = img.select(['VV','VH','Ratio']).toFloat();
print(img);
var img_display = img.select(['VV','VH','Ratio']).clip(geometry);
Map.centerObject(geometry);
Map.addLayer(img_display, {min: -25, max: 0});
Export.image.toDrive({
image: img,
description: 'Norway_mean_'+mth+year,
folder: 'Sentinel_1',
crs: 'EPSG:32632',
scale: 10,
maxPixels: 1e13,
region: geometry
});
The crs is a property of individual bands, not the images. I also haven't been able to find out if/how we can access the band properties for filtering.
However, here is a workaround:
var target_crs = 'EPSG:32671'
var s1c = ee.ImageCollection('COPERNICUS/S1_GRD')
.filterBounds(point)
.filterDate(year+'-'+mthno1+'-'+endday1,year+'-'+mthno2+'-'+endday2)
.filter(ee.Filter.eq('transmitterReceiverPolarisation', ['VV','VH']))
.filter(ee.Filter.eq('instrumentMode', 'IW'))
.map(function(image) {
var edge = image.lt(-30.0);
var maskedImage = image.mask().and(edge.not());
return image.updateMask(maskedImage);
})
.map(function(img){
var crs = img.select(['VV']).projection().crs()
var myImageWithProperties = x.set({
crs: crs})
return ee.Image(myImageWithProperties)
;})
.filter(ee.Filter.eq('crs', target_crs));
I added a .map() function that grabs the projection code (EPSG) from the VV band and sets it as an image property. Then we can filter the collection based on this property.
I've tried this on Sentinel-2 and it works fine. Still curious if there is a simpler way, though.
PS: this question is better suited for https://gis.stackexchange.com

recursive function on each pixel in google earth engine

I want to filter time series in the google earth engine which requires two for loops over time-series of a single pixel. I searched around and not found any example related to this. I know about .map function and I am using it for the generation of RVI on the earth engine. I found about .toArray function but not found any example related to my problem.
I will appreciate any help in this regard. Also, I am new to the earth engine so this may be a trivial question for others.
This is the code that I have. I took it from a blog and modified it according to my need. I am stuck after this.
var sentinel1 = ee.ImageCollection('COPERNICUS/S1_GRD_FLOAT');
// Filter VH, IW
var vh = sentinel1
// Filter to get images with VV and VH dual polarization.
//.filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VH'))
// Filter to get images collected in interferometric wide swath mode.
.filter(ee.Filter.eq('instrumentMode', 'IW'))
// reduce to VH polarization
//.select('VH')
// filter 10m resolution
.filter(ee.Filter.eq('resolution_meters', 10));
// Filter to orbitdirection Descending
var vhDescending = vh.filter(ee.Filter.eq('orbitProperties_pass', 'DESCENDING'));
// Filter time 2015
var vhDesc2015 = vhDescending.filterDate(ee.Date('2021-01-01'), ee.Date('2021-04-30'));
// Filter to MKD roi
var s1_mkd = vhDesc2015.filterBounds(roi);
print('All metadata:', s1_mkd);
var count = s1_mkd.size();
print('Count: ', count);
//var dates = s1_mkd.aggregate_array("system:time_start")
//print('dates: ', dates);
var dates = s1_mkd
.map(function(image) {
return ee.Feature(null, {'date': image.date().format('YYYY-MM-dd')})
})
.distinct('date')
.aggregate_array('date')
print('dates: ', dates);
var featureCollection = ee.FeatureCollection(dates
.map(function(element){
return ee.Feature(null,{prop:element})}))
//Export a .csv table of date, mean NDVI for watershed
Export.table.toDrive({
collection: featureCollection,
description: 'Timeseries',
folder: 'WC_raw',
fileFormat: 'CSV',
});
var rvi4s1 = function(img){
var vh = img.select('VH');
var vv = img.select('VV');
var col = vv.divide(vv.add(vh)).sqrt().rename('dop');
var dop = col.select('dop')
var value = dop.multiply(vh.multiply(4).divide(vv.add(vh))).rename('rvi4s1');
return value;
};
var rvi = s1_mkd.map(rvi4s1);
print(rvi);

Use videoTexture as opacityTexture

How do I use video (mp4) as alpha map in babylonJS?
In three.js applying a video as texture is as simple as assigning the video texture to alphaMap (instead of the diffuse map).
Here's the expected result in three.js - Demo.
I attempted to do the same in babylonJS to no avail. Here's what I have so far babylonJs demo
var mat = new BABYLON.StandardMaterial("mat", scene);
var videoTexture = new BABYLON.VideoTexture("video", ["textures/babylonjs.mp4", "textures/babylonjs.webm"], scene, true, true);
mat.opacityTexture = videoTexture;
Any ideas are welcome.
Thanks
You can use videoTexture.getAlphaFromRGB = true; to use all three channels combined for the alpha. By default it only uses the red channel, which does not have enough variance in the source video for it to show.
The complete example:
var mat = new BABYLON.StandardMaterial("mat", scene);
var videoTexture = new BABYLON.VideoTexture("video", ["textures/babylonjs.mp4", "textures/babylonjs.webm"], scene, true, true);
videoTexture.getAlphaFromRGB = true;
mat.opacityTexture = videoTexture;

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
});

Openlayers marker size

I have an openlayers map with markers added as geometry vector points. In the style option I set a size for each. However, the problem is, that if I zoom in or zoom out, they all become the same size until I load the entire page again. In other words, once I zoom in or out, they are all the same.
var layer_style = OpenLayers.Util.extend({},
OpenLayers.Feature.Vector.style['default']);
var style = OpenLayers.Util.extend({}, layer_style);
var pointLayer = new OpenLayers.Layer.Vector("Point Layer");
map.addLayers([terrain, road, satellite, hybrid, pointLayer]);
var lonlat = new OpenLayers.LonLat(0, 140);
lonlat.transform(proj, map.getProjectionObject());
map.setCenter(lonlat, 2);
var point = new OpenLayers.Geometry.Point(-40, -40);
point = point.transform(proj, map.getProjectionObject());
style.pointRadius = 10;
var pointFeature = new OpenLayers.Feature.Vector(point, null, style);
pointLayer.addFeatures([pointFeature]);
var point = new OpenLayers.Geometry.Point(-40, -40);
point = point.transform(proj, map.getProjectionObject());
style.pointRadius = 40;
var pointFeature = new OpenLayers.Feature.Vector(point, null, style);
pointLayer.addFeatures([pointFeature]);
When I load this, I get two markers, one size 10, the other 40. But when I zoom in or out, they all become same size.
You are overwriting the pointRadius property of the style object each time, so in the end the last value will be used as OpenLayers will only point to the style.
What you need to do is use a lookup to let the pointRadius depend on a given feature attribute.
See Rule-based Styling: http://trac.osgeo.org/openlayers/wiki/Styles#Rule-basedStyling

Resources