The recent project involves using these two software, where meteorological factors are needed for Maxent. However, the future bioclimatic factors downloaded from WorldClim only come as a single tif file containing 19 bands, which needs to be manually split into 19 files. I couldn't find a method to split using ArcGIS, so I turned to Python.
The code is as follows:
import gdal
import os
input_file = "G:/4.其他/wc2.1_30s_bioc_ACCESS-CM2_ssp126_2041-2060.tif"#Downloaded tif file
output_folder = "G:/4.其他/feature_bio/"#Output folder
if not os.path.exists(output_folder):
os.makedirs(output_folder)
dataset = gdal.Open(input_file)
num_bands = dataset.RasterCount
for i in range(num_bands):
band = dataset.GetRasterBand(i + 1)
output_file = os.path.join(output_folder, f"bio_{i + 1}.tif")
driver = gdal.GetDriverByName("GTiff")
new_dataset = driver.Create(output_file, dataset.RasterXSize, dataset.RasterYSize, 1, gdal.GDT_Float32)
new_dataset.SetProjection(dataset.GetProjection())
new_dataset.SetGeoTransform(dataset.GetGeoTransform())
new_dataset.GetRasterBand(1).WriteArray(band.ReadAsArray())
print(f"Creating {output_file}...")
new_dataset.FlushCache()
new_dataset = None
dataset = None
However, there is a problem; when installing GDAL, it is likely to report an error, and I haven't found the specific reason. The recommended method online is to use conda:
conda install gdal
In practice, there may be issues with installation or being unable to call it. Here are the solutions:
- If installation fails:
Download the corresponding .whl file from the GitHub repository, for example, for Python 3.11, download GDAL-3.7.1-cp311-cp311-win_amd64.whl, and then manually install it usingpip install GDAL-3.7.1-cp311-cp311-win_amd64.whl
. - If it cannot be called:
In the folder where Python installs various environment packages, such asD:Path\To\python3.9.12\Lib\site-packages\
, create a new gdal.py file and copy the following code into it:
# import osgeo.gdal as a convenience
from osgeo.gdal import deprecation_warn
deprecation_warn('gdal')
from osgeo.gdal import *