Select to view content in your preferred language

making fishnet

1445
12
04-29-2013 09:45 AM
nedamohammadi1
Emerging Contributor
Hello Every body !

I want to make a fishnet for around 80 images (.tif), I want to call data from a path and make a fishnet for raws and columns 2*2.
who has idea?
Regards
Nedaaaa
Tags (2)
0 Kudos
12 Replies
nedamohammadi1
Emerging Contributor
I made some change in my code :
# only import arcpy - don't use arcgisscripting/gp at 10.x
import arcpy
from arcpy import env

arcpy.env.overwriteOutput = True ## 1
env.workspace = r"X:\mohammadi\finalmunichGIS\fishnet4500\fishnet4500plus.gdb"

def sendmsg (msg):
    print msg
    arcpy.AddMessage(msg)

fcList = arcpy.ListFeatureClasses("","Polygon","")

for fc in fcList:
    env.workspace = r"X:\mohammadi\finalmunichGIS\fishnet4500\fishnet4500plus.gdb"

    lyrFC = "lyrFC"
    sendmsg (fc)
   
    # in_fc is your polygon feature class
    in_fc = fc

    arcpy.MakeFeatureLayer_management(in_fc,lyrFC)

    # in_tif is your input
    in_raster = r"X:\mohammadi\finalmunichGIS\Testdatenoriginaldata\munich_subset.tif"

    # output location
    outPath = r"X:\mohammadi\finalmunichGIS\clip0ffishnet9000\lastclip"

   

    # get list of object ids
    objIDs = []
    objIDField = arcpy.Describe(lyrFC).OIDFieldName
    rows = arcpy.SearchCursor(lyrFC)
    row = rows.next()
   
    while row:
        objIDs.append(row.getValue(objIDField))
        row = rows.next()

    del row, rows
       
    # temp polygons
    copyFC = r"X:\mohammadi\finalmunichGIS\test.gdb\fc_parent1_1c"

    # one by one clip raster to polygons
    arcpy.env.workspace = outPath
    for objID in objIDs:
        where = "{0} = {1}".format(objIDField,objID)
        arcpy.SelectLayerByAttribute_management(lyrFC,"NEW_SELECTION",where)
        arcpy.CopyFeatures_management(lyrFC,copyFC)
        ext = arcpy.Describe(copyFC).Extent
        extString = "{0} {1} {2} {3}".format(ext.xmin, ext.ymin, ext.xmax, ext.ymax)
        out_raster = "{0}_clip{1}.tif".format(in_fc, objID)
        arcpy.Clip_management(in_raster, extString, out_raster, copyFC)
        print arcpy.GetMessages(0)

    arcpy.Delete_management(copyFC)
    arcpy.Delete_management(lyrFC)



but the error is this:error 999999:the table name is invalid
who can help me?
I run this code befor , but I don,t know why it has problem?
thanks alot
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Neda,

From my understanding, you first created a 2x2 fishnet shapefile for each raster in a directory.  You now want to clip each one of those rasters by each polygon in the fishnet.  If that is correct, take a look at the following code:

import arcpy
from arcpy import env
env.workspace = r"C:\TIFFs"
env.overwriteOutput = 1

list = []

#append each raster to a list
lstFCs = arcpy.ListFeatureClasses("*")
for fc in lstFCs:
    list.append(fc)

#iterate through each raster
lstRasters = arcpy.ListRasters("*")
for raster in lstRasters:
    for n in list:
        #find the fishnet shapefile by checking if the raster name is in the shapefile name
        if raster.split(".")[0] in n:
            rows = arcpy.SearchCursor(n)
            for row in rows:
                FID = row.FID
                #create a feature layer for each polygon in the fishnet shapefile
                arcpy.MakeFeatureLayer_management(n, "FC_lyr", "FID = " + str(FID))
                #clip the raster by each polygon, and output a new TIFF to another directory
                arcpy.Clip_management(raster, "#", r"C:\TIFFs\Clip" + "\\" + raster + "_clip_" + str(FID) + ".tif", "FC_lyr", "256", "ClippingGeometry")

print "Finished"


Also, a quick note, when you are posting code, be sure to wrap it in CODE tags using the # symbol above.  This will preserve the indentation.  I believe you were previously wrapping the code with QUOTE tags.
0 Kudos
nedamohammadi1
Emerging Contributor
Sorry I didn,t have screen shot! I upload photo for you!
0 Kudos