Skip to content

Commit

Permalink
Port demo notebook to R (#31)
Browse files Browse the repository at this point in the history
* Port demo notebook to R

* Update based on review

Use httr2 instead of httr. Optimize geodataframe creation. Add custom breaks colormap.

* Match classification of R notebook
  • Loading branch information
zacdezgeo authored Sep 5, 2024
1 parent 021f960 commit 6fd9b7c
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 110 deletions.
210 changes: 100 additions & 110 deletions notebooks/space2stats_api_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@
"source": [
"from typing import Dict\n",
"\n",
"import numpy as np\n",
"import requests\n",
"import pandas as pd\n",
"import geopandas as gpd\n",
"from shapely.geometry import shape\n",
"\n",
"from lonboard.colormap import apply_continuous_cmap\n",
"from lonboard import Map, ScatterplotLayer\n",
"from palettable.cartocolors.sequential import BurgYl_2\n",
"from geojson_pydantic import Feature, Polygon"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -32,7 +30,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -54,68 +52,40 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"AOIModel = Feature[Polygon, Dict]\n",
"\n",
"# kenya\n",
"# ~kenya\n",
"aoi = {\n",
" \"type\": \"Feature\",\n",
" \"geometry\": {\n",
" \"type\": \"Feature\",\n",
" \"geometry\": {\n",
" \"type\": \"Polygon\",\n",
" \"coordinates\": [\n",
" [\n",
" [41.85508309264397, -1.68325],\n",
" [40.98105, -2.49979],\n",
" [40.993, -3.444],\n",
" [41.58513, -3.91909],\n",
" [40.88477, -4.95913],\n",
" [39.55938425876585, -4.437641590288629],\n",
" [39.25451, -3.42206],\n",
" [37.7669, -3.67712],\n",
" [37.69869, -3.09699],\n",
" [34.07262, -1.05982],\n",
" [33.90371119710453, -0.95],\n",
" [33.893568969666944, 0.109813537861896],\n",
" [34.18, 0.515],\n",
" [34.6721, 1.17694],\n",
" [35.03599, 1.90584],\n",
" [34.59607, 3.05374],\n",
" [34.47913, 3.5556],\n",
" [35.298007118232946, 4.77696566346189],\n",
" [35.817447662353516, 4.77696566346189],\n",
" [36.159078632855646, 4.447864127672769],\n",
" [36.85509323800812, 4.447864127672769],\n",
" [38.120915, 3.598605],\n",
" [38.43697, 3.58851],\n",
" [38.67114, 3.61607],\n",
" [38.89251, 3.50074],\n",
" [39.55938425876585, 3.42206],\n",
" [39.85494, 3.83879],\n",
" [40.76848, 4.25702],\n",
" [41.1718, 3.91909],\n",
" [41.85508309264397, 2.97959],\n",
" [41.58513, 2.09],\n",
" [40.993, 1.657],\n",
" [40.98105, 1.002],\n",
" [41.85508309264397, -1.68325]\n",
" ]\n",
" [\n",
" [33.78593974945852, 5.115816884114494],\n",
" [33.78593974945852, -4.725410543134203],\n",
" [41.94362577283266, -4.725410543134203],\n",
" [41.94362577283266, 5.115816884114494],\n",
" [33.78593974945852, 5.115816884114494]\n",
" ]\n",
" ]\n",
" },\n",
" \"properties\": {\n",
" \"name\": \"Kenya\"\n",
" }\n",
" },\n",
" \"properties\": {\n",
" \"name\": \"Updated AOI\"\n",
" }\n",
"\n",
"}\n",
" \n",
" \n",
"\n",
"feat = AOIModel(**aoi)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -138,7 +108,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -170,33 +140,33 @@
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>866a4a48fffffff</td>\n",
" <td>POINT (35.76352 2.99589)</td>\n",
" <td>399.860905</td>\n",
" <td>866a4a017ffffff</td>\n",
" <td>POINT (37.02598 -1.85051)</td>\n",
" <td>676.912804</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>866a4a497ffffff</td>\n",
" <td>POINT (40.58048 -3.79365)</td>\n",
" <td>582.555159</td>\n",
" <td>866a4a01fffffff</td>\n",
" <td>POINT (38.62960 -2.14241)</td>\n",
" <td>347.182722</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>866a4a49fffffff</td>\n",
" <td>POINT (41.10421 3.37873)</td>\n",
" <td>749.911237</td>\n",
" <td>866a4a04fffffff</td>\n",
" <td>POINT (36.51100 -0.42121)</td>\n",
" <td>285.943490</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>866a4a4d7ffffff</td>\n",
" <td>POINT (37.26153 3.74581)</td>\n",
" <td>863.888290</td>\n",
" <td>866a4a057ffffff</td>\n",
" <td>POINT (35.44853 -1.11991)</td>\n",
" <td>279.463912</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>866a5820fffffff</td>\n",
" <td>POINT (40.01148 1.53124)</td>\n",
" <td>525.085147</td>\n",
" <td>866a4a05fffffff</td>\n",
" <td>POINT (41.78961 -0.19877)</td>\n",
" <td>235.258474</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
Expand All @@ -205,58 +175,58 @@
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16212</th>\n",
" <td>867b5dd77ffffff</td>\n",
" <td>POINT (34.94474 1.24558)</td>\n",
" <td>-36.000000</td>\n",
" <th>25801</th>\n",
" <td>86969ed97ffffff</td>\n",
" <td>POINT (39.45061 -2.85307)</td>\n",
" <td>196.448391</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16213</th>\n",
" <td>867b5dd87ffffff</td>\n",
" <td>POINT (40.95343 -1.83280)</td>\n",
" <td>-36.000000</td>\n",
" <th>25802</th>\n",
" <td>86969ed9fffffff</td>\n",
" <td>POINT (35.00971 1.49503)</td>\n",
" <td>296.740893</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16214</th>\n",
" <td>867b5dd8fffffff</td>\n",
" <td>POINT (35.20290 -0.29666)</td>\n",
" <td>-36.000000</td>\n",
" <th>25803</th>\n",
" <td>86969eda7ffffff</td>\n",
" <td>POINT (35.16252 4.03827)</td>\n",
" <td>154.818453</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16215</th>\n",
" <td>867b5dd9fffffff</td>\n",
" <td>POINT (41.28333 -1.08552)</td>\n",
" <td>-36.000000</td>\n",
" <th>25804</th>\n",
" <td>86969edafffffff</td>\n",
" <td>POINT (37.66441 4.11738)</td>\n",
" <td>157.875350</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16216</th>\n",
" <td>867b5ddafffffff</td>\n",
" <td>POINT (36.63048 1.35038)</td>\n",
" <td>-36.000000</td>\n",
" <th>25805</th>\n",
" <td>86969edb7ffffff</td>\n",
" <td>POINT (35.12293 -4.05223)</td>\n",
" <td>236.127331</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>16217 rows × 3 columns</p>\n",
"<p>25806 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" hex_id geometry sum_pop_2020\n",
"0 866a4a48fffffff POINT (35.76352 2.99589) 399.860905\n",
"1 866a4a497ffffff POINT (40.58048 -3.79365) 582.555159\n",
"2 866a4a49fffffff POINT (41.10421 3.37873) 749.911237\n",
"3 866a4a4d7ffffff POINT (37.26153 3.74581) 863.888290\n",
"4 866a5820fffffff POINT (40.01148 1.53124) 525.085147\n",
"0 866a4a017ffffff POINT (37.02598 -1.85051) 676.912804\n",
"1 866a4a01fffffff POINT (38.62960 -2.14241) 347.182722\n",
"2 866a4a04fffffff POINT (36.51100 -0.42121) 285.943490\n",
"3 866a4a057ffffff POINT (35.44853 -1.11991) 279.463912\n",
"4 866a4a05fffffff POINT (41.78961 -0.19877) 235.258474\n",
"... ... ... ...\n",
"16212 867b5dd77ffffff POINT (34.94474 1.24558) -36.000000\n",
"16213 867b5dd87ffffff POINT (40.95343 -1.83280) -36.000000\n",
"16214 867b5dd8fffffff POINT (35.20290 -0.29666) -36.000000\n",
"16215 867b5dd9fffffff POINT (41.28333 -1.08552) -36.000000\n",
"16216 867b5ddafffffff POINT (36.63048 1.35038) -36.000000\n",
"25801 86969ed97ffffff POINT (39.45061 -2.85307) 196.448391\n",
"25802 86969ed9fffffff POINT (35.00971 1.49503) 296.740893\n",
"25803 86969eda7ffffff POINT (35.16252 4.03827) 154.818453\n",
"25804 86969edafffffff POINT (37.66441 4.11738) 157.875350\n",
"25805 86969edb7ffffff POINT (35.12293 -4.05223) 236.127331\n",
"\n",
"[16217 rows x 3 columns]"
"[25806 rows x 3 columns]"
]
},
"execution_count": 20,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -269,37 +239,57 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "00bbfea95ae440d3a73ebb161e3142ab",
"model_id": "25303c5410c44eda985a7b80299b95a3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map(layers=[ScatterplotLayer(get_fill_color=<pyarrow.lib.FixedSizeListArray object at 0x1631ef160>\n",
"Map(layers=[ScatterplotLayer(get_fill_color=<pyarrow.lib.FixedSizeListArray object at 0x129cf4a00>\n",
"[\n",
" [\n",
" 2…"
]
},
"execution_count": 21,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"max = gdf[\"sum_pop_2020\"].max()\n",
"min = gdf[\"sum_pop_2020\"].min()\n",
"normalized_sum_pop_2020 = (gdf[\"sum_pop_2020\"] - min) / (max - min)\n",
"normalized_sum_pop_2020\n",
"# 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",
"\n",
"# Function to assign colors based on custom bins\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",
"# 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",
"\n",
"# Create the scatterplot layer with the assigned colors\n",
"layer = ScatterplotLayer.from_geopandas(gdf, get_radius=2000, get_fill_color=colors)\n",
"\n",
"layer = ScatterplotLayer.from_geopandas(gdf, get_radius=2000, get_fill_color=apply_continuous_cmap(normalized_sum_pop_2020, BurgYl_2, alpha=0.7))\n",
"m = Map(layer)\n",
"m\n"
"m"
]
}
],
Expand Down
Loading

0 comments on commit 6fd9b7c

Please sign in to comment.