ErikNixdorf / sbat

The Repo for the Surface Water Balance Analysis Tool
BSD 3-Clause "New" or "Revised" License
3 stars 0 forks source link

TypeError after calculating water balance #11

Closed MarcoHannemann closed 1 year ago

MarcoHannemann commented 1 year ago

I have yet to understand what the program does in detail, but want to make it run once first. Assumed #7 and #8 are fixed, the program will still not run and throws a TypeError related to some geometric object. Any idea what is happening here?


`Water balance added to gauge guben_sprucke`

Traceback (most recent call last):
  File "/home/hannemam/Projects/sbat/sbat/sbat.py", line 525, in <module>
    main()
  File "/home/hannemam/Projects/sbat/sbat/sbat.py", line 506, in main
    sbat.get_recession_curve()
  File "/home/hannemam/Projects/sbat/sbat/sbat.py", line 298, in get_recession_curve
    self.get_water_balance(flow_type=self.config['recession']['curve_data']['flow_type'])
  File "/home/hannemam/Projects/sbat/sbat/sbat.py", line 484, in get_water_balance
    self.sections_meta, self.q_diff, self.gdf_network_map, self.section_basins = get_section_water_balance(
  File "/home/hannemam/Projects/sbat/sbat/waterbalance/waterbalance.py", line 682, in get_section_water_balance
    gdf_network_map = map_network_sections(network_dict=gauges_connection_dict,
  File "/home/hannemam/Projects/sbat/sbat/waterbalance/waterbalance.py", line 398, in map_network_sections
    section_line = LineString(MultiPoint(river_pnts.geometry.to_list()))
  File "/home/hannemam/Projects/envs/sbat/lib/python3.9/site-packages/shapely/geometry/linestring.py", line 66, in __new__
    coordinates = [_coords(o) for o in coordinates]
TypeError: 'MultiPoint' object is not iterable```
MarcoHannemann commented 1 year ago

Can you reproduce this error? The following code in waterbalance.py is responsible:

> section_line = LineString(MultiPoint(river_pnts.geometry.to_list()))

The LineString() is not able to iterate over the MultiPoint object. When removing the conversion from List to Multipoint, the LineString is successfully built:

> section_line = LineString(river_pnts.geometry.to_list())
LINESTRING Z (476034.453 5758002.275 49.016, 475985.295 5757993.422 49.275,...>

Happens again here

MarcoHannemann commented 1 year ago

I could identify the problem:

In the current master environment, an old Version of Shapely==1.7.1 is used. In the fresh environment I created using the current requirements.txt, the updated shapely==2.0.1 is used.

Shapely had a major release with Version 2.0, that made some fundamental changes. With Shapely 2.0, Multi-part geometries such as MultiPoint are not longer implemented as sequences, so that they are not iterable anymore. This information is available here: Migrating to Shapely 1.8 / 2.0.

To fix this, I would recommend to upgrade to Shapely 2.0.1 if there is no other functionality in the code that depends on an older version of Shapely. If that's not the case, I suggest to remove the Multipoint conversion as in the code above. I can confirm the code runs without problems after the change, @ErikNixdorf would have to check if the Linestring object stays the same.

ErikNixdorf commented 1 year ago

Can you reproduce this error? The following code in waterbalance.py is responsible:

> section_line = LineString(MultiPoint(river_pnts.geometry.to_list()))

The LineString() is not able to iterate over the MultiPoint object. When removing the conversion from List to Multipoint, the LineString is successfully built:

> section_line = LineString(river_pnts.geometry.to_list())
LINESTRING Z (476034.453 5758002.275 49.016, 475985.295 5757993.422 49.275,...>

Happens again here

I got the same error after upgrading shapely on my local machine