0

When I export an image in GEE code editor I can pass a geometry to the 'region' parameter without problem. However, it doesn't work for Python API and I get below error for the shown code:

task = ee.batch.Export.image.toCloudStorage(
    image=imageOfSeries.toArray(),
    description=AssetName,
    fileNamePrefix=AssetName,
    bucket=GCbucketName,
    scale=scale,
    region=box,
    fileFormat='TFRecord',
    formatOptions={
        'patchDimensions': [patch_size, patch_size],
        'tensorDepths': [tDepth],
        'compressed': True
    }
)


Traceback (most recent call last):
  File "D:/Shahriar/OneDrive - SUNY ESF/Thesis/Codes/Landsat_Classification/ExtractLandsatArrayImage.py", line 205, in <module>
    'compressed': True
  File "C:\Python36\lib\site-packages\ee\batch.py", line 332, in toCloudStorage
    Task.ExportDestination.GCS)
  File "C:\Python36\lib\site-packages\ee\batch.py", line 854, in _prepare_image_export_config
    _canonicalize_parameters(config, export_destination)
  File "C:\Python36\lib\site-packages\ee\batch.py", line 1418, in _canonicalize_parameters
    config['region'] = _canonicalize_region(config['region'])
  File "C:\Python36\lib\site-packages\ee\batch.py", line 1503, in _canonicalize_region
    return json.dumps(region)
  File "C:\Python36\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "C:\Python36\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Python36\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "C:\Python36\lib\json\encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'Geometry' is not JSON serializable

How can I solve this problem?

2
  • In order to help you we should be able to see what box is Commented Oct 4, 2019 at 10:25
  • Ah sorry I forgot to make it clear. box=image.geometry(), whee image is just a typical image.
    – Shahriar49
    Commented Oct 4, 2019 at 14:07

1 Answer 1

1

I think getting box as a dict makes it JSON serializable, try:

task = ee.batch.Export.image.toCloudStorage(
    image=imageOfSeries.toArray(),
    description=AssetName,
    fileNamePrefix=AssetName,
    bucket=GCbucketName,
    scale=scale,
    region=box.getInfo(),
    fileFormat='TFRecord',
    formatOptions={
        'patchDimensions': [patch_size, patch_size],
        'tensorDepths': [tDepth],
        'compressed': True
    }
)

In case you have another issue (or the same), you can also try a function I included in geetools:

from geetools import tools

region = tools.geometry.getRegion(box)

task = ee.batch.Export.image.toCloudStorage(
    image=imageOfSeries.toArray(),
    description=AssetName,
    fileNamePrefix=AssetName,
    bucket=GCbucketName,
    scale=scale,
    region=region,
    fileFormat='TFRecord',
    formatOptions={
        'patchDimensions': [patch_size, patch_size],
        'tensorDepths': [tDepth],
        'compressed': True
    }
)
1
  • 1
    Thanks. box.getInfo() didn't work but box.getInfo()['coordinates'] worked. I see in your code you also did the same.
    – Shahriar49
    Commented Oct 7, 2019 at 20:57

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.