{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Plotting Data\n\nIn this example, we plot some data obtained from a Tradesman model.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Imports\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\n\nos.environ[\"USE_PYGEOS\"] = \"0\"\n\nfrom uuid import uuid4\nfrom tempfile import gettempdir\nfrom aequilibrae.project import Project\nimport branca\nimport folium\nimport geopandas as gpd\nfrom zipfile import ZipFile\nfrom urllib.request import urlretrieve" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's download our project data.\nThis data is available as part of the project documentation, and presents\nLa Serena and Coquimbo metropolitan area in Chile.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "URL = \"https://github.com/AequilibraE/tradesman/releases/download/V0.1b/coquimbo.zip\"\n\nproj_fldr = os.path.join(gettempdir(), uuid4().hex)\nfile_fldr = os.path.join(gettempdir(), \"coquimbo.zip\")\n\nif not os.path.isfile(file_fldr):\n urlretrieve(URL, file_fldr)\n\nZipFile(file_fldr).extractall(proj_fldr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open an AequilibraE project\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "proj = Project()\nproj.open(proj_fldr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We establish a connection to identify the region we are plotting our data.\nFirst we import our subdivisions\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "with proj.db_connection as conn:\n subdivisions = gpd.read_postgis(\n \"SELECT division_name, level, ST_AsBinary(geometry)geom FROM political_subdivisions;\",\n con=conn,\n geom_col=\"geom\",\n crs=4326,\n )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can plot our map!\nGo ahead and check it out\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "colors = [\"#01BEFE\", \"#FFDD00\", \"#FF7D00\", \"#FF006D\", \"#ADFF02\", \"#8F00FF\"]\nm = None\n\nfor lvl in range(-1, subdivisions.level.max() + 1):\n gdf = subdivisions[subdivisions.level == lvl]\n\n if m:\n gdf.explore(\n m=m,\n name=f\"level {lvl}\",\n tiles=\"CartoDB positron\",\n tooltip=False,\n popup=True,\n location=[-29.935717, -71.260520],\n zoom_start=11,\n legend=False,\n color=colors[lvl + 1],\n )\n else:\n m = gdf.explore(\n name=f\"model_area\",\n tiles=\"CartoDB positron\",\n tooltip=False,\n popup=True,\n location=[-29.935717, -71.260520],\n zoom_start=11,\n legend=False,\n color=colors[lvl + 1],\n )\n\nfolium.LayerControl().add_to(m)\n\nm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Feel free to turn on/off all the layers. If you click on the subdivisions, you can also check its name and level.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's move on and import some information about our model's TAZs.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "with proj.db_connection as conn:\n zones = gpd.read_postgis(\"SELECT *, ST_AsBinary(geometry) geom FROM zones;\", con=conn, geom_col=\"geom\", crs=4326)\n zones.drop(columns=[\"geometry\"], inplace=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And create new columns\nPopulation per square kilometer\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "zones[\"POP_DENSITY\"] = zones[\"population\"] / (zones[\"geom\"].to_crs(3857).area * 10e-6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's plot our data!\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "zones.explore(\n \"POP_DENSITY\",\n tiles=\"CartoDB positron\",\n cmap=\"Greens\",\n tooltip=False,\n style_kwds={\"fillOpacity\": 1.0},\n zoom_start=11,\n location=[-29.935717, -71.260520],\n popup=True,\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Total female population per zone\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "zones[\"TOTALF_POP\"] = zones[[f\"POPF{i}\" for i in range(1, 19)]].sum(axis=1)\n# Total male population per zone\nzones[\"TOTALM_POP\"] = zones[[f\"POPM{i}\" for i in range(1, 19)]].sum(axis=1)\n# Ratio of the male population with respect to the female population\nzones[\"PP_FM\"] = zones.TOTALM_POP / zones.TOTALF_POP" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "zones.explore(\n \"PP_FM\",\n tiles=\"CartoDB positron\",\n cmap=\"RdPu\",\n tooltip=False,\n style_kwds={\"fillOpacity\": 1.0},\n zoom_start=11,\n location=[-29.935717, -71.260520],\n popup=True,\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In an ideal scenario, the ratio of the male population with respect to the female population would be close to 1.06. In countries such as India or China, this ratio is a bit larger, 1.12 and 1.15, respectively. This difference is responsible for creating abnormal sex ratios at birth.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's analyze the median age of male and female inhabitants per zone.\nTo plot this data, we shall do a little bit of math first, as our data is represented in intervals.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "interval_min = [0, 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80]\ninterval_mean = [0.5, 3, 7.5, 12.5, 17.5, 22.5, 27.5, 32.5, 37.5, 42.5, 47.5, 52.5, 57.5, 62.5, 67.5, 72.5, 77.5, 82.5]\ninterval_range = [1, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]\n\nfor sex in [\"F\", \"M\"]:\n list_values = zones[[f\"POP{sex}{i}\" for i in range(1, 19)]].to_numpy()\n\n median_values = []\n\n for idx, lst in enumerate(list_values):\n median = lst.sum() / 2\n counter = 0\n for pos, element in enumerate(lst):\n counter += element\n if counter > median:\n counter -= element\n break\n\n median_values.append(interval_min[pos - 1] + ((median - counter) * (interval_range[pos - 1] / lst[pos - 1])))\n\n zones[f\"MEDIAN_AGE_{sex}\"] = median_values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's take a look at our data!\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig = branca.element.Figure()\n\nsubplot1 = fig.add_subplot(1, 2, 1)\nsubplot2 = fig.add_subplot(1, 2, 2)\n\nmap1 = folium.Map(location=[-29.935717, -71.260520], zoom_start=12)\nmap1 = zones.explore(\n m=map1,\n column=\"MEDIAN_AGE_F\",\n linewidth=0.1,\n cmap=\"Oranges\",\n scheme=\"equal_interval\",\n k=5,\n legend=False,\n legend_kwds={\"loc\": \"upper left\", \"fmt\": \"{:.2f}\"},\n tiles=\"CartoDB positron\",\n)\nfolium.LayerControl().add_to(map1)\n\nmap2 = folium.Map(location=[-29.935717, -71.260520], zoom_start=12)\nmap2 = zones.explore(\n m=map2,\n column=\"MEDIAN_AGE_M\",\n linewidth=0.1,\n cmap=\"Blues\",\n legend=False,\n scheme=\"equal_interval\",\n k=5,\n legend_kwds={\"loc\": \"upper left\", \"fmt\": \"{:.2f}\"},\n tiles=\"CartoDB positron\",\n)\nfolium.LayerControl().add_to(map2)\n\nsubplot1.add_child(map1)\nsubplot2.add_child(map2)\n\nfig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our model also has OpenStreetMaps Building information. Let's take a look at the location of some building types.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import the data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "with proj.db_connection as conn:\n qry = \"SELECT building, zone_id, ST_AsBinary(geometry)geom FROM osm_building WHERE geometry IS NOT NULL;\"\n buildings = gpd.read_postgis(qry, con=conn, geom_col=\"geom\", crs=4326)\n buildings = buildings[buildings.building.isin([\"undetermined\", \"Religious\", \"residential\", \"commercial\"])]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And plot it\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "colors = [\"#73b7b8\", \"#0077b6\", \"#f05a29\", \"#f05a29\"]\n\nm = None\n\nfor idx, bld in enumerate(buildings.building.unique()):\n gdf = buildings[buildings.building == bld]\n\n if m:\n gdf.explore(\n m=m,\n name=bld,\n tiles=\"CartoDB positron\",\n tooltip=False,\n popup=True,\n zoom_start=15,\n location=[-29.9541855, -71.3479664],\n legend=False,\n color=colors[idx],\n )\n else:\n m = gdf.explore(\n name=bld,\n tiles=\"CartoDB positron\",\n tooltip=False,\n popup=True,\n zoom_start=15,\n location=[-29.9541855, -71.3479664],\n legend=False,\n color=colors[idx],\n )\n\nfolium.LayerControl().add_to(m)\n\nm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, let's check out our model's network.\nAs we imported data from OpenStreetMaps, it is possible that we have several _link_type_ categories. We'll plot only five of them.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "with proj.db_connection as conn:\n qry = \"SELECT link_type, distance, modes, ST_AsBinary(geometry) geom FROM links;\"\n links = gpd.read_postgis(qry, con=conn, geom_col=\"geom\", crs=4326)\n links = links[links.link_type.isin([\"motorway\", \"trunk\", \"primary\", \"secondary\", \"tertiary\"])]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "colors = [\"#219EBC\", \"#ffb703\", \"#8ECAE6\", \"#023047\", \"#fb8500\"]\nm = None\n\nfor idx, tp in enumerate(links.link_type.unique()):\n gdf = links[links.link_type == tp]\n if m:\n gdf.explore(\n m=m,\n name=tp,\n tiles=\"CartoDB positron\",\n tooltip=False,\n popup=True,\n zoom_start=11,\n location=[-29.935717, -71.260520],\n legend=False,\n color=colors[idx],\n )\n else:\n m = gdf.explore(\n name=tp,\n tiles=\"CartoDB positron\",\n tooltip=False,\n popup=True,\n zoom_start=11,\n location=[-29.935717, -71.260520],\n legend=False,\n color=colors[idx],\n )\n\nfolium.LayerControl().add_to(m)\n\nm" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.11" } }, "nbformat": 4, "nbformat_minor": 0 }