Python 代码以识别并跳过空栅格,但失败,并带有 numpy 数组语法



我有数百个裁剪栅格,需要使用python 2.7.13重新分类。当我测试其中大约 12 个栅格(包括 4 个空栅格(时,由于没有数据,脚本在空栅格上失败。

我试图跳过我在这里找到的具有 arcpy get 栅格属性和 numpy 数组语法的空栅格......https://gis.stackexchange.com/questions/208519/skip-empty-rasters-in-arcgis

arcpy.env.workspace = work_dir
rasters = arcpy.ListRasters("*", "tif")
for file in rasters:
filename, ext = os.path.splitext(file)
yr_mo = filename[10:17]
pattern = '*clip*'
reclass_name = 'Burn_Scar_' + yr_mo + '_' + 'reclass' +'.tif'
## Testing with numpy unique array    
array = arcpy.RasterToNumPyArray(file)
values = numpy.unique(array)
if file.endswith('.tif') and fnmatch.fnmatch(file,pattern):
if values > 1:
print values
## Testing with arcpy get raster properties
file_results = arcpy.GetRasterProperties_management(file, property_type="MAXIMUM")
file = file_results.getOutput(0)
if file_results > 1:
print file_results
else:
outReclass2 = Reclassify(file, "Value", RemapRange([[-2, 0, "NODATA"]]))
outReclass2.save(reclass_name)
print(reclass_name)
print ('skipping....' + file + 'raster is empty')

arcpy 代码不断打印所有最大值 - 而不仅仅是大于 1 的最大值。

numpy.unique(array( 错误与 'ValueError' 具有多个元素的数组的真值是不明确的。使用 a.any(( 或 a.all((。我很困惑a.any或a.all是什么意思,为什么在其他问题语法中不需要它。

任何其他跳过空栅格并仅处理具有数据的光栅的简单方法都值得赞赏!! 谢谢!!

由于您不提供示例输入,我将用我自己的输入进行演示:

In [171]: arr = np.arange(10)                                                          
In [172]: values = np.unique(arr)                                                      
In [173]: values                                                                       
Out[173]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [174]: values>1                                                                     
Out[174]: 
array([False, False,  True,  True,  True,  True,  True,  True,  True,
True])
In [175]: if np.any(values>1):print(values)                                            
[0 1 2 3 4 5 6 7 8 9]

如果unique生成一个包含 1 个以上元素的数组,则values>1是一个具有多个值的布尔数组。 这不能在if条件下使用。

我们可以使用 any/all 将数组减少到 1 个布尔值。 但你想做什么并不明显。

最新更新