stplanpy.od module

The functions in this module perform various operations on origin-denstination or flow data.

stplanpy.od.distances(fd: geopandas.geodataframe.GeoDataFrame) pandas.core.series.Series

Compute distance along origin-destination lines or routes

This function computes the distance along origin-destination (od) lines or routes in the fd GeoDataFrame.

Parameters

None

Returns

Series with distances

Return type

pandas.Series

See also

od_lines, routes

Examples

import pandas as pd
import geopandas as gpd
from shapely import wkt
from stplanpy import od

# Define two origin-destination lines
df = pd.DataFrame({
    "orig_taz" : ["73906", "00106"],
    "dest_taz" : ["00120", "04120"],
    "geometry": [
    "LINESTRING(-91785727.431 4453819.337, -11782276.436 4448452.023)",
    "LINESTRING(-11785787.392 4450797.573, -11787086.209 4449884.472)"]})

# Convert to WTK
df["geometry"] = gpd.GeoSeries.from_wkt(df["geometry"])

# Create GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry='geometry', crs="EPSG:6933")

# Compute distances
gdf["distance"] = gdf.distances()
stplanpy.od.frm(fd: geopandas.geodataframe.GeoDataFrame, code) geopandas.geodataframe.GeoDataFrame

Select origin-destination flow data

This function selects origin-destination flow data from a Traffic Analysis Zone (TAZ), Census Designated Place, or a County, designated by the code variable. The length of the code variable is used to determine whether it applies to a TAZ, Place, or County.

Parameters

code (str) – The code of Traffic Anaysis Zone (8 characters long), Census Designated Place (5 characters long), or County (3 characters long).

Returns

GeoDataFrame with origin-destination from data for selected code.

Return type

geopandas.GeoDataFrame

See also

to, to_frm

Examples

The example data files: “od_data.csv”, “tl_2011_06_taz10.zip”, and “tl_2020_06_place.zip”, can be downloaded from github.

from stplanpy import acs
from stplanpy import geo
from stplanpy import od

# Read origin-destination flow data
flow_data = acs.read_acs("od_data.csv")
flow_data = flow_data.clean_acs()

# San Francisco Bay Area counties
counties = ["001", "013", "041", "055", "075", "081", "085", "095", "097"]

# Read taz data
taz = geo.read_shp("tl_2011_06_taz10.zip")

# Rename columns for consistency
taz.rename(columns = {"countyfp10":"countyfp", "tazce10":"tazce"}, inplace = True)

# Filter on county codes
taz = taz[taz["countyfp"].isin(counties)]

# Place code East Palo Alto
places = ["20956"]

# Read place data
place = geo.read_shp("tl_2020_06_place.zip")

# Keep only East Palo Alto
place = place[place["placefp"].isin(places)]

# Compute which taz lay inside a place and which part
taz = taz.in_place(place)

# Add county and place codes to data frame.
flow_data = flow_data.orig_dest(taz)

# Select origin-destination data from East Palo Alto
flow_data = flow_data.frm("20956")
stplanpy.od.gradient(fd: geopandas.geodataframe.GeoDataFrame, elevation: geopandas.geodataframe.GeoDataFrame, orig='orig_taz', dest='dest_taz', dist_name='distance') pandas.core.series.Series

Compute the gradient along an origin-destination line or route

This function computes the gradient, \(\nabla = h/d\), along an origin-destination line or route. \(d\) is the distance and \(h\) is the elevation change.

Parameters
  • elevation (geopandas.GeoDataFrame) – GeoDataFrame with elevation information for all the begin and end points of the origin-destination lines or routes in the fd GeoDataFrame.

  • orig (str, defaults to "orig_taz") – Column name in the fd GeoDataFrame containing all origin codes.

  • dest (str, defaults to "dest_taz") – Column name in the fd GeoDataFrame containing all destination codes.

  • dist_name (str, defaults to "distance") – Column name in the fd GeoDataFrame containing distance information

Returns

Series with gradients

Return type

pandas.Series

Examples

The example data files: “od_data.csv”, “tl_2011_06_taz10.zip”, and “srtm_12_05.zip” can be downloaded from github.

from stplanpy import acs
from stplanpy import geo
from stplanpy import od
from stplanpy import srtm

# Read origin-destination flow data
flow_data = acs.read_acs("od_data.csv")
flow_data = flow_data.clean_acs()

# San Francisco Bay Area counties
counties = ["001", "013", "041", "055", "075", "081", "085", "095", "097"]

