Hi, I have been working on the code below and it is working, but the fishnet is square to the input polygon and does not follow the angle of the input polygon. I need the fishnets to be angled with the input polygons so the fishnet grid aligns the input polygon grid.
import arcpy
import os
import os, fnmatch, arcpy
from arcpy import env
env.workspace = r"C:\GIS\Projects\TetonRange\Geoprocesing.gdb"
env.overwriteOutput = True
fc = arcpy.GetParameterAsText(0)
output = arcpy.GetParameterAsText(1)
rows = arcpy.SearchCursor(fc)
for row in rows:
x = row.OBJECTID
arcpy.MakeFeatureLayer_management(fc, "fc_lyr", "OBJECTID = " + str(x))
rows2 = arcpy.SearchCursor("fc_lyr")
for row2 in rows2:
XMIN = row2.shape.extent.XMin
YMIN = row2.shape.extent.YMin
XMAX = row2.shape.extent.XMax
YMAX = row2.shape.extent.YMax
YMIN2 = row2.shape.extent.YMin + 10
orig_coord = str(XMIN) + " " + str(YMIN)
y_axis = str(XMIN) + " " + str(YMIN2)
corner_coord = str(XMAX) + " " + str(YMAX)
arcpy.CreateFishnet_management("Fishnet_" + str(x), orig_coord, y_axis, "0", "0", "2", "2", corner_coord, "NO_LABELS", "", "POLYGON")
#arcpy.analysis.Intersect("Fishnet_"+str(x) ; "fc_lyr","clip_"+str(x)), "ALL". None, "Input")
arcpy.Clip_analysis("Fishnet_"+str(x), "fc_lyr","clip_"+str(x))
del row, rows, row2, rows2
mergeList = arcpy.ListFeatureClasses("clip_*")
arcpy.Merge_management(mergeList, "merge_poly")
arcpy.AddField_management("merge_poly","ACRES","Double")
arcpy.management.DefineProjection("merge_poly", 'GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]')
arcpy.analysis.Intersect([fc,"merge_poly"], output, "ALL", None, "INPUT")
arcpy.management.CalculateGeometryAttributes(output, "ACRES AREA_GEODESIC", '', "ACRES_US", 'GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]', "SAME_AS_INPUT")
#arcpy.management.DeleteField(output, "FID_OK_sections1_test;OBJECTID;Shape_Leng;FID_merge_poly", "DELETE_FIELDS")
# Create a new field called "Quarter" in the fishnet grid feature class
#quarter_name = "Quarter"
arcpy.AddField_management(output, "Quarter", "TEXT", field_length=2)
# Update the "Quarter" field for each fishnet polygon
with arcpy.da.UpdateCursor(output, ["SHAPE@", "Quarter"]) as cursor:
for row in cursor:
fishnet_polygon = row[0]
quarter = ""
# Get the centroid of the fishnet polygon
fishnet_centroid = fishnet_polygon.centroid
# Iterate through each original polygon
with arcpy.da.SearchCursor(fc, ["SHAPE@"]) as poly_cursor:
for poly_row in poly_cursor:
original_polygon = poly_row[0]
# Compare the centroid of the original polygon with the fishnet centroid
if original_polygon.contains(fishnet_centroid):
# Determine the quarter based on the spatial location
if fishnet_centroid.X < original_polygon.centroid.X:
if fishnet_centroid.Y < original_polygon.centroid.Y:
quarter = "SW"
else:
quarter = "NW"
else:
if fishnet_centroid.Y < original_polygon.centroid.Y:
quarter = "SE"
else:
quarter = "NE"
break # Exit the loop if a match is found
# Update the "Quarter" field
row[1] = quarter
cursor.updateRow(row)
delete_identical_rows = arcpy.management.DeleteIdentical(output, ["FRSTDIVID","Quarter"])
print("Quarter field populated successfully.")
print("Complete")
Code formatting ... the Community Version - Esri Community
will provide proper formatting and line numbers for reference
Perhaps the following, but you had better check the indentation logic... I was beginning to lose its focus
import arcpy
import os
import os, fnmatch, arcpy
from arcpy import env
env.workspace = r"C:\GIS\Projects\TetonRange\Geoprocesing.gdb"
env.overwriteOutput = True
fc = arcpy.GetParameterAsText(0)
output = arcpy.GetParameterAsText(1)
rows = arcpy.SearchCursor(fc)
for row in rows:
x = row.OBJECTID
arcpy.MakeFeatureLayer_management(
fc,
"fc_lyr",
"OBJECTID = " + str(x)
)
rows2 = arcpy.SearchCursor("fc_lyr")
for row2 in rows2:
XMIN = row2.shape.extent.XMin
YMIN = row2.shape.extent.YMin
XMAX = row2.shape.extent.XMax
YMAX = row2.shape.extent.YMax
YMIN2 = row2.shape.extent.YMin + 10
orig_coord = str(XMIN) + " " + str(YMIN)
y_axis = str(XMIN) + " " + str(YMIN2)
corner_coord = str(XMAX) + " " + str(YMAX)
arcpy.CreateFishnet_management(
"Fishnet_" + str(x),
orig_coord, y_axis,
"0", "0", "2", "2",
corner_coord,
"NO_LABELS",
"",
"POLYGON"
)
# arcpy.analysis.Intersect("Fishnet_"+str(x) ; "fc_lyr","clip_"+str(x)), "ALL". None, "Input")
arcpy.Clip_analysis(
"Fishnet_" + str(x),
"fc_lyr",
"clip_" + str(x)
)
del row, rows, row2, rows2
mergeList = arcpy.ListFeatureClasses("clip_*")
arcpy.Merge_management(mergeList, "merge_poly")
arcpy.AddField_management("merge_poly", "ACRES", "Double")
# -- substitute the 2nd parameter below with thecoordinate system factory code
arcpy.management.DefineProjection(
"merge_poly",
'GEOGCS["GCS_North_American_1983", DATUM["D_North_American_1983", SPHEROID["GRS_1980", 6378137.0, 298.257222101]], PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
)
arcpy.analysis.Intersect(
[fc,"merge_poly"], output, "ALL", None, "INPUT"
)
# -- substitute the last parameter below with thecoordinate system factory code
arcpy.management.CalculateGeometryAttributes(
output,
"ACRES AREA_GEODESIC",
'',
"ACRES_US",
'GEOGCS["GCS_North_American_1983", DATUM["D_North_American_1983", SPHEROID["GRS_1980", 6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]', "SAME_AS_INPUT"
)
# arcpy.management.DeleteField(output, "FID_OK_sections1_test;OBJECTID;Shape_Leng;FID_merge_poly", "DELETE_FIELDS")
# Create a new field called "Quarter" in the fishnet grid feature class
#quarter_name = "Quarter"
arcpy.AddField_management(output, "Quarter", "TEXT", field_length=2)
# Update the "Quarter" field for each fishnet polygon
with arcpy.da.UpdateCursor(output, ["SHAPE@", "Quarter"]) as cursor:
for row in cursor:
fishnet_polygon = row[0]
quarter = ""
# Get the centroid of the fishnet polygon
fishnet_centroid = fishnet_polygon.centroid
# Iterate through each original polygon
with arcpy.da.SearchCursor(fc, ["SHAPE@"]) as poly_cursor:
for poly_row in poly_cursor:
original_polygon = poly_row[0]
# Compare the original polygon centroid with the fishnet centroid
if original_polygon.contains(fishnet_centroid):
# Determine the quarter based on the spatial location
if fishnet_centroid.X < original_polygon.centroid.X:
if fishnet_centroid.Y < original_polygon.centroid.Y:
quarter = "SW"
else:
quarter = "NW"
else:
if fishnet_centroid.Y < original_polygon.centroid.Y:
quarter = "SE"
else:
quarter = "NE"
break # Exit the loop if a match is found
# Update the "Quarter" field
row[1] = quarter
cursor.updateRow(row)
delete_identical_rows = arcpy.management.DeleteIdentical(output, ["FRSTDIVID", "Quarter"])
print("Quarter field populated successfully.")
print("Complete")
Sorry about that. It got messed up before I inserted the code properly.