Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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)?

share|improve this question
add comment

1 Answer

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 = '"' + focusField + '" = ' + "'" + 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
        arcpy.Delete_management('currentMask')

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

Good Luck

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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