I'm hoping to run some Python mathematics on a raster, outside of ArcGIS. It's important to have a cell by cell table for the analysis I want to run. I plan to store it in a python dictionary similar to this:

Dict = {

Obviously this is just and example, but this is what I want to turn my raster into. I always see these diagrams within the ArcGIS help:

Raster Math Times

And this is exactly what I would like out of a raster. I can easily go from a CSV to the dictionary I want, but I'm having trouble getting a raster to a cell by cell table.


Referencing the question you linked. This code should work for ya:

import arcpy
fOut = open('outputFile.py', w)                 # Open output python file
fOut.write('Dict = {' + '\n')                   # Write the dictionary
rstArray = arcpy.RasterToNumPyArray(rasterFile) # Change rasterFile to numpy array
rows, cols = rstArray.shape                     # Return the rows, columns
for rowNum in xrange(rows):                     # Loop through the rows
    fOut.write('row' + str(rowNum) + ':[')
    for colNum in xrange(cols):                 # Loop through the row's columns
        value = rstArray.item(rowNum, colNum)   # Get the value at the cell
        fOut.write(str(value) + ',')            # Write the value
    fOut.write('],' + '\n')                     # End the list and start new item
fOut.write('\t' + '}')                          # End the dictionary

I've tested it out on a small raster and it works to provide an output like you specified about. If you have a large raster this is going to get hard to read with python quickly. Look at indexing similar values.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.