Obviously here, the big question will be :
How to authenticate programmatically to the Earth engine API ?
Once you've done that, it's pretty much strait forward to use your standard test suit
Set up
to authenticate, you will need to have a Google Cloud Project and enable the Earth Engine API for that project.
I assume that you are already registered to Earth engine, if it's not the case follow this link : https://signup.earthengine.google.com/
So first create a cloud project : https://console.cloud.google.com/projectcreate
Then enable your Earth Engine account in your cloud project https://console.cloud.google.com/apis/library/earthengine.googleapis.com
You can manage the service accounts for your Cloud project by going to the Cloud Console menu and selecting IAM & Admin > Service accounts. (Choose the project if prompted.)
To create a new service account, click the + CREATE SERVICE ACCOUNT link.
Once you have a service account, click the menu for that account, then Create key > JSON. Download the JSON key file.
transform your key into a variable
Usually CI services use variables to deal with private key
Here you will transform your private_key.json file inta a encoded string that you'll upload to your CI.
import json
import base64
with open('ee_api_key.json') as jsonfile:
data = json.load(jsonfile)
datastr = json.dumps(data)
encoded = base64.b64encode(datastr.encode())
print(encoded)
copy past the content of the encoded variable into your CI system (travis CI, CircleCI...)
Usage
in your file, start with the following code:
# only use the key when I'm in my test env
if 'EE_PRIVATE_KEY' in os.environ:
# key need to be decoded in a file to work
content = base64.b64decode(os.environ['EE_PRIVATE_KEY']).decode()
with open('ee_private_key.json', 'w') as f:
f.write(content)
# connection to the service account
service_account = '[email protected]'
credentials = ee.ServiceAccountCredentials(service_account, 'ee_private_key.json')
ee.Initialize(credentials)
else:
ee.Initialize()
Then you'll be ready to go
troubleshooting
(2020-11-27) the six lib is not installed properly by EarthEngine API so if you bump into the following error :
AttributeError: module 'six.moves' has no attribute 'collections_abc'
Then force the version in your requirements.txt as six>=1.15.0
notes
This answer is a sum-up of 2 documentations pages of Google API, feel free to have a look for more specific information :
https://developers.google.com/earth-engine/earthengine_cloud_project_setup#create-a-cloud-project
https://developers.google.com/earth-engine/guides/service_account