fimeval 0.1.52__py3-none-any.whl → 0.1.54__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
fimeval/utilis.py CHANGED
@@ -6,24 +6,27 @@ from pathlib import Path
6
6
  import geopandas as gpd
7
7
  from rasterio.warp import calculate_default_transform, reproject, Resampling
8
8
 
9
- #Lossless compression to reduce the file size
9
+
10
+ # Lossless compression to reduce the file size
10
11
  def compress_tif_lzw(tif_path):
11
12
  # Read original file
12
13
  with rasterio.open(tif_path) as src:
13
14
  profile = src.profile.copy()
14
15
  data = src.read()
15
- profile.update(compress='lzw')
16
+ profile.update(compress="lzw")
16
17
 
17
- with rasterio.open(tif_path, 'w', **profile) as dst:
18
+ with rasterio.open(tif_path, "w", **profile) as dst:
18
19
  dst.write(data)
19
20
 
20
- #Check whether it is a projected CRS
21
+
22
+ # Check whether it is a projected CRS
21
23
  def is_projected_crs(crs):
22
24
  return crs and crs.is_projected
23
25
 
24
- #Check if the FIM bounds are within the CONUS
26
+
27
+ # Check if the FIM bounds are within the CONUS
25
28
  def is_within_conus(bounds, crs=None):
26
- CONUS_BBOX = (-125, 24, -66.5, 49.5)
29
+ CONUS_BBOX = (-125, 24, -66.5, 49.5)
27
30
  left, bottom, right, top = bounds
28
31
 
29
32
  if crs and crs.is_projected:
@@ -38,7 +41,8 @@ def is_within_conus(bounds, crs=None):
38
41
  and top <= CONUS_BBOX[3]
39
42
  )
40
43
 
41
- #Reproject the FIMs to EPSG:5070 if withinUS and user doesnot define any target CRS, else user need to define it
44
+
45
+ # Reproject the FIMs to EPSG:5070 if withinUS and user doesnot define any target CRS, else user need to define it
42
46
  def reprojectFIMs(src_path, dst_path, target_crs):
43
47
  with rasterio.open(src_path) as src:
44
48
  if src.crs != target_crs:
@@ -46,14 +50,16 @@ def reprojectFIMs(src_path, dst_path, target_crs):
46
50
  src.crs, target_crs, src.width, src.height, *src.bounds
47
51
  )
48
52
  kwargs = src.meta.copy()
49
- kwargs.update({
50
- 'crs': target_crs,
51
- 'transform': transform,
52
- 'width': width,
53
- 'height': height
54
- })
55
-
56
- with rasterio.open(dst_path, 'w', **kwargs) as dst:
53
+ kwargs.update(
54
+ {
55
+ "crs": target_crs,
56
+ "transform": transform,
57
+ "width": width,
58
+ "height": height,
59
+ }
60
+ )
61
+
62
+ with rasterio.open(dst_path, "w", **kwargs) as dst:
57
63
  for i in range(1, src.count + 1):
58
64
  reproject(
59
65
  source=rasterio.band(src, i),
@@ -62,14 +68,15 @@ def reprojectFIMs(src_path, dst_path, target_crs):
62
68
  src_crs=src.crs,
63
69
  dst_transform=transform,
64
70
  dst_crs=target_crs,
65
- resampling=Resampling.nearest
71
+ resampling=Resampling.nearest,
66
72
  )
67
73
  else:
68
74
  print(f"Source raster is already in {target_crs}. No reprojection needed.")
69
75
  shutil.copy(src_path, dst_path)
70
76
  compress_tif_lzw(dst_path)
71
77
 
72
- #Resample into the coarser resoution amoung all FIMS within the case
78
+
79
+ # Resample into the coarser resoution amoung all FIMS within the case
73
80
  def resample_to_resolution(src_path, x_resolution, y_resolution):
74
81
  src_path = Path(src_path)
75
82
  print(src_path)
@@ -78,20 +85,15 @@ def resample_to_resolution(src_path, x_resolution, y_resolution):
78
85
 
79
86
  with rasterio.open(src_path) as src:
80
87
  transform = rasterio.transform.from_origin(
81
- src.bounds.left, src.bounds.top,
82
- x_resolution, y_resolution
88
+ src.bounds.left, src.bounds.top, x_resolution, y_resolution
83
89
  )
