-
Notifications
You must be signed in to change notification settings - Fork 4
/
ShpFeature.py
216 lines (162 loc) · 7.21 KB
/
ShpFeature.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
class ShpFeature(object):
'''
This is a base class for each record in a shapefile.
each record corresponds to one row in the shapefile's dbf table.
To clarify the distinction between a feature and a record:
the record is the feature + the dbf data associated with the feature
the feature is shape (or multi-shape)
In this class, I attach the dbf data for each record to it's feature.
'''
def __init__(self, shpFile, recordNumber):
self.shpFile = shpFile
self.recordNumber = recordNumber
self.shapeType = shpFile.shapeType
self.dbfData = self.readDbfData()
def readDbfData(self):
db = self.shpFile.dbfTable
dbfData = {}
for i in range(len(db[0])): # for each column
dbfData[db[0][i]] = db[self.recordNumber + 2][i]
return dbfData
def make3D(self, zVals=None):
"""
This method will take a list of z values and use them to create a new
attribute called points3D, which contains 3d points. ESRI shapefiles
place z values in a separate list form x and y values for each point,
so this essentially integrates those values to make full 3d points.
If the number of z values does not match the number of points,
then this method will not work.
This method will overwrite the points3d attribute if it already exists.
"""
# if there's not enough z values for the number of points
# or not enough points for the number of z values
if not zVals:
zVals = [0 for i in range(self.numPoints)]
if len(zVals) != self.numPoints:
print "The number of Z values does not correspond to the number of points."
return
# clear points3D
self.points3D = []
for i in range(len(zVals)):
z = zVals[i]
x = self.points[i][0]
y = self.points[i][1]
point3d = (x,y,z)
self.points3D.append(point3d)
def chopParts(self, partlist, pointlist):
if type(partlist) == tuple:
indexSpread = list(partlist)
elif type(partlist) == list:
indexSpread = partlist
else:
indexSpread = [partlist]
indexSpread.append(len(pointlist))
chunks = []
for i in range(len(indexSpread) - 1):
chunks.append(pointlist[indexSpread[i]:indexSpread[i+1]])
return chunks
class ShpPoint(ShpFeature):
def __init__(self, ShpFile, recordNumber):
ShpFeature.__init__(self, ShpFile, recordNumber)
self.parts = [0]
self.numParts = 1
self.numPoints = 1
self.points = [self.shpFile._readPoint()]
self.x = self.points[0][0]
self.y = self.points[0][1]
self.make3D()
class ShpPointM(ShpPoint):
def __init__(self,ShpFile, recordNumber):
ShpPoint.__init__(self, ShpFile, recordNumber)
self.m = self.shpFile._readZ()
self.make3D()
class ShpPointZ(ShpPoint):
def __init__(self,ShpFile, recordNumber):
ShpPoint.__init__(self, ShpFile, recordNumber)
self.z = self.shpFile._readZ()
self.m = self.shpFile._readZ()
self.make3D([self.z])
class ShpMultiPoint(ShpFeature):
def __init__(self,ShpFile, recordNumber):
ShpFeature.__init__(self, ShpFile, recordNumber)
self.parts[0]
self.numParts = 1
self.boundingBox = self.shpFile._readBoundingBox()
self.numPoints = self.shpFile._readNumPoints()
self.points = self.shpFile._readPoints(self.numPoints)
self.make3D()
class ShpMultiPointM(ShpMultiPoint):
def __init__(self,ShpFile, recordNumber):
ShpMultiPoint.__init__(self, ShpFile, recordNumber)
self.mBounds = self.shpFile._readZBounds()
self.mArray = self.shpFile._readZArray(self.numPoints)
self.make3D()
class ShpMultiPointZ(ShpMultiPoint):
def __init__(self,ShpFile, recordNumber):
ShpMultiPoint.__init__(self, ShpFile, recordNumber)
self.zBounds = self.shpFile._readZBounds()
self.zArray = self.shpFile._readZArray(self.numPoints)
self.mBounds = self.shpFile._readZBounds()
self.mArray = self.shpFile._readZArray(self.numPoints)
self.make3D(self.zArray)
class ShpPolyLine(ShpFeature):
def __init__(self,ShpFile, recordNumber):
ShpFeature.__init__(self, ShpFile, recordNumber)
self.boundingBox = self.shpFile._readBoundingBox()
self.numParts = self.shpFile._readNumParts()
self.numPoints = self.shpFile._readNumPoints()
self.parts = self.shpFile._readParts(self.numParts)
self.points = self.shpFile._readPoints(self.numPoints)
self.make3D()
class ShpPolyLineM(ShpPolyLine):
def __init__(self,ShpFile, recordNumber):
ShpPolyLine.__init__(self, ShpFile, recordNumber)
self.mBounds = self.shpFile._readZBounds()
self.mArray = self.shpFile._readZArray(self.numPoints)
self.make3D()
class ShpPolyLineZ(ShpPolyLine):
def __init__(self,ShpFile, recordNumber):
ShpPolyLine.__init__(self, ShpFile, recordNumber)
self.zBounds = self.shpFile._readZBounds()
self.zArray = self.shpFile._readZArray(self.numPoints)
self.mBounds = self.shpFile._readZBounds()
self.mArray = self.shpFile._readZArray(self.numPoints)
self.make3D(self.zArray)
class ShpPolygon(ShpFeature):
def __init__(self,ShpFile, recordNumber):
ShpFeature.__init__(self, ShpFile, recordNumber)
self.boundingBox = self.shpFile._readBoundingBox()
self.numParts = self.shpFile._readNumParts()
self.numPoints = self.shpFile._readNumPoints()
self.parts = self.shpFile._readParts(self.numParts)
self.points = self.shpFile._readPoints(self.numPoints)
self.make3D()
class ShpPolygonM(ShpPolygon):
def __init__(self,ShpFile, recordNumber):
ShpPolygon.__init__(self, ShpFile, recordNumber)
self.mBounds = self.shpFile._readZBounds()
self.mArray = self.shpFile._readZArray(self.numPoints)
self.make3D()
class ShpPolygonZ(ShpPolygon):
def __init__(self,ShpFile, recordNumber):
ShpPolygon.__init__(self, ShpFile, recordNumber)
self.zBounds = self.shpFile._readZBounds()
self.zArray = self.shpFile._readZArray(self.numPoints)
self.mBounds = self.shpFile._readZBounds()
self.mArray = self.shpFile._readZArray(self.numPoints)
self.make3D(self.zArray)
class ShpMultiPatch(ShpFeature):
def __init__(self, ShpFile, recordNumber):
ShpFeature.__init__(self, ShpFile, recordNumber)
self.boundingBox = self.shpFile._readBoundingBox()
self.numParts = self.shpFile._readNumParts()
self.numPoints = self.shpFile._readNumPoints()
self.parts = self.shpFile._readParts(self.numParts)
self.partTypes = self.shpFile._readParts(self.numParts)
self.points = self.shpFile._readPoints(self.numPoints)
self.zBounds = self.shpFile._readZBounds()
self.zArray = self.shpFile._readZArray(self.numPoints)
self.mBounds = self.shpFile._readZBounds()
self.mArray = self.shpFile._readZArray(self.numPoints)
self.make3D(self.zArray)
#self.points = [self.shpFile._readPoint()]