Skip to content

Commit

Permalink
Merge pull request #250 from ASFHyP3/add-ruff
Browse files Browse the repository at this point in the history
Use `ruff` Instead of `flake8` for Static Analysis
  • Loading branch information
jtherrmann authored Dec 16, 2024
2 parents 9156bcf + 79a91d7 commit 6c2a6ad
Show file tree
Hide file tree
Showing 35 changed files with 999 additions and 546 deletions.
25 changes: 5 additions & 20 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,10 @@ name: Static analysis
on: push

jobs:
flake8:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: "3.10"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 flake8-import-order flake8-blind-except flake8-builtins
- name: Lint with flake8
run: |
flake8 --max-line-length=120 --import-order-style=pycharm --statistics \
--application-import-names asf_tools ArcGIS-toolbox/ASF_Tools.pyt src/asf_tools
call-secrets-analysis-workflow:
# Docs: https://github.com/ASFHyP3/actions
uses: ASFHyP3/actions/.github/workflows/[email protected]

call-ruff-workflow:
# Docs: https://github.com/ASFHyP3/actions
uses: ASFHyP3/actions/.github/workflows/[email protected]
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Fixed
- The [`release`](.github/workflows/release.yml) Github Actions workflow now uses the `gh` CLI instead of the archived `repo-sync/pull-request` action.

### Changed
- The [`static-analysis`](.github/workflows/static-analysis.yml) Github Actions workflow now uses `ruff` rather than `flake8`.

## [0.8.0]

### Removed
Expand Down
5 changes: 1 addition & 4 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ dependencies:
# For packaging, and testing
# - arcpy # windows only
- python-build
- flake8
- flake8-import-order
- flake8-blind-except
- flake8-builtins
- ruff
- setuptools>=61
- setuptools_scm>=6.2
- pytest
Expand Down
5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ flood_map = "asf_tools.hydrosar.flood_map:hyp3"

