Friday, 4 March 2016

arcpy - Can the field mapping be shortened in ArcToolbox tools?


I have converted a model to python in ArcGIS 10.3, and have found that the tool outputs a full and very very long field mapping. Is there a way to shorten this somehow? While I want all field names/types/lengths to stay the same, I don't necessarily want all fields to be output.



arcpy.TableToTable_conversion(SourceTable, TargetDB, "xxOutputTable", "", "RecordID \"RecordID\" true false false 8 Double 0 19 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,RecordID,-1,-1;Descript \"Descript\" true true false 65 Text 0 0 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,Descript,-1,-1;Status \"Status\" true true false 30 Text 0 0 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,Status,-1,-1;RecordName \"RecordName\" true true false 50 Text 0 0 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,RecordName,-1,-1;Owner \"Owner\" true true false 2000 Text 0 0 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,Owner,-1,-1;", "")

While the python works fine with it all there, I would like to be able to make it as simple as possible to modify if/when required, and I do find the long field mapping quite hard to read when scrolling through it across the screen.


Is there a way to simplify this expression?



Answer



Using http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-classes/fieldmappings.htm as a guide, the following python works and makes adding/removing/modifying fields a lot easier than within with arcpy tool


import arcpy

def generateFieldMapping(fms, fieldName, fieldType, fieldLength, fieldPrecision, fieldScale, fieldAlias):
# Create the FieldMap object

fm = arcpy.FieldMap()
fm.addInputField(SourceTable, fieldName)
fm_field = fm.outputField

# Set the different properties of the field
fm_field.name = fieldName
fm_field.aliasName = fieldAlias
fm_field.type = fieldType
if fieldType == "Text":
fm_field.length = fieldLength

if fieldType == "Integer" or fieldType == "Double":
fm_field.precision = fieldPrecision
if fieldType == "Double":
fm_field.scale = fieldScale

fm.outputField = fm_field

# Add FieldMap to the FieldMappings object
fms.addFieldMap(fm)


return fms

SourceDB = "Database Connections\\GISDatabase.sde"
TargetDB = "Database Connections\\GISDatabase.sde"

# Local variables:
SourceTable = SourceDB + "\\GISDatabase.dbo.v_InputTable"

fields = [
# [Field Name, Field type, Length, Precision, Scale, Field Alias]

["RecordID","Double",0,19,0,"RecordID"],
["Descript","Text",65,0,0,"Descript"],
["Status","Text",30,0,0,"Status"],
["RecordName","Text",50,0,0,"RecordName"],
["Owner","Text",2000,0,0,"Owner"],
]

# Create the FieldMappings objects
fms = arcpy.FieldMappings()


for field in fields:
generateFieldMapping(fms, field[0], field[1], field[2], field[3], field[4], field[5])

arcpy.TableToTable_conversion(SourceTable, TargetDB, "xxOutputTable", "", fms, "")

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...