making fishnet

1070
12
04-29-2013 09:45 AM
nedamohammadi1
New 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
JakeSkinner
Esri Esteemed Contributor
You can use the Create Fishnet function to do this.  If all your rasters are in one directory, you can iterate through each raster using the List Rasters function.
0 Kudos
nedamohammadi1
New Contributor
Thank you so much Jake!
It seems it was helpful, I had problem in the reading of data!I wrote this script:
#import arcpy and arcgisscripting
import arcgisscripting

gp = arcgisscripting.create(9.3)
gp.overwriteOutput = 1

import arcpy

# Set the current workspace
gp.env.workspace = r"X:\mohammadi\finalmunichGIS\clip0ffishnet9000\clip"
# Get and print a list of GRIDs from the workspace
rasters = arcpy.ListRasters("*", "TIF")
for raster in rasters:
    print(raster)
# input hard-coded but you can make it a script tool parameter to make it an interactive input.
#in_fc = r"X:\mohammadi\finalmunichGIS\clip0ffishnet9000\clip" # this polygon feature class has several polygons.

rows = gp.SearchCursor(rasters)
row = rows.Next()

while row:
    feat = row.raster
    ext = feat.extent
    orig = str(ext.xmin) + " " + str(ext.ymin)
    yaxis = str(ext.xmin) + " " + str(ext.ymin + 100.00)
    opp_corner = str(ext.xmax) + " " + str(ext.ymax)
    print orig, yaxis, opp_corner

outPath = r"X:\mohammadi\finalmunichGIS\fishnet4500\4500.shp"+ str(row.OBJECTID)


result = gp.CreateFishnet_management(outPath, orig, yaxis, "0", "0", "2", "2", opp_corner,"NO_LABELS","#","POLYGON")
  print(gp.GetMessages())

    row = rows.Next()

del row, rows


now error is in line 33: print(gp.GetMessages())
it is the first time I got such a problem?????
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Try the following:

import arcpy
from arcpy import env
env.workspace = r"X:\mohammadi\finalmunichGIS\clip0ffishnet9000\clip"

lstRasters = arcpy.ListRasters("*", "TIF")
for raster in lstRasters:
    xmin = arcpy.Describe(raster).extent.XMin
    ymin = arcpy.Describe(raster).extent.YMin
    xmax = arcpy.Describe(raster).extent.XMax
    ymax = arcpy.Describe(raster).extent.YMax
    ymin2 = arcpy.Describe(raster).extent.YMin + 10
    arcpy.CreateFishnet_management("fishnet_" + raster[:-4] + ".shp", str(xmin) + " " + str(ymin), str(xmin) + " " + str(ymin2), "0","0","2","2", str(xmax) + " " + str(ymax), "NO_LABELS", "#","POLYGON")

print "Finished"
0 Kudos
nedamohammadi1
New Contributor
Thank you so much, it worked!!:D
0 Kudos
nedamohammadi1
New Contributor
Dear Jake Skinner !!
thank you so much for your help!!now I want to divide this fishnets by clip_management, I wrote this script:
# 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\munich.gdb"


# in_fc is your polygon feature class
in_fc = r"X:\mohammadi\finalmunichGIS\fishnet4500"

fcList = arcpy.ListFeatureClasses("","Polygon","")
for fc in fcList:
    lyrFC = "lyrFC"

    # in_fc is your polygon feature class
    in_fc = fc

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

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

    arcpy.MakeFeatureLayer_management(in_fc,lyrFC)

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


it works but it makes clip likes as before for me it means whole of fishnet not four clips of every fishnet!!!
what is your sugestion?
thanks
neda
0 Kudos
nedamohammadi1
New Contributor
Dear Jake!

I need to make clip from that fishnet it almost work , the only problem is error 000732:

# 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\fishnet4500.gdb"


# in_fc is your polygon feature class
#in_fc = r"X:\mohammadi\finalmunichGIS\fishnet4500"

fcList = arcpy.ListFeatureClasses("","Polygon","")
for fc in fcList:
    lyrFC = "lyrFC"

    # in_fc is your polygon feature class
    in_fc = fc

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

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

    arcpy.MakeFeatureLayer_management(in_fc,lyrFC)

    # 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\test"

    # one by one clip raster to polygons
    arcpy.env.workspace= outPath
    for objID in objIDs:
        where = "{1} = {0}".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)


because of the name of clips , sometimes they are same so it makes problem , so I must change last part of script I mean loop !!
what do you sugest ?I mean the name of clips must not be 1,2,3,4 sth like as a, b, c , but I don,t know how to manage that .I am so new in python:)
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Are you trying to clip the fishnet polygon feature class by a directory of rasters?
0 Kudos
nedamohammadi1
New Contributor
Yes I am going to do that, because my clips must be rasters or TIF , I want to use them in fragstats
0 Kudos
nedamohammadi1
New Contributor
I must solve the problem in last part , Imean for the loop of :
for objID in objIDs:
        where = "{1} = {0}".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)


because the name of clips are like as this for example:fishnet_fc_parent1_10_clip1_clip1
so 1, 2 , 3, 4 makes problem , because some of them are same.error 000732 and error 000725
😞
0 Kudos