ncssar / sartopo_python

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

crop error of real track #43

Closed caver456 closed 2 years ago

caver456 commented 2 years ago

image

First guess: maybe this is due to a flaw in removeSpurs.. The log shows that several spurs were removed, including at coordinates very close to the split. Try the same crop operation without doing removeSpurs first.

caver456 commented 2 years ago

commented out the removeSpurs call for the tracks, and the crop error persists - for all tracks:

image

That's more than a coincidence... looking in the log, there's now just one occurrence of 'spur removed' and it appears to be talking about a spur on the boundary shape at the opposite corner of the assignment:

image

That might or might not be relevant. But - take a look at the corner below the bad crops - it's a crossover, so that is probably where the problem resides. Looks like the 'beyond' argument is resulting in an inward growth where the crossover happens, rather than an outward growth. So - need to look at the coordinates at each step around the buffer command, around line 1961:

        if boundaryType=='Polygon':
            cgc=cg['coordinates'][0]
            cgc=self.removeSpurs(cgc)
            boundaryGeom=Polygon(cgc).buffer(beyond) # Shapely object
caver456 commented 2 years ago

Sure enough, adding the grown boundary to the sartopo map shows it:

image

caver456 commented 2 years ago

The shapely docs say that doing a buffer of zero will split bowties into two polygons at the crossover point. So, split the bowties, do a buffer on each resulting polygon, then OR the results together into one polygon.

Holy cow that worked:

image

    def buffer2(self,boundaryGeom,beyond):
        a=boundaryGeom.buffer(0) # split bowties into separate polygons
        merged=unary_union(a)
        return merged.buffer(beyond)

...
in crop:
            # boundaryGeom=Polygon(cgc).buffer(beyond) # Shapely object
            boundaryGeom=self.buffer2(Polygon(cgc),beyond)
caver456 commented 2 years ago

if this fails in the future, note that the shapely docs say that results may vary when trying to fix bowties using zero size buffer and also when using unary_union, and that some users reported better success when using small positive values. So, if needed, could try progressively larger buffer values until is_valid is true for each resulting polygon, or something to that effect.