1

I am curious to find our if there is a way to create a python code that enables you to clip a raster into equal parts example: 5x5(25) tiles with the use of arcpy?

From what I can think of you would need to divide the lengths of the extents into 5 equal parts from the width and 5 equal parts from the length and then determine the coordinates from that.

Can anyone think of a way to perform this with arcpy? or possibly provide a better method than the one I am thinking of?

    import arcpy
arcpy.env.workspace = r"folder path"
outWorkspace = "folder path"
arcpy.env.OverwriteOutput = True

rasList = arcpy.ListRasters()
print "The rasters in the workspace are:"
for ras in rasList:
    print "-- " + ras

rasDescr = arcpy.Describe("raster name")
print "\nThe type of the data is : " + rasDescr.DataType
rasExtent = rasDescr.Extent

print "The extent of the data : "
print "\tX-Min: " + str(rasExtent.XMin) + " X-Max: " + \
str(rasExtent.XMax)
print "\tY-Min: " + str(rasExtent.YMin) + " Y-Max: " + \
str(rasExtent.YMax)

print arcpy.Usage("clip_management")
coords = ("X Min   Y Min   X Max   Y Max")
output = outWorkspace + "\\" + "output name"
arcpy.Clip_management(rasDescr.Name, coords, output)
5
  • 1
    Yes, get the extent, divide by 5, iterate and use Clip_management resources.arcgis.com/en/help/main/10.2/index.html#//… (not Clip_analysis) to extract the raster. Alternately you can set the arcpy.env.extent and use CopyRaster_management. Do you have any code so far? – Michael Stimson Mar 23 '15 at 22:22
  • @MichaelMiles-Stimson Thank you for the response I am posting my code now – user32496 Mar 23 '15 at 22:24
  • @MichaelMiles-Stimson I have posted the code, it clips a raster based off the extents found – user32496 Mar 23 '15 at 22:27
  • 1
    You're mostly there. You just need to iterate. – Michael Stimson Mar 23 '15 at 22:28
  • @MichaelMiles-Stimson I don't necessarily want to to redo my code for me as I feel it unfair for you to do it for me but can you show me how I would go about iterating it? – user32496 Mar 23 '15 at 22:31
1

Iterating is a little tricky for the first time user who has done VB or C, there is no for i = 1 to n iteration in python (yet). To iterate from one number to another you have to make a range (list) of integers then step through it with for value in list:

Use this as an example to adjust your code, I've written the whole thing to give you some context:

import os, sys, arcpy

InFolder = sys.argv[1]
OutFolder = sys.argv[2]

arcpy.env.workspace = InFolder
for InRaster in arcpy.ListRasters():
    rName, rExt = os.path.splitext(InRaster) # separate name and extension
    desc = arcpy.Describe(InRaster)
    rExt = desc.extent

    rWid = rExt.XMax - rExt.XMin
    rHgt = rExt.YMax - rExt.YMin

    Steps = range(5) # gives a range (list) containing [0, 1, 2, 3, 4]
    xStep = rWid / 5 # the value of each step
    yStep = rHgt / 5 # in the X and Y

    for xIndex in Steps: # python iterating for var in list:
        for yIndex in Steps:
            # calculate the extent, if you want overlap you can do it here
            Xmin = rExt.XMin + (xStep * xIndex)
            Xmax = Xmin + xStep
            Ymin = rExt.YMin + (yStep * yIndex)
            Ymax = Ymin + yStep

            # make the name of the out file using % formatting.
            OutName = "%s_%d_%d.%s" % (rName,xIndex,yIndex,rExt)
            # define the clipping box
            ClipBox = "%f %f %f %f" % (Xmin,Ymin,Xmax,Ymax)
            # do the clip using the box and out name into the out folder
            arcpy.Clip_management(InRaster,ClipBox,OutFolder + "\\" + OutName)

You were most of the way there with describe, extent and Clip_management.

2
  • 1
    You can do for xIndex in range(5) instead of assigning it to a variable Steps, although in this case I prefer it separated like this so you can (if desired) easily change the script to make 36 or 16 squares instead of 25 :) – Erica Mar 24 '15 at 0:06
  • 1
    Yes @Erica, if I was writing this for my use I would in this case normally put it in as a variable up the top (xBlocks = 5, yBlocks = 5) then separate xSteps = range(xBlocks), ySteps = range(yBlocks) and use xStep = rWid / xBlocks, yStep = rHgt / yBlocks... makes it more flexible. – Michael Stimson Mar 24 '15 at 0:12

Your Answer

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