Skip to content

Instantly share code, notes, and snippets.

@tylere
Last active July 8, 2023 00:49
Show Gist options
  • Select an option

  • Save tylere/a7c926576c0c464ff443c851794341f9 to your computer and use it in GitHub Desktop.

Select an option

Save tylere/a7c926576c0c464ff443c851794341f9 to your computer and use it in GitHub Desktop.
sloped-area-analysis.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNIC0snFoRxlJkhZO2CyBrS",
"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/tylere/a7c926576c0c464ff443c851794341f9/sloped-area-analysis.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Setup"
],
"metadata": {
"id": "XanzBHDC9mhm"
}
},
{
"cell_type": "code",
"source": [
"import ee\n",
"import math"
],
"metadata": {
"id": "TduBVP-m3Jl_"
},
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "L6t343ax3FGp",
"outputId": "5582ee83-83c1-488e-f93d-1846757b400e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"To authorize access needed by Earth Engine, open the following URL in a web browser and follow the instructions. If the web browser does not start automatically, please manually browse the URL below.\n",
"\n",
" https://code.earthengine.google.com/client-auth?scopes=https%3A//www.googleapis.com/auth/earthengine%20https%3A//www.googleapis.com/auth/devstorage.full_control&request_id=A-umrdtCMx57uydiFEO-TeoKvNVxxngo2SxnT4Tsudc&tc=VyYs8wYMDO_oo4FGXZGc1o3Rh6I1jEgPHIRnF_up75U&cc=Nd7WzMAR16DmTWF5nIPqFb1p9qzDZn8dDjyKX6ta_GA\n",
"\n",
"The authorization workflow will generate a code, which you should paste in the box below.\n",
"Enter verification code: 4/1AZEOvhWKorOuRzT6x4Lj-EsJ_97--uAZ4PgPPEC0mqo_LZrXfP8CeWvbCog\n",
"\n",
"Successfully saved authorization token.\n"
]
}
],
"source": [
"ee.Authenticate()\n",
"ee.Initialize()"
]
},
{
"cell_type": "markdown",
"source": [
"# Analysis\n",
"\n",
"Define the DEM and region of interest."
],
"metadata": {
"id": "l0M77SC79gvk"
}
},
{
"cell_type": "code",
"source": [
"DEGREES_TO_RADIANS = math.pi / 180\n",
"\n",
"dem = ee.Image(\"USGS/SRTMGL1_003\")\n",
"# dem = ee.Image(\"USGS/3DEP/10m\")\n",
"\n",
"roi = ee.FeatureCollection(\"TIGER/2018/States\").filter(\n",
" ee.Filter.eq('NAME', 'Colorado')\n",
")"
],
"metadata": {
"id": "5K-W5o_o3doj"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Calculate the slope of the terrain."
],
"metadata": {
"id": "iYvHWnzy9Qa4"
}
},
{
"cell_type": "code",
"source": [
"slope_degrees = ee.Terrain.slope(dem)\n",
"slope_rad = slope_degrees.multiply(DEGREES_TO_RADIANS)"
],
"metadata": {
"id": "0BERy3yw37CA"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Define images of pixel areas (flat), sloped areas, and the ratio between the two."
],
"metadata": {
"id": "80-ldkjP9EBb"
}
},
{
"cell_type": "code",
"source": [
"pixelArea = ee.Image.pixelArea()\n",
"slopedArea = pixelArea.divide(slope_rad.cos())\n",
"areaRatio = slopedArea.divide(pixelArea)"
],
"metadata": {
"id": "zZ5spX-L4Bv1"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Calculate the areas (flat and sloped) for the specified region and the difference between the two areas."
],
"metadata": {
"id": "5fQC_XGx8zWr"
}
},
{
"cell_type": "code",
"source": [
"# Get the projection (CRS) information from the DEM.\n",
"proj = dem.projection().getInfo()\n",
"\n",
"area_flat = ee.Number(\n",
" pixelArea.reduceRegion(\n",
" reducer= ee.Reducer.sum(),\n",
" geometry= roi,\n",
" crs= proj['crs'],\n",
" crsTransform= proj['transform'],\n",
" bestEffort= False,\n",
" maxPixels= 1e10,\n",
" ).get('area')\n",
")\n",
"\n",
"area_sloped = ee.Number(\n",
" slopedArea.reduceRegion(\n",
" reducer= ee.Reducer.sum(),\n",
" geometry= roi,\n",
" crs= proj['crs'],\n",
" crsTransform= proj['transform'],\n",
" bestEffort= False,\n",
" maxPixels= 1e10,\n",
" ).get('area')\n",
")\n",
"\n",
"area_diff = area_sloped.subtract(area_flat)\n",
"percent_increase = area_diff.divide(area_flat).multiply(100)"
],
"metadata": {
"id": "kiGp0JYV4JDK"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Print out the results."
],
"metadata": {
"id": "Aukr_9gi8b2V"
}
},
{
"cell_type": "code",
"source": [
"print(f'flat area = {area_flat.multiply(1e-6).getInfo():.2e} km2')\n",
"print(f'sloped area = {area_sloped.multiply(1e-6).getInfo():.2e} km2')\n",
"print(f'area difference = {area_diff.multiply(1e-6).getInfo():.2e} km2')\n",
"print(f'percent increase = {percent_increase.getInfo():.2f}%')\n",
"\n",
"# Colorado results\n",
"# SRTM 30m = 6611.7 km2 (2.45%) increase\n",
"# USGS 3DEP 10m = 8562.4 km2 (3.17%) increase"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "62xVcHGj3Oe_",
"outputId": "f2bafda5-5531-4c92-d3b3-c986f814ba8e"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"flat area = 2.70e+05 km2\n",
"sloped area = 2.76e+05 km2\n",
"area difference = 6.61e+03 km2\n",
"percent increase = 2.45%\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "-NaSo3Pk53NP"
},
"execution_count": 7,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment