-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversion.py
More file actions
348 lines (295 loc) · 11.4 KB
/
conversion.py
File metadata and controls
348 lines (295 loc) · 11.4 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import sys, os, arcpy
sys.path.append('.')
import common
class ConversionError(Exception):
pass
class FieldError(ConversionError):
pass
class FieldMapper(object):
TMP_ID_FLD = 'TMP_ID'
TMP_ID_TYPE = 'LONG'
shapeType = None
shapeFieldName = None
outputShapeFieldName = None
def __init__(self, source):
## common.debug('mapping on ' + str(source))
self.source = source
self.fieldTypes = common.fieldTypeList(self.source)
self.mappings = []
self.joinIDFields = []
self.removeSrcFields = []
self.silent = []
self.data = {}
self.idFld = None
self.shape = False
self.errors = 0
def addIDField(self, name, alias=None, transfer=True, check=True):
if transfer:
self.addMapField(name, alias)
elif check and name not in self.fieldTypes:
raise FieldError, 'field %s not found in %s' % (name, self.source)
self.idFld = name
def setIDTransfer(self, transfer, alias=None):
if transfer:
self.addMapField(self.idFld, alias)
else:
self.removeMapField(self.idFld)
if self.idFld not in self.fieldTypes:
raise FieldError, 'field %s not found in %s' % (self.idFld, self.source)
def loadShapeField(self, type=None):
desc = arcpy.Describe(self.source)
if type is None:
type = str(desc.shapeType.upper())
self.shape = True
self.shapeFieldName = desc.ShapeFieldName
self.shapeType = type
# self.mappings.append((self.shapeFieldName, common.SHAPE_KEY, type))
def removeMapField(self, name):
for i in range(len(self.mappings)):
if self.mappings[i][0] == name:
self.mappings.pop(i)
return
def addSrcRemoveField(self, name):
self.removeSrcFields.append(name)
def getMappingDict(self):
outDict = {}
for name, alias, type in self.mappings:
outDict[name] = alias
return outDict
def hasShape(self):
return self.shape
def getShapeType(self):
return self.shapeType
def getCRS(self):
return self.source
def getIntIDField(self, setID=False, unmapPrevious=False, progressor=None):
if self.idFld is not None and self.idFld in common.fieldList(self.source, type=common.INT_FIELD_DESCRIBE):
common.debug('integer field found')
if progressor is not None: progressor.end()
return self.idFld
else:
tmpFld = self.createTempIDFld(progressor)
self.addSrcRemoveField(tmpFld)
if setID:
if unmapPrevious:
self.removeMapField(self.idFld)
self.addIDField(tmpFld, transfer=False, check=False)
return tmpFld
def createTempIDFld(self, progressor=None):
arcpy.AddField_management(self.source, self.TMP_ID_FLD, self.TMP_ID_TYPE)
i = 1
rows = arcpy.UpdateCursor(self.source)
for row in rows:
row.setValue(self.TMP_ID_FLD, i)
rows.updateRow(row)
i += 1
if progressor is not None: progressor.move()
del rows
if progressor is not None: progressor.end()
return self.TMP_ID_FLD
def loadData(self, progressor=None):
cursor = arcpy.SearchCursor(self.source)
names = self.getMappingDict()
for row in cursor:
id = unicode(row.getValue(self.idFld))
self.data[id] = {}
for name in names:
val = row.getValue(name)
self.data[id][name] = val
if self.shape:
self.data[id][common.SHAPE_KEY] = self.parseShape(row.getValue(self.shapeFieldName))
if progressor is not None: progressor.move()
del row, cursor
# common.debug(self.data)
if progressor is not None: progressor.end()
def addSilentField(self, name, pyType=str):
self.silent.append((name, common.pyTypeToOut(pyType)))
def parseShape(self, geom):
if self.shapeType == 'POINT':
return geom.getPart()
else:
return geom
def open(self, output):
self.mapDict = self.getMappingDict()
for name, aliases, outType in self.mappings:
for alias in aliases:
arcpy.AddField_management(output, alias, outType)
for name, outType in self.silent:
arcpy.AddField_management(output, name, outType)
outDesc = arcpy.Describe(output)
if hasattr(outDesc, 'ShapeFieldName'):
self.outputShapeFieldName = outDesc.ShapeFieldName
def close(self):
if self.removeSrcFields:
arcpy.DeleteField_management(self.source, self.removeSrcFields)
def getErrorCount(self):
return self.errors
class LinkFieldMapper(FieldMapper):
def addMapField(self, name, alias=None):
if name not in self.fieldTypes:
raise FieldError, 'field %s not found in %s' % (name, self.source)
self.mappings.append((name, [(name if alias is None else alias)], common.describeToField(self.fieldTypes[name])))
def remap(self, inRow, outRow, processor=None):
for name in self.mapDict:
for alias in self.mapDict[name]:
outRow.setValue(alias, inRow.getValue(name))
return outRow
class ODFieldMapper(FieldMapper):
excludeSelf = False
def setJoinIDFields(self, ids):
self.joinIDFields = ids
def setJoinIDGetter(self, field, getter):
self.joinIDSourceField = field
self.joinIDGetter = getter
def excludeSelfInteractions(self, excludeSelf):
self.excludeSelf = excludeSelf
def addMapField(self, name, aliases=None):
if name not in self.fieldTypes:
raise FieldError, 'field %s not found in %s' % (name, self.source)
if aliases is None:
aliases = (common.ORIGIN_MARKER + name, common.DESTINATION_MARKER + name)
self.mappings.append((name, aliases, common.describeToField(self.fieldTypes[name])))
def getIDs(self, inRow):
if self.joinIDFields:
return tuple(unicode(inRow.getValue(idFld)) for idFld in self.joinIDFields)
elif self.joinIDSourceField:
return self.joinIDGetter(inRow.getValue(self.joinIDSourceField))
else:
raise FieldError, 'join IDs not specified'
def remap(self, inRow, outRow, processor=None):
odData = []
ids = self.getIDs(inRow)
if self.excludeSelf and len(ids) > len(frozenset(ids)):
return None
for i in range(len(ids)):
try:
data = self.data[ids[i]]
except KeyError:
return None
for name in self.mapDict:
outRow.setValue(self.mapDict[name][i], data[name])
odData.append(data)
if processor and odData:
attrs = processor(inRow, odData)
if attrs is None:
self.errors += 1
else:
if 'shape' in attrs:
attrs[self.outputShapeFieldName] = attrs['shape']
del attrs['shape']
for key, value in attrs.iteritems():
outRow.setValue(key, value)
return outRow
class Converter:
def __init__(self, location=None, **kwargs):
common.progress('initializing paths')
if location:
self.location = common.checkFile(location)
def folderPath(self, file):
return os.path.join(common.folder(self.location), file)
def path(self, file):
return os.path.join(self.location, file)
def tablePath(self, file):
return common.tablePath(self.location, file)
def createTable(self, outName):
return common.createTable(os.path.join(self.location, outName))
# return arcpy.CreateTable_management(self.location, common.tableName(self.location, outName)).getOutput(0)
def createFeatureClass(self, outName, type, crs):
# raise RuntimeError, (('%s ' * 4) % (self.location, outName, type, crs))
return common.addFeatureExt(arcpy.CreateFeatureclass_management(self.location, outName, type, spatial_reference=crs).getOutput(0))
def open(self, output, mappers):
common.progress('preparing output')
for mapper in mappers:
mapper.open(output)
def remapData(self, source, output, mappers=[], processor=None):
count = common.count(source)
if count == 0:
common.warning('remapping source is empty, empty output generated')
return
prog = common.progressor('remapping records', count)
inCur = arcpy.SearchCursor(source)
outCur = arcpy.InsertCursor(output)
# common.debug('remapping')
for inRow in inCur:
outRow = outCur.newRow()
for mapper in mappers:
outRow = mapper.remap(inRow, outRow, processor)
# common.debug(mapper, outRow)
if outRow is None:
break
if outRow is not None:
outCur.insertRow(outRow)
prog.move()
prog.end()
del inCur, inRow, outCur, outRow
def setOutput(self, output, paramIndex=None):
if paramIndex is not None:
common.setParameter(paramIndex, output)
class SpatialMatrixNeighbourLinker(Converter):
NEIGH_ID_FLD = 'NID'
SWM_FILE_NAME = 'tmp_swmf.swm'
TABLE_FILE_NAME = 'tmp_swmt'
def __init__(self, zones, zoneIDFld, method, location, **kwargs):
zones = common.toFeatureClass(common.checkFile(zones))
if not location:
location = common.location(zones)
Converter.__init__(self, location, **kwargs)
self.zones = zones
self.zoneMapper = ODFieldMapper(self.zones)
self.zoneMapper.addIDField(zoneIDFld, [common.NEIGH_FROM_FLD, common.NEIGH_TO_FLD], transfer=True)
self.method = method
self.tmpTable = self.tablePath(self.TABLE_FILE_NAME)
self.swmFile = self.folderPath(self.SWM_FILE_NAME)
common.overwrite(True)
def process(self):
# check if ID is integer - if not, create an integer field
count = common.count(self.zones)
idFld = self.zoneMapper.getIntIDField(setID=True, unmapPrevious=False, progressor=common.progressor('creating temporary IDs', count))
self.zoneMapper.loadData(common.progressor('loading zone data', count))
# generate SWM file
common.progress('generating spatial matrix')
arcpy.GenerateSpatialWeightsMatrix_stats(self.zones, idFld, self.swmFile, self.method)
common.progress('converting matrix to table')
arcpy.ConvertSpatialWeightsMatrixtoTable_stats(self.swmFile, self.tmpTable)
self.zoneMapper.setJoinIDFields([idFld, self.NEIGH_ID_FLD])
def output(self, outName, paramIndex=None):
output = self.createTable(outName)
self.open(output, [self.zoneMapper])
self.remapData(self.tmpTable, output, [self.zoneMapper])
self.setOutput(output, paramIndex)
return output
def close(self):
try:
self.zoneMapper.close()
os.unlink(self.swmFile)
arcpy.Delete_management(self.tmpTable)
except:
pass
class PolygonToPointConverter(Converter):
def __init__(self, polygons, inside=False, idFld=None, mapping=None, **kwargs):
Converter.__init__(self, **kwargs)
self.polygons = common.checkFile(polygons)
self.inside = inside
# if mapping:
# self.mapper = LinkFieldMapper(self.polygons)
# if idFld:
# self.mapper.addIDField(idFld, alias=(mapping[idFld] if mapping and idFld in mapping else None))
# if mapping:
# for fld, alias in mapping.iteritems():
# if fld != idFld:
# self.mapper.addMapField(fld, alias)
# else:
# self.mapper = None
def convert(self, target):
# if self.mapper:
# self.open(target, [self.mapper])
# self.remapData(self.polygons, target, [self.mapper])
arcpy.FeatureToPoint_management(self.polygons, target, ('INSIDE' if self.inside else 'CENTROID'))
def generateNeighbourTableFor(source, idFld, targetName=None, method='CONTIGUITY_EDGES_ONLY'):
if targetName is None:
targetName = os.path.splitext(os.path.basename(source))[0] + '_neigh'
linker = SpatialMatrixNeighbourLinker(source, idFld, method, os.path.dirname(source))
linker.process()
output = linker.output(targetName)
linker.close()
return output