I am working with raster data on a worldwide level with a resolution of 30 arc seconds. I need to export the data into a table format but given the high resolution you can not run the "Sample" (Spatial Analyst) command for the entire dataset at once.

Therefore, I have created a mask for each country(with The Set Null command). I was trying to write a code in Python, using a loop where each time the mask will become a different zone, i.e. env.mask = zone1, then zone2 and so on. However, I suppose that within a python session one cannot keep on changing the mask using a loop. Could you kindly suggest a way loop the mask (so that I can just have one code for all the countries but my program still runs)?


One way to do it is to use a cursor, which lets you work through your data row by row. Since each row should represent a polygon (Country in your case) you can loop through running the Clip each time. Something to be aware of are countries that have islands (multiple polygons). You'll want to make sure that the data are in multipart polygons as opposed to separate features.

This example runs in the python window since it references a feature layer in the ArcMap active window as opposed to a path name.

import arcpy

# path name variables
clipFeat = r'...'
output = r'...'
raster = r'...'

# Name of OID field
fieldName = "OID"
cursor = arcpy.SearchCursor(clipFeat)
    for poly in cursor:
    mask = poly.OID

        # Create feature layer of current mask polygon
        whereClause = '"' + fieldName + '" = ' + "'" + mask + "'"
        arcpy.MakeFeatureLayer_management(clipFeat, 'currentMask', whereClause)

        # Save the clipped raster
        outRaster = arcpy.sa.ExtractByMask(raster, 'currentMask')
        outRaster.save(output + "\\" + mask + ".tif") # Create your own output name

if arcpy.Exists('currentMask'):
del cursor, poly

Good Luck

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.