Google Earth Engine Clip – How to Clip with Geometry in Google Earth Engine for Accurate Geospatial Analysis

clipgeometrygoogle-earth-engineshapefile

I tried to clip using a loaded shapefile. It enables me to download but crushes at few seconds not completing the downloading task.

var clipToCol = function(image){
  return image.clip(export_geometry_2);
};

var dataset = ee.ImageCollection("WorldPop/GP/100m/pop").map(clipToCol)
                .filterDate('2017');
var worldPop = dataset.select('population');

var populationVis = {
  min: 0.0,
  max: 50.0,
  palette: ['24126c', '1fff4f', 'd4ff50'],
};

Map.addLayer(worldPop, populationVis, 'Population');

var export_geometry_2 = ee.FeatureCollection("users/gabiklm01/polyMosaic");

Export.image.toDrive({
image: dataset.select('population'),
description: 'worldPop',
scale: 100,
region: export_geometry_2
})

Best Answer

Without your geometry, I used one stored in my GEE assets and the following version of your code ran but, the downloading task also crushes at few seconds as you said.

var clipToCol = function(image){
  return image.clip(export_geometry_2);
};

var export_geometry_2 = ee.FeatureCollection("users/joseguerreroa/utah/utah_bounds");

var dataset = ee.ImageCollection("WorldPop/GP/100m/pop").map(clipToCol)
                .filterDate('2017');

var worldPop = dataset.select('population');

var populationVis = {
  min: 0.0,
  max: 50.0,
  palette: ['24126c', '1fff4f', 'd4ff50'],
};

Map.addLayer(worldPop, populationVis, 'Population');

Export.image.toDrive({
image: dataset.select('population'),
description: 'worldPop',
scale: 100,
region: export_geometry_2
});

So, I printed worldPop for evaluating its properties and, I found out that this Image Collection has 248 images (one for each country). I think that it's too much for being handled by Google Drive when you try to export it. As my clip area is located in USA (country code 233 in the Image Collection), I modified above code as follows and, in this case, the downloading task was completed without any problem (after downloading the image stored in Google Drive, it was also visualized in QGIS as expected).

var clipToCol = function(image){
  return image.clip(export_geometry_2);
};

var export_geometry_2 = ee.FeatureCollection("users/joseguerreroa/utah/utah_bounds");

var dataset = ee.ImageCollection("WorldPop/GP/100m/pop").map(clipToCol)
                .filterDate('2017');

var worldPop = dataset.select('population');

var listOfImages = worldPop.toList(worldPop.size());

var utah_lake = ee.Image(listOfImages.get(233)) //code for USA
                  .select('population');

var populationVis = {
  min: 0.0,
  max: 50.0,
  palette: ['24126c', '1fff4f', 'd4ff50'],
};

Map.addLayer(utah_lake, populationVis, 'Population');

Export.image.toDrive({
image: utah_lake,
description: 'utah_lake',
scale: 100,
region: export_geometry_2 
});

Editing Note:

I created a rectangle geometry in that zone and ran following script that includes Israel, Jordan, Syria, and Lebanon.

var export_geometry_2 = 
    /* color: #d63000 */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.Geometry.Polygon(
        [[[31.362499999999955, 34.50062471333177],
          [31.362499999999955, 30.27182796010966],
          [43.315624999999955, 30.27182796010966],
          [43.315624999999955, 34.50062471333177]]], null, false);

var clipToCol = function(image){
  return image.clip(export_geometry_2);
};

var dataset = ee.ImageCollection("WorldPop/GP/100m/pop")
                .filterDate('2017');

var worldPop = dataset.select('population');

print(worldPop);

var listOfImages = worldPop.toList(worldPop.size());

var israel = ee.Image(listOfImages
                  .get(108)) //code for Israel
                  .select('population');

var jordan = ee.Image(listOfImages
                  .get(112)) //code for Jordan
                  .select('population');

var lebanon = ee.Image(listOfImages
                  .get(124)) //code for Lebanon
                  .select('population');

var syria = ee.Image(listOfImages
                  .get(213)) //code for Syria
                  .select('population');

// Create a collection by giving a list.
var collection = ee.ImageCollection([israel, jordan, lebanon, syria])
                 .select('population');

print('collection: ', collection.getInfo());

var populationVis = {
  min: 0.0,
  max: 50.0,
  palette: ['24126c', '1fff4f', 'd4ff50'],
};

Map.addLayer(collection, populationVis, 'Population');

var listOfImages2 = collection.toList(collection.size());

var israel_clip = ee.Image(listOfImages2.get(0)).clip(export_geometry_2);

Export.image.toDrive({
image: israel_clip,
description: 'israel_clip',
scale: 100,
region: export_geometry_2 
});

After running the script, layers look as follows:

enter image description here

In my case, exporting task to my Google Drive took 21 minutes only for israel clipped image by export_geometry_2. For this reason I didn't export the rest of images (but, if it works for one it should work for remaining images).

After downloading israel_clip image, it looks as expected in QGIS:

enter image description here

Related Question