-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGrid_Fixed_Area_Plots.py
More file actions
172 lines (115 loc) · 4.96 KB
/
Grid_Fixed_Area_Plots.py
File metadata and controls
172 lines (115 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#### PARAMETERS____________________________________________________________________________________________________________________
#Imports
import arcpy as a
import math
import random as rr
import os
def find_min(x_list, y_list):
x = x_list[0][0]
for i in range(1, len(x_list)):
if x_list[i][0] < x:
x = x_list[i][0]
y = y_list[0][0]
for i in range(1, len(y_list)):
if y_list[i][0] < y:
y = y_list[i][0]
return '{} {}'.format(x, y)
def find_y(x_list, y_list):
x = x_list[0][0]
for i in range(1, len(x_list)):
if x_list[i][0] < x:
x = x_list[i][0]
y = y_list[0][0]
for i in range(1, len(y_list)):
if y_list[i][0] < y:
y = y_list[i][0]
return '{} {}'.format(x, float(y) + 1)
def find_max(x_list, y_list):
x = x_list[0][0]
for i in range(1, len(x_list)):
if x_list[i][0] > x:
x = x_list[i][0]
y = y_list[0][0]
for i in range(1, len(y_list)):
if y_list[i][0] > y:
y = y_list[i][0]
return '{} {}'.format(x, y)
#Environment Parameters
a.env.workspace = 'in_memory'
a.env.overwriteOutput = True
#Tool Feature Inputs
basefeat = a.GetParameterAsText(0)
acresperplot = float(a.GetParameterAsText(1))
plotsize = float(a.GetParameterAsText(2))
output = a.GetParameterAsText(3)
spatial = a.Describe(basefeat).spatialReference
#Random field for acres in case input feature already has an "ACRE" field
field = ''
for i in range(10):
field += str(chr(rr.randint(97, 122)))
a.CopyFeatures_management(basefeat, 'base_temp')
a.MakeFeatureLayer_management('base_temp', 'base_temp1')
a.AddField_management('base_temp1', field, 'SHORT', 7)
a.CalculateField_management('base_temp1', field, "!SHAPE.AREA@ACRES!", "PYTHON_9.3")
#Get total Acres of basefeat
acres = 0
with a.da.SearchCursor('base_temp1', field) as cur:
for row in cur:
acres += row[0]
a.AddMessage('Total Acres of Stand: {}'.format(acres))
plots_total = round(acres / acresperplot, 0)
a.AddMessage('Number of Plots: {}'.format(plots_total))
square = str(math.sqrt((acres * 43560) / (plots_total)))
a.AddMessage("Grid Size: {}' x {}'".format(int(float(square)), int(float(square))))
#Getting the min and max X/Y point values to use as delimeters for the fishnet
x_list = []
y_list = []
with a.da.SearchCursor('base_temp1', "SHAPE@") as cur_xy:
for row in cur_xy:
for part in row[0]:
for point in part:
if point:
x_list.append([float(point.X), point])
y_list.append([float(point.Y), point])
min_pt = find_min(x_list, y_list)
y_pt = find_y(x_list, y_list)
max_pt = find_max(x_list, y_list)
a.AddMessage('Completed Finding Max X and Y of Stand')
#Creating fishnet and getting the X/Y of the intersects
a.CreateFishnet_management('fishnet_temp', min_pt, y_pt, square, square, number_rows=None, number_columns=None, corner_coord=max_pt, labels='NO_LABELS', template = '#', geometry_type='POLYGON')
pt_list = []
with a.da.SearchCursor('fishnet_temp', "SHAPE@") as cur_xy:
for row in cur_xy:
for part in row[0]:
for point in part:
if (point.X, point.Y) not in pt_list:
pt_list.append((point.X, point.Y))
a.AddMessage('Completed Creating Fishnet')
#Creating 'in_memory' feature class to hold the point values
a.CreateFeatureclass_management('in_memory', 'pt_feat', 'POINT', template=None, has_m=None, has_z=None, spatial_reference=spatial)
for pt in pt_list:
with a.da.InsertCursor('pt_feat', ["SHAPE@X", "SHAPE@Y"]) as cur_pt:
cur_pt.insertRow(pt)
a.AddMessage('Completed Creating Points from Fishnet Vertices')
#Clipping points to base feat and buffering the point feature class to the desired plot size
a.Clip_analysis('pt_feat', basefeat, 'pt_feat_clip')
radius = '{} Feet'.format(math.sqrt((43560 / plotsize) / math.pi))
a.Buffer_analysis('pt_feat_clip', output, radius, 'FULL', 'ROUND', 'NONE', '', 'PLANAR')
a.AddMessage('Completed Clipping and Buffering Points to Fixed Area Plots')
#Adding fields and cleaning up attribute table
a.AddField_management(output, 'PLOT', 'SHORT', 4)
a.AddField_management(output, 'TREE_COUNT', 'SHORT', 4)
a.AddField_management(output, 'PLOT_RAD', 'DOUBLE', 3, 1)
a.AddField_management(output, 'PLOT_ACRE', 'DOUBLE', 4, 1)
a.AddField_management(output, 'TREE_ACRE', 'DOUBLE', 5, 1)
with a.da.UpdateCursor(output, ["BUFF_DIST", "ORIG_FID", "PLOT", "PLOT_RAD", "PLOT_ACRE", "TREE_ACRE"]) as cur_up:
for row in cur_up:
row[3] = row[0]
row[2] = row[1]
row[4] = plotsize
row[5] = 0
cur_up.updateRow(row)
a.DeleteField_management(output, "BUFF_DIST")
a.DeleteField_management(output, "ORIG_FID")
a.AddMessage('Completed Adding Fields and Cleaning Up Plots Feature')
#K:\users\zbee490\ARCPY\t11.shp