From 3348ceae4e63187a8c363edf1ab7cf2a05de7d93 Mon Sep 17 00:00:00 2001 From: rhutten Date: Mon, 23 Nov 2020 16:36:40 +0100 Subject: [PATCH] #11 Add determination of branch_id and offset in 'branch_to_prof' method --- delft3dfmpy/datamodels/common.py | 18 +++++++++++++++--- delft3dfmpy/datamodels/osm.py | 9 --------- examples/test_osm.py | 8 ++++++-- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/delft3dfmpy/datamodels/common.py b/delft3dfmpy/datamodels/common.py index 1401d4a..a34ec0b 100644 --- a/delft3dfmpy/datamodels/common.py +++ b/delft3dfmpy/datamodels/common.py @@ -18,7 +18,11 @@ class ExtendedGeoDataFrame(gpd.GeoDataFrame): # normal properties _metadata = ['required_columns', 'geotype'] + gpd.GeoDataFrame._metadata - def __init__(self, geotype, required_columns=None, *args, **kwargs): + def __init__(self, geotype, required_columns=None,logger=logging, *args, **kwargs): + + # Add logger object to self + # FIXME: logging below results in error + #self.logger = logger # Check type if required_columns is None: @@ -339,15 +343,23 @@ def check_projection(self, crs_out): logger.info(f'OSM data has same projection as projected crs in ini-file') def branch_to_prof(self, offset=0., vertex_end=False, rename_col=None, prefix='', suffix=''): + """Create profiles on branches from branch data""" + gdf_out = self.copy() + # interpolate over feature geometries if vertex_end: - p = self.interpolate(self.length - offset) + chainage = self.length - offset + p = self.interpolate(chainage) else: - p = self.interpolate(offset) + chainage = offset + p = self.interpolate(chainage) gdf_out.geometry = p + gdf_out['offset'] = chainage + if rename_col is not None: try: + gdf_out['branch_id'] = gdf_out[rename_col] gdf_out[rename_col] = [f'{prefix}{g[1][rename_col]}{suffix}' for g in self.iterrows()] except: raise ValueError(f"Column rename with '{rename_col}' did not succeed.") diff --git a/delft3dfmpy/datamodels/osm.py b/delft3dfmpy/datamodels/osm.py index 5f6e856..5e5a4ad 100644 --- a/delft3dfmpy/datamodels/osm.py +++ b/delft3dfmpy/datamodels/osm.py @@ -41,15 +41,6 @@ def __init__(self, extent_file=None, data_columns=None, proj_crs = None, logger= # FIXME: check available columns and required columns for the OSM data, and apply these here self.branches = ExtendedGeoDataFrame(geotype=LineString, required_columns=self.get_columns('branches')) - # FIXME: in openstreetmap, cross sections are not linestrings, perpendicular to stream, but profile types and dimensions of a channel - # It may be that this is "parameterised cross sections, and we simply don't need the property below. - self.crosssections = ExtendedGeoDataFrame(geotype=LineString, required_columns=[ - 'code', - 'geometry', - 'ruwheidswaarde', - 'ruwheidstypecode' - ]) - # FIXME: ensure that all required parameterised properties are provided. I can imagine this is a matter of making # several parameterised profiles for different profile types (e.g. trapezoidal, rectangular, circular, etc.) self.profiles = ExtendedGeoDataFrame(geotype=LineString, required_columns=self.get_columns('crosssections')) diff --git a/examples/test_osm.py b/examples/test_osm.py index f3aa66b..59e895e 100644 --- a/examples/test_osm.py +++ b/examples/test_osm.py @@ -13,6 +13,7 @@ fn_ini = os.path.join(root, 'osm_settings.ini') logger = initialize_logger('osm2fm.log', log_level=10) + # Read ini file logger.info(f'Read config from {fn_ini}') config = configparser.ConfigParser(inline_comment_prefixes=[";", "#"]) @@ -61,12 +62,15 @@ ) # retrieve profiles at start of each line segment -profiles_start = osm.profiles.branch_to_prof(offset=0.5, prefix='A_', rename_col='id') +profiles_start = osm.profiles.branch_to_prof(offset=0.5, prefix = 'Prof_', suffix='_A', rename_col='id') # retrieve profiles at end of each line segment -profiles_end = osm.profiles.branch_to_prof(offset=0.5, prefix='B_', rename_col='id', vertex_end=True) +profiles_end = osm.profiles.branch_to_prof(offset=0.5, prefix = 'Prof_', suffix='_B', rename_col='id', vertex_end=True) # concat into a new profiles object osm.profiles = pd.concat([profiles_start, profiles_end]) +# +#osm.profiles.sample_raster(rasterio,offset=None,geometry) + # Plot branches and cross-sections plt.rcParams['axes.edgecolor'] = 'w'