-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Fix string values tool to toolbox
- Loading branch information
1 parent
a47002f
commit ef18fcc
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Script to clean up string fields in a GeMS database | ||
## Removes leading and trailing spaces | ||
## Converts "<null>", "" and similar to <null> (system nulls). | ||
# Ralph Haugerud, 28 July 2020 | ||
# Updated to Python 3, 8/20/21 and added to toolbox, Evan Thoms | ||
|
||
import arcpy, os, os.path, sys | ||
from GeMS_utilityFunctions import * | ||
|
||
versionString = 'GeMS_FixStrings_AGP2.py, version of 20 August 2020' | ||
rawurl = 'https://raw.githubusercontent.com/usgs/gems-tools-pro/master/Scripts/GeMS_FixStrings_AGP2.py' | ||
checkVersion(versionString, rawurl, 'gems-tools-pro') | ||
|
||
def fixTableStrings(fc): | ||
fields1 = arcpy.ListFields(fc,'','String') | ||
fields = ['OBJECTID'] | ||
for f in fields1: | ||
fields.append(f.name) | ||
with arcpy.da.UpdateCursor(fc, fields) as cursor: | ||
for row in cursor: | ||
trash = '' | ||
updateRowFlag = False | ||
row1 = [row[0]] | ||
for f in row[1:]: | ||
updateFieldFlag = False | ||
f0 = f | ||
if f != None: | ||
if f != f.strip(): | ||
f = f.strip() | ||
updateFieldFlag = True | ||
if f.lower() == '<null>' or f == '': | ||
f = None | ||
updateFieldFlag = True | ||
if updateFieldFlag: | ||
updateRowFlag = True | ||
addMsgAndPrint( ' '+str(row[0])+' **'+ str(f0)+'**') | ||
row1.append(f) | ||
if updateRowFlag: | ||
try: | ||
cursor.updateRow(row1) | ||
except: | ||
addMsgAndPrint('failed to update row '+str(row[0])+'. Perhaps a field is not nullable') | ||
|
||
######################### | ||
|
||
db = sys.argv[1] | ||
|
||
addMsgAndPrint(versionString) | ||
arcpy.env.workspace = db | ||
|
||
tables = arcpy.ListTables() | ||
for tb in tables: | ||
addMsgAndPrint(' ') | ||
addMsgAndPrint(os.path.basename(tb)) | ||
fixTableStrings(tb) | ||
|
||
datasets = arcpy.ListDatasets(feature_type='feature') | ||
datasets = [''] + datasets if datasets is not None else [] | ||
for ds in datasets: | ||
for fc in arcpy.ListFeatureClasses(feature_dataset=ds): | ||
path = os.path.join(arcpy.env.workspace, ds, fc) | ||
addMsgAndPrint(' ') | ||
addMsgAndPrint(os.path.basename(fc)) | ||
try: | ||
fixTableStrings(path) | ||
except: | ||
addMsgAndPrint(' failed to fix strings') | ||
|
||
addMsgAndPrint('DONE') |