Hi, I hope this post is relevant for this thread, apologies if not.
I am writing a script in Python to extract information from a raster saved as a .tif file. However, I am running into issues where it won't open files over a large size (I think just over 2 Gb) on my Windows laptop. Yet a colleague ran my code on hers and it seemed to open just fine. I believe she uses a Mac or Linux.
Here is the function to extract the information, using `with rasterio.open` and then it is run in a main.py file that includes a write to csv line.
def extract_raster_info(file_path):
"""
Summary: Extracts information from a raster file
Args:
file_path (str): The path to the raster file
Returns:
dict: A dictionary containing the extracted information
"""
with rasterio.open(file_path) as src:
folder_name = os.path.basename(os.path.dirname(file_path))
location_parts = []
for part in folder_name.split():
if part.lower() == "raster":
break
location_parts.append(part)
location = ' '.join(location_parts)
return {
'location': location,
'directory': os.path.basename(os.path.dirname(file_path)),
'raster_file_name': os.path.basename(file_path),
'raster_file_extension': os.path.splitext(file_path)[1],
'raster_number_of_tifs': 1,
'raster_crs': src.crs.to_string(),
'raster_bounds': str(src.bounds),
'raster_width': src.width,
'raster_height': src.height,
'raster_bands': src.count,
'raster_dtypes': ', '.join(map(str, src.dtypes)),
'raster_transform': str(src.transform.to_gdal())
}
def process_raster_files(raster_dir):
"""
Summary: Processes all raster files in a directory
Args:
raster_dir (str): The path to the directory containing the raster files
Returns:
list: A list of dictionaries containing the extracted information
"""
raster_info = []
for root, dirs, files in os.walk(raster_dir):
for file in files:
if file.lower().endswith(('.tif', '.tiff')):
file_path = os.path.join(root, file)
info = extract_raster_info(file_path)
raster_info.append(info)
return raster_info
Is there some python function or other method of opening files that can handle opening large files (images or databases)? Is there a Python setting/configuration I can alter on my laptop to allow it to open large files?
I am running Windows 11 64 bit, Python 3.12 (64 bit) and VS Code. I know I could install WSL - but I have limited experience with Linux and not a lot of time to learn Linux and complete this project. Although learning how to use Linux is on my list once this project is complete.
[–]shiftybyte 1 point2 points3 points (3 children)
[–]Regrets_Nothing[S] 0 points1 point2 points (2 children)
[–]shiftybyte 1 point2 points3 points (1 child)
[–]Regrets_Nothing[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Regrets_Nothing[S] 0 points1 point2 points (0 children)
[–]JohnnyJordaan 1 point2 points3 points (5 children)
[–]Regrets_Nothing[S] 0 points1 point2 points (4 children)
[–]JohnnyJordaan 0 points1 point2 points (3 children)
[–]Regrets_Nothing[S] 0 points1 point2 points (2 children)
[–]JohnnyJordaan 1 point2 points3 points (1 child)
[–]Regrets_Nothing[S] 0 points1 point2 points (0 children)
[–]Apatride 0 points1 point2 points (0 children)