[project.optional-dependencies]
develop = [
"flake8",
"flake8-import-order",
"flake8-blind-except",
"flake8-builtins",
"gdal-utils",
"ruff",
"pytest",
"pytest-cov",
"pytest-console-scripts",
Expand Down
6 changes: 6 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
exclude = ["prototype"]

line-length = 120

[format]
quote-style = "single"
10 changes: 5 additions & 5 deletions src/asf_tools/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
def main():
parser = argparse.ArgumentParser(prefix_chars='+', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'++process', choices=['water_map', 'flood_map'], default='water_map',
help='Select the HyP3 entrypoint to use' # HyP3 entrypoints are specified in `pyproject.toml`
'++process',
choices=['water_map', 'flood_map'],
default='water_map',
help='Select the HyP3 entrypoint to use', # HyP3 entrypoints are specified in `pyproject.toml`
)

args, unknowns = parser.parse_known_args()
# NOTE: Cast to set because of: https://github.com/pypa/setuptools/issues/3649
(process_entry_point,) = set(entry_points(group='hyp3', name=args.process))

sys.argv = [args.process, *unknowns]
sys.exit(
process_entry_point.load()()
)
sys.exit(process_entry_point.load()())


if __name__ == '__main__':
Expand Down
9 changes: 1 addition & 8 deletions src/asf_tools/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@


def get_tag_set() -> dict:
tag_set = {
'TagSet': [
{
'Key': 'file_type',
'Value': 'product'
}
]
}
tag_set = {'TagSet': [{'Key': 'file_type', 'Value': 'product'}]}
return tag_set


Expand Down
55 changes: 41 additions & 14 deletions src/asf_tools/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,27 @@ def reproject_to_target(raster_info: dict, target_epsg_code: int, target_resolut
log.info(f'Reprojecting {raster}')
reprojected_raster = os.path.join(directory, os.path.basename(raster))
gdal.Warp(
reprojected_raster, raster, dstSRS=f'EPSG:{target_epsg_code}',
xRes=target_resolution, yRes=target_resolution, targetAlignedPixels=True
reprojected_raster,
raster,
dstSRS=f'EPSG:{target_epsg_code}',
xRes=target_resolution,
yRes=target_resolution,
targetAlignedPixels=True,
)

area_raster = get_area_raster(raster)
log.info(f'Reprojecting {area_raster}')
reprojected_area_raster = os.path.join(directory, os.path.basename(area_raster))
gdal.Warp(
reprojected_area_raster, area_raster, dstSRS=f'EPSG:{target_epsg_code}',
xRes=target_resolution, yRes=target_resolution, targetAlignedPixels=True
reprojected_area_raster,
area_raster,
dstSRS=f'EPSG:{target_epsg_code}',
xRes=target_resolution,
yRes=target_resolution,
targetAlignedPixels=True,
)

target_raster_info[reprojected_raster] = gdal.Info(reprojected_raster, format='json')
target_raster_info[reprojected_raster] = gdal.Info(reprojected_raster, format='json')
else:
log.info(f'No need to reproject {raster}')
target_raster_info[raster] = info
Expand Down Expand Up @@ -173,8 +181,12 @@ def make_composite(out_name: str, rasters: List[str], resolution: float = None):

# resample rasters to maximum resolution & common UTM zone
with TemporaryDirectory(prefix='reprojected_') as temp_dir:
raster_info = reproject_to_target(raster_info, target_epsg_code=target_epsg_code, target_resolution=resolution,
directory=temp_dir)
raster_info = reproject_to_target(
raster_info,
target_epsg_code=target_epsg_code,
target_resolution=resolution,
directory=temp_dir,
)

# Get extent of union of all images
full_ul, full_lr, full_trans = get_full_extent(raster_info)
Expand All @@ -188,8 +200,10 @@ def make_composite(out_name: str, rasters: List[str], resolution: float = None):

for raster, info in raster_info.items():
log.info(f'Processing raster {raster}')
log.debug(f"Raster upper left: {info['cornerCoordinates']['upperLeft']}; "
f"lower right: {info['cornerCoordinates']['lowerRight']}")
log.debug(
f"Raster upper left: {info['cornerCoordinates']['upperLeft']}; "
f"lower right: {info['cornerCoordinates']['lowerRight']}"
)

values = read_as_array(raster)

Expand Down Expand Up @@ -224,7 +238,13 @@ def make_composite(out_name: str, rasters: List[str], resolution: float = None):
out_raster = write_cog(f'{out_name}.tif', outputs, full_trans, target_epsg_code, nodata_value=0)
del outputs

out_counts_raster = write_cog(f'{out_name}_counts.tif', counts, full_trans, target_epsg_code, dtype=gdal.GDT_Int16)
out_counts_raster = write_cog(
f'{out_name}_counts.tif',
counts,
full_trans,
target_epsg_code,
dtype=gdal.GDT_Int16,
)
del counts

return out_raster, out_counts_raster
Expand All @@ -237,14 +257,21 @@ def main():
)
parser.add_argument('out_name', help='Base name of output composite GeoTIFF (without extension)')
parser.add_argument('rasters', nargs='+', help='Sentinel-1 GeoTIFF rasters to composite')
parser.add_argument('-r', '--resolution', type=float,
help='Desired output resolution in meters '
'(default is the max resolution of all the input files)')
parser.add_argument(
'-r',
'--resolution',
type=float,
help='Desired output resolution in meters ' '(default is the max resolution of all the input files)',
)
parser.add_argument('-v', '--verbose', action='store_true', help='Turn on verbose logging')
args = parser.parse_args()

level = logging.DEBUG if args.verbose else logging.INFO
logging.basicConfig(stream=sys.stdout, format='%(asctime)s - %(levelname)s - %(message)s', level=level)
logging.basicConfig(
stream=sys.stdout,
format='%(asctime)s - %(levelname)s - %(message)s',
level=level,
)
log.debug(' '.join(sys.argv))
log.info(f'Creating a composite of {len(args.rasters)} rasters')

Expand Down
3 changes: 2 additions & 1 deletion src/asf_tools/dem.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Prepare a Copernicus GLO-30 DEM virtual raster (VRT) covering a given geometry"""

from pathlib import Path
from typing import Union

Expand Down Expand Up @@ -31,7 +32,7 @@ def prepare_dem_vrt(vrt: Union[str, Path], geometry: Union[ogr.Geometry, BaseGeo
geometry = ogr.CreateGeometryFromWkb(geometry.wkb)

min_lon, max_lon, _, _ = geometry.GetEnvelope()
if min_lon < -160. and max_lon > 160.:
if min_lon < -160.0 and max_lon > 160.0:
raise ValueError(f'asf_tools does not currently support geometries that cross the antimeridian: {geometry}')

tile_features = vector.get_features(DEM_GEOJSON)
Expand Down
5 changes: 2 additions & 3 deletions src/asf_tools/hydrosar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from warnings import warn

HYDROSAR_MOVE_WARNING = \
"""
HYDROSAR_MOVE_WARNING = """
---------------------------------------------------------------------------
The HydroSAR codes (`flood_map`, `water_map` and `hand` modules) are being
moved to the HydroSAR project repository:
Expand All @@ -12,4 +11,4 @@
----------------------------------------------------------------------------
"""

warn(HYDROSAR_MOVE_WARNING, category=FutureWarning)
warn(HYDROSAR_MOVE_WARNING, category=FutureWarning)
Loading

0 comments on commit 6c2a6ad

Please sign in to comment.