Skip to content

Commit 2b956f9

Browse files
committed
fixed a shapely 2.0 incompatibility; modified a test to cover it. natcap#1657
1 parent e1698ab commit 2b956f9

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

HISTORY.rst

+11-7
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@ Unreleased Changes
4444
``pygeoprocessing.get_raster_info`` and
4545
``pygeoprocessing.get_vector_info``.
4646
https://github.com/natcap/invest/issues/1645
47-
* Forest Carbon Edge Effects
48-
* Updating vector reprojection to allow partial reprojection. Related to
49-
https://github.com/natcap/invest/issues/1645
50-
* Urban Nature Access
51-
* The model now works as expected when the user provides an LULC raster
52-
that does not have a nodata value defined.
53-
https://github.com/natcap/invest/issues/1293
5447
* Workbench
5548
* Several small updates to the model input form UI to improve usability
5649
and visual consistency (https://github.com/natcap/invest/issues/912).
@@ -64,6 +57,13 @@ Unreleased Changes
6457
(https://github.com/natcap/invest/issues/1609).
6558
* Improved error handling when a datastack cannot be saved with relative
6659
paths across drives (https://github.com/natcap/invest/issues/1608).
60+
* Coastal Vulnerability
61+
* Fixed a regression where an AOI with multiple features could raise a
62+
TypeError after intersecting with the landmass polygon.
63+
https://github.com/natcap/invest/issues/1657
64+
* Forest Carbon Edge Effects
65+
* Updating vector reprojection to allow partial reprojection. Related to
66+
https://github.com/natcap/invest/issues/1645
6767
* Habitat Quality
6868
* Access raster is now generated from the reprojected access vector
6969
(https://github.com/natcap/invest/issues/1615).
@@ -72,6 +72,10 @@ Unreleased Changes
7272
* Urban Flood Risk
7373
* Fields present on the input AOI vector are now retained in the output.
7474
(https://github.com/natcap/invest/issues/1600)
75+
* Urban Nature Access
76+
* The model now works as expected when the user provides an LULC raster
77+
that does not have a nodata value defined.
78+
https://github.com/natcap/invest/issues/1293
7579

7680
3.14.2 (2024-05-29)
7781
-------------------

src/natcap/invest/coastal_vulnerability.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from osgeo import gdal
2222
from osgeo import ogr
2323
from osgeo import osr
24+
from shapely.geometry import LineString, MultiLineString
2425
from shapely.geometry.base import BaseMultipartGeometry
2526
from shapely.strtree import STRtree
2627

@@ -1104,7 +1105,6 @@ def prepare_landmass_line_index_and_interpolate_shore_points(
11041105
# Get shapely geometries from landmass
11051106
landmass_polygon_shapely_list = _ogr_to_geometry_list(landmass_vector_path)
11061107
landmass_shapely = shapely.ops.unary_union(landmass_polygon_shapely_list)
1107-
11081108
landmass_polygon_shapely_list = None
11091109

11101110
# store polygon geom for point-in-poly check later in ray-casting
@@ -1170,19 +1170,13 @@ def prepare_landmass_line_index_and_interpolate_shore_points(
11701170
if aoi_shapely_prepped.intersects(landmass_line):
11711171
intersected_shapely_geom = aoi_shapely.intersection(
11721172
landmass_line)
1173-
if intersected_shapely_geom.geom_type == 'LineString':
1174-
lines_in_aoi_list.append(intersected_shapely_geom)
1175-
elif intersected_shapely_geom.geom_type == 'MultiLineString':
1176-
shapely_geom_explode = [
1177-
shapely.geometry.LineString(x)
1178-
for x in intersected_shapely_geom]
1179-
1180-
lines_in_aoi_list.extend(shapely_geom_explode)
1181-
else:
1182-
# intersection could generate a point geom
1183-
# or if somehow the intersection is empty,
1184-
# type will be GeometryCollection.
1185-
continue
1173+
# intersection could generate a point geom,
1174+
# or if somehow the intersection is empty,
1175+
# type will be GeometryCollection.
1176+
if isinstance(intersected_shapely_geom,
1177+
(LineString, MultiLineString)):
1178+
lines_in_aoi_list.extend(
1179+
_list_geometry(intersected_shapely_geom))
11861180

11871181
# if none of the lines were disjoint before this linemerge,
11881182
# unioned_line will now be a LineString.

tests/test_coastal_vulnerability.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1184,17 +1184,22 @@ def test_zero_shorepoints_created(self):
11841184
def test_aoi_multiple_features(self):
11851185
"""CV: test shore point creation in AOI with multiple features."""
11861186
from natcap.invest import coastal_vulnerability
1187-
workspace_dir = self.workspace_dir
1187+
# workspace_dir = self.workspace_dir
1188+
workspace_dir = 'scratch/cv_test'
11881189

11891190
aoi_path = os.path.join(workspace_dir, 'aoi.geojson')
11901191
srs = osr.SpatialReference()
11911192
srs.ImportFromEPSG(26910) # UTM Zone 10N
11921193
wkt = srs.ExportToWkt()
1194+
# These two disjoint AOI polygons intersect the same landmass line
1195+
# segment. This tests an edge case where a MultiLineString
1196+
# geometry is created when landmass lines are clipped by the AOI.
11931197
poly_a = Polygon([
11941198
(-200, -200), (-100, -200), (-100, -100), (-200, -100),
11951199
(-200, -200)])
11961200
poly_b = Polygon([
1197-
(100, 100), (200, 100), (200, 200), (100, 200), (100, 100)])
1201+
(100, -200), (200, -200), (200, -100), (100, -100),
1202+
(100, -200)])
11981203

11991204
pygeoprocessing.shapely_geometry_to_vector(
12001205
[poly_a, poly_b], aoi_path, wkt, 'GeoJSON')

0 commit comments

Comments
 (0)