Search code examples
pythonarcgisarcpy

Want to do some computation on Raster using ArcPY


I am want to calculate repetitive values from raster and save it into dict, how can i do it ? i have done some coding but its not working help me to solve the problem. below is my code:

import numpy as np  
import arcpy  
from arcpy import env  

env.workspace = r"D:Results"  
rasterlist = arcpy.ListRasters()  
array = arcpy.RasterToNumPyArray(raster,nodata_to_value=0)  
array1 = np.reshape(array, (1,np.product(array.shape)))  

mydict = {}  
for i in array1:  
    if i in mydict:  
        mydict[i] += 1  
    else:  
        mydict[i] = 1  
print mydict 

Actually i want to implement the following formula: 1.png where g is the number of tied groups and tp is the number of data in the pth group. For example, in the sequence {23, 24, trace, 6, trace, 24, 24, trace, 23} we have g = 3, t{ = 2 for the tied value 23, t2 = 3 for the tied value 24, and r3 = 3 for the three trace values, where as n = 1


Solution

  • Based on the code you provided, the problem might be that the "raster" variable is undefined. Something like this might work:

    rasterlist = arcpy.ListRasters()
    raster = rasterlist[0] #The first item in rasterlist
    array = arcpy.RasterToNumPyArray(raster,nodata_to_value=0)
    

    Hopefully this helps. More detail as to exactly what error you're getting would also be useful.

    Tom