程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python批量矢量裁剪柵格

編輯:Python

因為最近總是用到批量矢量裁剪柵格的步驟,還是寫個博客記錄一下,以免找不到代碼了,這個代碼也是我在網上找的。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Created on Fri Jan 8 16:31:59 2021 """
import os
# 對應自己的python包的安裝地址,也是prj.db文件存放的位置,直接電腦搜吧,選擇osgeo文件夾的那個路徑就可以,我是以為自己糊塗多裝了很多gdal吧,然後prj.db文件過多,代碼就找不到哪個了,所以這裡我加了一行代碼來指出我的Prj.bd文件存在哪裡。
os.environ['PROJ_LIB'] = r'E:\Users\Lib\site-packages\osgeo\data\proj'
import time,os
from osgeo import gdal
import numba
@numba.jit # 程序加速
def clip_batch(in_folder, out_folder, in_shape):
files = os.listdir(in_folder)
for file in files:
if file[-4:] == '.tif':
filename = os.path.join(in_folder,file)
in_raster = gdal.Open(filename)
out_raster = os.path.join(out_folder,file)
ds = gdal.Warp(out_raster,in_raster,format = 'GTiff',
cutlineDSName = in_shape,
cropToCutline = True,
creationOptions=["TILED=YES", "COMPRESS=LZW"],
cutlineWhere = None, dstNodata = -999)
ds=None
if __name__=="__main__":
start = time.perf_counter() # 開始時間
in_shape = r"E:\backup_1\small_area.shp" # 矢量范圍
in_folder = r"E:\backup_1" # 輸入柵格路徑
#out_folder=r"E:\caijian" # 輸出柵格路徑
out_folder=r"E:\backup_1\caijian2" # 輸出柵格路徑
clip_batch(in_folder, out_folder, in_shape)
end = time.perf_counter() # 結束時間
print('finish')
print('Running time: %s Seconds'%(end-start))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved