-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extract.py
101 lines (93 loc) · 2.89 KB
/
Extract.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Extract.py: Extracts ID from tables"""
__appname__ = "Main.py"
__author__ = "Keith E. Davey"
__email__ = "keithedavey@gmail.com"
__version__ = "0.1"
__license__ = "GNU GPL 3.0"
###########
# Modules #
###########
import arcpy
import FeatureClass as fc
import Table as tab
import time
import RegisterData as rd
import Text as t
from operator import itemgetter, attrgetter, methodcaller
# idFromTable: Extracts the unique identifier of each row from a table
# for given inField (CD30515413), extract output fields by splitting and assigning outPrefixField to CD and outIdField to 30515413
def idFromTable(infc, outfc, inField, outIdField, outPrefixField):
tab.copy(infc, outfc)
tables = rd.tables()
arcpy.management.AddField(tables[outfc], outIdField, 'TEXT')
arcpy.management.AddField(tables[outfc], outPrefixField, 'TEXT')
with arcpy.da.UpdateCursor(tables[outfc], (inField, outIdField, outPrefixField)) as cursor:
for row in cursor:
inSplit = row[0].split('[')[1].split(']')[-2]
fid = None
prefix = None
if 'CD' in inSplit:
fid = inSplit.split('CD')[1]
fid = fid[0:2] + fid[4:]
fid = fid[0:4]
prefix = 'CD'
elif 'CCS' in inSplit:
fid = inSplit.split('CCS')[1]
fid = fid[0:2] + fid[4:]
prefix = 'CCS'
elif 'CAR' in inSplit:
fid = inSplit.split('CAR')[1]
prefix = 'CAR'
elif 'PR' in inSplit:
fid = inSplit.split('PR')[1]
prefix = 'PR'
else:
fid = inSplit
prefix = 'CO'
# print fid
row[1] = fid
row[2] = prefix
cursor.updateRow(row)
# Extracts a list of the unique class fields from a table
# given a (assumed) triple of data (item, unit, value) or simple double of data (item, value), extract the unique class fields from the table and write to xml
def fieldsFromTable(tab, tableName, fieldPrimary, fieldSecondary):
tables = rd.tables()
firstRowFlag = True
attributes = {}
prefix = 'at'
i = 0
if fieldSecondary == '':
with arcpy.da.SearchCursor(tables[tab], ['id', fieldPrimary]) as cursor:
for row in sorted(cursor, key=itemgetter(0)):
if firstRowFlag:
ref = row[0]
fid = 'tc' + str(i).zfill(2)
attributes[fid] = row[1]
i+=1
firstRowFlag = False
elif ref == row[0]:
fid = 'tc' + str(i).zfill(2)
attributes[fid] = row[1]
i+=1
else:
break
t.writeToFile(tab[:9] + '.xml', tableName, attributes)
else:
with arcpy.da.SearchCursor(tables[tab], ['id', fieldPrimary, fieldSecondary]) as cursor:
for row in sorted(cursor, key=itemgetter(0)):
if firstRowFlag:
ref = row[0]
fid = 'tc' + str(i).zfill(2)
attributes[fid] = row[1] + ' (' + row[2] + ')'
i+=1
firstRowFlag = False
elif ref == row[0]:
fid = 'tc' + str(i).zfill(2)
attributes[fid] = row[1] + ' (' + row[2] + ')'
i+=1
else:
break
t.writeToFile(tab[:9] + '.xml', tableName, attributes)
return attributes