def histo(input1,inBins=100, inRange=None, inNormed=False, inWeights=None, inDensity=None,
NoDataValue = None, draw = True):
#test if input is a string (filepath) or already numpy array
if type(input1) == str:
array = singleTifToArray(input1)
else:
array = input1
array2 = ma.masked_values(array, NoDataValue)
pixNum = array2.count() #size on masked arrays would still return masked pixels
# returns tuple of 2 arrays, one with unique raster values and of with corresponding count
unique = np.unique(array2.compressed()) #array of unique values
uniqueMax = unique.max() #maximum value
array1D = array2.compressed()
#To draw histograms with values smaller one or more than 256 use 100 bins by default
if uniqueMax <= 1 or uniqueMax >= 256:
inBins = inBins
else:
inBins = range(1,int(uniqueMax)+2)
h = np.histogram(array2.compressed(),bins=inBins, normed=inNormed,
weights=inWeights, density=inDensity)
#Returns graphic histogram by standard
if draw:
plt.hist(array1D,bins=inBins, range = inRange, normed = inNormed, weights = inWeights)
#plt.show()
print("Total number of pixel (noData included): ", array2.size,
"(noData excluded): ", pixNum)
#return total pix number, masked pix number, and tuple of histogram array
return array2.size,pixNum, h
#########################################################################################
# Create a mask from vector and convert vectors to raster
#########################################################################################
评论列表
文章目录