Skip to content

pam.operations.snap

Methods for snapping elements to the network or facilities.

Reads a population, snaps activity facilities to a network geometry, and saves the results.

PARAMETER DESCRIPTION
path_population_in

Path to a PAM population.

TYPE: str

path_population_out

The path to save the output population.

TYPE: str

path_network_geometry

Path to the network geometry file.

TYPE: str

link_id_field

The link ID field to use in the network shapefile. Defaults to "id".

TYPE: str DEFAULT: 'id'

Source code in src/pam/operations/snap.py
def run_facility_link_snapping(
    path_population_in: str,
    path_population_out: str,
    path_network_geometry: str,
    link_id_field: str = "id",
) -> None:
    """Reads a population, snaps activity facilities to a network geometry, and saves the results.

    Args:
        path_population_in (str): Path to a PAM population.
        path_population_out (str): The path to save the output population.
        path_network_geometry (str): Path to the network geometry file.
        link_id_field (str, optional): The link ID field to use in the network shapefile. Defaults to "id".
    """
    population = read_matsim(path_population_in)
    if ".parquet" in Path(path_network_geometry).suffixes:
        network = gp.read_parquet(path_network_geometry)
    else:
        network = gp.read_file(path_network_geometry)
    snap_facilities_to_network(population=population, network=network, link_id_field=link_id_field)
    write_matsim(population=population, plans_path=path_population_out)

snap_facilities_to_network(population, network, link_id_field='id') #

Snaps activity facilities to a network geometry (in-place).

PARAMETER DESCRIPTION
population

A PAM population.

TYPE: Population

network

A network geometry shapefile.

TYPE: GeoDataFrame

link_id_field

The link ID field to use in the network shapefile. Defaults to "id".

TYPE: str DEFAULT: 'id'

Source code in src/pam/operations/snap.py
def snap_facilities_to_network(
    population: Population, network: gp.GeoDataFrame, link_id_field: str = "id"
) -> None:
    """Snaps activity facilities to a network geometry (in-place).

    Args:
        population (Population): A PAM population.
        network (gp.GeoDataFrame): A network geometry shapefile.
        link_id_field (str, optional): The link ID field to use in the network shapefile. Defaults to "id".
    """
    if network.geometry.geom_type[0] == "Point":
        coordinates = np.array(list(zip(network.geometry.x, network.geometry.y)))
    else:
        coordinates = np.array(list(zip(network.geometry.centroid.x, network.geometry.centroid.y)))

    tree = cKDTree(coordinates)
    link_ids = network[link_id_field].values

    for _, _, person in population.people():
        for act in person.activities:
            point = act.location.loc
            distance, index = tree.query([(point.x, point.y)])
            act.location.link = link_ids[index[0]]