# Read taz data
taz = geo.read_shp("tl_2011_06_taz10.zip")

# Rename columns for consistency
taz.rename(columns = {"countyfp10":"countyfp", "tazce10":"tazce"}, inplace = True)

# Filter on county codes
taz = taz[taz["countyfp"].isin(counties)]

# Compute centroids
taz_cent = taz.cent()

# Compute elevations
taz_cent = taz_cent.elev("srtm_12_05.zip")

# Compute origin-destination lines
flow_data["geometry"] = flow_data.od_lines(taz_cent)

# Compute distances
flow_data["distance"] = flow_data.distances()

# Compute gradients
flow_data["gradient"] = flow_data.gradient(taz_cent)
stplanpy.od.mode_share(gdf: geopandas.geodataframe.GeoDataFrame, flow_data: geopandas.geodataframe.GeoDataFrame, to_frm='to_from', modes=['bike'], orig=None, dest=None, code=None) pandas.core.frame.DataFrame

Compute mode share

Compute the mode share for Traffic Analysis Zones (TAZ), Census Designated Places, or Counties. This function tries to determine itself whether the gdf GeoDataFrame contains TAZ, Place, or County data, but this behavior can be overruled by specifying the orig, dest, and code variables. By default the “bike” mode shares is computed, but other modes of transportation can be specified.

Parameters
  • flow_data (geopandas.GeoDataFrame) – GeoDataFrame containing all the origin-destination data.

  • to_frm (str, defaults to "to_from") – Indicates whether only trips to a location, from a location, or both should be considered. The value of to_frm should be either: “to”, “from”, or “to_from”. The default is “to_from”.

  • modes (list of str, defaults to ["bike"]) – Default list of modes of transportation to compute the mode share for. Defaults to [“bike”].

  • orig (str, defaults to None) – Origin codes in gdf GeoDataFrame. Defaults to “orig_taz”, “orig_plc”, and “orig_cnt” for TAZ, Places, and Counties, respectively.

  • dest (str, defaults to None) – Destination codes in gdf GeoDataFrame. Defaults to “dest_taz”, “dest_plc”, and “dest_cnt” for TAZ, Places, and Counties, respectively.

  • code (str, defaults to None) – Codes in flow_data GeoDataFrame. Defaults to “tazce”, “placefp”, and “countyfp” for TAZ, Places, and Counties, respectively.

Returns

DataFrame containing the mode share for each mode of transporation.

Return type

pandas.DataFrame

See also

read_acs

Examples

The example data files: “od_data.csv”, “tl_2011_06_taz10.zip”, and “tl_2020_06_place.zip”, can be downloaded from github.

from stplanpy import acs
from stplanpy import geo
from stplanpy import od

# Read origin-destination flow data
flow_data = acs.read_acs("od_data.csv")
flow_data = flow_data.clean_acs()

# San Francisco Bay Area counties
counties = ["001", "013", "041", "055", "075", "081", "085", "095", "097"]

# Read taz data
taz = geo.read_shp("tl_2011_06_taz10.zip")

# Rename columns for consistency
taz.rename(columns = {"countyfp10":"countyfp", "tazce10":"tazce"}, inplace = True)

# Filter on county codes
taz = taz[taz["countyfp"].isin(counties)]

# Place code East Palo Alto
places = ["20956"]

# Read place data
place = geo.read_shp("tl_2020_06_place.zip")

# Keep only East Palo Alto
place = place[place["placefp"].isin(places)]

# Compute which taz lay inside a place and which part
taz = taz.in_place(place)

# Add county and place codes to data frame.
flow_data = flow_data.orig_dest(taz)

# Compute the mode share
place[["bike", "all"]] = place.mode_share(flow_data)
stplanpy.od.od_lines(fd: geopandas.geodataframe.GeoDataFrame, centroids: geopandas.geodataframe.GeoDataFrame, orig='orig_taz', dest='dest_taz') geopandas.geoseries.GeoSeries

Compute origin-destination lines

Compute origin-destination lines for all origin-destination pairs in GeoDataFrame fd. The centroids GeoDataFrame contains the coordinates of all the origins and destinations.

Parameters
  • centroids (geopandas.GeoDataFrame) – GeoDataFrame with the coordinates of all the locations in the fd GeoDataFrame.

  • orig (str, defaults to "orig_taz") – Column name of the fd GeoDataFrame containing all origin codes.

  • dest (str, defaults to "dest_taz") – Column name of the fd GeoDataFrame containing all destination codes.

Returns

GeoSeries with origin-destination lines

Return type

