From 40a00ce8602a25cf456c8b54460ba89f25ea4fea Mon Sep 17 00:00:00 2001 From: Markus Reinert Date: Tue, 14 Nov 2023 09:25:44 +0100 Subject: [PATCH] Move all imports to the beginning of the file It is common in Python to put all import-statements at the beginning of the code file. The script `discharge.py` contained an import within the function `p2mp` within the method `outlets`; this import is now moved to the top. Having moved the import to the top, the function `p2mp` served merely as a synonym for `unary_union`, so for simplicity, both calls to `p2mp` were replaced with `unary_union` and `p2mp` was removed. --- discharge.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/discharge.py b/discharge.py index 9f95ae6..15ddfbb 100644 --- a/discharge.py +++ b/discharge.py @@ -7,9 +7,11 @@ import xarray as xr import geopandas as gp from shapely.geometry import Point, Polygon +from shapely.ops import unary_union from argparse import ArgumentParser import fiona + fiona.drvsupport.supported_drivers["KML"] = "rw" # https://gis.stackexchange.com/a/258370/609 fiona.drvsupport.supported_drivers["LIBKML"] = "rw" # https://gis.stackexchange.com/a/258370/609 @@ -85,19 +87,16 @@ def outlets(self): # [aaaa] represents one basins with two parts, so it gets 2 table rows. We want 1. # df.groupby('id').first() solves this, except for the basin column that needs a custom aggregate fuction. # except we need to aggregate the 'basin' column to convert to multipolygon - def p2mp(da): # polygon to multipolygon - from shapely.ops import unary_union # cascaded_union deprecated, replace with unary_union - return unary_union(da) # https://stackoverflow.com/questions/36774049/ - + # See also https://stackoverflow.com/questions/36774049/ for key in self._outlets.keys(): if self._outlets[key] is not None: aggdict = dict(zip(self._outlets[key].columns,['first']*self._outlets[key].columns.size)) - aggdict['basin'] = p2mp + aggdict['basin'] = unary_union self._outlets[key] = self._outlets[key].groupby('cat').agg(aggdict) for key in self._outlets_u.keys(): if self._outlets_u[key] is not None: aggdict = dict(zip(self._outlets_u[key].columns,['first']*self._outlets_u[key].columns.size)) - aggdict['basin'] = p2mp + aggdict['basin'] = unary_union self._outlets_u[key] = self._outlets_u[key].groupby('cat').agg(aggdict) # Return datastructure