I am looking for a good solution to get a particular subset of raster pixels. For example:

  • I have raster 5x5
  • I need to clip:

--column = 1

--row between 1 and 5

What will be the best method?

I was trying to get results using NumPy array by slicing it and exporting it back to raster.

I am wondering if there is much better way of doing it.

Here is my code. The problem is - I cannot export my numpy array slice to raster, because it is a 1 dimentional array.

    import arcpy, numpy
    arcpy.env.overwriteOutput = True

    raster_path = r'D:\raster.tif'
    raster = arcpy.Raster(raster_path)
    lowerLeft = arcpy.Point(raster.extent.XMin, raster.extent.YMin)

    raster_numpy = arcpy.RasterToNumPyArray(friction_raster_buffered_path, lowerLeft)
    x_length = raster_numpy.shape[0]
    print raster_numpy [0,1:x_length-1]
    arcpy.NumPyArrayToRaster(raster_numpy [0,1:x_length-1], lowerLeft, raster.meanCellHeight).save(r'd:\rstrLC.tif')

Error: ValueError: Argument in_array: A two or three dimensional NumPy array is required.

  • does it work? sorry for bad explanation. – alby Aug 19 '16 at 17:43

You can simply reshape it to 2-D. For instance:

raster_numpy[0, 1:x_length-1].reshape(1, raster_numpy.shape[1] - 2)

Also, if you wish to take column 0 and the inner rows, you need to slice it differently. I believe this is what you're looking for:

raster_numpy[1:-1, 0].reshape(raster_numpy.shape[0] - 2, 1)

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.