Skip to content

Commit

Permalink
read of branches from osm data #11
Browse files Browse the repository at this point in the history
  • Loading branch information
rhutten committed Oct 29, 2020
1 parent daf32f9 commit 69420a3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
39 changes: 20 additions & 19 deletions delft3dfmpy/datamodels/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)



Expand Down Expand Up @@ -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.')
Expand Down Expand Up @@ -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()
))
Expand Down
3 changes: 1 addition & 2 deletions delft3dfmpy/datamodels/osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions examples/test_osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 69420a3

Please sign in to comment.