1

I have recently been having some issues converting rasters to numpy arrays. The RasterToNumPyArray tool seems to be adding or subtracting rows or columns depending on which dataset I'm working on.

For instance I have a raster that contains 114 rows, and 160 columns. After converting it to an array it has 115 rows, and 160 column. If I convert this array back to a raster the data appears to be shifted up.

I have tried specifying the number of rows and columns to process in the RasterToNumPyArray tool. The row and column count issue is fixed but the output raster is still shifted up one row.

This issue happens whether the data is in a geographic or projected coordinate system.

Any idea why this would be happening? I am including a python script where I was seeing this issue.

import arcpy
import numpy as np

from arcpy import env
from os import path

env.overwriteOutput = True
env.outputCoordinateSystem = arcpy.SpatialReference(4326)

input_ras = r'PATH_TO_INPUT_RASTER'
input_ras_name = path.basename(input_ras)

ras_desc = arcpy.Describe(input_ras)
rows = ras_desc.height
cols = ras_desc.width
x_cell_size = ras_desc.meanCellWidth
y_cell_size = ras_desc.meanCellHeight

lower_left_corner = arcpy.Point(ras_desc.extent.XMin, ras_desc.extent.YMin)

print input_ras_name
print 'rows:', rows
print 'cols:', cols
print lower_left_corner

arr = arcpy.RasterToNumPyArray(input_ras, lower_left_corner, cols, rows, -9999)
print '\narry based on input raster'
print 'rows:', np.shape(arr)[0]
print 'cols:', np.shape(arr)[1]

print '\nconverting array back to a raster'

output_ras = arcpy.NumPyArrayToRaster(arr, lower_left_corner, input_ras, input_ras)
output_ras.save(r'PATH_TO_OUTPUT_RASTER')

edit: I explored the row and column count a little more. The following screen shot shows the row and column count three different ways - one of which is different. Could this mean anything?

count

edit 2: The problem does not seem to occur after converting my tif to a GRID and then processing the data. Ideally thought I would not want to perform this intermediate step.

4
  • Perhaps experiment with environment extent settings
    – FelixIP
    Apr 5, 2017 at 19:20
  • I just tried playing around with the processing extent and snap raster options and neither seem to fix the issue. I did realize that the extent of the input and output raster are the same. A row of no data is being added to the bottom of the raster and values are being cut off at the top.
    – spalka
    Apr 5, 2017 at 19:47
  • This is how I do it: dirArray = arcpy.RasterToNumPyArray(fdir,"","","",-9999) , never had an issue
    – FelixIP
    Apr 5, 2017 at 19:59
  • @FelixIP When I try to create an array that way the array has different dimension than the input raster. 115x160 rather than 114x160 (as it should be). The resulting dataset created when converting that array back to a raster reflect this change along with a change in extent due to the extra row.
    – spalka
    Apr 5, 2017 at 20:08

2 Answers 2

1

I did this from Python window in ArcMap with extent set to "dem" extent.

>>> import numpy
>>> dirArray = arcpy.RasterToNumPyArray("dem","","","",-9999)
>>> dirArray.shape
(1201, 1673)
>>> d=arcpy.Describe("dem")
>>> d.height
1201
>>> d.width
1673
>>> 

Everything is working as it should. Where from do you run your script?

3
  • I run my script from the command prompt. I also just tried running through your steps in the Python command window within ArcGIS. Same result as before - the array contains one more row than the raster does.
    – spalka
    Apr 5, 2017 at 20:33
  • Snap raster and cell size in environment settings?
    – FelixIP
    Apr 5, 2017 at 20:55
  • Snap raster and cell size are both set to the input raster values. I am still seeing the row difference.
    – spalka
    Apr 5, 2017 at 20:59
0

Had a similar problem today, where the resulting .tif raster from NumPyArrayToRaster conversion was shifted upwards by a cell. For me, it was about the environment settings. To be more precise, removing the arcpy.env.extent setting solved it in my case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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