예제 #1
0
 def snap_to_branch(self, branches, snap_method, maxdist=5):
     """Snap the geometries to the branch"""
     geometry.find_nearest_branch(branches=branches,
                                  geometries=self,
                                  method=snap_method,
                                  maxdist=maxdist)
예제 #2
0
def read_file(path,
              hydamo_attribute,
              index_col=None,
              attribute_filter=None,
              snap_to_branches=None,
              keep_columns=None,
              column_mapping=None,
              z_coord=False
              ):
        """
        Read any OGR supported feature-file to match hydamo-property.

        A mask file can be specified to clip the selection.

        Parameters
        ----------
        path : str or Path
            Path to the feature file
        hydamo_attribute : HyDAMO property
            property to map to (HyDAMO.branches, HyDAMO.crosssections)
        index_col: str
            column to be used as index (after optional mapping)
        attribute_filter: dict
            dict with lists or strings of the format {'column_name': [values to keep]}
        snap_to_branches: dict
            snap to a geodataframe with LineStrings. Keep only geometries that snap
            to a LineString with a certain attribute value.
            Format: {'branches': GeoDataFrame,
                     attribute_filter: {'column_name': [values to keep]}}
        column_mapping: dict
            dict for renaming input colunns to required columns
        Result: GeoDataFrame matching the HyDAMO property
        """
        gdf = gpd.read_file(path)
        gdf.columns = gdf.columns.str.lower()

        # filter by gdf_snap
        if snap_to_branches:
            distance = snap_to_branches["distance"]
            branches = snap_to_branches["branches"]
            find_nearest_branch(branches, gdf, maxdist=distance)
            gdf = gdf.loc[gdf["branch_offset"].notna()]
            gdf.loc[:, "hydromodel"] = gdf.apply(
                (lambda x: branches.loc[x["branch_id"]]["hydromodel"]),
                axis=1)

            if snap_to_branches["attribute_filter"]:
                snap_attribute_filter = {
                    key.lower(): value for key, value in snap_to_branches[
                        "attribute_filter"].items()}
                gdf = _filter(gdf, snap_attribute_filter)

        # filter by attribute
        if attribute_filter:
            attribute_filter = {
                key.lower(): value for key, value in attribute_filter.items()}
            gdf = _filter(gdf, attribute_filter)

        # map to hydamo columns
        if column_mapping:
            column_mapping = {key.lower():value.lower() for key,value in column_mapping.items()}
            gdf.rename(columns=column_mapping, inplace=True)
            
        # drop all columns not needed 
        required_columns = getattr(hydamo,hydamo_attribute).required_columns.copy()

        if keep_columns:
           required_columns += [col.lower() for col in keep_columns]
        
        if hydamo_attribute =='crosssections':
            if z_coord:
                required_columns += ['z', 'order']

        drop_cols = [col for col in gdf.columns if not col in required_columns + ['geometry']]
        if len(drop_cols) > 0:
            gdf = gdf.drop(drop_cols, axis=1)
        
        return gdf