Skip to content

Commit

Permalink
Adapt to shapely 1.8.0 and 2.0 (#543)
Browse files Browse the repository at this point in the history
* adapt to shapely 1.8.0 and 2.0

* finish updating np.bool to np.bool_
  • Loading branch information
maximlt authored Nov 23, 2021
1 parent 59ccc79 commit 3f5504a
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 74 deletions.
8 changes: 4 additions & 4 deletions geoviews/data/geom_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def has_holes(cls, dataset):
if isinstance(geom, Polygon) and geom.interiors:
return True
elif isinstance(geom, MultiPolygon):
for g in geom:
for g in geom.geoms:
if isinstance(g, Polygon) and g.interiors:
return True
return False
Expand All @@ -148,7 +148,7 @@ def holes(cls, dataset):
if isinstance(geom, Polygon):
return [[[geom_to_array(h) for h in geom.interiors]]]
elif isinstance(geom, MultiPolygon):
return [[[geom_to_array(h) for h in g.interiors] for g in geom]]
return [[[geom_to_array(h) for h in g.interiors] for g in geom.geoms]]
return []

@classmethod
Expand Down Expand Up @@ -276,9 +276,9 @@ def iloc(cls, dataset, index):

if isinstance(geom, MultiPoint):
if isscalar(rows) or isinstance(rows, slice):
geom = geom[rows]
geom = geom.geoms[rows]
elif isinstance(rows, (set, list)):
geom = MultiPoint([geom[r] for r in rows])
geom = MultiPoint([geom.geoms[r] for r in rows])
data['geometry'] = geom
return data

Expand Down
16 changes: 8 additions & 8 deletions geoviews/data/geopandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def has_holes(cls, dataset):
if isinstance(geom, Polygon) and geom.interiors:
return True
elif isinstance(geom, MultiPolygon):
for g in geom:
for g in geom.geoms:
if isinstance(g, Polygon) and g.interiors:
return True
return False
Expand All @@ -147,7 +147,7 @@ def holes(cls, dataset):
if isinstance(geom, Polygon) and geom.interiors:
holes.append([[geom_to_array(h) for h in geom.interiors]])
elif isinstance(geom, MultiPolygon):
holes += [[[geom_to_array(h) for h in g.interiors] for g in geom]]
holes += [[[geom_to_array(h) for h in g.interiors] for g in geom.geoms]]
else:
holes.append([[]])
return holes
Expand Down Expand Up @@ -208,7 +208,7 @@ def shape_mask(cls, dataset, selection):

@classmethod
def select_mask(cls, dataset, selection):
mask = np.ones(len(dataset.data), dtype=np.bool)
mask = np.ones(len(dataset.data), dtype=np.bool_)
for dim, k in selection.items():
if isinstance(k, tuple):
k = slice(*k)
Expand All @@ -233,7 +233,7 @@ def select_mask(cls, dataset, selection):
index_mask = arr == k
if dataset.ndims == 1 and np.sum(index_mask) == 0:
data_index = np.argmin(np.abs(arr - k))
mask = np.zeros(len(dataset), dtype=np.bool)
mask = np.zeros(len(dataset), dtype=np.bool_)
mask[data_index] = True
else:
mask &= index_mask
Expand Down Expand Up @@ -427,10 +427,10 @@ def iloc(cls, dataset, index):
count = 0
new_geoms, indexes = [], []
for i, geom in enumerate(geoms):
length = len(geom)
length = len(geom.geoms)
if np.isscalar(rows):
if count <= rows < (count+length):
new_geoms.append(geom[rows-count])
new_geoms.append(geom.geoms[rows-count])
indexes.append(i)
break
elif isinstance(rows, slice):
Expand All @@ -444,13 +444,13 @@ def iloc(cls, dataset, index):
dataset.param.warning(".iloc step slicing currently not supported for"
"the multi-tabular data format.")
indexes.append(i)
new_geoms.append(geom[start:stop])
new_geoms.append(geom.geoms[start:stop])
elif isinstance(rows, (list, set)):
sub_rows = [(r-count) for r in rows if count <= r < (count+length)]
if not sub_rows:
continue
indexes.append(i)
new_geoms.append(MultiPoint([geom[r] for r in sub_rows]))
new_geoms.append(MultiPoint([geom.geoms[r] for r in sub_rows]))
count += length

new = dataset.data.iloc[indexes].copy()
Expand Down
2 changes: 1 addition & 1 deletion geoviews/operation/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def _process_element(self, element):
np.isnan(edge_lengths)
)
if np.any(to_mask):
mask = np.zeros(zs.shape, dtype=np.bool)
mask = np.zeros(zs.shape, dtype=np.bool_)
mask[:, 1:][to_mask] = True
mask[:, 2:][to_mask[:, :-1]] = True
mask[:, :-1][to_mask] = True
Expand Down
207 changes: 163 additions & 44 deletions geoviews/tests/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,74 @@ def test_single_geom_conversion(self):
rects = Rectangles([(0, 0, 1, 1)])
geom = rects.geom()
self.assertIsInstance(geom, Polygon)
self.assertEqual(np.array(geom.array_interface_base['data']),
np.array([1, 0, 1, 1, 0, 1, 0, 0, 1, 0]))
self.assertEqual(
np.array(geom.exterior.coords),
np.array([
[1., 0.],
[1., 1.],
[0., 1.],
[0., 0.],
[1., 0.]
])
)

