Fishnet creation with ArcPy with origin coordinates from a bounding box (ArcGis 10.0)

3987
3
Jump to solution
09-17-2012 12:00 AM
ThomasChudy
New Contributor II
Hello GIS-Community,
I'm trying to create a script that helps to create Fishnet based on an irregular formed shape (polygon). It should use the bounding box of that shape as origin coordinate (Xmin, Ymin) and opposite corner (Xmax, Ymax). I managed it to somehow get these values from a created Envelope based on that irregular shape but I can not get it to use it as an input for the Fishnet creation. Maybe it's a wrong data type!? As he sometimes stated 'Input for orig. coordinate should be numeric'.
As I never tried to create a scrit before, I mostly took ArcPy script parts from ArcGIS web help from different geoprocessing tools trying to combine them with try and error method.

It's not necessary to create all the temporary shapes such as the bounding box, but it's not a problem neither.
We're on ArcInfo licensing level.

So here is the messy script text I sticked together...
Envelope gets created as I want it. Coordinates from corners of the envelope are taken right (as 'Print' command shows), but giving these values to the fishnet creation doesn't work out.
If i give absolute numbers to 'origin coordinate', 'y-axis orientation' and 'opposite corner' it works as it should... so there must be some mistakes.

Any suggestions on that?
Thank you for your help!

# creating bounding box (envelope) import arcpy from arcpy import env env.workspace = "C:/data" arcpy.FeatureEnvelopeToPolygon_management("E:/ArcMap/1412-boundary2012_proj.shp",                                           "E:/ArcMap/1412extent.shp",                                           "SINGLEPART")  # Create a Describe object from the shapefile # desc = arcpy.Describe("E:/ArcMap/1412extent.shp")  # Print dataset properties # minX=(desc.extent.XMin)  maxX=(desc.extent.XMax)  minY=(desc.extent.YMin)  maxY=(desc.extent.YMax)  print(minX) print(maxX) print(minY) print(maxY)  #create boundary points from envelope # pxy = arcpy.Point(minX, minY) ptGeometry = arcpy.PointGeometry(pxy)  pxY = arcpy.Point(minX, maxY) ptGeometry = arcpy.PointGeometry(pxY)  pXy = arcpy.Point(maxX, minY) ptGeometry = arcpy.PointGeometry(pXy)  pXY = arcpy.Point(maxX, maxY) ptGeometry = arcpy.PointGeometry(pXY)  print(pxy) print(pxY) print(pXy) print(pXY)  print pxy.X,pxy.Y print pxy.X,pxy.Y  # Name: CreateFishnet.py # Description: Creates rectangular cells # Author: ESRI  # import system module import arcpy from arcpy import env  # set workspace environment #  env.workspace = "C:/data/output"   # Set the extent environment using a keyword. arcpy.env.extent = "E:/ArcMap/1412extent.shp"  # Set coordinate system of the output fishnet #  env.outputCoordinateSystem = "<install directory>/Coordinate Systems/Projected Coordinate Systems/UTM/NAD 1983/NAD 1983 UTM Zone 11N.prj"  outFeatureClass = "E:/ArcMap/bound_net.shp"  # Set the origin of the fishnet originCoordinate = pxy.X,pxy.Y  # Set the orientation yAxisCoordinate = pxy.X,(pxy.Y+1)  # Enter 0 for width and height - these values will be calcualted by the tool cellSizeWidth = '60' cellSizeHeight = '60'  # Number of rows and columns together with origin and opposite corner  # determine the size of each cell  numRows =  '0' numColumns = '0'  oppositeCoorner = pXY.X,pXY.Y  # Create a point label feature class  labels = 'false'  # Extent is set by origin and opposite corner - no need to use a template fc templateExtent = 'E:/ArcMap/1412extent.shp'  # Each output cell will be a polygon geometryType = 'POLYGON'  arcpy.CreateFishnet_management(outFeatureClass, originCoordinate, yAxisCoordinate, cellSizeWidth, cellSizeHeight, numRows, numColumns, oppositeCoorner, labels, templateExtent, geometryType)
0 Kudos
1 Solution

Accepted Solutions
FabianBlau
Occasional Contributor II
The inputs have to be strings (look at the examples):
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000002q000000

# Set the origin of the fishnet originCoordinate = "%f %f" % (pxy.X,pxy.Y)  # Set the orientation yAxisCoordinate = "%f %f" % (pxy.X,(pxy.Y+1))

View solution in original post

0 Kudos
3 Replies
FabianBlau
Occasional Contributor II
The inputs have to be strings (look at the examples):
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000002q000000

# Set the origin of the fishnet originCoordinate = "%f %f" % (pxy.X,pxy.Y)  # Set the orientation yAxisCoordinate = "%f %f" % (pxy.X,(pxy.Y+1))
0 Kudos
ThomasChudy
New Contributor II
Thanks! That made it for me. %f seems to force it to understand the points as a string? So I'll need to play around with it a bit more.
0 Kudos
curtvprice
MVP Esteemed Contributor
%f seems to force it to understand the points as a string? So I'll need to play around with it a bit more.


%f is a Python string substitution code that formats a number into a text representation of floating point numbers.

>>> "%f %f" % (1,2)
'1.000000 2.000000'


Python is moving toward a new method for substituting strings in situations like this. This new method was implemented at 2.4 and future versions of 3.x will stop supporting the old C-style string substitution, so we might as well start using it, especially since the new method is a bit less cryptic.

originCoordinate = "{0} {1}".format(pxy.X,pxy.Y)
0 Kudos