geopandas.GeoSeries

See also

orig_dest, cent

Examples

The example data files: “od_data.csv”, “tl_2011_06_taz10.zip”, and “tl_2020_06_place.zip”, can be downloaded from github.

from stplanpy import acs
from stplanpy import geo
from stplanpy import od

# Read origin-destination flow data
flow_data = acs.read_acs("od_data.csv")
flow_data = flow_data.clean_acs()

# San Francisco Bay Area counties
counties = ["001", "013", "041", "055", "075", "081", "085", "095", "097"]

# Place code East Palo Alto
places = ["20956"]

# Read place data
place = geo.read_shp("tl_2020_06_place.zip")

# Keep only East Palo Alto
place = place[place["placefp"].isin(places)]

# Read taz data
taz = geo.read_shp("tl_2011_06_taz10.zip")

# Rename columns for consistency
taz.rename(columns = {"countyfp10":"countyfp", "tazce10":"tazce"}, inplace = True)

# Filter on county codes
taz = taz[taz["countyfp"].isin(counties)]

# Compute centroids
taz_cent = taz.cent()

# Compute which taz lay inside a place and which part
taz = taz.in_place(place)

# Add county and place codes to data frame.
flow_data = flow_data.orig_dest(taz)

# Compute origin-destination lines
flow_data["geometry"] = flow_data.od_lines(taz_cent)
stplanpy.od.orig_dest(fd: geopandas.geodataframe.GeoDataFrame, taz: geopandas.geodataframe.GeoDataFrame, taz_name='tazce', plc_name='placefp', cnt_name='countyfp') geopandas.geodataframe.GeoDataFrame

Add County and Place codes to origin-destination data

This function adds County and Census Designated Place codes from the GeoDataFrame taz to the origin-destination or flow GeoDataFrame fd. The relevant column names are defined in taz_name, plc_name, and cnt_name, respectively. The column names in the output GeoDataFrame are “orig_taz”, “dest_taz”, “orig_plc”, “dest_plc”, “orig_cnt”, and “dest_cnt”.

Parameters
  • taz (geopandas.GeoDataFrame) – GeoDataFrame containing Traffic Analysis (TAZ) codes, Census Designated Place codes, and County codes.

  • taz_name (str, defaults to "tazce") – Column name in taz GeoDataFrame that contains TAZ codes. Defaults to “tazce”.

  • plc_name (str, defaults to "placefp") – Column name in taz GeoDataFrame that contains Census Designated Place codes. Defaults to “placefp”.

  • cnt_name (str, defaults to "countyfp") – Column name in taz GeoDataFrame that contains County codes. Defaults to “countyfp”.

Returns

GeoDataFrame with origin and destination TAZ, County, and Place codes. The column names are “orig_taz”, “dest_taz”, “orig_plc”, “dest_plc”, “orig_cnt”, and “dest_cnt”.

Return type

geopandas.GeoDataFrame

See also

read_acs, in_place

Examples

The example data files: “od_data.csv”, “tl_2011_06_taz10.zip”, and “tl_2020_06_place.zip”, can be downloaded from github.

from stplanpy import acs
from stplanpy import geo
from stplanpy import od

# Read origin-destination flow data
flow_data = acs.read_acs("od_data.csv")
flow_data = flow_data.clean_acs()

# San Francisco Bay Area counties
counties = ["001", "013", "041", "055", "075", "081", "085", "095", "097"]

# Place code East Palo Alto
places = ["20956"]

# Read place data
place = geo.read_shp("tl_2020_06_place.zip")

# Keep only East Palo Alto
place = place[place["placefp"].isin(places)]

# Read taz data
taz = geo.read_shp("tl_2011_06_taz10.zip")

# Rename columns for consistency
taz.rename(columns = {"countyfp10":"countyfp", "tazce10":"tazce"}, inplace = True)

# Filter on county codes
taz = taz[taz["countyfp"].isin(counties)]

# Compute which taz lay inside a place and which part
taz = taz.in_place(place)

# Add county and place codes to data frame.
flow_data = flow_data.orig_dest(taz)
stplanpy.od.rm_taz(fd: geopandas.geodataframe.GeoDataFrame, code, orig='orig_taz', dest='dest_taz')

Remove Traffic Analysis Zones from origin-destination data

This function removes a Traffic Analysis Zone (TAZ) from the origin-destination GeoDataFrame fd. Occurences of TAZ code are removed both when occuring as the origin and the destination. The column names of the origin and destination data are orig and dest, and default to “orig_taz” and “dest_taz”, respectively.

