Tuesday, 17 July 2018

google earth engine - How to Export NDVI image using Fusion Table as featurecollection boundary in GEE python API


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



Answer



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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...