-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShapefileProperties.py
153 lines (119 loc) · 5.52 KB
/
ShapefileProperties.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
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
# Purpose: Extract shapefile shape parameters
# Contains code modified from https://www.e-education.psu.edu/geog485/book/export/html/59, ArcGIS 10 help
# OSU GEO 599: https://dl.dropbox.com/u/37858409/Geo599_GIS_Programming/07_3_AccessingAttributes.html
# Created by: Cara Walter
# Modified: 2/27/2013
import arcpy
################################################
# Purpose: Extract feature polygon areas
# Input: PolygonShapefile - Polygon shapefile
# Output: PolyAreas - List of polygon areas
def Area(PolygonShapefile):
try:
# Create search cursor
rows = arcpy.SearchCursor(PolygonShapefile)
# extract the name of the Shape (geometry) field name for the shapefile
ShapeName = arcpy.Describe(PolygonShapefile).shapeFieldName
#Create empty list
PolygonAreas=[]
# For each row, extract the area of the feature
for row in rows:
feat = row.getValue(ShapeName)
PolygonAreas = PolygonAreas + [feat.area]
return(PolygonAreas)
#Print out error from Python
except Exception as TheError:
raise RuntimeError("An error has occurred in ShapeProperties LineArea: "+format(TheError))
################################################
# Purpose: Extract feature coordinates: X, Y, Z
# Input: Shapefile - shapefile
# Output: XYZCoords: [Feature[Point[(X,Y,Z)]]]
# code modified from: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001t000000
def Coordinates(Shapefile):
try:
# Create search cursor
rows = arcpy.SearchCursor(Shapefile)
# extract the name of the Shape (geometry) field name for the shapefile
ShapeName = arcpy.Describe(Shapefile).shapeFieldName
# Create empty list for all features
XYZAllFeatures=[]
# Enter loop for each row
for row in rows:
# Create the geometry object
feat = row.getValue(ShapeName)
# Create counter for each part of each feature
partnum=0
# Create empty list for the coordinates of each feature
XYZIndivFeature=[]
# Step through each part of the feature - make each feature part of a list
for part in feat:
# Step through each vertex in the feature - make each point a tuple (list within list)
for pnt in feat.getPart(partnum):
XYZIndivFeature=XYZIndivFeature+[(pnt.X,pnt.Y,pnt.Z)]
partnum += 1
XYZAllFeatures=XYZAllFeatures+[XYZIndivFeature]
return(XYZAllFeatures)
#Print out error from Python
except Exception as TheError:
raise RuntimeError("An error has occurred in ShapeProperties Coordinates: "+format(TheError))
############################################
# Purpose: Extract feature line lengths
# Input: PolyShapefile - Polyline or polygon shapefile
# Output: PolyLengths - List of polyline lengths
def Length(PolyShapefile):
try:
# Create search cursor
rows = arcpy.SearchCursor(PolyShapefile)
# extract the name of the Shape (geometry) field name for the shapefile
ShapeName = arcpy.Describe(PolyShapefile).shapeFieldName
#Create empty list
PolyLengths=[]
# For each row, extract the length of the feature
for row in rows:
feat = row.getValue(ShapeName)
PolyLengths= PolyLengths + [feat.length]
return(PolyLengths)
#Print out error from Python
except Exception as TheError:
raise RuntimeError("An error has occurredin ShapeProperties LineLength: "+format(TheError))
############################################
# Purpose: Extract list from a field
# Input: TheShapefile
# TheField - string for field name
# Output: TheList - List of field entries
def ListFromField(TheShapefile,TheField):
try:
# Create search cursor
rows = arcpy.SearchCursor(TheShapefile,"","",TheField)
#Create empty list
TheList=[]
# Loop through each row in the attribute field and add to list
for row in rows:
TheList= TheList + [row.getValue(TheField)]
return(TheList)
#Print out error from Python
except Exception as TheError:
raise RuntimeError("An error has occurred in ShapeProperties ListFromField: "+format(TheError))
############################################
# Purpose: Determine which type of shapefile
# Input: Shapefile - point, polyline or polygon shapefile
# Output: ShpType - string of shapefile type
def ShapefileType(Shapefile):
try:
# extract the type of the Shape (geometry) field name for the shapefile
ShpType = arcpy.Describe(Shapefile).shapeType
return(ShpType)
#Print out error from Python
except Exception as TheError:
raise RuntimeError("An error has occurred in ShapeType: "+format(TheError))
############################################
# Purpose: Extract spatial reference
# Input: TheFile - shapefile or raster name and path
# Output: OutParameter - name of the spatial reference as a string
def SpatialReference(TheFile):
try:
TheSpatialReference = arcpy.Describe(TheFile).SpatialReference
return(TheSpatialReference)
#Print out error from Python
except Exception as TheError:
raise RuntimeError("An error has occurred in ShapeProperties SpatialReference: "+format(TheError))