From 60441ab7340a26dc78d8da8394ff2e9c437edbb0 Mon Sep 17 00:00:00 2001 From: f-PLT Date: Wed, 15 May 2024 15:36:27 -0400 Subject: [PATCH] Add basic polygons for grid creation --- ...etary_computer_sentinel2_exploration.ipynb | 410 +++++++++++++++++- 1 file changed, 388 insertions(+), 22 deletions(-) diff --git a/notebooks/planetary_computer_sentinel2_exploration.ipynb b/notebooks/planetary_computer_sentinel2_exploration.ipynb index ddfc9b5..4749981 100644 --- a/notebooks/planetary_computer_sentinel2_exploration.ipynb +++ b/notebooks/planetary_computer_sentinel2_exploration.ipynb @@ -2,58 +2,424 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, "id": "initial_id", "metadata": { "ExecuteTime": { - "end_time": "2024-05-06T18:05:49.303408Z", - "start_time": "2024-05-06T18:05:47.607910Z" + "end_time": "2024-05-14T20:23:59.377323Z", + "start_time": "2024-05-14T20:23:58.368463Z" } }, + "source": [ + "import leafmap\n", + "import geopandas as gpd\n", + "import numpy as np\n", + "import pathlib\n", + "\n", + "from geospatial_tools import DATA_DIR\n", + "from shapely.geometry import Polygon" + ], "outputs": [], + "execution_count": 1 + }, + { + "metadata": {}, + "cell_type": "markdown", "source": [ - "import leafmap" - ] + "## Base data\n", + "\n", + "The USA polygon is base off 2018's `cb_2018_us_nation_5m` shapefile, taken from here: \n", + "https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.html\n", + "\n", + "It was then processed using QGIS to keep only the contiguous states, without any islands.\n", + "\n", + "The Sentinel 2 grid was taken from the kml file found here: \n", + "https://sentinels.copernicus.eu/web/sentinel/missions/sentinel-2/data-products\n", + "\n", + "It was then processed using QGIS to keep only the grid cells that overlap with the \n", + "contiguous states, meaning the polygon layer which is described just above.\n", + "\n", + "Since our area of study is quite large, the `EPSG:5070` projection was chosen, as it\n", + "covers the whole area, introduces minimal distortion while preserving area." + ], + "id": "f2c6690384600863" }, { "cell_type": "code", - "execution_count": 3, "id": "fcb67d1eb21aafc1", "metadata": { "ExecuteTime": { - "end_time": "2024-05-06T18:05:50.046157Z", - "start_time": "2024-05-06T18:05:49.980358Z" + "end_time": "2024-05-14T21:20:51.493217Z", + "start_time": "2024-05-14T21:20:51.490927Z" + } + }, + "source": [ + "USA_POLYGON_FILE = DATA_DIR / \"usa/usa_polygon_5070.gpkg\"\n", + "S2_USA_GRID_FILE = DATA_DIR / \"usa/s2_grid_usa_polygon_5070.gpkg\"" + ], + "outputs": [], + "execution_count": 27 + }, + { + "cell_type": "code", + "id": "1f3b83d1d0e55f60", + "metadata": { + "ExecuteTime": { + "end_time": "2024-05-14T21:20:52.498922Z", + "start_time": "2024-05-14T21:20:52.324704Z" } }, + "source": [ + "usa_polygon = gpd.read_file(USA_POLYGON_FILE)\n", + "s2_grid = gpd.read_file(S2_USA_GRID_FILE)" + ], + "outputs": [], + "execution_count": 28 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-05-14T21:20:52.882552Z", + "start_time": "2024-05-14T21:20:52.828451Z" + } + }, + "cell_type": "code", + "source": "usa_polygon", + "id": "314e7c068786ad4d", + "outputs": [ + { + "data": { + "text/plain": [ + " AFFGEOID GEOID NAME \\\n", + "0 0100000US US United States \n", + "\n", + " geometry \n", + "0 MULTIPOLYGON (((-2123555.702 3120381.564, -212... " + ], + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
AFFGEOIDGEOIDNAMEgeometry
00100000USUSUnited StatesMULTIPOLYGON (((-2123555.702 3120381.564, -212...
\n", + "
" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 29 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-05-14T21:20:53.795062Z", + "start_time": "2024-05-14T21:20:53.785683Z" + } + }, + "cell_type": "code", + "source": "s2_grid", + "id": "3e9461ed5b602319", "outputs": [ { "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "68dfc3b05fd74aa49f70143af800fa9f", - "version_major": 2, - "version_minor": 0 - }, "text/plain": [ - "Map(center=[20, 0], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title', 'zoom_out_text…" + " name folders description \\\n", + "0 12TUP Features TILE PROPERTIES
\n", + "\n", + "
\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
namefoldersdescriptionaltitudealt_modetime_begintime_endtime_whengeometry
012TUPFeaturesTILE PROPERTIES<br><table border=0 cellpadding...0.0MULTIPOLYGON Z (((-1386334.944 2487548.770 0.0...
112TYQFeaturesTILE PROPERTIES<br><table border=0 cellpadding...0.0MULTIPOLYGON Z (((-976300.478 2523767.452 0.00...
212TYRFeaturesTILE PROPERTIES<br><table border=0 cellpadding...0.0MULTIPOLYGON Z (((-960099.705 2622374.255 0.00...
312TYNFeaturesTILE PROPERTIES<br><table border=0 cellpadding...0.0MULTIPOLYGON Z (((-1008622.024 2325748.358 0.0...
412TYPFeaturesTILE PROPERTIES<br><table border=0 cellpadding...0.0MULTIPOLYGON Z (((-992478.385 2424861.340 0.00...
..............................
97712TTMFeaturesTILE PROPERTIES<br><table border=0 cellpadding...0.0MULTIPOLYGON Z (((-1515431.586 2304192.826 0.0...
97812TUKFeaturesTILE PROPERTIES<br><table border=0 cellpadding...0.0MULTIPOLYGON Z (((-1448525.813 2089886.667 0.0...
97912TUQFeaturesTILE PROPERTIES<br><table border=0 cellpadding...0.0MULTIPOLYGON Z (((-1371006.917 2586590.133 0.0...
98012TURFeaturesTILE PROPERTIES<br><table border=0 cellpadding...0.0MULTIPOLYGON Z (((-1355793.563 2685354.080 0.0...
98112TUNFeaturesTILE PROPERTIES<br><table border=0 cellpadding...0.0MULTIPOLYGON Z (((-1401759.785 2388321.400 0.0...
\n", + "

982 rows × 9 columns

\n", + "" ] }, - "execution_count": 3, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], + "execution_count": 30 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-05-14T21:25:37.809992Z", + "start_time": "2024-05-14T21:25:37.457487Z" + } + }, + "cell_type": "code", "source": [ - "m = leafmap.Map()\n", + "m = leafmap.Map(center=[40, -98], zoom=4)\n", + "\n", + "# In blue, the USA polygon\n", + "m.add_gdf(usa_polygon, layer='usa')\n", + "# In red, the Sentinel 2 grid\n", + "m.add_gdf(s2_grid, layer='s2_grid', style={\"color\": \"red\"})\n", + "\n", "m" - ] + ], + "id": "a06e0387c33e38d0", + "outputs": [ + { + "data": { + "text/plain": [ + "Map(center=[40, -98], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title', 'zoom_out_te…" + ], + "application/vnd.jupyter.widget-view+json": { + "version_major": 2, + "version_minor": 0, + "model_id": "05d399ea1d8a4eba8e67890082ed36cb" + } + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 33 }, { - "cell_type": "code", - "execution_count": null, - "id": "1f3b83d1d0e55f60", "metadata": {}, - "outputs": [], - "source": [] + "cell_type": "markdown", + "source": [ + "## Creating our inference grid\n", + "\n", + "From this, we want to create a grid of square polygons with which we will later on\n", + "query the [Planetary Computer](https://planetarycomputer.microsoft.com/dataset/sentinel-2-l2a)\n", + "Sentinel 2 dataset and clip the selected Sentinel 2 images." + ], + "id": "576a9f79593417b9" } ], "metadata": {