9

I can’t use the tool arcpy.CreateFishnet_management because define the parameter “templateExtent” with a shapefile it is not filling automatically the parameters “originCoordinate” and “yAxisCoordinate”.

import arcpy
from arcpy import env
env.overwriteOutput = True
env.workspace = r"D:\Users\julia\erste_aufg"

#Process: Create Fishnet
outFeatureClass = r"D:\Users\julia\erste_aufg\at001l_wien\at001l_wien\wien.shp"
cellSizeWidth = '200'
cellSizeHeight = '200'
templateExtent = r"D:\Users\julia\erste_aufg\at001l_wien\at001l_wien\at001l_wien.shp"

arcpy.CreateFishnet_management(outFeatureClass, "", "", cellSizeWidth, cellSizeHeight, '0', '0', "", "NO_LABELS", templateExtent, "POLYGON")

enter image description here

It is working in the ModelBulider, so something is running in the background of the ModelBulider that it could create the parameters “originCoordinate” and “yAxisCoordinate” when it has a “templateExtent”. How can I get this tool running in ArcPy by having just the parameter “templateExtent”?

I would be really happy if someone has a solution because I need the Fishnet in a scripttool and cannot go one without because in the end there is a loop so the values of the extent are always different. the first part of the whole script

13

here is an example. You need to extract the bounding box from a describe object.

desc = arcpy.Describe(fc)
arcpy.CreateFishnet_management(fc[:-4]+"_c200.shp",str(desc.extent.lowerLeft),str(desc.extent.XMin) + " " + str(desc.extent.YMax + 10),"200","200","0","0",str(desc.extent.upperRight),"NO_LABELS","#","POLYGON")
  • @@radouxju, what is the purpose of + 10 in str(desc.extent.YMax + 10)? – maycca Oct 4 at 7:49
  • good question. Actually not necessary in this case. I've got the habit to add an arbitrary value on the Ymin to build a vertical axis, but here I used Ymax, therefore it is overkill. – radouxju Oct 4 at 12:11
4

Here is an alternate approach that I used to create multiple fishnets within the extents of each feature within a feature class. The search_extents variable defines the path to that feature class defining the extents of each fishnet I wanted to create. There was no rotation of the fishnet.

search_extents = "path to extents" 
rows = arcpy.SearchCursor(search_extents)
shapeName = arcpy.Describe(search_extents).shapeFieldName
for row in rows:
    print("Starting Extent" + row.getValue("Extent_Num"))
    feat = row.getValue(shapeName)
    extent = feat.extent
    arcpy.CreateFishnet_management(arcpy.env.workspace + "/extents/extentgrid" + row.getValue("Extent_Num"),str(extent.lowerLeft), str(extent.upperLeft),"0","0","200","200",str(extent.upperRight),"NO_LABELS","#","POLYGON")
    print("Finishing Extent" + row.getValue("Extent_Num"))
1

Here is the code that I finally successfully got working (with help from the examples above) to solve the problem described here:

    env.workspace = "C:/Holly/Work/Projects/NavigationStudy2019/Data"

    # Fetch each feature from the cursor and examine the extent properties
    for row in arcpy.da.SearchCursor(feature_class, ['SHAPE@', 'id']):
        extent = row[0].extent
        print('Extent of home range {}:'.format(row[1]))
        print('XMin: {}, YMin: {}'.format(extent.XMin, extent.YMin))
        print('XMax: {}, YMax: {}'.format(extent.XMax, extent.YMax))
        arcpy.CreateFishnet_management("fishnet_temp.shp",
                                       str(extent.XMin) + " " + str(extent.YMax),
                                       str(extent.XMin) + " " + str(extent.YMax + 10),
                                       "100",
                                       "100",
                                       "",
                                       "",
                                       "",
                                       "LABELS",
                                       feature_class,
                                       "POLYGON")

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.