Last active
March 10, 2023 00:38
-
-
Save emileten/bb1f15f10cef40710e97c81ad91d56c0 to your computer and use it in GitHub Desktop.
some-notebook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"id": "054f49cc", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from folium import Map, TileLayer\n", | |
"import json\n", | |
"from matplotlib import cm\n", | |
"import requests" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "1c1df385", | |
"metadata": {}, | |
"source": [ | |
"# Discover data from STAC" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"id": "e45da7c7", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'s3://nasa-maap-data-store/file-staging/nasa-map/AfriSAR_AGB_Maps_1681___1/Rabi_AGB_50m.tif'" | |
] | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"stac_endpoint = \"https://stac.maap-project.org\"\n", | |
"titiler_endpoint = \"https://titiler.dit.maap-project.org\"\n", | |
"collection = \"AfriSAR_AGB_Maps_1681\"\n", | |
"r = requests.get(f\"{stac_endpoint}/collections/{collection}/items\").json()\n", | |
"url = r['features'][0]['assets']['data']['href']\n", | |
"url" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8e9883c3", | |
"metadata": {}, | |
"source": [ | |
"# Get data values from the `/statistics` endpoint" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"id": "2787cadf", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# You can use gdalinfo /vsis3/nasa-maap-data-store/file-staging/nasa-map/SRTMGL1_COD___001/N21W089.SRTMGL1.tif -stats\n", | |
"# or you can get metadata from titiler.\n", | |
"r = requests.get(\n", | |
" f\"{titiler_endpoint}/cog/statistics\",\n", | |
" params = {\n", | |
" \"url\": url\n", | |
" }\n", | |
").json()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"id": "868d860c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'b1': {'min': 0.08733899891376495,\n", | |
" 'max': 516.4199829101562,\n", | |
" 'mean': 275.47825792468166,\n", | |
" 'count': 3691.0,\n", | |
" 'sum': 1016790.25,\n", | |
" 'std': 77.72327324432969,\n", | |
" 'median': 279.57000732421875,\n", | |
" 'majority': 245.52000427246094,\n", | |
" 'minority': 0.08733899891376495,\n", | |
" 'unique': 3463.0,\n", | |
" 'histogram': [[40.0,\n", | |
" 47.0,\n", | |
" 142.0,\n", | |
" 386.0,\n", | |
" 814.0,\n", | |
" 981.0,\n", | |
" 831.0,\n", | |
" 347.0,\n", | |
" 92.0,\n", | |
" 11.0],\n", | |
" [0.08733899891376495,\n", | |
" 51.720603942871094,\n", | |
" 103.35386657714844,\n", | |
" 154.9871368408203,\n", | |
" 206.62039184570312,\n", | |
" 258.253662109375,\n", | |
" 309.8869323730469,\n", | |
" 361.52020263671875,\n", | |
" 413.1534423828125,\n", | |
" 464.7867126464844,\n", | |
" 516.4199829101562]],\n", | |
" 'valid_percent': 34.8,\n", | |
" 'masked_pixels': 6914.0,\n", | |
" 'valid_pixels': 3691.0,\n", | |
" 'percentile_2': 95.27279968261719,\n", | |
" 'percentile_98': 421.5600097656249}}" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"r" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"id": "0a086740", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[0.08733899891376495, 51.720603942871094],\n", | |
" [51.720603942871094, 103.35386657714844],\n", | |
" [103.35386657714844, 154.9871368408203],\n", | |
" [154.9871368408203, 206.62039184570312],\n", | |
" [206.62039184570312, 258.253662109375],\n", | |
" [258.253662109375, 309.8869323730469],\n", | |
" [309.8869323730469, 361.52020263671875],\n", | |
" [361.52020263671875, 413.1534423828125],\n", | |
" [413.1534423828125, 464.7867126464844],\n", | |
" [464.7867126464844, 516.4199829101562]]" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"buckets = r['b1']['histogram'][1]\n", | |
"bucket_ranges = [[buckets[i],buckets[i+1]] for i in range(len(buckets)-1)]\n", | |
"bucket_ranges" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "ce6b5b33", | |
"metadata": {}, | |
"source": [ | |
"# Pick a color map and create a linear mapping" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"id": "78a9cf0d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# All colormaps can be found here: https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html\n", | |
"sequential_cmaps = [\n", | |
" 'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',\n", | |
" 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',\n", | |
" 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'\n", | |
"]\n", | |
"\n", | |
"sequential_cmaps2 = [\n", | |
" 'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink',\n", | |
" 'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia',\n", | |
" 'hot', 'afmhot', 'gist_heat', 'copper']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"id": "daa67372", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[([0.08733899891376495, 51.720603942871094], [0, 0, 0, 255]),\n", | |
" ([51.720603942871094, 103.35386657714844], [18, 48, 119, 255]),\n", | |
" ([103.35386657714844, 154.9871368408203], [37, 102, 124, 255]),\n", | |
" ([154.9871368408203, 206.62039184570312], [54, 135, 111, 255]),\n", | |
" ([206.62039184570312, 258.253662109375], [67, 151, 77, 255]),\n", | |
" ([258.253662109375, 309.8869323730469], [123, 167, 82, 255]),\n", | |
" ([309.8869323730469, 361.52020263671875], [169, 179, 91, 255]),\n", | |
" ([361.52020263671875, 413.1534423828125], [191, 164, 100, 255]),\n", | |
" ([413.1534423828125, 464.7867126464844], [221, 186, 167, 255]),\n", | |
" ([464.7867126464844, 516.4199829101562], [253, 250, 250, 255])]" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"rgbs = [[int(value) for value in rgb] for rgb in cm.ScalarMappable(cmap='gist_earth').to_rgba(x=buckets[:-1], bytes=True)]\n", | |
"color_map = list(zip(bucket_ranges, rgbs))\n", | |
"# some tweaking may be necessary\n", | |
"color_map" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c4c8124d", | |
"metadata": {}, | |
"source": [ | |
"# Preview the data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"id": "f77dc66d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"cmap = json.dumps(color_map)\n", | |
"\n", | |
"r = requests.get(\n", | |
" f\"{titiler_endpoint}/cog/tilejson.json\",\n", | |
" params = {\n", | |
" \"url\": url,\n", | |
" \"colormap\": cmap\n", | |
" }\n", | |
").json()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"id": "f0c7eaac", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'tilejson': '2.2.0',\n", | |
" 'version': '1.0.0',\n", | |
" 'scheme': 'xyz',\n", | |
" 'tiles': ['https://titiler.dit.maap-project.org/cog/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?url=s3%3A%2F%2Fnasa-maap-data-store%2Ffile-staging%2Fnasa-map%2FAfriSAR_AGB_Maps_1681___1%2FRabi_AGB_50m.tif&colormap=%5B%5B%5B0.08733899891376495%2C+51.720603942871094%5D%2C+%5B0%2C+0%2C+0%2C+255%5D%5D%2C+%5B%5B51.720603942871094%2C+103.35386657714844%5D%2C+%5B18%2C+48%2C+119%2C+255%5D%5D%2C+%5B%5B103.35386657714844%2C+154.9871368408203%5D%2C+%5B37%2C+102%2C+124%2C+255%5D%5D%2C+%5B%5B154.9871368408203%2C+206.62039184570312%5D%2C+%5B54%2C+135%2C+111%2C+255%5D%5D%2C+%5B%5B206.62039184570312%2C+258.253662109375%5D%2C+%5B67%2C+151%2C+77%2C+255%5D%5D%2C+%5B%5B258.253662109375%2C+309.8869323730469%5D%2C+%5B123%2C+167%2C+82%2C+255%5D%5D%2C+%5B%5B309.8869323730469%2C+361.52020263671875%5D%2C+%5B169%2C+179%2C+91%2C+255%5D%5D%2C+%5B%5B361.52020263671875%2C+413.1534423828125%5D%2C+%5B191%2C+164%2C+100%2C+255%5D%5D%2C+%5B%5B413.1534423828125%2C+464.7867126464844%5D%2C+%5B221%2C+186%2C+167%2C+255%5D%5D%2C+%5B%5B464.7867126464844%2C+516.4199829101562%5D%2C+%5B253%2C+250%2C+250%2C+255%5D%5D%5D'],\n", | |
" 'minzoom': 12,\n", | |
" 'maxzoom': 12,\n", | |
" 'bounds': [9.859138724101811,\n", | |
" -1.9460214484122889,\n", | |
" 9.906363400458433,\n", | |
" -1.9003139362380275],\n", | |
" 'center': [9.882751062280121, -1.9231676923251582, 12]}" | |
] | |
}, | |
"execution_count": 39, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"r" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"id": "60213a44", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><span style=\"color:#565656\">Make this Notebook Trusted to load map: File -> Trust Notebook</span><iframe srcdoc=\"<!DOCTYPE html>\n", | |
"<html>\n", | |
"<head>\n", | |
" \n", | |
" <meta http-equiv="content-type" content="text/html; charset=UTF-8" />\n", | |
" \n", | |
" <script>\n", | |
" L_NO_TOUCH = false;\n", | |
" L_DISABLE_3D = false;\n", | |
" </script>\n", | |
" \n", | |
" <style>html, body {width: 100%;height: 100%;margin: 0;padding: 0;}</style>\n", | |
" <style>#map {position:absolute;top:0;bottom:0;right:0;left:0;}</style>\n", | |
" <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.js"></script>\n", | |
" <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>\n", | |
" <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>\n", | |
" <script src="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.js"></script>\n", | |
" <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.css"/>\n", | |
" <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"/>\n", | |
" <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"/>\n", | |
" <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css"/>\n", | |
" <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css"/>\n", | |
" <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/python-visualization/folium/folium/templates/leaflet.awesome.rotate.min.css"/>\n", | |
" \n", | |
" <meta name="viewport" content="width=device-width,\n", | |
" initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />\n", | |
" <style>\n", | |
" #map_492d60febf2415a1f631a74a55f2bf2d {\n", | |
" position: relative;\n", | |
" width: 100.0%;\n", | |
" height: 100.0%;\n", | |
" left: 0.0%;\n", | |
" top: 0.0%;\n", | |
" }\n", | |
" .leaflet-container { font-size: 1rem; }\n", | |
" </style>\n", | |
" \n", | |
"</head>\n", | |
"<body>\n", | |
" \n", | |
" \n", | |
" <div class="folium-map" id="map_492d60febf2415a1f631a74a55f2bf2d" ></div>\n", | |
" \n", | |
"</body>\n", | |
"<script>\n", | |
" \n", | |
" \n", | |
" var map_492d60febf2415a1f631a74a55f2bf2d = L.map(\n", | |
" "map_492d60febf2415a1f631a74a55f2bf2d",\n", | |
" {\n", | |
" center: [-1.9231676923251582, 9.882751062280121],\n", | |
" crs: L.CRS.EPSG3857,\n", | |
" zoom: 13,\n", | |
" zoomControl: true,\n", | |
" preferCanvas: false,\n", | |
" }\n", | |
" );\n", | |
"\n", | |
" \n", | |
"\n", | |
" \n", | |
" \n", | |
" var tile_layer_f2c503109a3887e3fb018367d04feeb9 = L.tileLayer(\n", | |
" "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",\n", | |
" {"attribution": "Data by \\u0026copy; \\u003ca target=\\"_blank\\" href=\\"http://openstreetmap.org\\"\\u003eOpenStreetMap\\u003c/a\\u003e, under \\u003ca target=\\"_blank\\" href=\\"http://www.openstreetmap.org/copyright\\"\\u003eODbL\\u003c/a\\u003e.", "detectRetina": false, "maxNativeZoom": 18, "maxZoom": 18, "minZoom": 0, "noWrap": false, "opacity": 1, "subdomains": "abc", "tms": false}\n", | |
" ).addTo(map_492d60febf2415a1f631a74a55f2bf2d);\n", | |
" \n", | |
" \n", | |
" var tile_layer_4d8720612c19f8fd43db2cbfe42122ac = L.tileLayer(\n", | |
" "https://titiler.dit.maap-project.org/cog/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?url=s3%3A%2F%2Fnasa-maap-data-store%2Ffile-staging%2Fnasa-map%2FAfriSAR_AGB_Maps_1681___1%2FRabi_AGB_50m.tif\\u0026colormap=%5B%5B%5B0.08733899891376495%2C+51.720603942871094%5D%2C+%5B0%2C+0%2C+0%2C+255%5D%5D%2C+%5B%5B51.720603942871094%2C+103.35386657714844%5D%2C+%5B18%2C+48%2C+119%2C+255%5D%5D%2C+%5B%5B103.35386657714844%2C+154.9871368408203%5D%2C+%5B37%2C+102%2C+124%2C+255%5D%5D%2C+%5B%5B154.9871368408203%2C+206.62039184570312%5D%2C+%5B54%2C+135%2C+111%2C+255%5D%5D%2C+%5B%5B206.62039184570312%2C+258.253662109375%5D%2C+%5B67%2C+151%2C+77%2C+255%5D%5D%2C+%5B%5B258.253662109375%2C+309.8869323730469%5D%2C+%5B123%2C+167%2C+82%2C+255%5D%5D%2C+%5B%5B309.8869323730469%2C+361.52020263671875%5D%2C+%5B169%2C+179%2C+91%2C+255%5D%5D%2C+%5B%5B361.52020263671875%2C+413.1534423828125%5D%2C+%5B191%2C+164%2C+100%2C+255%5D%5D%2C+%5B%5B413.1534423828125%2C+464.7867126464844%5D%2C+%5B221%2C+186%2C+167%2C+255%5D%5D%2C+%5B%5B464.7867126464844%2C+516.4199829101562%5D%2C+%5B253%2C+250%2C+250%2C+255%5D%5D%5D",\n", | |
" {"attribution": "Swisstopo", "detectRetina": false, "maxNativeZoom": 18, "maxZoom": 18, "minZoom": 0, "noWrap": false, "opacity": 1, "subdomains": "abc", "tms": false}\n", | |
" ).addTo(map_492d60febf2415a1f631a74a55f2bf2d);\n", | |
" \n", | |
"</script>\n", | |
"</html>\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>" | |
], | |
"text/plain": [ | |
"<folium.folium.Map at 0x7f2524e9b310>" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\n", | |
"bounds = r[\"bounds\"]\n", | |
"m = Map(\n", | |
" location=((bounds[1] + bounds[3]) / 2,(bounds[0] + bounds[2]) / 2),\n", | |
" zoom_start=r[\"minzoom\"] + 1\n", | |
")\n", | |
"\n", | |
"aod_layer = TileLayer(\n", | |
" tiles=r[\"tiles\"][0],\n", | |
" opacity=1,\n", | |
" attr=\"Swisstopo\"\n", | |
")\n", | |
"aod_layer.add_to(m)\n", | |
"m" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "9c19261f", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment