<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Matthew Tenney on Medium]]></title>
        <description><![CDATA[Stories by Matthew Tenney on Medium]]></description>
        <link>https://medium.com/@matthewtenneyto?source=rss-c3b4eaa7b265------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*BtZWiNA8JnG4L56hB_KSFg.jpeg</url>
            <title>Stories by Matthew Tenney on Medium</title>
            <link>https://medium.com/@matthewtenneyto?source=rss-c3b4eaa7b265------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 22:30:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@matthewtenneyto/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Measuring Walking Times Across Toronto to Nearest TTC Stop Using the Pedestrian Network and Python]]></title>
            <link>https://medium.com/@matthewtenneyto/measuring-walking-times-across-toronto-to-nearest-ttc-stop-using-the-pedestrian-network-and-python-4263e0392a9b?source=rss-c3b4eaa7b265------2</link>
            <guid isPermaLink="false">https://medium.com/p/4263e0392a9b</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[smart-cities]]></category>
            <category><![CDATA[gis]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[city-of-toronto]]></category>
            <dc:creator><![CDATA[Matthew Tenney]]></dc:creator>
            <pubDate>Fri, 06 Sep 2019 14:52:34 GMT</pubDate>
            <atom:updated>2019-09-06T15:37:05.629Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>Toronto Walkability Analysis — Example Using Open Data</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Rq7sBKvNkzWHH3kq" /><figcaption><a href="https://ckan0.cf.opendata.inter.prod-toronto.ca/dataset/4b5c7a84-dea1-4137-875d-71d7f662c83f/resource/eed58e9b-6303-42fc-b6e8-04b811261c96/download/toronto-walkability-analysis.pdf">Output for Walk Times to Nearest TTC Stop for every address in the City of Toronto</a></figcaption></figure><p><strong>Walk times to Closest TTC Stop </strong><br>In this tutorial we will analyze walk times from every address point within the City of Toronto limits to the closest Toronto Transit Commission (TTC) Stops. TTC stops can include subway, LRT, street car and bus. The analysis will use <a href="https://github.com/UDST/pandana">Pandana </a>to perform the network distance calculations on a new open data set called the “Pedestrian Network” that our team (Data Analytics and Visualization -DAV — Team) created in conjunction with Transportation Services to better understand walkable access to various amenities across Toronto. <a href="https://ckan0.cf.opendata.inter.prod-toronto.ca/dataset/4b5c7a84-dea1-4137-875d-71d7f662c83f/resource/eed58e9b-6303-42fc-b6e8-04b811261c96/download/toronto-walkability-analysis.pdf">Our full report can be found here</a>. In this tutorial we will display our results at the level of an individual City of Toronto Neighborhood Improvement Areas. <a href="https://github.com/gcc-dav-official-github/dav_cot_walkability">Our code and full tutorial is available on our GitHub</a>.</p><h3><strong>Getting Example Data — Open Data Toronto</strong></h3><p>We will use data from the <a href="https://open.toronto.ca/">City of Toronto’s Open Data </a>site including TTC Stop (<a href="https://open.toronto.ca/dataset/ttc-routes-and-schedules/">https://open.toronto.ca/dataset/ttc-routes-and-schedules/</a>), the City of Toronto’s pedestrian network (<a href="https://open.toronto.ca/dataset/pedestrian-network/">https://open.toronto.ca/dataset/pedestrian-network/</a>), Toronto One Address Repository address point data (<a href="https://open.toronto.ca/dataset/address-points-municipal-toronto-one-address-repository/">https://open.toronto.ca/dataset/address-points-municipal-toronto-one-address-repository/</a>) and Neighborhood Improvement Areas (<a href="https://open.toronto.ca/dataset/neighbourhood-improvement-areas/">https://open.toronto.ca/dataset/neighbourhood-improvement-areas/</a>). Make sure you download a fresh copy and grab the right coordinate system for use in this example.</p><p><strong>TTC GTFS Data</strong></p><p>The TTC Routes and Schedules zip file contains route definitions, stop patterns, stop locations, and schedules. For this tutorial we will be using the stops.txt file.</p><p><strong>The Pedestrian Network</strong></p><p>The City of Toronto pedestrian network was a collective effort by the City of Toronto’s Information and Technology Division’s Data Analytics and Visualization team (DAV) and the City of Toronto’s Transportation Services Division. A pedestrian network based on sidewalk centerlines and pedestrian-assets (e.g., sidewalks, crosswalks, pedestrian controlled crossings) was created with the focus being placed on the topological consistency over spatial accuracy. Meaning that the sidewalk centerlines are not necessarily spatially located along with the actual locations of the sidewalk geographically, but the connections between different sidewalk segments were maintained as being a higher priority.</p><p><strong>Address Points</strong></p><p>The City of Toronto address point data contains over 500,000 address points within the city. A readme file is also included which explains the each of the attributes.</p><p><strong>Neighborhood Improvement Areas</strong></p><p>Neighborhood Improvement Areas are specially-selected and designated neighborhoods in the City of Toronto found to have inequities on several indicators of well-being. Currently 31 out of 140 neighborhoods are designated as NIAs as part of the Toronto Strong Neighborhoods 2020 (TSNS2020) project. Please see the link for further information (<a href="https://www.toronto.ca/city-government/data-research-maps/research-reports/social-reports/toronto-strong-neighbourhoods-strategy-2020/">https://www.toronto.ca/city-government/data-research-maps/research-reports/social-reports/toronto-strong-neighbourhoods-strategy-2020/</a>).</p><h3>Example Analysis:</h3><p><strong>Loading and Pre-Processing the Data</strong></p><p>First we need to load the data and reproject everything into a coordinate system using meters instead of lat/long. Using <a href="http://geopandas.org/">geopandas</a> reprojecting is as easy as a few lines of code, we’ll use <a href="https://epsg.io/2019">EPSG:2019 </a>— The City of Toronto has recently migrated to a new coordinate system <a href="https://epsg.io/7991">EPSG:7991</a>, you’ll need to pay attention what you’re getting from Open Data in order to have everything work (a lot of stuff is in WGS84).</p><h4>Importing Data</h4><p><strong>Read TTC Stop Data</strong></p><p>Read the TTC Stop data text file and create the spatial layer using the latitude and longitude provided in the file. The layer will be reprojected to EPSG 2019.</p><pre># reading ttc stop csv file<br># <a href="https://open.toronto.ca/dataset/ttc-routes-and-schedules/">https://open.toronto.ca/dataset/ttc-routes-and-schedules/</a><br># Go to Download Data<br>ttc_stop = pd.read_csv(&#39;./stops.txt&#39;, delimiter= &#39;,&#39;)<br>#include columns needed for the project and renaming stop_lat and stop_lon columns</pre><pre>ttc_stop = ttc_stop[[&#39;stop_id&#39;, &#39;stop_code&#39;,&#39;stop_name&#39;,&#39;stop_lat&#39;, &#39;stop_lon&#39;]]</pre><pre>ttc_stop = ttc_stop.rename(columns={&#39;stop_lat&#39;: &#39;latitude&#39;,&#39;stop_lon&#39;:&#39;longitude&#39; })</pre><pre># create geopanda dataframe add crs lat long<br>ttc_stop[&#39;geometry&#39;] = list(zip(ttc_stop.longitude, ttc_stop.latitude))<br>ttc_stop[&#39;geometry&#39;] = ttc_stop[&#39;geometry&#39;].apply(Point)</pre><pre># reprojecting epsg 4386 (wgs84) to epsg 2019 (mtm nad 27)<br>crs = {&#39;init&#39;: &#39;epsg:4326&#39;}<br>ttc_stop = gpd.GeoDataFrame(ttc_stop, crs=crs, geometry=&#39;geometry&#39;)<br>ttc_stop = ttc_stop.to_crs({&#39;init&#39;: &#39;epsg:2019&#39;})</pre><pre># calculating x and y which will be used later to find nearest ttc stop to address <br>ttc_stop[&#39;x&#39;] = ttc_stop.geometry.x<br>ttc_stop[&#39;y&#39;] = ttc_stop.geometry.y</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*BMrqD-CI63ABAhMp4qyK7Q.png" /><figcaption>Example of TTC Dataframe</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*-Dc37NLPdYE5ioxuydnJWw.png" /><figcaption>TTC Stops Plotted</figcaption></figure><h4>Read Pedestrian Network Data</h4><p>Read the Pedestrian Network line shape file and reproject to EPSG 2019</p><pre># reading pednet file</pre><pre>pednet = gpd.read_file(&quot;zip://./pednet.zip&quot;)</pre><pre>pednet.head(2)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*G8wF6IAGhdyRTFelDXbvog.png" /><figcaption>Pedestrian Network (pednet) Dataframe</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*PCILHdXuN5-vtuSd1zwbbQ.png" /><figcaption>Plot of pednet</figcaption></figure><h4>Load Address Data</h4><p>Read the Address point shape file and reproject to EPSG 2019</p><pre># reading municipal address point shape file</pre><pre># <a href="https://open.toronto.ca/dataset/address-points-municipal-toronto-one-address-repository/">https://open.toronto.ca/dataset/address-points-municipal-toronto-one-address-repository/</a></pre><pre># Go to Download Data</pre><pre>address = gpd.read_file(&quot;zip://./municipal-address-points-wgs84-latitude-longitude.zip&quot;)</pre><pre># reprojecting epsg 4386 (wgs84) to epsg 2019 (mtm nad 27)<br>#crs = {&#39;init&#39;: &#39;epsg:4326&#39;}<br>address = gpd.GeoDataFrame(address, crs=crs, geometry=&#39;geometry&#39;)<br>address = address.to_crs({&#39;init&#39;: &#39;epsg:2019&#39;})</pre><pre>address.head(2)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1005/1*Ymy6HUl9zxkB7rPdXy7JYQ.png" /><figcaption>Municipal Address Dataframe</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*VkaPHWVpKT4wASCc4OQOww.png" /><figcaption>Plot of Municipal Addresses</figcaption></figure><p>Address points will be used as the locations on the pedestrian network that we will calculate distances from, using the shortest-route, to a point of interest (in this case TTC stops).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*UaRXzjYrp2ee-KAR-wGwQA.png" /><figcaption>Pedestrian Network with Address Point Plot at the City a block-scale</figcaption></figure><h4>Build (Pandana) Network Model</h4><p>Pandana is a Python package that uses contraction hierarchies to perform rapid network calculations including shortest paths and accessibility buffers.</p><p>The link provides an introduction to the Pandana Network, these folks have created some extremely powerful tools that DAV uses every day: <a href="https://udst.github.io/pandana/introduction.html)">https://udst.github.io/pandana/introduction.html</a></p><p><strong>Create Network Graph — Function</strong></p><p>In this function we are using the library Networkx to build the network model from the pednet shapefile. This is basically just a convenience function to get the endpoint coordinates of each sidewalk link and “match” nodes with edge-endpoints.</p><pre># creating network graph code<br>def create_graph(gdf, precision=3):<br>&#39;&#39;&#39;Create a networkx given a GeoDataFrame of lines. Every line will correspond to two directional graph edges, one forward, one reverse. The original line row and direction will be stored in each edge. Every nodewill be where endpoints meet (determined by being very close together) and will store a clockwise ordering of incoming edges.<br>    &#39;&#39;&#39;<br>      <br>    G = nx.Graph()<br><br>    def make_node(coord, precision):<br>        return tuple(np.round(coord, precision))<br><br># Edges are stored as (from, to, data), where from and to are nodes.<br>    def add_edges(row, G):<br>        geometry = row.geometry<br>        coords = list(geometry.coords)<br>        geom_r = LineString(coords[::-1])<br>        coords_r = geom_r.coords<br>        start = make_node(coords[0], precision)<br>        end = make_node(coords[-1], precision)<br>        # Add forward edge<br>        fwd_attr ={}<br>        for k,v in row.items():<br>            fwd_attr[k]=v<br>        fwd_attr[&#39;forward&#39;]= 1<br>       #fwd_attr[&#39;geometry&#39;]=  geometry<br>        fwd_attr[&#39;length&#39;]=  geometry.length<br><br>        fwd_attr[&#39;visited&#39;]= 0<br><br>        G.add_edge(start, end, **fwd_attr)<br><br>    gdf.apply(add_edges, axis=1, args=[G])<br><br>    return G</pre><p>Create a network graph from the function above</p><pre># creating network graph<br>G = create_graph(pednet)<br>#from G to urbanaccess network model - one node idx,x,y one edge #from to weight</pre><pre># get network &quot;from&quot; and &quot;to&quot; from nodes<br>edges = nx.to_pandas_edgelist(G,&#39;from&#39;,&#39;to&#39;)<br>to = edges[&#39;to&#39;].tolist()<br>fr = edges[&#39;from&#39;].tolist()<br>fr = list(set(fr))<br>to = list(set(to))<br>to.extend(fr)<br>nodes = list(set(to))<br>nodes = pd.DataFrame(nodes)<br>nodes.columns=[&#39;x&#39;, &#39;y&#39;]<br>nodes[&#39;xy&#39;] = nodes.apply(lambda z: (z.x,z.y),axis=1)</pre><pre># Assigning node ids to to_node and from_node</pre><pre>nodes[&#39;id&#39;] = nodes.index<br>edges[&#39;to_node&#39;]=edges[&#39;to&#39;].map(nodes.set_index(&#39;xy&#39;).id)<br>edges[&#39;from_node&#39;]=edges[&#39;from&#39;].map(nodes.set_index(&#39;xy&#39;).id)<br>edges.head(3)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W-zo0lbEDDDrKxd1upyUIg.png" /><figcaption>Dataframe for the edgelist with from,to columns</figcaption></figure><h4>Creating and Saving Pandana Network</h4><p>Create the Pandana Network and save the file in case you get disconnected ry</p><pre># creating pandana network</pre><pre>transit_ped_net = pdna.Network(nodes[&quot;x&quot;],<br>                               nodes[&quot;y&quot;],<br>                               edges[&quot;from_node&quot;],<br>                               edges[&quot;to_node&quot;],<br>                               pd.DataFrame([edges[&#39;length&#39;]]).T,<br>                               twoway=True)</pre><pre># saving walkability file is optional. It can be used in the next steps if you don&#39;t have transit_ped_net in memory<br>transit_ped_net.save_hdf5(&#39;./walkability.hd5&#39;)</pre><h4>Precompute a Horizon Distance</h4><p>It’s probably a good idea to precompute a given cutoff distance so that aggregations don’t perform the network queries unnecessarily. This is done by calling the following code, where 1000 meters is used as the cut off distance:</p><pre>#precompute a given horizon distance of 1000 meters<br>transit_ped_net.precompute(1000)<br>transit_ped_net.edges_df.head(2)</pre><h4>Get the nearest pednet intersection node ids for each point from the address and ttc stop layers</h4><p>At this stage we’re adding the TTC stops as destination location points onto the network. Next we will actually calculate their distance to the Nth closest point of interest from every node location in the network.</p><pre>#get node_ids for points for each layer<br>x, y = ttc_stop.x, ttc_stop.y <br>ttc_stop[&quot;node_ids&quot;] = transit_ped_net.get_node_ids(x, y)<br>transit_ped_net.set(ttc_stop[&quot;node_ids&quot;], name=&quot;ttc_stop&quot;)</pre><pre># using x and y coordinates from address that are utm nad27 <br>x, y = address.x, address.y<br>address[&quot;node_ids&quot;] = transit_ped_net.get_node_ids(x, y)</pre><h4>Get 10 nearest ttc stop points</h4><p>The 10 nearest TTC stops will be determined. In most cases you should probably think of TTC stops in “pairs”, meaning the 2nd closest stop typically represents 1-full route and direction of travel (e.g., going N or S on the 24).</p><pre>#get nearest points with id<br>n=10<br>maxdistance = 1000</pre><pre>transit_ped_net.set_pois(&quot;ttc_stop&quot;, maxdistance , n, ttc_stop.x, ttc_stop.y)<br>ttc_stop_walk_distances = transit_ped_net.nearest_pois(maxdistance , &quot;ttc_stop&quot;, num_pois=n, include_poi_ids=False)</pre><h4>Calculate minutes fields based on distances</h4><p>We are applying an average pedestrian walking speed of 1.2 m/s which is used by the City of Toronto Transportation Services uses.The link below is to the TRANSPORTATION SERVICES STANDARD OPERATING PRACTICE — TRAFFIC SYSTEMS OPERATIONS document which discusses the standardized walk speed of 1.2 m/s. In reality we’d want to have a few different kinds of “maps” perhaps some for seniors, small-children, etc. that all may vary in the total speeds they walk and a better sense of the accessibility for a certain community or demographic.</p><p><a href="https://www.toronto.ca/wp-content/uploads/2019/01/96a8-TS_Traffic-Management_Pedestrian-Timing-Guidelines.pdf">https://www.toronto.ca/wp-content/uploads/2019/01/96a8-TS_Traffic-Management_Pedestrian-Timing-Guidelines.pdf</a></p><pre># Calculate minutes fields based on distances. This cell takes 1.5 minutes to run.</pre><pre>address[&#39;m_ttcstop_0&#39;] = address.apply(lambda row:</pre><pre>row.d_ttcstop_9/(1.2*60), axis=1)</pre><p>Also, we can “alter” the distances as a weighted impedance, for example on crosswalks, where people might take more time or it “costs more” to traverse. In our report we incorporated that by adding 20% onto the cross-walk lengths to make them artificially “longer” the more cross-walks you needed to cross.</p><h3>Visualize Walking Times and Aggregating to NIA’s</h3><p>In this step we will create a series of visualizations illustrating the walking times to the nearest TTC stop across the pedestrian network.</p><h3>Walking Times To Closest TTC Stop</h3><p>In this section we will create a chloropleth map showing the walk times to the closest TTC stop across the City of Toronto. As expected the shortest walk times are along major roads where TTC stops are located as shown in the second map below where the TTC stops are coloured red.</p><pre># Walking times To closest TTC Stop with TTC stops added</pre><pre>ax = address.plot(figsize=(15, 15), column=&#39;m_ttcstop_0&#39;, scheme=&#39;quantiles&#39;, legend=True,markersize = 1)<br>ttc_stop.plot(ax=ax, color=&#39;red&#39;, markersize =1)<br>plt.title(&#39;Walking Times To Closest TTC Stop&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GBrY_f4iPcdYkhF_oYch6g.png" /><figcaption>Address points symbolized by walk-time from nearest TTC Stop</figcaption></figure><h3>Walking Times To Closest TTC Stop with Neighbourhood Improvement Areas</h3><p>In this step we will invesitgate the walk times in the Neighbourhood Improvement Areas.</p><h3>Get the Neighbourhood Improvement Area centre points</h3><p>To place a label on each Neighbourhood Improvement Areas in the plot we need to find an identifiable point which exists within each polygon so that we can say where we want the text to be placed.</p><pre># Here we find the center points, copy our original df to a new df, and then set <br># the geometry column to the newly created center points column <br># (because a GeoPandas df can only have one geometry column)<br>nia[&quot;center&quot;] = nia[&quot;geometry&quot;].centroid<br>nia_points = nia.copy()<br>nia_points.set_geometry(&quot;center&quot;, inplace = True)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u_dqoPxOtxN3bvQlODpcZA.png" /><figcaption>NIA’s with Labels</figcaption></figure><h3>Addresses within Neighbourhood Improvement Areas</h3><p>We will now perform a spatial join between Neighbourhood Improvement Area layer and address layer in order to allocate addresses to Neighbourhood Improvement Areas. This basically grabs all the addresses within the polygon and assigns a column with the NIA information.</p><pre># Spatial Join where Neighbourhood Improvement Area polygons are intersected with address points<br>nia_address = gpd.sjoin(nia, address, op=&#39;intersects&#39;, how=&#39;left&#39;)<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3CUEKWlzLy3XsUQDFVVEPA.png" /><figcaption>Number of addresses per NIA</figcaption></figure><p>Ok, so now lets move to a different geographic scale of inquiry. We’ll aggregate these to the Neighborhood level.</p><h3>Average Walking Times To Closest TTC Stop in Neighbourhood Improvement Areas</h3><p>We will now use the mean function to calculate the mean walking time to the closest TTC stop for each of the Neighbourhood Improvement Areas.</p><pre>mins_avgs = nia_address.groupby(&#39;area_name&#39;)[&#39;m_ttcstop_0&#39;].mean()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W8f0m9mtmsJjSemk2WKi7g.png" /><figcaption>Average walktimes per NIA</figcaption></figure><p>Now we can drill back down and explore a particular neighborhood, Scarborough Village, using this same data. Here we will plot a block-level view of the symbolized address points by walk time, the pednet (black lines), TTC stops (red), and the NIA boundary (pink line):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iengcdvxQvATAlxEqwhSOA.png" /><figcaption>Block-View of pednet analysis for TTC Walktimes</figcaption></figure><p>Well that was the DAV teams brief tutorial on some of the work we’ve been doing and hope it spurs many great ideas to help better the lives of people living in the City.</p><p>I’d like to give a big thanks to my team: Karl Merrem, Ablajan Sulaiman, Zivorad Djokic, George Epure, and Bruce Walker for putting together this tutorial and of course creating the pednet and walkability report you can find on Open Data Toronto or our github.</p><p>Please feel free to email me with any questions or comments matthew.tenney@toronto.ca.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4263e0392a9b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>