I want to prepocess a batch of Sentinel-2 images using the code provided by ndminhhus. Earth Engine authentication, Py6S, custom module "atmospheric", basically all necessary modules have been installed and updated. The code seems to connect to functions in batch.py. This script uses the GEE Python API method prepare_for_export() (as stated in the documentation).
Upon running my code, I receive the error AttributeError: 'Element' object has no attribute 'prepare_for_export'.
Relevant snippet in ndminhhus' code:
def func1(img):
# Omitted lines 2 - 97
fname = ee.String(img.get('system:index')).getInfo()
export = ee.batch.Export.image.toAsset(\
image=ref,
description= 'S2_BOA_'+fname,
assetId = 'users/Bmperezaraujo/Py6S_Harmonisation'+'S2_BOA'+fname,
region = region,
scale = 10,
maxPixels = 1e13)
# uncomment to run the export
export.start()
print('exporting ' +fname + '--->done')
S2_col = ee.ImageCollection('COPERNICUS/S2').filterBounds(region).filterDate(startDate,endDate).filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',50))
img1 = ee.Image(S2_list.get(9))
toa = img1
boa = ee.Image(func1(toa))
Relevant snippet in batch.py:
class Export(object):
# Omitted lines 170 - 242
def toAsset(
image,
description='myExportImageTask',
assetId=None,
pyramidingPolicy=None,
dimensions=None,
region=None,
scale=None,
crs=None,
crsTransform=None,
maxPixels=None,
**kwargs):
# Omitted explanatory comments in lines 255 - 291
config = _capture_parameters(locals(), ['image'])
config = _prepare_image_export_config(image, config,
Task.ExportDestination.ASSET)
return _create_export_task(config, Task.Type.EXPORT_IMAGE)
# Omitted lines 296 - 962
def _prepare_image_export_config(image, config, export_destination):
"""Performs all preparation steps for an image export.
Args:
image: The Image to be exported.
config: All the user-specified export parameters. May be modified.
export_destination: One of the Task.ExportDestination values.
Returns:
A config dict containing all information required for the export.
"""
# Supply some defaults.
if (export_destination != Task.ExportDestination.ASSET and
'fileFormat' not in config):
config['fileFormat'] = 'GeoTIFF'
_canonicalize_parameters(config, export_destination)
image, config = image.prepare_for_export(config)
# Build an ExportImageRequest. Delete values from "config" as we go so we
# can check at the end for any leftovers. Any computed objects will be
# serialised in data.py before the request is sent.
request = {}
request['expression'] = image
if 'description' in config:
request['description'] = config.pop('description')
if export_destination == Task.ExportDestination.ASSET:
asset_export_options = {}
asset_export_options[
'earthEngineDestination'] = _build_earth_engine_destination(config)
# This can only be set by internal users.
if 'tileSize' in config and 'shardSize' in config:
raise ee_exception.EEException(
'Both "shardSize" and "tileSize" cannot be set.')
if 'tileSize' in config:
asset_export_options['tileSize'] = {
'value': int(config.pop('tileSize'))}
# "shardSize" is the old name for "tileSize".
if 'shardSize' in config:
asset_export_options['tileSize'] = {'value': int(config.pop('shardSize'))}
if 'pyramidingPolicy' in config:
pyramiding_policy = config.pop('pyramidingPolicy')
if '.default' in pyramiding_policy:
asset_export_options[
'pyramidingPolicy'] = pyramiding_policy.pop('.default').upper()
if pyramiding_policy:
asset_export_options['pyramidingPolicyOverrides'] = {
band: policy.upper() for band, policy in pyramiding_policy.items()
}
request['assetExportOptions'] = asset_export_options
else:
request['fileExportOptions'] = _build_image_file_export_options(
config, export_destination)
if 'maxPixels' in config:
# This field is an Int64Value, so it needs an inner "value" field, and
# the value itself is a string, not an integer, in the JSON encoding.
request['maxPixels'] = {'value': str(int(config.pop('maxPixels')))}
# This can only be set by internal users.
if 'maxWorkers' in config:
request['maxWorkers'] = {'value': int(config.pop('maxWorkers'))}
# Of the remaining fields in ExportImageRequest:
# - All the values that would go into the PixelGrid should have been folded
# into the image's Expression.
# - The request ID will be populated when the Task is created.
# We've been deleting config parameters as we handle them. Anything left
# over is a problem.
if config:
raise ee_exception.EEException(
'Unknown configuration options: {}.'.format(config))
return request
The same snippets are in traceback. Cell is ndminhhus' code, File is batch.py.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[14], line 2
1 toa = img1
----> 2 boa = ee.Image(func1(toa))
Cell In[10], line 99, in func1(img)
93 # define YOUR assetID
94 # in my case it was something like this..
95 #assetID = 'users/samsammurphy/shared/sentinel2/6S/ESRIN_'+dateString
96 #assetID = 'users/ndminhhus/eLEAF/nt/s2_SIAC/'+fname,
97 #export
98 fname = ee.String(img.get('system:index')).getInfo()
---> 99 export = ee.batch.Export.image.toAsset(\
100 image=ref,
101 description= 'S2_BOA_'+fname,
102 assetId = 'users/Bmperezaraujo/Py6S_Harmonisation'+'S2_BOA'+fname,
103 region = region,
104 scale = 10,
105 maxPixels = 1e13)
107 # uncomment to run the export
108 export.start()
File c:\Users\bpere\miniconda3\lib\site-packages\ee\batch.py:293, in Export.image.toAsset(image, description, assetId, pyramidingPolicy, dimensions, region, scale, crs, crsTransform, maxPixels, **kwargs)
255 """Creates a task to export an EE Image to an EE Asset.
256
257 Args:
(...)
290 An unstarted Task that exports the image as an Earth Engine asset.
291 """
292 config = _capture_parameters(locals(), ['image'])
--> 293 config = _prepare_image_export_config(image, config,
294 Task.ExportDestination.ASSET)
295 return _create_export_task(config, Task.Type.EXPORT_IMAGE)
File c:\Users\bpere\miniconda3\lib\site-packages\ee\batch.py:981, in _prepare_image_export_config(image, config, export_destination)
977 config['fileFormat'] = 'GeoTIFF'
979 _canonicalize_parameters(config, export_destination)
--> 981 image, config = image.prepare_for_export(config)
982 # Build an ExportImageRequest. Delete values from "config" as we go so we
983 # can check at the end for any leftovers. Any computed objects will be
984 # serialised in data.py before the request is sent.
985 request = {}
AttributeError: 'Element' object has no attribute 'prepare_for_export'
I have double-checked that my input img is of ee.image.Image type.
I have a functioning GEE Python API.
I checked whether batch has the prepare_for_export method with hasattr(ee.batch, 'prepare_for_export')
, but it returned me a negative.
How can I import the missing GEE Python API method?