Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

Extract Polygons out of a raster

$
0
0

I want to extract every row from a .shp from a raster (Extract by Mask).
With the following code i get 4 output raster, wich concludes every row.

import arcpy
arcpy.CheckOutExtension('spatial')
arcpy.env.overwriteOutput = 1
# path name variables
clipFeat = r'../all.shp'
output = r'/output'
raster = r'/Karte1.jpg'
# Name of OID field
fieldName = "FID"
cursor = arcpy.SearchCursor(clipFeat)
for poly in cursor:
    mask = str(poly.FID)
    # Create feature layer of current mask polygon
    whereClause = "poly.FID = poly.FID "
    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

If I type “whereClause = ""FID" = 0“, I Got four files with the export of row 0.

My goal is that i got 4 differnt output, one für FID=0, FID=1, FID=2 and so on.


Viewing all articles
Browse latest Browse all 767

Trending Articles