84
90
  width = int((src.bounds.right - src.bounds.left) / x_resolution)
85
91
  height = int((src.bounds.top - src.bounds.bottom) / y_resolution)
86
92
  kwargs = src.meta.copy()
87
- kwargs.update({
88
- 'transform': transform,
89
- 'width': width,
90
- 'height': height
91
- })
93
+ kwargs.update({"transform": transform, "width": width, "height": height})
92
94
 
93
95
  # Write to temporary file
94
- with rasterio.open(temp_path, 'w', **kwargs) as dst:
96
+ with rasterio.open(temp_path, "w", **kwargs) as dst:
95
97
  for i in range(1, src.count + 1):
96
98
  reproject(
97
99
  source=rasterio.band(src, i),
@@ -100,22 +102,23 @@ def resample_to_resolution(src_path, x_resolution, y_resolution):
100
102
  src_crs=src.crs,
101
103
  dst_transform=transform,
102
104
  dst_crs=src.crs,
103
- resampling=Resampling.nearest
105
+ resampling=Resampling.nearest,
104
106
  )
105
107
 
106
- os.remove(src_path) # delete original
107
- os.rename(temp_path, src_path)
108
+ os.remove(src_path) # delete original
109
+ os.rename(temp_path, src_path)
110
+
108
111
 
109
- #Check if the FIMs are in the same CRS or not else do further operation
112
+ # Check if the FIMs are in the same CRS or not else do further operation
110
113
  def MakeFIMsUniform(fim_dir, target_crs=None, target_resolution=None):
111
114
  fim_dir = Path(fim_dir)
112
- tif_files = list(fim_dir.glob('*.tif'))
115
+ tif_files = list(fim_dir.glob("*.tif"))
113
116
  if not tif_files:
114
117
  print(f"No TIFF files found in {fim_dir}")
115
118
  return
116
119
 
117
120
  # Create processing folder to save standardized files
118
- processing_folder = fim_dir / 'processing'
121
+ processing_folder = fim_dir / "processing"
119
122
  processing_folder.mkdir(exist_ok=True)
120
123
 
121
124
  # Collect info about each TIFF
@@ -131,7 +134,7 @@ def MakeFIMsUniform(fim_dir, target_crs=None, target_resolution=None):
131
134
  print(f"Error opening {tif_path}: {e}")
132
135
  return
133
136
 
134
- #CRS Check & Reproject if needed
137
+ # CRS Check & Reproject if needed
135
138
  all_projected = all(projected_flags)
136
139
  all_same_crs = len(set(crs_list)) == 1
137
140
 
@@ -143,22 +146,26 @@ def MakeFIMsUniform(fim_dir, target_crs=None, target_resolution=None):
143
146
  final_crs = "EPSG:5070"
144
147
  print(f"Defaulting to CONUS CRS: {final_crs}")
145
148
  else:
146
- print("Mixed or non-CONUS CRS detected. Please provide a valid target CRS.")
149
+ print(
150
+ "Mixed or non-CONUS CRS detected. Please provide a valid target CRS."
151
+ )
147
152
  return
148
-
153
+
149
154
  print(f"Reprojecting all rasters to {final_crs}")
150
155
  for src_path in tif_files:
151
156
  dst_path = processing_folder / src_path.name
152
157
  reprojectFIMs(str(src_path), str(dst_path), final_crs)
153
- compress_tif_lzw(dst_path)
154
-
158
+ compress_tif_lzw(dst_path)
159
+
155
160
  else:
156
- print("All rasters are in the same projected CRS. Copying to processing folder.")
161
+ print(
162
+ "All rasters are in the same projected CRS. Copying to processing folder."
163
+ )
157
164
  for src_path in tif_files:
158
165
  shutil.copy(src_path, processing_folder / src_path.name)
159
166
 
160
167
  # Resolution Check & Resample if needed
161
- processed_tifs = list(processing_folder.glob('*.tif'))
168
+ processed_tifs = list(processing_folder.glob("*.tif"))
162
169
  final_resolutions = []
163
170
  for tif_path in processed_tifs:
164
171
  with rasterio.open(tif_path) as src: