banner
Riceneeder

Riceneeder

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

用arcgis和maxent適應性區劃中的一些點

最近的課題有用到這兩個軟體,其中做 maxent 的時候需要氣象因子,而在 worldclim 下載的未來生物氣象因子只有一張包含 19 個波段的 tif 文件,需要手動拆分成 19 個文件。實在是沒有找到用 arcgis 拆分的方法,所以轉向了 python

代碼如下:

import gdal
import os

input_file = "G:/4.其他/wc2.1_30s_bioc_ACCESS-CM2_ssp126_2041-2060.tif"#下載的tif文件
output_folder = "G:/4.其他/feature_bio/"#輸出文件夾

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

但是有個問題,安裝 gdal 的時候大概率會報錯,具體原因我也沒查到。網上的推薦辦法是使用 conda:

conda install gdal

實際情況可能會出現無法安裝或者安裝了沒法調用,解決辦法:

  1. 如果安裝失敗:
    到 Github 倉庫下載對應的.whl 文件,比如 python 3.11 就下載 GDAL-3.7.1-cp311-cp311-win_amd64.whl
    ,然後手動安裝pip install GDAL-3.7.1-cp311-cp311-win_amd64.whl
  2. 如果無法調用:
    在 python 安裝各種環境包的文件夾下,如:D:Path\To\python3.9.12\Lib\site-packages\文件夾下,新建 gdal.py 文件,將以下代碼複製進去:
# import osgeo.gdal as a convenience
from osgeo.gdal import deprecation_warn
deprecation_warn('gdal')
from osgeo.gdal import *
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。