{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "ec9bc838-b121-4e65-9731-2045bcd9368c", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import geopandas as gpd\n", "import overturemaps \n", "from shapely import wkb\n", "from lonboard import Map, PolygonLayer\n", "from lonboard.colormap import apply_categorical_cmap" ] }, { "cell_type": "code", "execution_count": null, "id": "dae3c84a-abfe-430e-b168-3896266fa852", "metadata": {}, "outputs": [], "source": [ "# specify bounding box\n", "bbox = -78.6429, 39.463, -73.7806, 41.6242" ] }, { "cell_type": "code", "execution_count": null, "id": "e56dfe61-6d91-4ad4-a039-05bc04f2a8cd", "metadata": {}, "outputs": [], "source": [ "# read in Overture Maps land_cover data type\n", "table = overturemaps.record_batch_reader(\"land_cover\", bbox).read_all()\n", "table = table.combine_chunks()" ] }, { "cell_type": "code", "execution_count": null, "id": "9814b527-d2be-4d2d-ad5b-feba409b71d1", "metadata": {}, "outputs": [], "source": [ "# convert to dataframe\n", "df = table.to_pandas()" ] }, { "cell_type": "code", "execution_count": 2, "id": "830d1bee-9977-41c3-82cf-177c3a2e10c1", "metadata": {}, "outputs": [], "source": [ "# check shape of dataframe\n", "df.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "b4473f62-35d9-4824-b884-04d50c044b27", "metadata": {}, "outputs": [], "source": [ "# filter for higher resolution land_cover features\n", "df_h = df[df.cartography.apply(lambda x: x['min_zoom'] == 8)]" ] }, { "cell_type": "code", "execution_count": null, "id": "a5fabb73-2287-49a8-9bd1-27d78ad5477e", "metadata": {}, "outputs": [], "source": [ "# check shape of filtered dataframe\n", "df_h.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "82c4b13f-56b0-4168-8639-1087fc7a7be7", "metadata": {}, "outputs": [], "source": [ "# create color map for land_cover subtypes, loosely based on natural-color palette: https://www.shadedrelief.com/shelton/c.html\n", "color_map = {\n", " \"urban\": [167, 162, 186],\n", " \"forest\": [134, 178, 137],\n", " \"barren\": [245, 237, 213],\n", " \"shrub\": [239, 218, 182],\n", " \"grass\": [254, 239, 173],\n", " \"crop\": [222, 223, 154],\n", " \"wetland\": [158, 207, 195], \n", " \"mangrove\": [83, 171, 128],\n", " \"moss\": [250, 230, 160],\n", " \"snow\": [255, 255, 255], \n", "}" ] }, { "cell_type": "code", "execution_count": null, "id": "1033e221-aa08-4eb2-895b-b29befbaa24c", "metadata": {}, "outputs": [], "source": [ "# apply color map to land_cover subtypes\n", "colors = apply_categorical_cmap(df_h.subtype, color_map)" ] }, { "cell_type": "code", "execution_count": null, "id": "2dbb9ca1-f85b-40a1-9fb8-2e1c18a7178d", "metadata": {}, "outputs": [], "source": [ "# dataframe to geodataframe, set crs\n", "gdf = gpd.GeoDataFrame(\n", " df_h, \n", " geometry=df_h['geometry'].apply(wkb.loads), \n", " crs=\"EPSG:4326\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "84a63e84-47bc-4327-88ca-a5407e0aca5d", "metadata": {}, "outputs": [], "source": [ "# create map layer \n", "layer = PolygonLayer.from_geopandas(\n", " gdf= gdf[['id','subtype', 'cartography', 'geometry']].reset_index(drop=True),\n", " get_fill_color=colors,\n", " get_line_color=colors,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "0a66685e-7276-4a4f-9c25-2e6a2ad836c7", "metadata": {}, "outputs": [], "source": [ "#render map\n", "view_state = {\n", " \"longitude\": -76.2,\n", " \"latitude\": 39.6,\n", " \"zoom\": 8,\n", " \"pitch\": 65,\n", " \"bearing\": 5,\n", "}\n", "m = Map(layer, view_state=view_state)\n", "m" ] } ], "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.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }