From d087b14ad25f4bc845fbccfb336aa396b2810158 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Thu, 5 Oct 2023 17:07:10 -0500 Subject: [PATCH] Updated URL quoting for data API example (#280) --- quickstarts/using-the-data-api.ipynb | 215 ++++++++++++++++++--------- 1 file changed, 142 insertions(+), 73 deletions(-) diff --git a/quickstarts/using-the-data-api.ipynb b/quickstarts/using-the-data-api.ipynb index 4d8f16c7..5f5586dd 100755 --- a/quickstarts/using-the-data-api.ipynb +++ b/quickstarts/using-the-data-api.ipynb @@ -24,7 +24,9 @@ "cell_type": "code", "execution_count": 1, "id": "3a61c5bb-8079-4617-bc6e-253595e93352", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "import requests\n", @@ -48,13 +50,15 @@ "cell_type": "code", "execution_count": 2, "id": "d55b7e1f-0563-4cba-8b54-a640cc961770", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "https://planetarycomputer.microsoft.com/api/data/v1/item/preview.png?collection=sentinel-2-l2a&item=S2B_MSIL2A_20220606T080609_R078_T36PUR_20220606T193343&assets=visual&asset_bidx=visual%7C1%2C2%2C3&nodata=0\n" + "https://planetarycomputer.microsoft.com/api/data/v1/item/preview.png?collection=sentinel-2-l2a&item=S2B_MSIL2A_20220606T080609_R078_T36PUR_20220606T193343&assets=visual&asset_bidx=visual%7C1%2C2%2C3&nodata=0&format=png\n" ] } ], @@ -79,12 +83,14 @@ "cell_type": "code", "execution_count": 3, "id": "bb085f46-a95e-4860-8d4e-681d25619d72", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -115,13 +121,15 @@ "cell_type": "code", "execution_count": 4, "id": "4f6d013e-98f2-438d-9185-9745929e9129", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "https://planetarycomputer.microsoft.com/api/data/v1/item/tilejson.json?collection=sentinel-2-l2a&item=S2B_MSIL2A_20220606T080609_R078_T36PUR_20220606T193343&assets=visual&asset_bidx=visual%7C1%2C2%2C3&nodata=0\n" + "https://planetarycomputer.microsoft.com/api/data/v1/item/tilejson.json?collection=sentinel-2-l2a&item=S2B_MSIL2A_20220606T080609_R078_T36PUR_20220606T193343&assets=visual&asset_bidx=visual%7C1%2C2%2C3&nodata=0&format=png\n" ] } ], @@ -141,7 +149,9 @@ "cell_type": "code", "execution_count": 5, "id": "f4941392-1424-49f1-a37e-191d9a7b3f6e", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { @@ -149,7 +159,7 @@ "{'tilejson': '2.2.0',\n", " 'version': '1.0.0',\n", " 'scheme': 'xyz',\n", - " 'tiles': ['https://planetarycomputer.microsoft.com/api/data/v1/item/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?collection=sentinel-2-l2a&item=S2B_MSIL2A_20220606T080609_R078_T36PUR_20220606T193343&assets=visual&asset_bidx=visual%7C1%2C2%2C3&nodata=0'],\n", + " 'tiles': ['https://planetarycomputer.microsoft.com/api/data/v1/item/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?collection=sentinel-2-l2a&item=S2B_MSIL2A_20220606T080609_R078_T36PUR_20220606T193343&assets=visual&asset_bidx=visual%7C1%2C2%2C3&nodata=0&format=png'],\n", " 'minzoom': 0,\n", " 'maxzoom': 24,\n", " 'bounds': [31.17569761, 8.95381176, 32.17948101, 9.95039568],\n", @@ -170,7 +180,9 @@ "cell_type": "code", "execution_count": 6, "id": "162a7443-a2b5-441b-ad5e-d4d4fc92ffb7", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "tiles = r[\"tiles\"][0]" @@ -189,13 +201,17 @@ "cell_type": "code", "execution_count": 7, "id": "4c93cf83-8c3b-46d0-bbe7-b677b555eb36", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + "</script>\n", + "</html>\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen>" ], "text/plain": [ - "" + "" ] }, "execution_count": 7, @@ -304,7 +324,9 @@ "cell_type": "code", "execution_count": 8, "id": "07d00326-e88a-4b4d-b28d-1f953780015d", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "import pystac_client\n", @@ -344,7 +366,9 @@ "cell_type": "code", "execution_count": 9, "id": "c5d8fc6c-e758-4290-bd7e-4e69ee40725d", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { @@ -384,7 +408,9 @@ "cell_type": "code", "execution_count": 10, "id": "2d14f7da-ecd3-4dff-99b2-742fc889549e", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "tilejson_url = registered[\"links\"][1][\"href\"]" @@ -402,14 +428,18 @@ "cell_type": "code", "execution_count": 11, "id": "7dd3dbb6-64d1-4272-b176-6f17851b9d03", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { "text/plain": [ "{'name': 'Natural color',\n", " 'description': 'True color composite of visible bands (B04, B03, B02)',\n", + " 'type': 'raster-tile',\n", " 'options': 'assets=B04&assets=B03&assets=B02&nodata=0&color_formula=Gamma RGB 3.2 Saturation 0.8 Sigmoidal RGB 25 0.35',\n", + " 'vectorOptions': None,\n", " 'minZoom': 9,\n", " 'legend': None,\n", " 'conditions': None}" @@ -429,6 +459,37 @@ "render_config" ] }, + { + "cell_type": "markdown", + "id": "254b516a-1196-4ae8-80ce-dcec384658b2", + "metadata": {}, + "source": [ + "We need to modify that slightly before it's ready to go to the `tilejson` endpoint." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "00e0e038-dcfa-4240-9e63-85c4a467ef72", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import itertools\n", + "\n", + "\n", + "def key(s):\n", + " return s.split(\"=\")[0]\n", + "\n", + "\n", + "params = {\n", + " k: [x.split(\"=\")[1] for x in v]\n", + " for k, v in itertools.groupby(render_config[\"options\"].split(\"&\"), key=key)\n", + "}\n", + "params[\"collection\"] = item.collection_id" + ] + }, { "cell_type": "markdown", "id": "2738b160-e7a8-4f20-aa42-6bb260f058c9", @@ -439,25 +500,25 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "id": "c7e6eb7c-4790-43f9-9988-b5425037afc8", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { "text/plain": [ - "'https://planetarycomputer.microsoft.com/api/data/v1/mosaic/tiles/3c377cdaf6bddee4f5379ee6707505fd/WebMercatorQuad/{z}/{x}/{y}@1x?collection=sentinel-2-l2a&assets=B04&assets=B03&assets=B02&nodata=0&color_formula=Gamma+RGB+3.2+Saturation+0.8+Sigmoidal+RGB+25+0.35'" + "'https://planetarycomputer.microsoft.com/api/data/v1/mosaic/tiles/3c377cdaf6bddee4f5379ee6707505fd/WebMercatorQuad/{z}/{x}/{y}@1x?assets=B04&assets=B03&assets=B02&nodata=0&color_formula=Gamma+RGB+3.2+Saturation+0.8+Sigmoidal+RGB+25+0.35&collection=sentinel-2-l2a'" ] }, - "execution_count": 12, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "tiles = requests.get(\n", - " f\"{tilejson_url}?collection={item.collection_id}&{render_config['options']}\"\n", - ").json()[\"tiles\"][0]\n", + "tiles = requests.get(tilejson_url, params=params).json()[\"tiles\"][0]\n", "tiles" ] }, @@ -471,15 +532,19 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "id": "d07db66a-7b73-4349-aa6e-f530c5b6bc74", - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + "</script>\n", + "</html>\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen>" ], "text/plain": [ - "" + "" ] }, - "execution_count": 13, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -624,7 +693,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.11.4" }, "vscode": { "interpreter": {