Skip to content

Instantly share code, notes, and snippets.

@jdbcode
Last active March 1, 2022 21:43
Show Gist options
  • Save jdbcode/558f5c4f1ec40070d55b8bd102f889c2 to your computer and use it in GitHub Desktop.
Save jdbcode/558f5c4f1ec40070d55b8bd102f889c2 to your computer and use it in GitHub Desktop.
Include coordinates of image pixels transferred from Earth Engine to Numpy array
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "ee_numpy_array_with_pixel_coordinates.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyMhRNowYDypWkbnAZ3IlWZk",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/jdbcode/558f5c4f1ec40070d55b8bd102f889c2/ee_numpy_array_with_pixel_coordinates.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"import ee\n",
"import io\n",
"import numpy as np\n",
"import requests\n",
"\n",
"ee.Authenticate()\n",
"ee.Initialize()"
],
"metadata": {
"id": "NLwBoCI9a6us"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "4VDPUvyRa432"
},
"outputs": [],
"source": [
"# A Sentinel-2 surface reflectance image.\n",
"img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')\n",
"\n",
"# Longitude and latitude image.\n",
"lon_lat = ee.Image.pixelLonLat()\n",
"\n",
"# Add longitude and latitude bands to the primary image.\n",
"img = img.addBands(lon_lat)\n",
"\n",
"# A small region within the image.\n",
"region = ee.Geometry.BBox(-122.0859, 37.0436, -122.0626, 37.0586)\n",
"\n",
"# Image chunk as a NumPy structured array.\n",
"url = img.getDownloadUrl({\n",
" 'bands': ['B3', 'B8', 'B11', 'longitude', 'latitude'],\n",
" 'region': region,\n",
" 'scale': 20,\n",
" 'format': 'NPY'\n",
"})\n",
"response = requests.get(url)\n",
"data = np.load(io.BytesIO(response.content))\n",
"print(data)\n",
"print(data.dtype)"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment