Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Separate out updating of distance for links as a method #293

Open
4 tasks done
e-lo opened this issue Apr 19, 2022 · 0 comments · May be fixed by #327
Open
4 tasks done

[FEATURE] Separate out updating of distance for links as a method #293

e-lo opened this issue Apr 19, 2022 · 0 comments · May be fixed by #327
Assignees
Labels
enhancement New feature or request refactor
Milestone

Comments

@e-lo
Copy link
Collaborator

e-lo commented Apr 19, 2022

As a user, I'd like to have a single method for specifically updating the distance of links since this is a common chore.

Status

  • Defined
  • Planned
  • Implemented
  • Tested

Resolution Ideas

    def update_distance(
        self,
        links_df: GeoDataFrame = None,
        use_shapes: bool = False,
        units: str = "miles",
        network_variable: str = "distance",
        overwrite: bool = True,
        inplace=True,
    ):
        """
        Calculate link distance in specified units to network variable using either straight line
        distance or (if specified) shape distance if available.
        Args:
            links_df: Links GeoDataFrame.  Useful if want to update a portion of network links
                (i.e. only centroid connectors). If not provided, will use entire self.links_df.
            use_shapes: if True, will add length information from self.shapes_df rather than crow-fly.
                If no corresponding shape found in self.shapes_df, will default to crow-fly.
            units: units to use. Defaults to the standard unit of miles. Available units: "meters", "miles".
            network_variable: variable to store link distance in. Defaults to "distance".
            overwrite: Defaults to True and will overwrite all existing calculated distances.
                False will only update NaNs.
            inplace: updates self.links_df
        Returns:
            links_df with updated distance
        """
        if units not in ["miles", "meters"]:
            raise NotImplementedError

        if links_df is None:
            links_df = self.links_df.copy()

        msg = "Update distance in {} to variable: {}".format(units, network_variable)
        if overwrite:
            msg + "\n  - overwriting existing calculated values if found."
        if use_shapes:
            msg + "\n  - using shapes_df length if found."
        WranglerLogger.debug(msg)

        """
        Start actual process
        """

        temp_links_gdf = links_df.copy()
        temp_links_gdf.crs = "EPSG:4326"
        temp_links_gdf = temp_links_gdf.to_crs(epsg=26915)  # in meters

        conversion_from_meters = {"miles": 1 / 1609.34, "meters": 1}
        temp_links_gdf[network_variable] = (
            temp_links_gdf.geometry.length * conversion_from_meters[units]
        )

        if use_shapes:
            _needed_shapes_gdf = self.shapes_df.merge(
                links_df[self.shape_foreign_key],
                on=self.shape_foreign_key,
                how="inner",
            )

            _needed_shapes_gdf = _needed_shapes_gdf.to_crs(epsg=26915)
            _needed_shapes_gdf[network_variable] = (
                _needed_shapes_gdf.geometry.length * conversion_from_meters[units]
            )

            temp_links_gdf = update_df(
                temp_links_gdf,
                _needed_shapes_gdf,
                merge_key=self.shape_foreign_key,
                update_fields=[network_variable],
                method="update if found",
            )

        if overwrite:
            links_df[network_variable] = temp_links_gdf[network_variable]
        else:
            links_df = update_df(
                links_df,
                temp_links_gdf,
                merge_key=self.unique_link_key,
                update_fields=[network_variable],
                method="update nan",
            )

        if inplace:
            self.links_df = links_df
        else:
            return links_df
@e-lo e-lo added enhancement New feature or request refactor labels Apr 19, 2022
@e-lo e-lo added this to the v1.0 milestone Apr 19, 2022
@e-lo e-lo self-assigned this Apr 19, 2022
@e-lo e-lo removed this from the v1.0 milestone Nov 16, 2022
@e-lo e-lo added this to the v1.0 milestone Apr 15, 2024
@e-lo e-lo linked a pull request Apr 15, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request refactor
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant