python批量計算ndvi
做了少量修改,剔除了異常值,執行代價時需要更換影像對應波段及檔案儲存位置
import os
import numpy as np
from osgeo import gdal
import glob
import time
list_tif = glob.glob(
"f://2020treeclassification//ndvipy//maskgf//*.tif"
)out_path =
'f://2020treeclassification//ndvipy//ndvigf'
start = time.perf_counter(
)for tif in list_tif:
in_ds = gdal.open(tif)
# 獲取檔案所在路徑以及不帶字尾的檔名
(filepath, fullname)
= os.path.split(tif)
(prename, suffix)
= os.path.splitext(fullname)
if in_ds is
none
:print
('could not open the file '
+ tif)
else
:# 將modis原始資料型別轉化為反射率
red = in_ds.getrasterband(3)
.readasarray()*
0.0001
nir = in_ds.getrasterband(4)
.readasarray()*
0.0001
np.seterr(divide=
'ignore'
, invalid=
'ignore'
) ndvi =
(nir - red)
/(nir + red)
ndvi[ndvi >1]
=0# 過濾異常值
ndvi[ndvi <0]
=0# 過濾異常值
# 將nan轉化為0值
nan_index = np.isnan(ndvi)
ndvi[nan_index]=0
ndvi = ndvi.astype(np.float32)
# 將計算好的ndvi儲存為geotiff檔案
gtiff_driver = gdal.getdriverbyname(
'gtiff'
)# 批量處理需要注意檔名是變數,這裡擷取對應原始檔案的不帶字尾的檔名
out_ds = gtiff_driver.create(out_path + prename +
'_ndvi.tif'
, ndvi.shape[1]
, ndvi.shape[0]
,1, gdal.gdt_float32)
# 將ndvi資料座標投影設定為原始座標投影
out_ds.setprojection(in_ds.getprojection())
out_ds.setgeotransform(in_ds.getgeotransform())
out_band = out_ds.getrasterband(1)
out_band.writearray(ndvi)
out_band.flushcache()
elapsed =
(time.perf_counter(
)- start)
print
("影像輸出完畢"
)print
("計算ndvi耗時:"
, elapsed)
如果原影像是.dat檔案
import os
import numpy as np
from osgeo import gdal
import glob
import time
list_dat = glob.glob(
"f://2020treeclassification//ndvipy//maskgf//*.dat"
)out_path =
'f://2020treeclassification//ndvipy//ndvi//ndvi_'
start = time.perf_counter(
)for dat in list_dat:
in_ds = gdal.open(dat)
# 獲取檔案所在路徑以及不帶字尾的檔名
(filepath, fullname)
= os.path.split(dat)
(prename, suffix)
= os.path.splitext(fullname)
if in_ds is
none
:print
('could not open the file '
+ dat)
else
:# 將modis原始資料型別轉化為反射率
red = in_ds.getrasterband(3)
.readasarray()*
0.0001
nir = in_ds.getrasterband(4)
.readasarray()*
0.0001
np.seterr(divide=
'ignore'
, invalid=
'ignore'
) ndvi =
(nir - red)
/(nir + red)
ndvi[ndvi >1]
=0# 過濾異常值
ndvi[ndvi <0]
=0# 過濾異常值
# 將nan轉化為0值
nan_index = np.isnan(ndvi)
ndvi[nan_index]=0
ndvi = ndvi.astype(np.float32)
# 將計算好的ndvi儲存為geotiff檔案
gtiff_driver = gdal.getdriverbyname(
'gtiff'
)# 批量處理需要注意檔名是變數,這裡擷取對應原始檔案的不帶字尾的檔名
out_ds = gtiff_driver.create(out_path + prename +
'_ndvi.tif'
, ndvi.shape[1]
, ndvi.shape[0]
,1, gdal.gdt_float32)
# 將ndvi資料座標投影設定為原始座標投影
out_ds.setprojection(in_ds.getprojection())
out_ds.setgeotransform(in_ds.getgeotransform())
out_band = out_ds.getrasterband(1)
out_band.writearray(ndvi)
out_band.flushcache(
)elapsed =
(time.perf_counter(
)- start)
print
("影像輸出完畢"
)print
("計算ndvi耗時:"
, elapsed)
Python 批量計算變數iv值
import pandas as pd import numpy as np from sklearn.tree import decisiontreeclassifier data pd.read excel r e lll 20200311人工客群分布 sx all data 0311.xlsx...
hadoop批量計算框架 MapReduce
結合自身的經驗記錄,mapreduce中的一些知識點以及乙個wordcount小實踐 核心思想 分而治之 map程式 需要根據自己的需求開發 shuffle 緩衝區大小設定 core site.xml設定為100m io.file.buffer.size 100000000 以位元組為單位 hdfs...
ArcPy批量計算Mean Center的兩個例項
很久沒用arcpy了,碰了好幾次壁,把這次做的貼上來,以備下次可以跳過這些簡單的問題 1 import arcpy 2 arcpy.env.workspace c users qian documents arcgis default.gdb 3 a sichuan1990 sichuan2000 ...