From 69420a39442cfdb17680554946cf91861294d56e Mon Sep 17 00:00:00 2001 From: rhutten Date: Thu, 29 Oct 2020 11:48:38 +0100 Subject: [PATCH] read of branches from osm data #11 --- delft3dfmpy/datamodels/common.py | 39 ++++++++++++++++---------------- delft3dfmpy/datamodels/osm.py | 3 +-- examples/test_osm.py | 1 + 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/delft3dfmpy/datamodels/common.py b/delft3dfmpy/datamodels/common.py index a89b132..76ea5db 100644 --- a/delft3dfmpy/datamodels/common.py +++ b/delft3dfmpy/datamodels/common.py @@ -12,17 +12,17 @@ from delft3dfmpy.core import geometry - -def explode_multilinestring(gdf, id_col='code'): - gdf_dst = gpd.GeoDataFrame() - for f in gdf: - for n, g in enumerate(f.geometry): - row = deepcopy(f) - row['geometry'] = g - if len(f.geometry) > 1: - # add _{n} to id_col - row[id_col] += f'_{n}' - gdf_dst.append(row) +# FIXME: check if function still is needed +# def explode_multilinestring(gdf, id_col='code'): +# gdf_dst = gpd.GeoDataFrame() +# for f in gdf: +# for n, g in enumerate(f.geometry): +# row = deepcopy(f) +# row['geometry'] = g +# if len(f.geometry) > 1: +# # add _{n} to id_col +# row[id_col] += f'_{n}' +# gdf_dst.append(row) @@ -92,22 +92,23 @@ def read_shp(self, path, index_col=None, column_mapping=None, check_columns=True # Read GeoDataFrame gdf = gpd.read_file(path) + #FIXME: add method to handle features with geometry is None. + gdf.drop(gdf.index[gdf.geometry.isnull()], inplace =True) # temporary fix + #FIXME: add reprojection of gdf via crs inifile - if 'MultiPolygon' in str(gdf.geometry.type): + if 'MultiPolygon' or 'MultiLineString' in str(gdf.geometry.type): #gdf = gdf[gdf.geometry.type != 'MultiPolygon'] - sfx = ['_'+str(i) for i in range(100)] + #sfx = ['_'+str(i) for i in range(100)] gdf = gdf.explode() for ftc in gdf[id_col].unique(): if len(gdf[gdf[id_col] == ftc])>1: - gdf.loc[gdf[id_col] == ftc, id_col] = [i + sfx[ii] for ii, i in enumerate(gdf[gdf[id_col] == ftc][id_col])] - print(f'{ftc} is MultiPolygon; split into single parts.') + #FIXME: Check if method below works instead of sfx[ii] + gdf.loc[gdf[id_col] == ftc, id_col] = [i + f'_{ii}' for ii, i in enumerate(gdf[gdf[id_col] == ftc][id_col])] + print(f'{ftc} is MultiPolygon or MuliLineString; split into single parts.') #print('Features of type \"Multipolygon\" encountered: they are skipped.') - if 'MultiLineString' in str(gdf.geometry.type): - print('hello world') - # Check number of entries if gdf.empty: raise IOError('Imported shapefile contains no rows.') @@ -164,7 +165,7 @@ def _check_geotype(self): Check geometry type """ if not all(isinstance(geo, self.geotype) for geo in self.geometry): - raise TypeError('Geometrytype "{}" required. The input shapefile has geometry type(n) {}.'.format( + raise TypeError('Geometrytype "{}" required. The input shapefile has geometry type(s) {}.'.format( re.findall('([A-Z].*)\'', repr(self.geotype))[0], self.geometry.type.unique().tolist() )) diff --git a/delft3dfmpy/datamodels/osm.py b/delft3dfmpy/datamodels/osm.py index 905bc41..3cfe055 100644 --- a/delft3dfmpy/datamodels/osm.py +++ b/delft3dfmpy/datamodels/osm.py @@ -25,8 +25,7 @@ def __init__(self, extent_file=None, data_columns=None): # Create standard dataframe for network, cross sections, orifices, weirs # 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')) + 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. diff --git a/examples/test_osm.py b/examples/test_osm.py index 5773ea1..4cae130 100644 --- a/examples/test_osm.py +++ b/examples/test_osm.py @@ -27,6 +27,7 @@ # TODO: BRANCHES - read id column from json. Do not deviate between drain type osm.branches.read_shp(os.path.join(path,config.get('input','datafile')),index_col='id',clip = osm.clipgeo, id_col='id') +print('hello world') # TODO: BRANCHES - connect branches on the right locations # TODO: plot branches