-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
574 lines (461 loc) · 17.1 KB
/
common.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# COMMON.PY
# A common module for all scripts in the Interactions toolbox.
import sys, os, arcpy, operator, traceback, numpy, random
# constants defining neighbour table field names
NEIGH_FROM_FLD = 'ID_FROM'
NEIGH_TO_FLD = 'ID_TO'
# signals if debug messages are to be printed
debugMode = False
try:
from debug import *
except ImportError:
pass
# field type names for new fields
PY_TYPE_TO_OUT = {unicode : 'TEXT', str : 'TEXT', int : 'LONG', float : 'DOUBLE', numpy.float64 : 'DOUBLE'}
IN_TYPE_TO_PY = {'Integer' : int, 'Double' : float, 'String' : unicode, 'SmallInteger' : int, 'OID' : int}
PY_TYPE_TO_STR = {unicode : 'unicode', str : 'str', int : 'int', float : 'float', numpy.float64 : 'float'}
INT_FIELD_DESCRIBE = 'Integer'
NET_FIELDS = [('SourceID', 'SourceID', None), ('SourceOID', 'SourceOID', None), ('PosAlong', 'PosAlong', None), ('SideOfEdge', 'SideOfEdge', None)]
NET_FIELD_MAPPINGS = 'SourceID SourceID #;SourceOID SourceOID #;PosAlong PosAlong #;SideOfEdge SideOfEdge #'
DEFAULT_SPATIAL_EXT = 'shp' # nondatabase default file extension
DEFAULT_TABLE_EXT = 'dbf'
ORIGIN_MARKER = 'O_' # field markers for od attributes of interactions
DESTINATION_MARKER = 'D_'
SHAPE_KEY = 'SHAPE' # auxiliary key for shape information
SHAPE_TYPE = 'SHAPE'
def checkFile(file):
if not arcpy.Exists(file):
raise IOError, '%s does not exist' % file.decode('cp1250').encode('utf8')
return file
def query(target, mssql, *args):
'''Prepares a mssql query to target such that fields are properly quoted.'''
main = (mssql % args)
if '.mdb' in target: # is in personal geodatabase (square brackets used)
return main
else: # quotes used
return main.replace('[', '"').replace(']', '"')
def inTypeToOut(inType):
try:
return PY_TYPE_TO_OUT[IN_TYPE_TO_PY[inType]]
except KeyError:
raise ValueError, 'field of unknown type: ' + str(inType)
def inTypeToPy(inType):
try:
return IN_TYPE_TO_PY[inType]
except KeyError:
raise ValueError, 'field of unknown type: ' + str(inType)
def pyTypeToOut(pyType):
try:
return PY_TYPE_TO_OUT[pyType]
except KeyError:
raise ValueError, 'field of unknown type: ' + str(pyType)
def fieldType(pythonType):
'''Returns a string for the passed Python type that may be used in arcpy.AddField as field type.'''
return pyTypeToOut(pythonType)
def describeToField(describe):
return inTypeToOut(describe)
def typeOfField(layer, field):
return outTypeOfField(layer, field)
def outTypeOfField(layer, field):
return inTypeToOut(inTypeOfField(layer, field))
def pyTypeOfField(layer, field):
return inTypeToPy(inTypeOfField(layer, field))
def inTypeOfField(layer, field):
typeList = fieldTypeList(layer)
if field in typeList:
return typeList[field]
else:
fldNames = list(typeList.keys())
for lyrFld in fldNames:
typeList[lyrFld.upper()] = typeList[lyrFld]
if field.upper() in typeList:
return typeList[field.upper()]
else:
raise ValueError, u'field %s not found in %s' % (field, layer)
def pyStrOfType(pyType):
try:
return PY_TYPE_TO_STR[pyType]
except KeyError:
raise ValueError, 'field of unknown type: ' + str(pyType)
def addFields(layer, fields, strict=False):
fieldList = fieldTypeList(layer)
for name, fldType in fields:
if name in fieldList:
if strict:
raise ValueError, 'field %s already exists' % name
else:
if inTypeToPy(fieldList[name]) == fldType:
warning('field %s already exists' % name)
continue
else:
warning('field %s already exists with different type %s: will be deleted' % (name, fieldList[name]))
arcpy.DeleteField_management(layer, [name])
arcpy.AddField_management(layer, name, pyTypeToOut(fldType))
def featurePath(location, file, ext=DEFAULT_SPATIAL_EXT):
return addExt(os.path.join(location, file), ext)
def tablePath(location, file, ext=DEFAULT_TABLE_EXT):
return addExt(os.path.join(location, file), ext)
def tableName(location, file, ext=DEFAULT_TABLE_EXT):
if isInDatabase(location):
return file
else:
return addTableExt(file, ext)
def addExt(path, ext=DEFAULT_SPATIAL_EXT):
if not isInDatabase(path) and not hasExt(path):
return path + '.' + ext
else:
return path
def addFeatureExt(path, ext=DEFAULT_SPATIAL_EXT):
return addExt(path, ext)
def addTableExt(path, ext=DEFAULT_TABLE_EXT):
return addExt(path, ext)
def hasExt(path):
return path.rfind('.') > max(path.rfind('/'), path.rfind('\\'))
def parameters(number):
'''Returns tool parameters from the tool input as strings.'''
if len(sys.argv) == 1:
sys.exit(1)
params = []
for i in range(number):
params.append(arcpy.GetParameterAsText(i))
return params
def setParameter(paramIndex, output):
arcpy.SetParameterAsText(paramIndex, output)
def count(layer):
'''Counts the number of features (rows) in the layer.'''
return int(arcpy.GetCount_management(layer).getOutput(0))
def fieldTypeList(layer, type=None):
'''Returns a dict of field names : types of the specified layer attributes.'''
if type is None:
flist = arcpy.ListFields(layer)
else:
flist = arcpy.ListFields(layer, '', type)
fields = {}
for field in flist:
fields[field.name] = field.type
return fields
def fieldList(layer, type=None):
'''Returns a list of field names of the specified layer attributes.'''
if type is None:
return [field.name for field in arcpy.ListFields(layer)]
else:
return [field.name for field in arcpy.ListFields(layer, '', type)]
def parseFields(fieldList):
'''Parses fields passed from the tool input.'''
fields = fieldList.split(';')
i = 0
while i < len(fields):
if fields[i] == '':
fields.pop(i)
else:
fields[i] = fields[i].strip("'")
i += 1
return fields
def parseStats(stats):
'''Parses statistics setup.'''
return [item.split(' ') for item in stats.split(';')]
def statListToFields(statList):
'''Converts a list of statistics setup to resulting field names.'''
return [item[1] + '_' + item[0] for item in statList]
def isView(layer):
desc = arcpy.Describe(layer)
return (desc.dataType in (u'FeatureLayer', u'TableView'))
def isLayer(layer):
desc = arcpy.Describe(layer)
return (desc.dataType == u'FeatureLayer')
def isShapefile(layer):
desc = arcpy.Describe(layer)
return (desc.dataType == u'ShapeFile' or (desc.dataType == u'FeatureLayer' and desc.dataElement.dataType == u'ShapeFile'))
def isTableView(layer):
desc = arcpy.Describe(layer)
return (desc.dataType == u'TableView')
def isFeatureClass(layer):
desc = arcpy.Describe(layer)
return (desc.dataType in (u'FeatureClass', u'ShapeFile'))
def hasGeometry(layer):
desc = arcpy.Describe(layer)
print desc.dataType, desc.dataType, desc.dataType
return (desc.dataType in (u'FeatureLayer', u'FeatureClass', u'ShapeFile'))
def toFeatureClass(layer):
desc = arcpy.Describe(layer)
if desc.dataType in (u'FeatureClass', u'ShapeFile'):
return layer
elif desc.dataType == u'FeatureLayer':
return desc.dataElement.catalogPath
else:
raise ValueError, 'cannot convert %s (type %s) to feature class' % (layer, desc.dataType, desc.dataElementType)
def selection(source, target, query):
if hasGeometry(source):
arcpy.MakeFeatureLayer_management(source, target, query)
else:
arcpy.MakeTableView_management(source, target, query)
def select(source, target, query):
if hasGeometry(source):
arcpy.Select_analysis(source, target, query)
else:
arcpy.TableSelect_analysis(source, target, query)
def copy(source, target):
if hasGeometry(source):
arcpy.CopyFeatures_management(source, target)
else:
arcpy.CopyRows_management(source, target)
def multiplyDistance(dist, mult):
distNum, distUnit = dist.split()
return str(int(float(distNum) * mult)) + ' ' + distUnit
def getSource(layer):
desc = arcpy.Describe(layer)
return (desc.dataType == u'FeatureClass')
def isInDatabase(location):
'''Returns True if the location is inside a file or personal geodatabase, False otherwise.'''
return ('.gdb' in location or'.mdb' in location)
def folder(location):
'''Returns a system folder in which the specified file is located.'''
while isInDatabase(location):
location = os.path.dirname(location)
return location
def location(path):
if hasExt(path):
while hasExt(path):
path = os.path.dirname(path)
return path
else:
if isFeatureClass(path):
return os.path.dirname(path)
else:
return location(toFeatureClass(path))
def toInt(value, name):
try:
return int(str(value))
except (ValueError, UnicodeEncodeError):
raise ValueError, 'invalid ' + name + ' format: must be an integral number, got ' + str(value)
def toFloat(value, name):
try:
return float(str(value).replace(',', '.'))
except (ValueError, UnicodeEncodeError):
raise ValueError, 'invalid ' + name + ' format: must be a number, got ' + str(value)
def toBool(value, name):
try:
return bool(str(value).lower() == 'true' or str(value) == '1')
except (ValueError, TypeError):
raise ValueError, 'invalid ' + name + ' format: must be a number or "true" or "false", got ' + str(value)
def invertQuery(query):
return '' if not query else ('NOT (' + query + ')')
def inBounds(val, min, max):
if val < min: return min
elif val > max: return max
else: return val
def maxKey(dictionary):
'''Returns a key from the dictionary that corresponds to the highest value.'''
return max(dictionary.iteritems(), key=operator.itemgetter(1))[0]
def constantLambda(value):
return (lambda whatever: value)
def sublayer(layer, subName):
return arcpy.mapping.ListLayers(layer, subName)[0]
def getShapeType(layer):
return arcpy.Describe(layer).shapeType.upper()
def recreate(layer, location, outName, transferFlds):
outPath = addFeatureExt(arcpy.CreateFeatureclass_management(location, outName, getShapeType(layer), spatial_reference=layer).getOutput(0))
fieldTypes = fieldTypeList(layer)
for fld in transferFlds:
arcpy.AddField_management(outPath, fld, inTypeToOut(fieldTypes[fld]))
return outPath
def createTable(path, useDBF=True):
location = os.path.dirname(path)
name = os.path.basename(path)
return arcpy.CreateTable_management(
location, (addTableExt(name) if (useDBF and not isInDatabase(location)) else name)
).getOutput(0)
def createFeatureClass(path, shapeType=None, template=None, crs=None):
if not (template or crs):
raise ValueError, 'insufficient shape data for {} provided'.format(path)
if shapeType is None and crs is not None:
shapeType = getShapeType(crs)
return addFeatureExt(arcpy.CreateFeatureclass_management(
os.path.dirname(path), os.path.basename(path),
geometry_type=shapeType, template=template, spatial_reference=crs).getOutput(0))
def overwrite(val=True):
arcpy.env.overwriteOutput = val
def delete(*args):
try:
for item in args:
arcpy.Delete_management(item)
except:
pass
def addField(layer, name, fldType):
if isShapefile(layer):
name = name[:10]
arcpy.AddField_management(layer, name, pyTypeToOut(fldType))
class runtool:
def __init__(self, parcount=0, debug=None, overwrite=True):
if debug is not None:
debugMode = debug
arcpy.env.overwriteOutput = overwrite
if parcount:
self.params = parameters(parcount)
def __getitem__(self, index):
return self.params[index]
def __enter__(self):
return self.params
def __exit__(self, exc_type, exc_value, tb):
if exc_type is not None:
if debugMode:
debug('\n'.join(traceback.format_exception(exc_type, exc_value, tb)))
else:
arcpy.AddError(u'{} {}'.format(exc_type, exc_value))
return True
else:
done()
class Messenger:
def __init__(self, debugMode=False):
pass
def done(self):
'''Signals ArcPy that the script has successfully terminated. If the script is running in a debug mode, raises an error to bring the tool dialog up again for debugger's convenience; otherwise just displays the message.'''
done()
def message(self, mess):
'''Adds a simple message to the output log.'''
message(mess)
def progress(self, message):
'''Signals tool progress by setting the progressor label. In debug mode, prints a message instead.'''
progress(message)
def progressor(self, message, count):
return progressor(message, count)
def debug(self, message):
'''Displays a debug message (only in debug mode).'''
debug(message)
def warning(self, message):
'''Displays a warning.'''
warning(message)
def error(self, message):
'''Raises an error with the specified message using the ArcPy mechanism.'''
error(message)
def getDebugMode(self):
return debugMode
class MutedMessenger(Messenger):
def __init__(self):
pass
def message(self, message):
pass
def progress(self, message):
pass
def progressor(self, message, count):
return MutedProgressBar()
class ProgressBar:
'''ArcPy progress bar control class.'''
def __init__(self, text, count):
'''Initializes the progress bar going from 0 to 100 % with the text to display above it and the number of steps to be performed.'''
self.text = text
self.count = count
self.progressBy = 100.0 / self.count
self.posBy = int(self.progressBy) if int(self.progressBy) >= 1 else 1
self.position = 0 # progressbar position
self.posExact = 0 # exact position (noninteger)
self.progress = 0 # how many of count has passed
arcpy.SetProgressor('step', self.text, self.position, 100, self.posBy)
def move(self):
'''Signals the progress bar that one step has been performed. The progress bar may move forward if the step means at least one per cent difference.'''
self.progress += 1
self.posExact += self.progressBy
if int(self.posExact) > self.position: # if crossed per cent to one more
self.position = int(self.posExact)
arcpy.SetProgressorPosition(self.position)
def end(self):
'''Ends the counting and resets the progressor.'''
arcpy.ResetProgressor()
class MutedProgressBar(ProgressBar):
def __init__(self, *args):
pass
def move(self):
pass
def end(self):
pass
class MessagingClass:
def __init__(self, messenger=Messenger(True)):
self.messenger = messenger
def done(self):
self.messenger.done()
class PathManager:
def __init__(self, outPath, delete=True):
self.outPath = outPath
self.location = os.path.dirname(self.outPath)
self.outName = os.path.basename(self.outPath)
self.tmpCount = -1
self.delete = True
def __enter__(self):
self.tmpFiles = []
self.tmpFields = {}
random.seed(self.location)
return self
def tmpFile(self):
tmp = self._tmpPath()
self.tmpFiles.append(tmp)
return tmp
def tmpLayer(self):
return self._tmpName()
def tmpField(self, layer, fldType):
name = self._tmpName().upper()
arcpy.AddField_management(layer, name, pyTypeToOut(fldType))
if layer not in self.tmpFields: self.tmpFields[layer] = []
self.tmpFields[layer].append(name)
return name
def _tmpName(self):
self.tmpCount += 1
return 'tmp_{:02d}{:04d}'.format(self.tmpCount, int(1e4 * random.random()))
def _tmpPath(self):
return featurePath(self.location, self._tmpName())
def __exit__(self, *args):
if self.delete:
progress('deleting temporary files')
for file in self.tmpFiles:
try:
arcpy.Delete_management(file)
except:
pass
progress('deleting temporary fields')
for layer in self.tmpFields:
try:
arcpy.DeleteField_management(layer, self.tmpFields[layer])
except:
pass
def getLocation(self):
return self.location
def getOutputName(self):
return self.outName
def warning(text):
'''Displays a warning to the tool user.'''
arcpy.AddWarning((u'WARNING: ' if debugMode else u'') + encodeMessage(text))
def debug(*args):
'''Displays a debug message (only in debug mode).'''
if debugMode:
arcpy.AddMessage(u'DEBUG: ' + ' '.join(encodeMessage(arg) for arg in args))
def progress(text):
'''Signals tool progress by setting the progressor label.'''
if debugMode:
arcpy.AddMessage(u'PROGRESS: ' + encodeProgress(text))
arcpy.SetProgressorLabel(encodeProgress(text))
def done():
'''Signals ArcPy that the script has successfully terminated. If the script is running in a debug mode, raises an error to bring the tool dialog up again for debugger's convenience; otherwise just displays the message.'''
if debugMode:
arcpy.AddMessage('PROGRESS: Done.')
# else:
arcpy.SetProgressor('default', 'Done.')
def message(text):
'''Signals an ordinary message to the user.'''
arcpy.AddMessage(encodeMessage(text))
def encodeMessage(text):
'''Encodes the message to UNICODE.'''
try:
if isinstance(text, unicode):
return text
elif isinstance(text, str):
return unicode(text, encoding='utf8')
else:
return str(text)
except (UnicodeEncodeError, UnicodeDecodeError):
return 'unknown message'
def encodeProgress(text):
return encodeMessage(text[:1].upper() + text[1:] + '...')
def progressor(text, count):
progress(text)
return ProgressBar(encodeProgress(text), count)
def getDebugMode():
return debugMode