-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcExtent.py
84 lines (75 loc) · 2.51 KB
/
fcExtent.py
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
#--------------------------------------------------------------
# Purpose: Creates a rectangle polygon feature class
# around an existing feature class.
#
# Author:
# Website:
#
# Created: 04/28/2015
# Modified
#
#--------------------------------------------------------------
import arcpy
from arcpy import env
env.overwriteOutput = True
# todo: get arg from inside arcmap toc fc
fc = r"c:\Temp\hotel.shp"
desc = arcpy.Describe(fc)
extent = desc.extent
pts = [arcpy.Point(extent.XMin, extent.YMin),
arcpy.Point(extent.XMax, extent.YMin),
arcpy.Point(extent.XMax, extent.YMax),
arcpy.Point(extent.XMin, extent.YMax)]
array = arcpy.Array(items = pts)
poly = arcpy.Polygon(array)
# todo: get arg for output fc.
arcpy.CopyFeatures_management(poly, r"%scratchgdb%\way1")
# Method for individual features using a cursor with extent XMin,
# YMin, XMax, and YMax, and arcpy.CreateFeatureClass().
import arcpy
from arcpy import env
env.overwriteOutput = True
fc = r"c:\States.shp"
out_fc = r"%scratchgdb%\way2"
if not arcpy.Exists(out_fc):
arcpy.CreateFeatureclass_management(out_path="%scratchgdb%",
out_name="way2", geometry_type="POLYGON",
spatial_reference=arcpy.SpatialReference(4326))
icur = arcpy.da.InsertCursor(out_fc, "SHAPE@")
with arcpy.da.SearchCursor(fc, "SHAPE@") as rows:
for row in rows:
extent = row[0].extent
pts = [arcpy.Point(extent.XMin, extent.YMin),
arcpy.Point(extent.XMax, extent.YMin),
arcpy.Point(extent.XMax, extent.YMax),
arcpy.Point(extent.XMin, extent.YMax)]
array = arcpy.Array(items=pts)
poly = arcpy.Polygon(array)
icur.insertRow([poly])
del array
del poly
del pts
del extent
del row
# Method for individual features using a cursor and arcpy.CopyFeatures().
import arcpy
from arcpy import env
env.overwriteOutput = True
fc = r"c:\States.shp"
out_fc = r"%scratchgdb%\way3"
with arcpy.da.SearchCursor(fc, "SHAPE@") as rows:
polys = []
array = arcpy.Array()
for row in rows:
extent = row[0].extent
array.add(extent.lowerLeft)
array.add(extent.lowerRight)
array.add(extent.upperRight)
array.add(extent.upperLeft)
array.add(extent.lowerLeft)
polys.append(arcpy.Polygon(array))
array.removeAll()
del row
del array
arcpy.CopyFeatures_management(polys, out_fc)
del polys