banner
Riceneeder

Riceneeder

白天研究生,晚上研究死
github
email

Some points on adaptive zoning using ArcGIS and MaxEnt

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:

  1. 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 using pip install GDAL-3.7.1-cp311-cp311-win_amd64.whl.
  2. If it cannot be called:
    In the folder where Python installs various environment packages, such as D: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 *
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.