Skip to content

Commit edd63dc

Browse files
committed
Fix using Ruff
1 parent d861194 commit edd63dc

File tree

6 files changed

+56
-44
lines changed

6 files changed

+56
-44
lines changed

geospatial_tools/geotools_types.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""This module contains constants and functions pertaining to data types."""
22

3-
from typing import Union
4-
53
from shapely.geometry import (
64
GeometryCollection,
75
LineString,
@@ -13,4 +11,4 @@
1311
)
1412

1513
BBoxLike = tuple[float, float, float, float]
16-
IntersectsLike = Union[Point, Polygon, LineString, MultiPolygon, MultiPoint, MultiLineString, GeometryCollection]
14+
IntersectsLike = Point | Polygon | LineString | MultiPolygon | MultiPoint | MultiLineString | GeometryCollection

geospatial_tools/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ def create_crs(dataset_crs: str | int, logger=LOGGER):
182182
return CRS.from_epsg(recovered_code)
183183

184184
logger.error(f"Encountered problem while trying to format EPSG code from input : [{dataset_crs}]")
185+
return None
185186

186187

187188
def download_url(url: str, filename: str | pathlib.Path, overwrite: bool = False, logger=LOGGER) -> pathlib.Path | None:

geospatial_tools/vector.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,13 @@ def add_and_fill_contained_column(
458458
logger.info(f"Selecting all vector features that are within {feature_name}")
459459
selected_features = select_all_within_feature(polygon_feature=polygon_feature, vector_features=vector_features)
460460
logger.info(f"Writing [{feature_name}] to selected vector features")
461-
[ # pylint: disable=W0106
462-
vector_features.at[idx, vector_column_name].add(feature_name) for idx in selected_features.index
463-
]
461+
462+
vector_features.loc[selected_features.index, vector_column_name] = vector_features.loc[
463+
selected_features.index, vector_column_name
464+
].apply(lambda s: s | {feature_name})
464465

465466

467+
# Potential outdated function
466468
def find_and_write_all_contained_features(
467469
polygon_features: gpd.GeoDataFrame,
468470
polygon_column: str,

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,16 @@ ignore = [
203203
"E266", # too many leading '#' for comments
204204
"E501", # line too long (we enforce via line-length instead)
205205
"RET504",
206-
"RUF013"
206+
"RUF013",
207+
"PTH123"
207208
]
208209

209210
[tool.ruff.lint.pydocstyle]
210211
convention = "numpy" # Corresponds to flake8's docstring-convention and docformatter style.
211212

213+
[tool.ruff.lint.pylint]
214+
max-args = 16
215+
212216
[tool.ruff.lint.mccabe]
213217
# cyclomatic complexity
214218
max-complexity = 18

tests/test_notebooks/test_planetary_computer_sentinel2.ipynb

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,8 +1810,9 @@
18101810
"\n",
18111811
"intersect_polygons_subset_filename = TEST_TMP_DIR / \"intersecting_polygon_grid_10km_subset.gpkg\"\n",
18121812
"print(\"Starting intersect selection\")\n",
1813-
"intersect_polygons_subset = select_polygons_by_location(intersecting_polygons, s2_grid_subset, predicate=\"within\",\n",
1814-
" num_of_workers=4)\n",
1813+
"intersect_polygons_subset = select_polygons_by_location(\n",
1814+
" intersecting_polygons, s2_grid_subset, predicate=\"within\", num_of_workers=4\n",
1815+
")\n",
18151816
"# intersect_polygons_subset = gpd.read_file(intersect_polygons_subset_filename)\n",
18161817
"\n",
18171818
"# Optionally save to a new file\n",
@@ -1842,11 +1843,13 @@
18421843
"vector_column_name = \"s2_tiles\"\n",
18431844
"\n",
18441845
"# Initiating our client\n",
1845-
"best_products_client = BestProductsForFeatures(sentinel2_tiling_grid=s2_grid_subset,\n",
1846-
" sentinel2_tiling_grid_column=s2_feature_name_columns,\n",
1847-
" vector_features=intersect_polygons_subset,\n",
1848-
" vector_features_column=vector_column_name,\n",
1849-
" max_cloud_cover=15)"
1846+
"best_products_client = BestProductsForFeatures(\n",
1847+
" sentinel2_tiling_grid=s2_grid_subset,\n",
1848+
" sentinel2_tiling_grid_column=s2_feature_name_columns,\n",
1849+
" vector_features=intersect_polygons_subset,\n",
1850+
" vector_features_column=vector_column_name,\n",
1851+
" max_cloud_cover=15,\n",
1852+
")"
18501853
]
18511854
},
18521855
{
@@ -1970,7 +1973,7 @@
19701973
"\n",
19711974
"best_results_path = TEST_TMP_DIR / \"vector_tiles_with_s2tiles_subset.gpkg\"\n",
19721975
"best_results = best_products_client.select_best_products_per_feature()\n",
1973-
"to_geopackage(best_results, best_results_path)\n"
1976+
"to_geopackage(best_results, best_results_path)"
19741977
]
19751978
},
19761979
{
@@ -2201,10 +2204,9 @@
22012204
"download_directory = TEST_TMP_DIR / \"example_s2_download_and_processing\"\n",
22022205
"\n",
22032206
"for p in product_list:\n",
2204-
" processed_product = download_and_process_sentinel2_asset(product_id=p,\n",
2205-
" product_bands=bands,\n",
2206-
" base_directory=download_directory,\n",
2207-
" target_projection=5070)\n",
2207+
" processed_product = download_and_process_sentinel2_asset(\n",
2208+
" product_id=p, product_bands=bands, base_directory=download_directory, target_projection=5070\n",
2209+
" )\n",
22082210
"\n",
22092211
" product_asset_list.append(processed_product)"
22102212
]
@@ -2238,7 +2240,7 @@
22382240
"source": [
22392241
"for p in product_asset_list:\n",
22402242
" print(f\"Asset ID : [{p.asset_id}]\")\n",
2241-
" print(f\"Reprojected ID path : \\n[{p.reprojected_asset_path}]\\n\")\n"
2243+
" print(f\"Reprojected ID path : \\n[{p.reprojected_asset_path}]\\n\")"
22422244
]
22432245
},
22442246
{
@@ -2255,8 +2257,10 @@
22552257
"source": [
22562258
"# Here, we are creating a new Asset object simply for convenience, from the printed outputs above\n",
22572259
"\n",
2258-
"product = Asset(asset_id=\"S2A_MSIL2A_20240705T185921_R013_T10SDJ_20240706T050346\",\n",
2259-
" reprojected_asset=download_directory / \"S2A_MSIL2A_20240705T185921_R013_T10SDJ_20240706T050346_reprojected.tif\", )"
2260+
"product = Asset(\n",
2261+
" asset_id=\"S2A_MSIL2A_20240705T185921_R013_T10SDJ_20240706T050346\",\n",
2262+
" reprojected_asset=download_directory / \"S2A_MSIL2A_20240705T185921_R013_T10SDJ_20240706T050346_reprojected.tif\",\n",
2263+
")"
22602264
]
22612265
},
22622266
{
@@ -2298,7 +2302,6 @@
22982302
}
22992303
],
23002304
"source": [
2301-
"\n",
23022305
"s2_product_id = product.asset_id\n",
23032306
"product_path = product.reprojected_asset_path\n",
23042307
"product_id_series = group_by_product[group_by_product[\"best_s2_product_id\"] == s2_product_id]\n",
@@ -2340,10 +2343,12 @@
23402343
}
23412344
],
23422345
"source": [
2343-
"clip_list = clip_raster_with_polygon(raster_image=product_path,\n",
2344-
" polygon_layer=vector_features_path,\n",
2345-
" base_output_filename=s2_product_id,\n",
2346-
" output_dir=download_directory / \"test_sentinel2_clip\")"
2346+
"clip_list = clip_raster_with_polygon(\n",
2347+
" raster_image=product_path,\n",
2348+
" polygon_layer=vector_features_path,\n",
2349+
" base_output_filename=s2_product_id,\n",
2350+
" output_dir=download_directory / \"test_sentinel2_clip\",\n",
2351+
")"
23472352
]
23482353
},
23492354
{

tests/test_notebooks/test_stac_api_tools.ipynb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@
107107
"end_year = 2024\n",
108108
"start_month = 6\n",
109109
"end_month = 7\n",
110-
"date_ranges = create_date_range_for_specific_period(start_year=start_year,\n",
111-
" end_year=end_year,\n",
112-
" start_month_range=start_month,\n",
113-
" end_month_range=end_month)\n",
110+
"date_ranges = create_date_range_for_specific_period(\n",
111+
" start_year=start_year, end_year=end_year, start_month_range=start_month, end_month_range=end_month\n",
112+
")\n",
114113
"\n",
115114
"# Search arguments\n",
116115
"collection = \"sentinel-2-l2a\"\n",
@@ -122,10 +121,9 @@
122121
"\n",
123122
"# Searching for results\n",
124123
"search_client = StacSearch(PLANETARY_COMPUTER)\n",
125-
"search_client.search_for_date_ranges(date_ranges=date_ranges,\n",
126-
" collections=collection,\n",
127-
" query=query,\n",
128-
" sortby=sortby, limit=100)\n",
124+
"search_client.search_for_date_ranges(\n",
125+
" date_ranges=date_ranges, collections=collection, query=query, sortby=sortby, limit=100\n",
126+
")\n",
129127
"\n",
130128
"# Selecting optimal result\n",
131129
"search_client.sort_results_by_cloud_coverage()\n",
@@ -166,13 +164,17 @@
166164
"source": [
167165
"print(\"\\nSorted results\")\n",
168166
"for item in filtered_items:\n",
169-
" print(f\"Item: {item.id}, {item.datetime}, \"\n",
170-
" f\"Cloud cover: {item.properties['eo:cloud_cover']}, \"\n",
171-
" f\"Nodata: {item.properties['s2:nodata_pixel_percentage']}\")\n",
167+
" print(\n",
168+
" f\"Item: {item.id}, {item.datetime}, \"\n",
169+
" f\"Cloud cover: {item.properties['eo:cloud_cover']}, \"\n",
170+
" f\"Nodata: {item.properties['s2:nodata_pixel_percentage']}\"\n",
171+
" )\n",
172172
"\n",
173-
"print(f\"\\nOptimal result: {optimal_result.id}, {optimal_result.datetime}, \"\n",
174-
" f\"Cloud cover: {optimal_result.properties['eo:cloud_cover']}, \"\n",
175-
" f\"Nodata: {optimal_result.properties['s2:nodata_pixel_percentage']}\")"
173+
"print(\n",
174+
" f\"\\nOptimal result: {optimal_result.id}, {optimal_result.datetime}, \"\n",
175+
" f\"Cloud cover: {optimal_result.properties['eo:cloud_cover']}, \"\n",
176+
" f\"Nodata: {optimal_result.properties['s2:nodata_pixel_percentage']}\"\n",
177+
")"
176178
]
177179
},
178180
{
@@ -266,9 +268,9 @@
266268
],
267269
"source": [
268270
"merged = best_result.merge_asset(base_directory=file_base_path, delete_sub_items=True)\n",
269-
"reprojected = best_result.reproject_merged_asset(target_projection=5070,\n",
270-
" base_directory=file_base_path,\n",
271-
" delete_merged_asset=True)"
271+
"reprojected = best_result.reproject_merged_asset(\n",
272+
" target_projection=5070, base_directory=file_base_path, delete_merged_asset=True\n",
273+
")"
272274
]
273275
},
274276
{

0 commit comments

Comments
 (0)