Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update internet speeds example #129

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 234 additions & 38 deletions examples/internet-speeds.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"import shapely\n",
"from palettable.colorbrewer.diverging import BrBG_10\n",
"\n",
"from lonboard import ScatterplotLayer\n",
"from lonboard import ScatterplotLayer, viz\n",
"from lonboard.colormap import apply_continuous_cmap"
]
},
Expand Down Expand Up @@ -211,38 +211,234 @@
},
{
"cell_type": "markdown",
"id": "63537833-887d-4d40-acd2-8e212dc0412c",
"id": "65436a4a-c498-4f40-ba79-1082062376bf",
"metadata": {},
"source": [
"This is all we need to get data onto the map! Let's render a simple `ScatterplotLayer`, drawing each point as blue:"
"To ensure that this demo is snappy on most computers, we'll filter to a bounding box over Europe."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "498d466e-0f9f-497a-b4b7-8e274dffd9cf",
"id": "80326895-70ba-4f4b-a7b3-106b4bbd36d9",
"metadata": {},
"outputs": [],
"source": [
"gdf = gdf.cx[-11.83:25.5, 34.9:59]"
]
},
{
"cell_type": "markdown",
"id": "3cc2215e-7706-4ab3-b674-3de4ca41899c",
"metadata": {},
"source": [
"Even this filtered data frame still has 800,000 rows, so it's still a lot of data to explore:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a34a6a27-0259-4da9-94c4-923466da05fb",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>avg_d_kbps</th>\n",
" <th>geometry</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>383429</th>\n",
" <td>13570</td>\n",
" <td>POINT (-2.94159 58.99673)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>383430</th>\n",
" <td>18108</td>\n",
" <td>POINT (-3.29865 58.96276)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>383431</th>\n",
" <td>5569</td>\n",
" <td>POINT (-3.29315 58.97125)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>383432</th>\n",
" <td>9349</td>\n",
" <td>POINT (-3.29315 58.96842)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>383433</th>\n",
" <td>11216</td>\n",
" <td>POINT (-3.23273 58.98541)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1840929</th>\n",
" <td>104723</td>\n",
" <td>POINT (25.38666 35.09519)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1840930</th>\n",
" <td>62540</td>\n",
" <td>POINT (25.44708 35.04124)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1840931</th>\n",
" <td>88068</td>\n",
" <td>POINT (25.47455 35.04124)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1840938</th>\n",
" <td>38255</td>\n",
" <td>POINT (25.38116 35.00075)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1840939</th>\n",
" <td>91750</td>\n",
" <td>POINT (25.45807 34.99175)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>807221 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
" avg_d_kbps geometry\n",
"383429 13570 POINT (-2.94159 58.99673)\n",
"383430 18108 POINT (-3.29865 58.96276)\n",
"383431 5569 POINT (-3.29315 58.97125)\n",
"383432 9349 POINT (-3.29315 58.96842)\n",
"383433 11216 POINT (-3.23273 58.98541)\n",
"... ... ...\n",
"1840929 104723 POINT (25.38666 35.09519)\n",
"1840930 62540 POINT (25.44708 35.04124)\n",
"1840931 88068 POINT (25.47455 35.04124)\n",
"1840938 38255 POINT (25.38116 35.00075)\n",
"1840939 91750 POINT (25.45807 34.99175)\n",
"\n",
"[807221 rows x 2 columns]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gdf"
]
},
{
"cell_type": "markdown",
"id": "81a61ec4-2a09-40c0-aa92-7dca570bbd49",
"metadata": {},
"source": [
"The simplest way to get data on the map is to use `viz`:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "326f5f4e-a8f7-425b-8e9f-6744ba65cf62",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "bcbf7e3fa1bc4dc3ac46bbf3f97d48da",
"model_id": "30ba30dd2b9f4d47b307fa98ea9bf896",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"ScatterplotLayer(get_fill_color=[0, 0, 200, 30], table=pyarrow.Table\n",
"geometry: fixed_size_list<item: double>[2…"
"ScatterplotLayer(table=pyarrow.Table\n",
"avg_d_kbps: int64\n",
"__index_level_0__: int64\n",
"geometry: fixed_size_list<item…"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"map_ = viz(gdf)\n",
"map_"
]
},
{
"cell_type": "markdown",
"id": "91af2a88-9363-4c00-8e54-f06a635ff991",
"metadata": {},
"source": [
"This map object is a `ScatterplotLayer` type. You could have created the same map by using\n",
"```py\n",
"map_ = lonboard.ScatterplotLayer.from_geopandas(gdf)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "7d0f6f03-ca11-4842-8e53-4a619ad7d3dd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"lonboard.layer.ScatterplotLayer"
]
},
"execution_count": 8,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layer = ScatterplotLayer.from_geopandas(gdf[[\"geometry\"]], get_fill_color=[0, 0, 200, 30])\n",
"layer"
"type(map_)"
]
},
{
"cell_type": "markdown",
"id": "6f4d89c3-282a-4beb-9f35-68eb9645e8c0",
"metadata": {},
"source": [
"We can look at the [documentation for `ScatterplotLayer`](https://developmentseed.org/lonboard/api/layers/scatterplot-layer/) to see what other rendering options it allows. Let's set the fill color to something other than black:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "3912b241-577f-4ac3-b78f-2702e89d6010",
"metadata": {},
"outputs": [],
"source": [
"map_.get_fill_color = [0, 0, 200, 200]"
]
},
{
Expand All @@ -258,18 +454,18 @@
"id": "ce630455-3e19-47f1-bb69-81fdcd99b126",
"metadata": {},
"source": [
"Here we compute a linear statistic for the download speed. Given a minimum bound of `1000` and a maximum bound of `30,000` the normalized speed is linearly scaled to between 0 and 1."
"Here we compute a linear statistic for the download speed. Given a minimum bound of `5000` and a maximum bound of `50,000` the normalized speed is linearly scaled to between 0 and 1."
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 13,
"id": "179071b3",
"metadata": {},
"outputs": [],
"source": [
"min_bound = 1000\n",
"max_bound = 30000\n",
"min_bound = 5000\n",
"max_bound = 50000\n",
"download_speed = gdf['avg_d_kbps']\n",
"normalized_download_speed = (download_speed - min_bound) / (max_bound - min_bound)"
]
Expand All @@ -284,28 +480,28 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 14,
"id": "a8df3963-2bc2-4f89-8a38-20e232a13932",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 0.171828\n",
"1 0.094759\n",
"2 0.081517\n",
"3 0.047621\n",
"4 0.070586\n",
"383429 0.190444\n",
"383430 0.291289\n",
"383431 0.012644\n",
"383432 0.096644\n",
"383433 0.138133\n",
" ... \n",
"3231240 0.638897\n",
"3231241 0.506655\n",
"3231242 0.887828\n",
"3231243 2.310172\n",
"3231244 0.007931\n",
"Name: avg_d_kbps, Length: 3231245, dtype: float64"
"1840929 2.216067\n",
"1840930 1.278667\n",
"1840931 1.845956\n",
"1840938 0.739000\n",
"1840939 1.927778\n",
"Name: avg_d_kbps, Length: 807221, dtype: float64"
]
},
"execution_count": 10,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -324,7 +520,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 15,
"id": "9d5347e2-84c7-40bc-af45-c8638188709e",
"metadata": {},
"outputs": [
Expand All @@ -335,10 +531,10 @@
"<div style=\"vertical-align: middle;\"><strong>BrBG</strong> </div><div class=\"cmap\"><img alt=\"BrBG colormap\" title=\"BrBG\" style=\"border: 1px solid #555;\" src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAABACAYAAABsv8+/AAAAE3RFWHRUaXRsZQBCckJHIGNvbG9ybWFwMTXIUAAAABl0RVh0RGVzY3JpcHRpb24AQnJCRyBjb2xvcm1hcLqHWMgAAAAwdEVYdEF1dGhvcgBNYXRwbG90bGliIHYzLjcuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZ7rJ3hAAAAAydEVYdFNvZnR3YXJlAE1hdHBsb3RsaWIgdjMuNy4zLCBodHRwczovL21hdHBsb3RsaWIub3JnlG9BNwAAAilJREFUeJzt1k1u2zAURlGSQffTxXT/O7GYgSUHejahBPGgwHfOROWPKCYBitv//f0zW2ut99Zaa21cPu//GOPd+/t5/O7949h/39iP8WP+GI/TfO9l3yj79vXn88bL9/riO/Ueva4v7tHLd/rHx/7+6+d4rI/z+uq9sn8s18u5i/PG4tyrexz3bsffr5f3HvP7vnEer/a3Xu6x2N/K+V/7X8+3i++u7jn3v/Ns9+fWxv6s8+f15/lvnvPj7/32fne3Oe/j/XmbWxkf69ti/2/XV9970/q2nZ5bGd+2uZg/9pdzZj1nlv3fO/9p3/zhPco5x88763xZr/NzMb967+n8i/eX56/us1hv+++97b+Hr/GxPi/G/+n7+779fysAIIkAAIBAAgAAAgkAAAgkAAAgkAAAgEACAAACCQAACCQAACCQAACAQAIAAAIJAAAIJAAAIJAAAIBAAgAAAgkAAAgkAAAgkAAAgEACAAACCQAACCQAACCQAACAQAIAAAIJAAAIJAAAIJAAAIBAAgAAAgkAAAgkAAAgkAAAgEACAAACCQAACCQAACCQAACAQAIAAAIJAAAIJAAAIJAAAIBAAgAAAgkAAAgkAAAgkAAAgEACAAACCQAACCQAACCQAACAQAIAAAIJAAAIJAAAIJAAAIBAAgAAAgkAAAgkAAAgkAAAgEACAAACCQAACCQAACCQAACAQAIAAAIJAAAIJAAAIJAAAIBAAgAAAn0CyK5V+uT9Ti4AAAAASUVORK5CYII=\"></div><div style=\"vertical-align: middle; max-width: 514px; display: flex; justify-content: space-between;\"><div style=\"float: left;\"><div title=\"#543005ff\" style=\"display: inline-block; width: 1em; height: 1em; margin: 0; vertical-align: middle; border: 1px solid #555; background-color: #543005ff;\"></div> under</div><div style=\"margin: 0 auto; display: inline-block;\">bad <div title=\"#00000000\" style=\"display: inline-block; width: 1em; height: 1em; margin: 0; vertical-align: middle; border: 1px solid #555; background-color: #00000000;\"></div></div><div style=\"float: right;\">over <div title=\"#003c30ff\" style=\"display: inline-block; width: 1em; height: 1em; margin: 0; vertical-align: middle; border: 1px solid #555; background-color: #003c30ff;\"></div></div>"
],
"text/plain": [
"<matplotlib.colors.LinearSegmentedColormap at 0x156515290>"
"<matplotlib.colors.LinearSegmentedColormap at 0x1105c2950>"
]
},
"execution_count": 11,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -357,12 +553,12 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 16,
"id": "5a77f728-9cbe-4372-9bfd-d6dee4b93a01",
"metadata": {},
"outputs": [],
"source": [
"layer.get_fill_color = apply_continuous_cmap(normalized_download_speed, BrBG_10, alpha=0.3)"
"map_.get_fill_color = apply_continuous_cmap(normalized_download_speed, BrBG_10)"
]
},
{
Expand All @@ -385,15 +581,15 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 17,
"id": "579233ef-e077-4c8f-a111-f33d44f30a0d",
"metadata": {},
"outputs": [],
"source": [
"# for now, cast to a numpy array until the layer is updated to support pandas series\n",
"layer.get_radius = np.array(normalized_download_speed) * 200\n",
"layer.radius_units = \"meters\"\n",
"layer.radius_min_pixels = 0.5"
"map_.get_radius = np.array(normalized_download_speed) * 200\n",
"map_.radius_units = \"meters\"\n",
"map_.radius_min_pixels = 0.5"
]
},
{
Expand All @@ -407,9 +603,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "lonboard",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "lonboard"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand Down