0

I have a raster file and I would like to convert it to an array and then save it as a csv file with using ArcPy. The reason is that I want to do some statistical analysis over each row and column in the output csv file . I have around 100 rasters that I would like to have at the end 100 csv files for the further analysis. this is why I need to use arcpy in this case. As you can see the raster output is tilted and the first row in the top part of the raster is not fully straight and it mixed with NoData as well. I would like to have the output file without the local coordinate system. the raster resolution is 25 cm :

enter image description here

the example of the csv file:

enter image description here

closed as off-topic by PolyGeo May 15 at 8:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

1

Converting raster data to CSV format to do your calculations is very computationally expensive and messy. Rather, a more efficient way to accomplish your goal would be to convert your raster data into an n-dimensional numpy array and do the calculations with numpy.

For example, let's assume we converted three 3x3 pixel rasters into an n-dimensional array:

import numpy as np

arr = np.array([[[ 0,  1,  2],
                 [ 3,  4,  5],
                 [ 6,  7,  8]],

                [[ 9, 10, 11],
                 [12, 13, 14],
                 [15, 16, 17]],

                [[18, 19, 20],
                 [21, 22, 23],
                 [24, 25, 26]]])

You can see the shape is a stack of 3x3 arrays:

>>> print(arr.shape)
(3, 3, 3)

Let's do some calculations along different axes.

# Calculate sum along axis 0:

>>> arr.sum(0)
array([[27, 30, 33],
       [36, 39, 42],
       [45, 48, 51]])

# Calculate sum along axis 1:

>>> arr.sum(1)
array([[ 9, 12, 15],
       [36, 39, 42],
       [63, 66, 69]])

# Calculate sum along axis 2:

>>> arr.sum(2)
array([[ 3, 12, 21],
       [30, 39, 48],
       [57, 66, 75]])

There are a multiple approaches to convert your raster data to numpy arrays, here is a good source.

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