{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from lets_plot import *\n",
"import pandas as pd\n",
"import requests\n",
"import json\n",
"from shapely.geometry import Point\n",
"from shapely.geometry import MultiPolygon, Polygon, LinearRing, Point, mapping\n",
"import geopandas as gpd\n",
"import fiona\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"LetsPlot.setup_html()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"votes=dict(state=['California','Nevada','Utah', 'Arizona'],\\\n",
" vote=['Clinton','Clinton','Trump','Trump'])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" key | \n",
" coord | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" California | \n",
" POINT (-119.99411 37.27734) | \n",
"
\n",
" \n",
" | 1 | \n",
" Nevada | \n",
" POINT (-116.66696 38.50308) | \n",
"
\n",
" \n",
" | 2 | \n",
" Utah | \n",
" POINT (-111.54916 39.49887) | \n",
"
\n",
" \n",
" | 3 | \n",
" Arizona | \n",
" POINT (-111.66859 34.16854) | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" key coord\n",
"0 California POINT (-119.99411 37.27734)\n",
"1 Nevada POINT (-116.66696 38.50308)\n",
"2 Utah POINT (-111.54916 39.49887)\n",
"3 Arizona POINT (-111.66859 34.16854)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"centroids = gpd.GeoDataFrame(\n",
" data={'key': ['California', 'Nevada', 'Utah', 'Arizona'],\n",
" 'coord': [\n",
" Point(-119.99411, 37.27734), \n",
" Point(-116.66696, 38.50308),\n",
" Point(-111.54916, 39.49887),\n",
" Point(-111.66859, 34.16854)]},\n",
" geometry='coord')\n",
"centroids"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def download_geometry(osm_id, key_value):\n",
" response = requests.get('http://polygons.openstreetmap.fr/get_geojson.py?id={0}¶ms=0'.format(osm_id))\n",
" with fiona.BytesCollection(bytes(str.encode(json.dumps(response.json()['geometries'][0])))) as f:\n",
" frame = gpd.GeoDataFrame.from_features(f, crs=f.crs)\n",
" # frame['geometry'] = frame.simplify(0.1)\n",
" frame['key'] = key_value\n",
" return frame"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"D:\\anaconda3\\envs\\lets-plot-docs\\lib\\site-packages\\pyproj\\crs\\crs.py:131: FutureWarning: '+init=:' syntax is deprecated. ':' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6\n",
" in_crs_string = _prepare_from_proj_string(in_crs_string)\n"
]
}
],
"source": [
"nv = download_geometry(165473, 'Nevada')\n",
"ca = download_geometry(165475, 'California')\n",
"ut = download_geometry(161993, 'Utah')\n",
"ar = download_geometry(162018, 'Arizona')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" geometry | \n",
" key | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" POLYGON ((-114.81358 32.49408, -111.07483 31.3... | \n",
" Arizona | \n",
"
\n",
" \n",
" | 1 | \n",
" MULTIPOLYGON (((-124.32884 41.99833, -119.9994... | \n",
" California | \n",
"
\n",
" \n",
" | 2 | \n",
" POLYGON ((-114.05283 37.57353, -114.05014 36.9... | \n",
" Utah | \n",
"
\n",
" \n",
" | 3 | \n",
" POLYGON ((-120.00556 39.25841, -120.00102 38.9... | \n",
" Nevada | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" geometry key\n",
"0 POLYGON ((-114.81358 32.49408, -111.07483 31.3... Arizona\n",
"1 MULTIPOLYGON (((-124.32884 41.99833, -119.9994... California\n",
"2 POLYGON ((-114.05283 37.57353, -114.05014 36.9... Utah\n",
"3 POLYGON ((-120.00556 39.25841, -120.00102 38.9... Nevada"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"boundaries = ar\n",
"boundaries = pd.concat([boundaries, ca], ignore_index=True)\n",
"boundaries = pd.concat([boundaries, ut], ignore_index=True)\n",
"boundaries = pd.concat([boundaries, nv], ignore_index=True)\n",
"boundaries['geometry'] = boundaries.simplify(0.1) \n",
"boundaries"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" key | \n",
" coord | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Polygon | \n",
" POLYGON ((-123.00000 34.00000, -120.00000 35.0... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" key coord\n",
"0 Polygon POLYGON ((-123.00000 34.00000, -120.00000 35.0..."
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"polygon = gpd.GeoDataFrame(\n",
" data={'key': ['Polygon'],\n",
" 'coord': [Polygon(LinearRing([(-123, 34), (-120, 35), (-118, 32)]))]},\n",
" geometry='coord')\n",
"polygon\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### GeoDataFrame in the map parameter using map_id\n",
"The data specified in the *data* and *map* parameters is merged by a key value from the *map_id* aesthetic. "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot() + geom_polygon(aes(fill='vote'), data = votes, map = boundaries, map_join=('state', 'key'), color='gray', alpha=0.4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### GeoDataFrame in data parameter\n",
"GeoDataFrame is supported natively in the *data* parameter. It works without *map_id.* \n",
"Geometries are automatically taken from GeoDataFrame."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "geom_polygon() takes from 0 to 1 positional arguments but 2 were given",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32mC:\\Temp/ipykernel_5284/2853313611.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mggplot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mp\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0mgeom_polygon\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maes\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfill\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'key'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mboundaries\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0malpha\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mscale_fill_hue\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mh\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m90\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mp\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0mgeom_path\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmap\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcentroids\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolor\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'red'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlinetype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msize\u001b[0m \u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mp\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0mgeom_point\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maes\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfill\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'key'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcentroids\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolor\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'gray'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msize\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mp\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0mgeom_rect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maes\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfill\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'key'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpolygon\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolor\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'gray'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msize\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: geom_polygon() takes from 0 to 1 positional arguments but 2 were given"
]
}
],
"source": [
"p = ggplot() \n",
"p += geom_polygon(aes(fill='key'), boundaries, alpha=1) + scale_fill_hue(h = (0, 90))\n",
"p += geom_path(map=centroids, color='red', linetype=2, size =1)\n",
"p += geom_point(aes(fill='key'), data = centroids, color='gray', size = 10)\n",
"p += geom_rect(aes(fill='key'), polygon, color='gray', size=2)\n",
"p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Combining layers from the data and map parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ggplot() + geom_map(aes(fill='key'), boundaries, alpha=0.4) + geom_point(aes(color='vote'), votes, map=centroids, map_join=('state', 'key'), size=10)+ scale_fill_hue()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}