def test_multi_geom_conversion(self):
rects = Rectangles([(0, 0, 1, 1), (3, 2, 2.5, 1.5)])
geom = rects.geom()
self.assertIsInstance(geom, MultiPolygon)
self.assertEqual(len(geom), 2)
self.assertEqual(np.array(geom[0].array_interface_base['data']),
np.array([1, 0, 1, 1, 0, 1, 0, 0, 1, 0]))
self.assertEqual(np.array(geom[1].array_interface_base['data']),
np.array([2.5, 2, 2.5, 1.5, 3, 1.5, 3, 2, 2.5, 2]))
self.assertEqual(len(geom.geoms), 2)
self.assertEqual(
np.array(geom.geoms[0].exterior.coords),
np.array([
[1., 0.],
[1., 1.],
[0., 1.],
[0., 0.],
[1., 0.]
])
)
self.assertEqual(
np.array(geom.geoms[1].exterior.coords),
np.array([
[2.5, 2],
[2.5, 1.5],
[3, 1.5],
[3, 2],
[2.5, 2]
])
)

def test_geom_union(self):
rects = Rectangles([(0, 0, 1, 1), (1, 0, 2, 1)])
geom = rects.geom(union=True)
self.assertIsInstance(geom, Polygon)
array = np.array(geom.array_interface_base['data'])
array = np.array(geom.exterior.coords)
try:
self.assertEqual(array, np.array([0, 0, 0, 1, 1, 1, 2, 1, 2, 0, 1, 0, 0, 0]))
self.assertEqual(
array,
np.array([
[0, 0],
[0, 1],
[1, 1],
[2, 1],
[2, 0],
[1, 0],
[0, 0]
])
)
except Exception:
self.assertEqual(array, np.array([1, 0, 0, 0, 0, 1, 1, 1, 2, 1, 2, 0, 1, 0]))
self.assertEqual(
array,
np.array([
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[2, 1],
[2, 0],
[1, 0]
])
)

class TestPath(ComparisonTestCase):

Expand All @@ -56,19 +102,35 @@ def test_single_geom_conversion(self):
path = Path([[(0, 0), (1, 1), (2, 0)]])
geom = path.geom()
self.assertIsInstance(geom, LineString)
self.assertEqual(np.array(geom.array_interface_base['data']),
np.array([0, 0, 1, 1, 2, 0]))
self.assertEqual(
np.array(geom.coords),
np.array([
[0, 0],
[1, 1],
[2, 0]
])
)

def test_multi_geom_conversion(self):
path = Path([[(0, 0), (1, 1), (2, 0)], [(3, 2), (2.5, 1.5)]])
geom = path.geom()
self.assertIsInstance(geom, MultiLineString)
self.assertEqual(len(geom), 2)
self.assertEqual(np.array(geom[0].array_interface_base['data']),
np.array([0, 0, 1, 1, 2, 0]))
self.assertEqual(np.array(geom[1].array_interface_base['data']),
np.array([3, 2, 2.5, 1.5]))

self.assertEqual(len(geom.geoms), 2)
self.assertEqual(
np.array(geom.geoms[0].coords),
np.array([
[0, 0],
[1, 1],
[2, 0]
])
)
self.assertEqual(
np.array(geom.geoms[1].coords),
np.array([
[3, 2],
[2.5, 1.5]
])
)


class TestPolygons(ComparisonTestCase):
Expand All @@ -81,37 +143,79 @@ def test_single_geom_conversion(self):
path = Polygons([[(0, 0), (1, 1), (2, 0)]])
geom = path.geom()
self.assertIsInstance(geom, Polygon)
self.assertEqual(np.array(geom.array_interface_base['data']),
np.array([0, 0, 1, 1, 2, 0, 0, 0]))
self.assertEqual(
np.array(geom.exterior.coords),
np.array([
[0, 0],
[1, 1],
[2, 0],
[0, 0]
])
)

