mzcoyle

Problem with Field Map and start/end string position

Discussion created by mzcoyle on May 10, 2011
Hi all, I am trying to create a field mapping and combine several string and int fields into one ID field. I can map each field with the order given in the dataset by simply not defining a start/end position for each field in the output field. However, when I try to modify this order (which is necessary) I cannot get the results I want.

Here is the code I am trying to get to work. This works in its entirety if I remove the start/end positions, it simply dumps the input fields into the output fields in the order they are given in the dataset.

field_mappings = arcpy.FieldMappings()
fldmap_OBJID = arcpy.FieldMap()

#field_mappings.addTable(block_lyr)

fldmap_OBJID.addInputField(block_lyr, "OBJECTID")
fld_OBJID = fldmap_OBJID.outputField
fld_OBJID.name = "O_OID"
fldmap_OBJID.outputField = fld_OBJID
field_mappings.addFieldMap(fldmap_OBJID)

fldmap_SOURCEID = arcpy.FieldMap()
fldmap_SOURCEID.mergeRule = "Join"
fldmap_SOURCEID.joinDelimiter = ""
#SYNTAX
#addInputField (table_dataset, field_name, {start_position}, {end_position})
fldmap_SOURCEID.addInputField(block_lyr, "SOURCECODE", 0,1)#length 1
fldmap_SOURCEID.addInputField(block_lyr, "ZONECODE", 1,2)#length 1
#fldmap_SOURCEID.addInputField(block_lyr, "TOWNSHIP", 2,5)#length 3
#fldmap_SOURCEID.addInputField(block_lyr, "RANGE", 5,7)#length 2
#fldmap_SOURCEID.addInputField(block_lyr, "MERIDIAN", 7,8)#length 1
#fldmap_SOURCEID.addInputField(block_lyr, "BLOCKSECTION", 8,10)#length 2
#fldmap_SOURCEID.addInputField(block_lyr, "GRIDNUMBER", 10,12)#length 2
#fldmap_SOURCEID.addInputField(block_lyr, "PASS", 12,13)#length 1
fld_SOURCEID = fldmap_SOURCEID.outputField
fld_SOURCEID.name = "SOURCEID"
fld_SOURCEID.type = "TEXT"
fld_SOURCEID.length = 15
fldmap_SOURCEID.outputField = fld_SOURCEID
field_mappings.addFieldMap(fldmap_SOURCEID)


After beating my head against the wall for a while I gave up and came up with a work around using updatecursor below. I would just like to know what I was doing wrong with the field mapping.

    #arcpy.FeatureClassToFeatureClass_conversion(block_lyr, exportdir, block_out, block_query, field_mappings)
    arcpy.FeatureClassToFeatureClass_conversion(block_lyr, exportdir, block_out, block_query)

    arcpy.AddField_management(block_out+".shp","SOURCEID","TEXT","","",15)
    rows = arcpy.UpdateCursor(block_out+".shp")
    for row in rows:
        sourcecode = row.getValue("SOURCECODE")
        zonecode = row.getValue("ZONECODE")
        township = str(row.getValue("TOWNSHIP"))
        Range = row.getValue("RANGE")
        meridian = row.getValue("MERIDIAN")
        blocksection = row.getValue("BLOCKSECTI")
        gridnumber = row.getValue("GRIDNUMBER")
        Pass = row.getValue("PASS")
        if len(township) == 2:
            township = "0"+township
        
        row.SOURCEID = str(sourcecode)+str(zonecode)+str(township)+str(Range)+str(meridian)+str(blocksection)+str(gridnumber)+str(Pass)
        rows.updateRow(row)

Outcomes