Skip to content

Commit

Permalink
Merge ef53cd2 into b7c835a
Browse files Browse the repository at this point in the history
  • Loading branch information
fmaussion committed Feb 24, 2023
2 parents b7c835a + ef53cd2 commit 2f65736
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
42 changes: 21 additions & 21 deletions oggm/core/centerlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,28 +353,28 @@ def _filter_heads(heads, heads_height, radius, polygon):
head = heads[i]
pbuffer = head.buffer(radius)
inter_poly = pbuffer.intersection(polygon.exterior)
if inter_poly.type in ['MultiPolygon',
'GeometryCollection',
'MultiLineString']:
if inter_poly.geom_type in ['MultiPolygon',
'GeometryCollection',
'MultiLineString']:
# In the case of a junction point, we have to do a check
# http://lists.gispython.org/pipermail/community/
# 2015-January/003357.html
if inter_poly.type == 'MultiLineString':
if inter_poly.geom_type == 'MultiLineString':
inter_poly = shapely.ops.linemerge(inter_poly)

if inter_poly.type != 'LineString':
if inter_poly.geom_type != 'LineString':
# keep the local polygon only
for sub_poly in inter_poly.geoms:
if sub_poly.intersects(head):
inter_poly = sub_poly
break
elif inter_poly.type == 'LineString':
elif inter_poly.geom_type == 'LineString':
inter_poly = shpg.Polygon(np.asarray(inter_poly.xy).T)
elif inter_poly.type == 'Polygon':
elif inter_poly.geom_type == 'Polygon':
pass
else:
extext = ('Geometry collection not expected: '
'{}'.format(inter_poly.type))
'{}'.format(inter_poly.geom_type))
raise InvalidGeometryError(extext)

# Find other points in radius and in polygon
Expand Down Expand Up @@ -436,7 +436,7 @@ def _filter_lines(lines, heads, k, r):
# loop over all remaining lines and compute their diff
# to the last longest line
diff = l.difference(toremove)
if diff.type == 'MultiLineString':
if diff.geom_type == 'MultiLineString':
# Remove the lines that have no head
diff = list(diff.geoms)
for il in diff:
Expand Down Expand Up @@ -830,10 +830,10 @@ def _line_extend(uline, dline, dx):
while True:
pref = points[-1]
pbs = pref.buffer(dx).boundary.intersection(dline)
if pbs.type in ['LineString', 'GeometryCollection']:
if pbs.geom_type in ['LineString', 'GeometryCollection']:
# Very rare
pbs = pref.buffer(dx+1e-12).boundary.intersection(dline)
if pbs.type == 'Point':
if pbs.geom_type == 'Point':
pbs = [pbs]

try:
Expand Down Expand Up @@ -1376,13 +1376,13 @@ def _point_width(normals, point, centerline, poly, poly_no_nunataks):

# First use the external boundaries only
line = normal.intersection(poly_no_nunataks)
if line.type == 'LineString':
if line.geom_type == 'LineString':
pass # Nothing to be done
elif line.type in ['MultiLineString', 'GeometryCollection']:
elif line.geom_type in ['MultiLineString', 'GeometryCollection']:
# Take the one that contains the centerline
oline = None
for l in line.geoms:
if l.type != 'LineString':
if l.geom_type != 'LineString':
continue
if l.intersects(centerline.line):
oline = l
Expand All @@ -1391,33 +1391,33 @@ def _point_width(normals, point, centerline, poly, poly_no_nunataks):
return np.NaN, shpg.MultiLineString()
line = oline
else:
extext = 'Geometry collection not expected: {}'.format(line.type)
extext = 'Geometry collection not expected: {}'.format(line.geom_type)
raise InvalidGeometryError(extext)

# Then take the nunataks into account
# Make sure we are always returning a MultiLineString for later
line = line.intersection(poly)
if line.type == 'LineString':
if line.geom_type == 'LineString':
try:
line = shpg.MultiLineString([line])
except shapely.errors.EmptyPartError:
return np.NaN, shpg.MultiLineString()
elif line.type == 'MultiLineString':
elif line.geom_type == 'MultiLineString':
pass # nothing to be done
elif line.type == 'GeometryCollection':
elif line.geom_type == 'GeometryCollection':
oline = []
for l in line:
if l.type != 'LineString':
if l.geom_type != 'LineString':
continue
oline.append(l)
if len(oline) == 0:
return np.NaN, shpg.MultiLineString()
line = shpg.MultiLineString(oline)
else:
extext = 'Geometry collection not expected: {}'.format(line.type)
extext = 'Geometry collection not expected: {}'.format(line.geom_type)
raise InvalidGeometryError(extext)

