Skip to content

Commit

Permalink
Move Earth_Engine_REST_API_Quickstart.ipynb to Earth Engine community…
Browse files Browse the repository at this point in the history
… repo

PiperOrigin-RevId: 559740530
  • Loading branch information
gino-m authored and copybara-github committed Sep 7, 2023
1 parent 1c9504d commit fe36ea3
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions guides/linked/Earth_Engine_REST_API_Quickstart.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Earth_Engine_REST_API_Quickstart.ipynb","provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"pr1eEWIUjiBc","colab_type":"text"},"source":["# Earth Engine REST API Quickstart\n","\n","This is a demonstration notebook for using the Earth Engine REST API. See the complete guide for more information: https://developers.google.com/earth-engine/reference/Quickstart.\n"]},{"cell_type":"markdown","metadata":{"id":"OfMAA6YhPuFl","colab_type":"text"},"source":["## Authentication\n","\n","The first step is to choose a project and login to Google Cloud."]},{"cell_type":"code","metadata":{"id":"FRm2HczTIlKe","colab_type":"code","colab":{}},"source":["# INSERT YOUR PROJECT HERE\n","PROJECT = 'your-project'\n","!gcloud auth login --project {PROJECT}"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"hnufOtSfP0jX","colab_type":"text"},"source":["## Define service account credentials"]},{"cell_type":"code","metadata":{"id":"tLxOnKL2Nk5p","colab_type":"code","colab":{}},"source":["# INSERT YOUR SERVICE ACCOUNT HERE\n","SERVICE_ACCOUNT='[email protected]'\n","KEY = 'private-key.json'\n","\n","!gcloud iam service-accounts keys create {KEY} --iam-account {SERVICE_ACCOUNT}"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"6QksNfvaY5em","colab_type":"text"},"source":["## Create an authorized session to make HTTP requests"]},{"cell_type":"code","metadata":{"id":"h2MHcyeqLufx","colab_type":"code","colab":{}},"source":["from google.auth.transport.requests import AuthorizedSession\n","from google.oauth2 import service_account\n","\n","credentials = service_account.Credentials.from_service_account_file(KEY)\n","scoped_credentials = credentials.with_scopes(\n"," ['https://www.googleapis.com/auth/cloud-platform'])\n","\n","session = AuthorizedSession(scoped_credentials)\n","\n","url = 'https://earthengine.googleapis.com/v1alpha/projects/earthengine-public/assets/LANDSAT'\n","\n","response = session.get(url)\n","\n","from pprint import pprint\n","import json\n","pprint(json.loads(response.content))\n"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"_KjWa7KJY_7m","colab_type":"text"},"source":["## Get a list of images at a point"]},{"cell_type":"markdown","metadata":{"id":"5kKbIDctpZeH","colab_type":"text"},"source":["Query for Sentinel-2 images at a specific location, in a specific time range and with estimated cloud cover less than 10%."]},{"cell_type":"code","metadata":{"id":"0bENTPjMQr5h","colab_type":"code","colab":{}},"source":["import urllib\n","\n","coords = [-122.085, 37.422]\n","\n","project = 'projects/earthengine-public'\n","asset_id = 'COPERNICUS/S2'\n","name = '{}/assets/{}'.format(project, asset_id)\n","url = 'https://earthengine.googleapis.com/v1alpha/{}:listImages?{}'.format(\n"," name, urllib.parse.urlencode({\n"," 'startTime': '2017-04-01T00:00:00.000Z',\n"," 'endTime': '2017-05-01T00:00:00.000Z',\n"," 'region': '{\"type\":\"Point\", \"coordinates\":' + str(coords) + '}',\n"," 'filter': 'CLOUDY_PIXEL_PERCENTAGE < 10',\n","}))\n","\n","response = session.get(url)\n","content = response.content\n","\n","for asset in json.loads(content)['images']:\n"," id = asset['id']\n"," cloud_cover = asset['properties']['CLOUDY_PIXEL_PERCENTAGE']\n"," print('%s : %s' % (id, cloud_cover))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"DY0MfkjiAAW_","colab_type":"text"},"source":["## Inspect an image\n","\n","Get the asset name from the previous output and request its metadata."]},{"cell_type":"code","metadata":{"id":"ddzrXIl4ADLk","colab_type":"code","colab":{}},"source":["asset_id = 'COPERNICUS/S2/20170430T190351_20170430T190351_T10SEG'\n","name = '{}/assets/{}'.format(project, asset_id)\n","url = 'https://earthengine.googleapis.com/v1alpha/{}'.format(name)\n","\n","response = session.get(url)\n","content = response.content\n","\n","asset = json.loads(content)\n","print('Band Names: %s' % ','.join(band['id'] for band in asset['bands']))\n","print('First Band: %s' % json.dumps(asset['bands'][0], indent=2, sort_keys=True))"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"O5I63cC6ZDQn","colab_type":"text"},"source":["## Get pixels from one of the images"]},{"cell_type":"code","metadata":{"id":"xJhv6bfEZHa2","colab_type":"code","colab":{}},"source":["import numpy\n","import io\n","\n","name = '{}/assets/{}'.format(project, asset_id)\n","url = 'https://earthengine.googleapis.com/v1alpha/{}:getPixels'.format(name)\n","body = json.dumps({\n"," 'fileFormat': 'NPY',\n"," 'bandIds': ['B2', 'B3', 'B4', 'B8'],\n"," 'grid': {\n"," 'affineTransform': {\n"," 'scaleX': 10,\n"," 'scaleY': -10,\n"," 'translateX': 499980,\n"," 'translateY': 4200000,\n"," },\n"," 'dimensions': {'width': 256, 'height': 256},\n"," },\n","})\n","\n","pixels_response = session.post(url, body)\n","pixels_content = pixels_response.content\n","\n","array = numpy.load(io.BytesIO(pixels_content))\n","print('Shape: %s' % (array.shape,))\n","print('Data:')\n","print(array)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"jcwE2W8kFojo","colab_type":"text"},"source":["## Get a thumbnail of an image\n","\n","Note that `name` and `asset` are already defined from the request to get the asset metadata. "]},{"cell_type":"code","metadata":{"id":"xs0ZHHKmFovV","colab_type":"code","colab":{}},"source":["url = 'https://earthengine.googleapis.com/v1alpha/{}:getPixels'.format(name)\n","body = json.dumps({\n"," 'fileFormat': 'PNG',\n"," 'bandIds': ['B4', 'B3', 'B2'],\n"," 'region': asset['geometry'],\n"," 'grid': {\n"," 'dimensions': {'width': 256, 'height': 256},\n"," },\n"," 'visualizationOptions': {\n"," 'ranges': [{'min': 0, 'max': 3000}],\n"," },\n","})\n","\n","image_response = session.post(url, body)\n","image_content = image_response.content\n","\n","# Import the Image function from the IPython.display module. \n","from IPython.display import Image\n","Image(image_content)"],"execution_count":0,"outputs":[]}]}

0 comments on commit fe36ea3

Please sign in to comment.