Skip to content

Commit

Permalink
Update notebook based on nbqa
Browse files Browse the repository at this point in the history
  • Loading branch information
zacdezgeo committed Nov 21, 2024
1 parent 886493f commit c23af15
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions notebooks/user-docs/s2s_python_lib.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@
"metadata": {},
"outputs": [],
"source": [
"from typing import Dict\n",
"import json\n",
"from typing import Dict\n",
"\n",
"import geopandas as gpd\n",
"import numpy as np\n",
"from dotenv import load_dotenv\n",
"import pandas as pd\n",
"import geopandas as gpd\n",
"from shapely.geometry import shape\n",
"from lonboard import Map, ScatterplotLayer\n",
"from dotenv import load_dotenv\n",
"from geojson_pydantic import Feature, Polygon\n",
"\n",
"from lonboard import Map, ScatterplotLayer\n",
"from shapely.geometry import shape\n",
"from space2stats import StatsTable"
]
},
Expand Down Expand Up @@ -155,16 +154,13 @@
" [33.78593974945852, -4.725410543134203],\n",
" [41.94362577283266, -4.725410543134203],\n",
" [41.94362577283266, 5.115816884114494],\n",
" [33.78593974945852, 5.115816884114494]\n",
" [33.78593974945852, 5.115816884114494],\n",
" ]\n",
" ]\n",
" ],\n",
" },\n",
" \"properties\": {\n",
" \"name\": \"Updated AOI\"\n",
" }\n",
" \"properties\": {\"name\": \"Updated AOI\"},\n",
"}\n",
" \n",
" \n",
"\n",
"\n",
"feat = AOIModel(**aoi)"
]
Expand Down Expand Up @@ -397,7 +393,9 @@
],
"source": [
"with StatsTable.connect() as stats_table:\n",
" data = stats_table.summaries(aoi=feat, spatial_join_method=\"centroid\", fields=fields, geometry=\"point\")\n",
" data = stats_table.summaries(\n",
" aoi=feat, spatial_join_method=\"centroid\", fields=fields, geometry=\"point\"\n",
" )\n",
" df = pd.DataFrame(data)\n",
"\n",
"df.head()"
Expand Down Expand Up @@ -810,8 +808,8 @@
}
],
"source": [
"df['geometry'] = df['geometry'].apply(lambda geom: shape(json.loads(geom)))\n",
"gdf = gpd.GeoDataFrame(df, geometry='geometry', crs='EPSG:4326')\n",
"df[\"geometry\"] = df[\"geometry\"].apply(lambda geom: shape(json.loads(geom)))\n",
"gdf = gpd.GeoDataFrame(df, geometry=\"geometry\", crs=\"EPSG:4326\")\n",
"gdf"
]
},
Expand Down Expand Up @@ -839,26 +837,31 @@
"source": [
"# Define custom breaks and corresponding RGBA colors\n",
"breaks = [0, 1, 1000, 10000, 50000, 100000, 200000, gdf[\"sum_pop_2020\"].max()]\n",
"colors = np.array([\n",
" [211, 211, 211, 255], # Light gray for 0\n",
" [255, 255, 0, 255], # Yellow for 1-1000\n",
" [255, 165, 0, 255], # Orange for 1000-10000\n",
" [255, 0, 0, 255], # Red for 10000-50000\n",
" [128, 0, 128, 255], # Purple for 50000-100000\n",
" [0, 0, 255, 255], # Blue for 100000-200000\n",
" [0, 0, 139, 255], # Dark blue for 200000+\n",
"])\n",
"colors = np.array(\n",
" [\n",
" [211, 211, 211, 255], # Light gray for 0\n",
" [255, 255, 0, 255], # Yellow for 1-1000\n",
" [255, 165, 0, 255], # Orange for 1000-10000\n",
" [255, 0, 0, 255], # Red for 10000-50000\n",
" [128, 0, 128, 255], # Purple for 50000-100000\n",
" [0, 0, 255, 255], # Blue for 100000-200000\n",
" [0, 0, 139, 255], # Dark blue for 200000+\n",
" ]\n",
")\n",
"\n",
"# Function to assign colors based on custom bins\n",
"\n",
"\n",
"def assign_color(value, breaks, colors):\n",
" for i in range(len(breaks) - 1):\n",
" if breaks[i] <= value < breaks[i + 1]:\n",
" return colors[i]\n",
" return colors[-1] # In case value exceeds all breaks\n",
"\n",
"\n",
"# Map sum_pop_2020 values to colors using the custom function\n",
"gdf['color'] = gdf[\"sum_pop_2020\"].apply(lambda x: assign_color(x, breaks, colors))\n",
"colors = np.uint8(gdf['color'].tolist())\n",
"gdf[\"color\"] = gdf[\"sum_pop_2020\"].apply(lambda x: assign_color(x, breaks, colors))\n",
"colors = np.uint8(gdf[\"color\"].tolist())\n",
"\n",
"# Create the scatterplot layer with the assigned colors\n",
"layer = ScatterplotLayer.from_geopandas(gdf, get_radius=2000, get_fill_color=colors)\n",
Expand Down

0 comments on commit c23af15

Please sign in to comment.