assert line.type == 'MultiLineString', 'Should be MultiLineString'
assert line.geom_type == 'MultiLineString', 'Should be MultiLineString'
width = np.sum([l.length for l in line.geoms])

return width, line
Expand Down
4 changes: 2 additions & 2 deletions oggm/core/gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def project_coarse(x, y, c=2):
'OGGM.')

# sometimes the glacier gets cut out in parts
if tmp.type == 'MultiPolygon':
if tmp.geom_type == 'MultiPolygon':
# If only small arms are cut out, remove them
area = np.array([_tmp.area for _tmp in tmp.geoms])
_tokeep = np.argmax(area).item()
Expand All @@ -211,7 +211,7 @@ def project_coarse(x, y, c=2):
for b in np.arange(0., 1., 0.01):
tmp = shapely.ops.transform(project, polygon.buffer(b))
tmp = tmp.buffer(0)
if tmp.type == 'MultiPolygon':
if tmp.geom_type == 'MultiPolygon':
continue
if tmp.is_valid:
break
Expand Down
22 changes: 11 additions & 11 deletions oggm/utils/_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,25 +307,25 @@ def line_interpol(line, dx):
while True:
pref = points[-1]
pbs = pref.buffer(dx).boundary.intersection(line)
if pbs.type == 'Point':
if pbs.geom_type == 'Point':
pbs = [pbs]
elif pbs.type == 'LineString':
elif pbs.geom_type == 'LineString':
# This is rare
pbs = [shpg.Point(c) for c in pbs.coords]
assert len(pbs) == 2
elif pbs.type == 'GeometryCollection':
elif pbs.geom_type == 'GeometryCollection':
# This is rare
opbs = []
for p in pbs.geoms:
if p.type == 'Point':
if p.geom_type == 'Point':
opbs.append(p)
elif p.type == 'LineString':
elif p.geom_type == 'LineString':
opbs.extend([shpg.Point(c) for c in p.coords])
pbs = opbs
else:
if pbs.type != 'MultiPoint':
if pbs.geom_type != 'MultiPoint':
raise RuntimeError('line_interpol: we expect a MultiPoint '
'but got a {}.'.format(pbs.type))
'but got a {}.'.format(pbs.geom_type))

try:
# Shapely v2 compat
Expand Down Expand Up @@ -527,7 +527,7 @@ def polygon_intersections(gdf):
if not isinstance(line, shpg.linestring.LineString):
raise RuntimeError('polygon_intersections: we expect'
'a LineString but got a '
'{}.'.format(line.type))
'{}.'.format(line.geom_type))
line = gpd.GeoDataFrame([[i, j, line]],
columns=out_cols)
out = pd.concat([out, line])
Expand All @@ -553,10 +553,10 @@ def multipolygon_to_polygon(geometry, gdir=None):
# Log
rid = gdir.rgi_id + ': ' if gdir is not None else ''

if 'Multi' in geometry.type:
if 'Multi' in geometry.geom_type:
parts = np.array(geometry)
for p in parts:
assert p.type == 'Polygon'
assert p.geom_type == 'Polygon'
areas = np.array([p.area for p in parts])
parts = parts[np.argsort(areas)][::-1]
areas = areas[np.argsort(areas)][::-1]
Expand All @@ -580,7 +580,7 @@ def multipolygon_to_polygon(geometry, gdir=None):
if np.any(areas[1:] > (areas[0] / 4)):
log.info('Geometry {} lost quite a chunk.'.format(rid))

if geometry.type != 'Polygon':
if geometry.geom_type != 'Polygon':
raise InvalidGeometryError('Geometry {} is not a Polygon.'.format(rid))
return geometry

Expand Down
4 changes: 2 additions & 2 deletions oggm/utils/_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ def get_centerline_lonlat(gdir,

# Intersect with exterior geom
line = line.intersection(exterior)
if line.type in ['MultiLineString', 'GeometryCollection']:
if line.geom_type in ['MultiLineString', 'GeometryCollection']:
# Take the longest
lens = [il.length for il in line.geoms]
line = line.geoms[np.argmax(lens)]
Expand Down Expand Up @@ -876,7 +876,7 @@ def write_centerlines_to_shape(gdirs, *, path=True, to_tar=False,
odf = odf.sort_values(by='RGIID')
odf.crs = to_crs
# Sanity checks to avoid bad surprises
gtype = np.array([g.type for g in odf.geometry])
gtype = np.array([g.geom_type for g in odf.geometry])
if 'GeometryCollection' in gtype:
errdf = odf.loc[gtype == 'GeometryCollection']
with warnings.catch_warnings():
Expand Down

0 comments on commit 2f65736

Please sign in to comment.