Parameters
  • code (str) – The code of Traffic Anaysis Zone.

  • orig (str, defaults to "orig_taz") – Column name of the fd GeoDataFrame containing all origin codes.

  • dest (str, defaults to "dest_taz") – Column name of the fd GeoDataFrame containing all destination codes.

Returns

GeoDataFrame with TAZ code removed.

Return type

geopandas.GeoDataFrame

See also

read_acs

Examples

The example data file “od_data.csv” can be downloaded from github.

from stplanpy import acs
from stplanpy import od

# Read origin-destination flow data
flow_data = acs.read_acs("od_data.csv")
flow_data = flow_data.clean_acs()

# Remove TAZ
flow_data = flow_data.rm_taz("00103024")
stplanpy.od.to(fd: geopandas.geodataframe.GeoDataFrame, code) geopandas.geodataframe.GeoDataFrame

Select origin-destination flow data

This function selects origin-destination flow data to a Traffic Analysis Zone (TAZ), Census Designated Place, or a County, designated by the code variable. The length of the code variable is used to determine whether it applies to a TAZ, Place, or County.

Parameters

code (str) – The code of Traffic Anaysis Zone (8 characters long), Census Designated Place (5 characters long), or County (3 characters long).

Returns

GeoDataFrame with origin-destination from data for selected code.

Return type

geopandas.GeoDataFrame

See also

frm, to_frm

Examples

The example data files: “od_data.csv”, “tl_2011_06_taz10.zip”, and “tl_2020_06_place.zip”, can be downloaded from github.

from stplanpy import acs
from stplanpy import geo
from stplanpy import od

# Read origin-destination flow data
flow_data = acs.read_acs("od_data.csv")
flow_data = flow_data.clean_acs()

# San Francisco Bay Area counties
counties = ["001", "013", "041", "055", "075", "081", "085", "095", "097"]

# Read taz data
taz = geo.read_shp("tl_2011_06_taz10.zip")

# Rename columns for consistency
taz.rename(columns = {"countyfp10":"countyfp", "tazce10":"tazce"}, inplace = True)

# Filter on county codes
taz = taz[taz["countyfp"].isin(counties)]

# Place code East Palo Alto
places = ["20956"]

# Read place data
place = geo.read_shp("tl_2020_06_place.zip")

# Keep only East Palo Alto
place = place[place["placefp"].isin(places)]

# Compute which taz lay inside a place and which part
taz = taz.in_place(place)

# Add county and place codes to data frame.
flow_data = flow_data.orig_dest(taz)

# Select origin-destination data to East Palo Alto
flow_data = flow_data.to("20956")
stplanpy.od.to_frm(fd: geopandas.geodataframe.GeoDataFrame, code) geopandas.geodataframe.GeoDataFrame

Select origin-destination flow data

This function selects origin-destination flow data to and from a Traffic Analysis Zone (TAZ), Census Designated Place, or a County, designated by the code variable. The length of the code variable is used to determine whether it applies to a TAZ, Place, or County.

Parameters

code (str) – The code of Traffic Anaysis Zone (8 characters long), Census Designated Place (5 characters long), or County (3 characters long).

Returns

GeoDataFrame with origin-destination from data for selected code.

Return type

geopandas.GeoDataFrame

See also

to, frm

Examples

The example data files: “od_data.csv”, “tl_2011_06_taz10.zip”, and “tl_2020_06_place.zip”, can be downloaded from github.

from stplanpy import acs
from stplanpy import geo
from stplanpy import od

# Read origin-destination flow data
flow_data = acs.read_acs("od_data.csv")
flow_data = flow_data.clean_acs()

# San Francisco Bay Area counties
counties = ["001", "013", "041", "055", "075", "081", "085", "095", "097"]

# Read taz data
taz = geo.read_shp("tl_2011_06_taz10.zip")

# Rename columns for consistency
taz.rename(columns = {"countyfp10":"countyfp", "tazce10":"tazce"}, inplace = True)

# Filter on county codes
taz = taz[taz["countyfp"].isin(counties)]

# Place code East Palo Alto
places = ["20956"]

# Read place data
place = geo.read_shp("tl_2020_06_place.zip")

# Keep only East Palo Alto
place = place[place["placefp"].isin(places)]

# Compute which taz lay inside a place and which part
taz = taz.in_place(place)

# Add county and place codes to data frame.
flow_data = flow_data.orig_dest(taz)

# Select origin-destination data to and from East Palo Alto
flow_data = flow_data.to_frm("20956")