I want to clip an image by a feature collection (roi shapefile) and export it as TIFF. When I add the clipped image to Map, it shows exactly what I want with only values inside the roi showing. But when I export it to GeoTIFF, it clips the image to bounding box of the roi rather than actual roi borderlines.

IMERG = ee.ImageCollection('NASA/GPM_L3/IMERG_MONTHLY_V06').first().select('precipitation')
clip = IMERG.clipToCollection(roi)  #roi is a feature collection
geemap.ee_export_image(clip, filename='test2.tif', scale=11132, region=roi.geometry())

This is what it exports This is what it exports

This is what I would like (obtained by clipping the exported image by country shapefile again in QGIS).
This is what I would like

This question also asks pretty much the same thing but don't seem to have a proper answer to this issue although an answer is accepted.

1 Answer 1


Using .clip() should work

import json

## load geojson of region of interest
gjson_path = r"C:\Users\Matt\Documents\roi.geojson"

with open(gjson_path) as f:
    gjson = json.load(f)

## get gjson features
features = gjson['features']

## make Earth Engine FeatureCollection of features
fc = ee.FeatureCollection(features)

## get image
IMERG = ee.ImageCollection('NASA/GPM_L3/IMERG_MONTHLY_V06').first().select('precipitation')

## clip image
clip = IMERG.clipToCollection(fc)  # fc is a feature collection

## export result
region = fc.geometry()
scale = 50
folder = 'test folder'
img_name = 'test_img'

export_config = {'scale': scale, 'maxPixels': 1.0E13, 'driveFolder': folder, 'region': region}

task = ee.batch.Export.image(clip, img_name, export_config)

enter image description here

  • No, it doesn't work. Don't think there is much difference between these two anyway, clipToCollection is for more complex geometries.
    – Moon Child
    Sep 16, 2021 at 11:27
  • .clip() is what I use and it works as expected, but then I don't use the geemap wrapper. does removing .geometry() from roi.geometry() work? Perhaps it is exporting with regards to the bounding box, rather than the features themselves.
    – Matt
    Sep 16, 2021 at 11:40
  • roi is a FeatureCollection but the export function requires Geometry object. That is why I did roi.geometry . The other question I linked to also gets the same results although done from GEE code editor so don't think there's any problem in geemap.
    – Moon Child
    Sep 16, 2021 at 11:50
  • I updated my answer with the code I used to clip with a feature collection and export to Drive. I wonder then if your issue is caused by how your feature collection was constructed.
    – Matt
    Sep 16, 2021 at 13:47
  • hmmm.. the feature collection I used is actually a shapefile of country map. I used the following code to convert it into a feature collection: shp = '/MyDrive/country.shp' roi = geemap.shp_to_ee(shp)
    – Moon Child
    Sep 16, 2021 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.