def test_single_geom_with_hole_conversion(self):
holes = [[((0.5, 0.2), (1, 0.8), (1.5, 0.2))]]
path = Polygons([{'x': [0, 1, 2], 'y': [0, 1, 0], 'holes': holes}], ['x', 'y'])
geom = path.geom()
self.assertIsInstance(geom, Polygon)
self.assertEqual(np.array(geom.exterior.array_interface_base['data']),
np.array([0, 0, 1, 1, 2, 0, 0, 0]))
self.assertEqual(
np.array(geom.exterior.coords),
np.array([
[0, 0],
[1, 1],
[2, 0],
[0, 0]
])
)
self.assertEqual(len(geom.interiors), 1)
self.assertIsInstance(geom.interiors[0], LinearRing)
self.assertEqual(np.array(geom.interiors[0].array_interface_base['data']),
np.array([0.5, 0.2, 1, 0.8, 1.5, 0.2, 0.5, 0.2]))
self.assertEqual(
np.array(geom.interiors[0].coords),
np.array([
[0.5, 0.2],
[1, 0.8],
[1.5, 0.2],
[0.5, 0.2]
])
)

def test_multi_geom_conversion(self):
holes = [[((0.5, 0.2), (1, 0.8), (1.5, 0.2))]]
path = Polygons([{'x': [0, 1, 2], 'y': [0, 1, 0], 'holes': holes},
{'x': [5, 6, 7], 'y': [2, 1, 2]}], ['x', 'y'])
geom = path.geom()
self.assertIsInstance(geom, MultiPolygon)
self.assertEqual(len(geom), 2)
self.assertEqual(np.array(geom[0].exterior.array_interface_base['data']),
np.array([0, 0, 1, 1, 2, 0, 0, 0]))
self.assertEqual(len(geom[0].interiors), 1)
self.assertIsInstance(geom[0].interiors[0], LinearRing)
self.assertEqual(np.array(geom[0].interiors[0].array_interface_base['data']),
np.array([0.5, 0.2, 1, 0.8, 1.5, 0.2, 0.5, 0.2]))
self.assertEqual(np.array(geom[1].exterior.array_interface_base['data']),
np.array([5, 2, 6, 1, 7, 2, 5, 2]))
self.assertEqual(len(geom[1].interiors), 0)
self.assertEqual(len(geom.geoms), 2)
self.assertEqual(
np.array(geom.geoms[0].exterior.coords),
np.array([
[0, 0],
[1, 1],
[2, 0],
[0, 0]
])
)
self.assertEqual(len(geom.geoms[0].interiors), 1)
self.assertIsInstance(geom.geoms[0].interiors[0], LinearRing)
self.assertEqual(
np.array(geom.geoms[0].interiors[0].coords),
np.array([
[0.5, 0.2],
[1, 0.8],
[1.5, 0.2],
[0.5, 0.2]
])
)
self.assertEqual(
np.array(geom.geoms[1].exterior.coords),
np.array([
[5, 2],
[6, 1],
[7, 2],
[5, 2]
])
)
self.assertEqual(len(geom.geoms[1].interiors), 0)



Expand All @@ -131,9 +235,9 @@ def test_multi_geom_conversion(self):
points = Points([(0, 0), (1, 2.5)])
geom = points.geom()
self.assertIsInstance(geom, MultiPoint)
self.assertEqual(len(geom), 2)
self.assertEqual(np.column_stack(geom[0].xy), np.array([[0, 0]]))
self.assertEqual(np.column_stack(geom[1].xy), np.array([[1, 2.5]]))
self.assertEqual(len(geom.geoms), 2)
self.assertEqual(np.column_stack(geom.geoms[0].xy), np.array([[0, 0]]))
self.assertEqual(np.column_stack(geom.geoms[1].xy), np.array([[1, 2.5]]))


class TestSegments(ComparisonTestCase):
Expand All @@ -146,15 +250,30 @@ def test_single_geom_conversion(self):
segs = Segments([(0, 0, 1, 1)])
geom = segs.geom()
self.assertIsInstance(geom, LineString)
self.assertEqual(np.array(geom.array_interface_base['data']),
np.array([0, 0, 1, 1]))
self.assertEqual(
np.array(geom.coords),
np.array([
[0, 0],
[1, 1]
])
)

def test_multi_geom_conversion(self):
segs = Segments([(0, 0, 1, 1), (1.5, 2, 3, 1)])
geom = segs.geom()
self.assertIsInstance(geom, MultiLineString)
self.assertEqual(len(geom), 2)
self.assertEqual(np.array(geom[0].array_interface_base['data']),
np.array([0, 0, 1, 1]))
self.assertEqual(np.array(geom[1].array_interface_base['data']),
np.array([1.5, 2, 3, 1]))
self.assertEqual(len(geom.geoms), 2)
self.assertEqual(
np.array(geom.geoms[0].coords),
np.array([
[0, 0],
[1, 1]
])
)
self.assertEqual(
np.array(geom.geoms[1].coords),
np.array([[
1.5, 2],
[3, 1]
])
)
Loading

0 comments on commit 3f5504a

Please sign in to comment.