Skip to content

Commit

Permalink
Added Fix string values tool to toolbox
Browse files Browse the repository at this point in the history
  • Loading branch information
ethoms-usgs committed Aug 20, 2021
1 parent a47002f commit ef18fcc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
Binary file modified GeMS_Tools_AGP2.tbx
Binary file not shown.
69 changes: 69 additions & 0 deletions Scripts/GeMS_FixStrings_AGP2.py
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')

0 comments on commit ef18fcc

Please sign in to comment.