{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "---------------------------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# $SPA$tial $G$rap$H$s: n$ET$works, $T$opology, & $I$nference\n", "\n", "## Tutorial for `pysal.spaghetti`: Working with point patterns: empirical observations\n", "#### James D. Gaboardi []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Instantiating a `pysal.spaghetti.Network`\n", "2. Allocating observations to a network\n", " * snapping\n", "3. Visualizing original and snapped locations\n", " * visualization with `geopandas` and `matplotlib`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-05-14T00:25:25.919391Z", "start_time": "2019-05-14T00:25:25.896788Z" } }, "outputs": [], "source": [ "import os\n", "last_modified = None\n", "if os.name == \"posix\":\n", " last_modified = !stat -f\\\n", " \"# This notebook was last updated: %Sm\"\\\n", " Spaghetti_Pointpatterns_Empirical.ipynb\n", "elif os.name == \"nt\":\n", " last_modified = !for %a in (Spaghetti_Pointpatterns_Empirical.ipynb)\\\n", " do echo # This notebook was last updated: %~ta\n", " \n", "if last_modified:\n", " get_ipython().set_next_input(last_modified[-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-05-14T00:25:25.933253Z", "start_time": "2019-05-14T00:25:25.926333Z" } }, "outputs": [], "source": [ "# This notebook was last updated: Dec 9 14:23:58 2018" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-----------------" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-05-14T00:25:27.016615Z", "start_time": "2019-05-14T00:25:25.942023Z" } }, "outputs": [], "source": [ "from pysal.explore import spaghetti as spgh\n", "from pysal.lib import examples\n", "import geopandas as gpd\n", "import matplotlib.pyplot as plt\n", "import matplotlib.lines as mlines\n", "from shapely.geometry import Point, LineString\n", "\n", "%matplotlib inline\n", "\n", "__author__ = \"James Gaboardi \"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Instantiating a `pysal.spaghetti.Network`\n", "### Instantiate the network from `.shp` file" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-05-14T00:25:27.155155Z", "start_time": "2019-05-14T00:25:27.026125Z" } }, "outputs": [], "source": [ "ntw = spgh.Network(in_data=examples.get_path('streets.shp'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Allocating observations to a network\n", "### Snap point patterns to the network" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-05-14T00:25:27.473692Z", "start_time": "2019-05-14T00:25:27.167257Z" } }, "outputs": [], "source": [ "# Crimes with attributes\n", "ntw.snapobservations(examples.get_path('crimes.shp'),\n", " 'crimes',\n", " attribute=True)\n", "\n", "# Schools without attributes\n", "ntw.snapobservations(examples.get_path('schools.shp'),\n", " 'schools',\n", " attribute=False)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# 3. Visualizing original and snapped locations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## True and snapped school locations" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-05-14T00:25:27.494084Z", "start_time": "2019-05-14T00:25:27.475176Z" } }, "outputs": [], "source": [ "schools_df = spgh.element_as_gdf(ntw,\n", " pp_name='schools',\n", " snapped=False)\n", "\n", "snapped_schools_df = spgh.element_as_gdf(ntw,\n", " pp_name='schools',\n", " snapped=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## True and snapped crime locations" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-05-14T00:25:27.522298Z", "start_time": "2019-05-14T00:25:27.498845Z" } }, "outputs": [], "source": [ "crimes_df = spgh.element_as_gdf(ntw,\n", " pp_name='crimes',\n", " snapped=False)\n", "\n", "snapped_crimes_df = spgh.element_as_gdf(ntw,\n", " pp_name='crimes',\n", " snapped=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create `geopandas.GeoDataFrame` objects of the vertices and arcs" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-05-14T00:25:27.949426Z", "start_time": "2019-05-14T00:25:27.525105Z" } }, "outputs": [], "source": [ "# network nodes and edges\n", "vertices_df, arcs_df = spgh.element_as_gdf(ntw,\n", " vertices=True,\n", " arcs=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting `geopandas.GeoDataFrame` objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-05-14T00:25:27.964697Z", "start_time": "2019-05-14T00:25:27.952322Z" } }, "outputs": [], "source": [ "# legend patches\n", "arcs = mlines.Line2D([], [], color='k', label='Network Arcs', alpha=.5)\n", "vtxs = mlines.Line2D([], [], color='k', linewidth=0, markersize=2.5,\n", " marker='o', label='Network Vertices', alpha=1)\n", "schl = mlines.Line2D([], [], color='k', linewidth=0, markersize=25,\n", " marker='X', label='School Locations', alpha=1)\n", "snp_schl = mlines.Line2D([], [], color='k', linewidth=0, markersize=12,\n", " marker='o', label='Snapped Schools', alpha=1)\n", "crme = mlines.Line2D([], [], color='r', linewidth=0, markersize=7,\n", " marker='x', label='Crime Locations', alpha=.75)\n", "snp_crme = mlines.Line2D([], [], color='r', linewidth=0, markersize=3,\n", " marker='o', label='Snapped Crimes', alpha=.75)\n", "\n", "patches = [arcs, vtxs, schl, snp_schl, crme, snp_crme]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-05-14T00:25:28.589923Z", "start_time": "2019-05-14T00:25:27.969251Z" } }, "outputs": [], "source": [ "# plot figure\n", "base = arcs_df.plot(color='k', alpha=.25, figsize=(12,12), zorder=0)\n", "vertices_df.plot(ax=base, color='k', markersize=5, alpha=1)\n", "\n", "crimes_df.plot(ax=base, color='r', marker='x',\n", " markersize=50, alpha=.5, zorder=1)\n", "snapped_crimes_df.plot(ax=base, color='r',\n", " markersize=20, alpha=.5, zorder=1)\n", "\n", "schools_df.plot(ax=base, cmap='tab20', column='id', marker='X',\n", " markersize=500, alpha=.5, zorder=2)\n", "snapped_schools_df.plot(ax=base,cmap='tab20', column='id',\n", " markersize=200, alpha=.5, zorder=2)\n", "\n", "# add legend\n", "plt.legend(handles=patches, fancybox=True, framealpha=0.8,\n", " scatterpoints=1, fontsize=\"xx-large\", bbox_to_anchor=(1.04, .6))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-----------" ] } ], "metadata": { "_draft": { "nbviewer_url": "https://gist.github.com/cbf4e5b556b77f3a03ff2e65c3481b63" }, "anaconda-cloud": {}, "gist": { "data": { "description": "notebooks/Spaghetti_Pointpatterns_Empirical.ipynb", "public": true }, "id": "cbf4e5b556b77f3a03ff2e65c3481b63" }, "kernelspec": { "display_name": "Python [conda env:py3_spgh_dev]", "language": "python", "name": "conda-env-py3_spgh_dev-py" }, "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.6.7" } }, "nbformat": 4, "nbformat_minor": 1 }