import arcpy
raster = arcpy.Raster("image.tif")
array = arcpy.RasterToNumPyArray(raster) (height, width)=array.shape
for row in range(0,height):
for col in range(0,width):
print str(row)+","+str(col)+":"+str(array.item(row,col))What I'm trying to do is capture the value of each pixel in a raster, but I'm only interested in each unique value per row.
In the 10 x 10 example below, the first row pixel values that would be listed are:
191,192,188,190,181,203,183,192,203,195
where the second row would have 8 unique values because 181 and 172 are repeated:
181,187,172,186,182,189,161,208
[ATTACH=CONFIG]30468[/ATTACH]
I suppose ideally the output would be like this:
row:values
0:191,192,188,190,181,203,183,192,203,195
1:181,187,172,186,182,189,161,208
I'm stuck with 10.1 at the moment so I'll have to work within those parameters.
import arcpy
import numpy
raster = 'path\to\raster'
output = 'the\output.file'
data = arcpy.RasterToNumPyArray(raster).tolist
result = open(output, 'w')
rowNum = 0
for row in data:
unique = set(row)
result.write('%s:'%rowNum)
line = ''
for val in unique:
line += '%s,'%val
line = line[:-1] + '\n' #eliminates a trailing comma
result.write(line)
rowNum += 1
result.close()
import arcpy raster = 'path\to\ASCIIraster.asc' output = 'the\output.file' ASCIIData = open(raster, 'r') OutData = open(output, 'w') rowNum = 0 for line in ASCIIData: values = line.split() writeLine = '%s:'%rowNum try: test = int(values[0]) #This will fail for the headers unique = set(values) #All strings already for val in unique: writeLine += val + ',' writeLine = writeLine[:-1] + '\n' #eliminates a trailing comma OutData.write(writeLine) rowNum += 1 #Has to be here because we don't increment for the header except ValueError: pass #Pass because we are skipping the text header OutData.close()