To read a COG (Cloud-Optimized GeoTIFF) file in Python, you can use the rasterio
library. It supports efficient reading of GeoTIFFs, including those optimized for cloud access (COG files).
Steps to Read a COG TIFF File
Install the rasterio
library:
pip install rasterio
Or install with requirements.txt
rasterio==1.4.3
Read the COG file:
Here’s an example of how to open and read data from a COG file:
import rasterio
# Open the COG file
cog_file_path = "path_to_your_cog_file.tif"
with rasterio.open(cog_file_path) as dataset:
# Print metadata
print("Metadata:", dataset.meta)
# Read the data as a NumPy array (e.g., the first band)
band1 = dataset.read(1)
# Print shape of the array
print("Band 1 shape:", band1.shape)
# Access geospatial transform
print("Transform:", dataset.transform)
# Access coordinate reference system (CRS)
print("CRS:", dataset.crs)
Work with the Data:
- You can process the NumPy array (
band1
) as required. - Use the metadata to understand the geospatial information.
Notes:
- Rasterio supports partial reads and efficient data access, which are key benefits of COG.
- Make sure you have network access if working with remote files.
This approach is ideal for reading and working with geospatial data stored in COG format.