0

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
ee.Initialize()

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

image = (ee.ImageCollection('COPERNICUS/S2').
         filterBounds(boundary).
         filterDate(datetime.datetime(2018,01,24),
                     datetime.datetime(2018,02,28)).
         sort("CLOUD_COVERAGE_ASSESMENT").
         limit(20))



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

2 Answers 2

2

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']
    }
0
0

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

boundary = ee.Geometry(boundary.getInfo()).toGeoJSONString()

1
  • 1
    Providing further explanation of your answer would increase the value to users.
    – Kingfisher
    Commented Nov 7, 2019 at 14:17

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.