Python 柵格數據處理教程(一)
本文將介紹通過 ArcGIS Pro 的 Python 模塊(arcpy)對柵格數據定義投影及裁剪的方法。
1 數據來源及介紹
降水量數據:國家青藏高原科學數據中心的 中國1km分辨率逐月降水量數據集。
行政區數據:天地圖行政區劃數據中的吉林省邊界面數據,該數據為 GeoJSON 格式,可通過 QGIS 等軟件將其轉換為 shapefile 格式文件。
中國1km分辨率逐月降水量數據集為 NetCDF 格式,通過《NetCDF 文件批量轉柵格并導出柵格各波段》文中的方法轉換后的 tif 格式數據沒有確定坐標系,而數據官網中對于坐標系統建議使用 WGS84,故本文通過 arcpy 的 DefineProjection 函數批量定義柵格投影后,通過 ExtractByMask 函數根據吉林省邊界面數據裁剪得到吉林省范圍的降水量數據。
2 示例代碼
2.1 定義柵格投影
import os
import arcpy
# "TIF":柵格數據文件夾相對路徑
for fileName in os.listdir("TIF"):
# 遍歷文件夾中所有后綴名為 .tif 的文件
if fileName[-4:] == ".tif":
# 讀取柵格數據文件路徑
raster = os.path.join("TIF", fileName)
# 定義柵格投影為 WGS 1984
sr = arcpy.SpatialReference("WGS 1984")
arcpy.DefineProjection_management(raster, sr)
print(f"{fileName[:-4]} conversion successful!")
將定義投影后的數據導入 ArcGIS,其空間參考信息如下:

2.2 按掩膜提取
import os
import arcpy
from arcpy import sa
# 如果在代碼同級文件夾中沒有結果數據庫,則創建該數據庫
if "Pre_Jilin.gdb" not in os.listdir("."):
print("即將為您創建用于保存結果柵格的文件地理數據庫(Pre_Jilin.gdb)……")
arcpy.CreateFileGDB_management(".", "Pre_Jilin")
print("數據庫創建完成!")
for fileName in os.listdir("TIF"):
if fileName[-4:] == ".tif":
# 讀取輸入柵格及掩膜數據的文件路徑,可根據實際情況調整
inRaster = os.path.join("TIF", fileName)
inMaskData = "Data/Jilin.shp"
# 執行按掩膜提取,并將提取結果保存至數據庫
# 結果文件名稱與不包含后綴部分的原始文件名稱相同
sa.ExtractByMask(inRaster, inMaskData).save(f"Pre_Jilin.gdb/{fileName[:-4]}")
print(f"{fileName[:-4]} conversion successful!")
按掩膜提取結果柵格如下所示:

3 降水數據參考文獻格式
3.1 數據的引用
彭守璋. (2020). 中國1km分辨率逐月降水量數據集(1901-2023). 國家青藏高原數據中心. https://doi.org/10.5281/zenodo.3114194.
Peng, S. (2020). 1-km monthly precipitation dataset for China (1901-2023). National Tibetan Plateau / Third Pole Environment Data Center. https://doi.org/10.5281/zenodo.3114194.
3.2 文章的引用
1、Peng, S.Z., Ding, Y.X., Wen, Z.M., Chen, Y.M., Cao, Y., & Ren, J.Y. (2017). Spatiotemporal change and trend analysis of potential evapotranspiration over the Loess Plateau of China during 2011-2100. Agricultural and Forest Meteorology, 233, 183-194. https://doi.org/10.1016/j.agrformet.2016.11.129
2、Ding, Y.X., & Peng, S.Z. (2020). Spatiotemporal trends and attribution of drought across China from 1901–2100. Sustainability, 12(2), 477.
3、Peng, S.Z., Ding, Y.X., Liu, W.Z., & Li, Z. (2019). 1 km monthly temperature and precipitation dataset for China from 1901 to 2017. Earth System Science Data, 11, 1931–1946. https://doi.org/10.5194/essd-11-1931-2019
4、Peng, S., Gang, C., Cao, Y., & Chen, Y. (2017). Assessment of climate change trends over the loess plateau in china from 1901 to 2100. International Journal of Climatology.
浙公網安備 33010602011771號