I am using python api of google earth engine to create NDVI raster. Now I want to export the raster in my google drive. But during exporting it is giving the following error:

ee.featurecollection.FeatureCollection object at 0x03D29EF0> is not JSON serializable

I am using the following piece of code for the entire work

import datetime
import ee
import ee.mapclient
import json
from ee.batch import Export

boundary = ee.FeatureCollection('ft:1rbhNtC1TqDBvY9Rt2BZR-DjhpIPuC3nU5kmz49WW')

image = (ee.ImageCollection('COPERNICUS/S2').

mosaic = image.mosaic();
cloudBitMask = ee.Number(2).pow(10).int();
cirrusBitMask = ee.Number(2).pow(11).int();

def maskS2clouds(image): #(
  qa = mosaic.select('QA20');
  #// Both flags should be set to zero, indicating clear conditions.
  mask = qa.bitwiseAnd(cloudBitMask).eq(0)and(qa.bitwiseAnd(cirrusBitMask).eq(0));
  return image.updateMask(mask);

cloudMasked = image.filterBounds(boundary).map(maskS2clouds)
median = cloudMasked.median()

clip = median.clip(boundary)

ndvi = clip.normalizedDifference(['B8', 'B4'])

task_config = {
    #'image:': ndvi,
    'description': 'PythonExample',
    'scale': 30,  
    'region': boundary

task = ee.batch.Export.image(ndvi, 'nd', task_config)

Please help me how I can covert FeatureCollection as json object? My per my understanding from the error I might have to convert the fusion table FeatureCollection into JSON object


There is a package with some tools for GEE that can be used in this case: geetools

from geetools import tools

task_config = {
    #'image:': ndvi,
    'description': 'PythonExample',
    'scale': 30,  
    'region': tools.geometry.getRegion(boundary)

This should solve it. The function getRegion is designed to be used in exporting functions and can manage different situations, so you don't have to worry about that. But if you want to do it without geetools this is how:

task_config = {
    #'image:': ndvi,
    'description': 'PythonExample',
    'scale': 30,  
    'region': boundary.geometry().getInfo()['coordinates']

Your Answer

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

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