ncssar / sartopo_python

Python calls for the caltopo / sartopo API
GNU General Public License v3.0
14 stars 2 forks source link

deal with ShapelyDeprecationWarning messages #36

Closed caver456 closed 1 month ago

caver456 commented 2 years ago

During crop:

C:\Users\caver\Documents\GitHub\plans_console\sartopo_python.py:1375: ShapelyDeprecationWarning: getitem for multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the geoms property to access the constituent parts of a multi-part geometry. for r in result[1:]: C:\Users\caver\Documents\GitHub\plans_console\sartopo_python.py:1375: ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the geoms property to access the constituent parts of a multi-part geometry. for r in result[1:]:

caver456 commented 2 years ago

updated line number in the code = 2026 (in 'crop') = the last line of the snippet below, 'for r in result[1:]:'

 elif isinstance(result,MultiLineString):
            rids.append(self.editFeature(id=targetShape['id'],geometry={'coordinates':list(result[0].coords)}))
            if rids==[]:
                logging.error('crop: target shape not found; operation aborted.')
                return False
            suffix=0
            for r in result[1:]:
caver456 commented 4 months ago

It is time... now on Shapely 2.0.2 and the geometry operations seem to be failing. Discovered by Steve during testing of the signed GET issue.

caver456 commented 4 months ago

Not sure what the Feb 25 2022 comment refers to...

With Shapely 2.0.2, this is the error in question:

20:42:58 [sartopo_python:2489:CRITICAL] Uncaught exception:
Traceback (most recent call last):
  File "C:\Users\caver\Documents\GitHub\sartopo_python\sartopo_python\test.py", line 54, in <module>
    sts.cut('AB','zz') # cut area assignment using line
    ^^^^^^^^^^^^^^^^^^
  File "C:\Users\caver\Documents\GitHub\sartopo_python\sartopo_python\sartopo_python.py", line 1838, in cut
    rids.append(self.editFeature(id=targetShape['id'],geometry={'coordinates':[list(result[0].exterior.coords)]}))
                                                                                    ~~~~~~^^^
TypeError: 'MultiPolygon' object is not subscriptable
caver456 commented 4 months ago

Getting .geoms like so

        elif isinstance(result,MultiPolygon):
            ##### EDIT FEATURE is used to update the original feature information (geometry)
            resultGeoms=result.geoms

returns a sequence of polygons; trying to slice it like so

for r in resultGeoms[1:]:

returns this error:

21:16:58 [sartopo_python:2494:CRITICAL] Uncaught exception:
Traceback (most recent call last):
  File "C:\Users\caver\Documents\GitHub\sartopo_python\sartopo_python\test.py", line 54, in <module>
    sts.cut('AB','zz') # cut area assignment using line
    ^^^^^^^^^^^^^^^^^^
  File "C:\Users\caver\Documents\GitHub\sartopo_python\sartopo_python\sartopo_python.py", line 1844, in cut
    for r in resultGeoms[1:]:
TypeError: 'MultiPolygon' object is not iterable

Need to turn it into a list first:

for r in list(resultGeoms)[1:]:

which iterates as expected.

        elif isinstance(result,MultiPolygon):
            ##### EDIT FEATURE is used to update the original feature information (geometry)
            resultGeoms=result.geoms
            rids.append(self.editFeature(id=targetShape['id'],geometry={'coordinates':[list(resultGeoms[0].exterior.coords)]}))
            if rids==[]:
                logging.warning('cut: target shape not found; operation aborted.')
                return False
            for r in list(resultGeoms)[1:]:

Sending to Steve for testing, but, I also need to make sure the tests exercise all places in the code that this could happen. Found at least four spots searching for 'if.*Multi'

caver456 commented 1 month ago

After first commit: testing a simple crop:

15:29:41 [sartopo_python:2250:INFO] crop: target=z  boundary=AA
15:29:41 [sartopo_python:2496:CRITICAL] Uncaught exception:
Traceback (most recent call last):
  File "C:\Users\caver\Documents\GitHub\sartopo_python\sartopo_python\test.py", line 55, in <module>
    sts.crop('z','AA')
  File "C:\Users\caver\Documents\GitHub\sartopo_python\sartopo_python\sartopo_python.py", line 2282, in crop
    result=self.intersection2(targetGeom,boundaryGeom)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\caver\Documents\GitHub\sartopo_python\sartopo_python\sartopo_python.py", line 2085, in intersection2
    mpcoords=[(p.x,p.y) for p in mp]
             ^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'MultiPoint' object is not iterable
15:29:41 [sartopo_python:684:INFO] SartopoSession instance deleted for map PVTGT.
caver456 commented 1 month ago

all complex cut operations are working as expected