Merge with field mapping

1658
10
Jump to solution
09-04-2019 08:31 AM
CCWeedcontrol
Occasional Contributor III

I  am trying to merge five layers into one with only three fields. The code merges the layers fine with out error but the output layer has all the layers fields and the RD_Owner field doesn't have the attributes merged. so i am guessing my fieldmapping isn't correct. I would appreciate any help.

HwyNpaPub = 'C:/Temp/HighwayDistricts.gdb/HWY1'
HwyNpaPr = 'C:/Temp/HighwayDistricts.gdb/HWY2'
HwyCan = 'C:/Temp/HighwayDistricts.gdb/HWY3'
HwyG = 'C:/Temp/HighwayDistricts.gdb/HWY4'
HwyNP = 'C:/Temp/HighwayDistricts.gdb/HWY5'

FieldMapString = '''

STREET "STREET" true true false 50 Text 0 0,First,#,{0},STREET,0,11;
ROAD_TYPE "ROAD_TYPE" true true false 70 Text 0 0,First,#,{0},ROAD_TYPE,0,11;
RD_Owner "RD_Owner" true true false 255 Text 0 0 ,Join,#, {0}, RD_Owner,-1,-1;

'''

def Layers1(HwyNpaPub,HwyNpaPr,HwyCan,HwyG,HNP1):
    fieldmappings = arcpy.FieldMappings()
    fieldmappings.loadFromString(FieldMapString)
    return fieldmappings

def main(args=None):
        if args is None:
                args = sys.argv


arcpy.Merge_management([HwyNpaPub, HwyNpaPr, HwyCan, HwyG, HwyNP], 'D:/GIS Folder/HighwayDistricts.gdb/TEST', Layers1(HwyNpaPub, HwyNpaPr, HwyCan, HwyG, HwyNP))
0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

I was having success with the following:

import arcpy

outFeature = 'D:/GIS Folder/HighwayDistricts.gdb/TEST'

features = [
    'C:/Temp/HighwayDistricts.gdb/HWY1',
    'C:/Temp/HighwayDistricts.gdb/HWY2',
    'C:/Temp/HighwayDistricts.gdb/HWY3',
    'C:/Temp/HighwayDistricts.gdb/HWY4',
    'C:/Temp/HighwayDistricts.gdb/HWY5'
    ]

fields = [
    ['STREET "STREET" true true false 50 Text 0 0,First,#,{0};', '{0},STREET,0,11'],
    ['ROAD_TYPE "ROAD_TYPE" true true false 70 Text 0 0,First,#,{0};', '{0},ROAD_TYPE,0,11'],
    ['RD_Owner "RD_Owner" true true false 255 Text 0 0 ,Join,#,{0}', '{0}, RD_Owner,-1,-1']
    ]

fmap = ''
for fld in fields:
    fm = []
    for fc in features:
        fm.append(fld[1].format(fc))
    fmap = fmap + fld[0].format(','.join(fm))

print fmap
print
print ';'.join(features)

arcpy.Merge_management(
    inputs= ';'.join(features), # semicolon delimited
    output= outFeature,
    field_mappings= fmap
    )‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The print statements output the following:

STREET "STREET" true true false 50 Text 0 0,First,#,C:/Temp/HighwayDistricts.gdb/HWY1,STREET,0,11,C:/Temp/HighwayDistricts.gdb/HWY2,STREET,0,11,C:/Temp/HighwayDistricts.gdb/HWY3,STREET,0,11,C:/Temp/HighwayDistricts.gdb/HWY4,STREET,0,11,C:/Temp/HighwayDistricts.gdb/HWY5,STREET,0,11;ROAD_TYPE "ROAD_TYPE" true true false 70 Text 0 0,First,#,C:/Temp/HighwayDistricts.gdb/HWY1,ROAD_TYPE,0,11,C:/Temp/HighwayDistricts.gdb/HWY2,ROAD_TYPE,0,11,C:/Temp/HighwayDistricts.gdb/HWY3,ROAD_TYPE,0,11,C:/Temp/HighwayDistricts.gdb/HWY4,ROAD_TYPE,0,11,C:/Temp/HighwayDistricts.gdb/HWY5,ROAD_TYPE,0,11;RD_Owner "RD_Owner" true true false 255 Text 0 0 ,Join,#,C:/Temp/HighwayDistricts.gdb/HWY1,RD_Owner,-1,-1,C:/Temp/HighwayDistricts.gdb/HWY2,RD_Owner,-1,-1,C:/Temp/HighwayDistricts.gdb/HWY3,RD_Owner,-1,-1,C:/Temp/HighwayDistricts.gdb/HWY4,RD_Owner,-1,-1,C:/Temp/HighwayDistricts.gdb/HWY5,RD_Owner,-1,-1

C:/Temp/HighwayDistricts.gdb/HWY1;C:/Temp/HighwayDistricts.gdb/HWY2;C:/Temp/HighwayDistricts.gdb/HWY3;C:/Temp/HighwayDistricts.gdb/HWY4;C:/Temp/HighwayDistricts.gdb/HWY5‍‍‍‍‍‍‍‍‍‍‍‍‍‍

My test values were slightly different than yours as I am not completely sure of your set-up.  I ran this outside ArcMap, hence the path to the features.  If you have a map with the layers added, you could substitute the layer names for the layer path in ArcMap's Python window.

I think the '0 ,11' and '-1,-1'  in your field map indicate the start and end positions of the data being transferred.  0,11 would be the first 12 characters of the field's data, and -1,-1 would take the whole field.  In my experience, if the field data being transferred is bigger than the target field size, an error will occur.

I have not tested for spaces in feature names/paths, nor have I tested  where renaming the field is attempted with field mapping.

Hope this helps.

View solution in original post

0 Kudos
10 Replies
DanPatterson_Retired
MVP Emeritus

perhaps your true and false should be True and False if they do indeed represent booleans

0 Kudos
RandyBurton
MVP Alum

I think the "true true false" is using the right case based on my experiences with field mapping.  I think the field you are mapping needs the feature name included as in:

STREET "STREET" true true false 50 Text 0 0,First,#,{0},{0}.STREET,0,11;

{0}.STREET

0 Kudos
CCWeedcontrol
Occasional Contributor III

Guys, thanks for the replay. Changing the True and False to upper case and adding {0}.STREET also didn't do anything. The layer still got merged but the fieldmapping didn't.

0 Kudos
CCWeedcontrol
Occasional Contributor III

Are mapping field not allowed with the Merge? I guess i am not seeing what i am doing wrong.

0 Kudos
RandyBurton
MVP Alum

Field mappings have always been a bit confusing for me.  To see how various tools use mappings, I will put some test features into ArcMap and then run a tool like Merge and then copy the Python snippet of the tool's last run under Geoprocessing > Results.  This will show all the parameters used for a specific running of the tool.  Then you can see how the mappings were formatted and create a script that will format your variables in a similar fashion.

Using that approach, it looks like the field mappings would be something like:

STREET "STREET" true true false 50 Text 0 0,First,#,Feature1,STREET,0,11,Feature2,STREET,0,11,Feature3,STREET,0,11;

where Feature1, Feature2 and Feature3 would be the 3 layers that are being merged into the STREET field of the new, merged feature.  (I am assuming the 0, 11 are the correct values used by the features you are merging.)

0 Kudos
DanPatterson_Retired
MVP Emeritus
FieldMapString = '''STREET "STREET" true true false 50 Text 0 0,First,#,{0},STREET,0,11;\
ROAD_TYPE "ROAD_TYPE" true true false 70 Text 0 0,First,#,{0},ROAD_TYPE,0,11;\
RD_Owner "RD_Owner" true true false 255 Text 0 0 ,Join,#, {0}, RD_Owner,-1,-1'''

Got rid of the \n 's within the string by reformatting with line continuation ( \ ) and removed the last ; (semi-colon) in your string, which I presume that you have to split on the ; (semi-colons)

0 Kudos
RandyBurton
MVP Alum

I was having success with the following:

import arcpy

outFeature = 'D:/GIS Folder/HighwayDistricts.gdb/TEST'

features = [
    'C:/Temp/HighwayDistricts.gdb/HWY1',
    'C:/Temp/HighwayDistricts.gdb/HWY2',
    'C:/Temp/HighwayDistricts.gdb/HWY3',
    'C:/Temp/HighwayDistricts.gdb/HWY4',
    'C:/Temp/HighwayDistricts.gdb/HWY5'
    ]

fields = [
    ['STREET "STREET" true true false 50 Text 0 0,First,#,{0};', '{0},STREET,0,11'],
    ['ROAD_TYPE "ROAD_TYPE" true true false 70 Text 0 0,First,#,{0};', '{0},ROAD_TYPE,0,11'],
    ['RD_Owner "RD_Owner" true true false 255 Text 0 0 ,Join,#,{0}', '{0}, RD_Owner,-1,-1']
    ]

fmap = ''
for fld in fields:
    fm = []
    for fc in features:
        fm.append(fld[1].format(fc))
    fmap = fmap + fld[0].format(','.join(fm))

print fmap
print
print ';'.join(features)

arcpy.Merge_management(
    inputs= ';'.join(features), # semicolon delimited
    output= outFeature,
    field_mappings= fmap
    )‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The print statements output the following:

STREET "STREET" true true false 50 Text 0 0,First,#,C:/Temp/HighwayDistricts.gdb/HWY1,STREET,0,11,C:/Temp/HighwayDistricts.gdb/HWY2,STREET,0,11,C:/Temp/HighwayDistricts.gdb/HWY3,STREET,0,11,C:/Temp/HighwayDistricts.gdb/HWY4,STREET,0,11,C:/Temp/HighwayDistricts.gdb/HWY5,STREET,0,11;ROAD_TYPE "ROAD_TYPE" true true false 70 Text 0 0,First,#,C:/Temp/HighwayDistricts.gdb/HWY1,ROAD_TYPE,0,11,C:/Temp/HighwayDistricts.gdb/HWY2,ROAD_TYPE,0,11,C:/Temp/HighwayDistricts.gdb/HWY3,ROAD_TYPE,0,11,C:/Temp/HighwayDistricts.gdb/HWY4,ROAD_TYPE,0,11,C:/Temp/HighwayDistricts.gdb/HWY5,ROAD_TYPE,0,11;RD_Owner "RD_Owner" true true false 255 Text 0 0 ,Join,#,C:/Temp/HighwayDistricts.gdb/HWY1,RD_Owner,-1,-1,C:/Temp/HighwayDistricts.gdb/HWY2,RD_Owner,-1,-1,C:/Temp/HighwayDistricts.gdb/HWY3,RD_Owner,-1,-1,C:/Temp/HighwayDistricts.gdb/HWY4,RD_Owner,-1,-1,C:/Temp/HighwayDistricts.gdb/HWY5,RD_Owner,-1,-1

C:/Temp/HighwayDistricts.gdb/HWY1;C:/Temp/HighwayDistricts.gdb/HWY2;C:/Temp/HighwayDistricts.gdb/HWY3;C:/Temp/HighwayDistricts.gdb/HWY4;C:/Temp/HighwayDistricts.gdb/HWY5‍‍‍‍‍‍‍‍‍‍‍‍‍‍

My test values were slightly different than yours as I am not completely sure of your set-up.  I ran this outside ArcMap, hence the path to the features.  If you have a map with the layers added, you could substitute the layer names for the layer path in ArcMap's Python window.

I think the '0 ,11' and '-1,-1'  in your field map indicate the start and end positions of the data being transferred.  0,11 would be the first 12 characters of the field's data, and -1,-1 would take the whole field.  In my experience, if the field data being transferred is bigger than the target field size, an error will occur.

I have not tested for spaces in feature names/paths, nor have I tested  where renaming the field is attempted with field mapping.

Hope this helps.

0 Kudos
CCWeedcontrol
Occasional Contributor III

Randy,

Thanks that worked, at some point i thought it was the Merged Join features so i tried

'"' + HwyNpaPub + ';' + HwyNpaPr + ';' + HwyCan + ';' + HwyG + ';' + HwyNP + '"','D:/GIS Folder/HighwayDistricts.gdb/TEST'
0 Kudos
StuartMoore
Occasional Contributor III

i'm trying to use your python to merge 25 or so shp files and map the fields but using the code i get a strange output

StuartMoore_0-1704212040714.png

any suggestions?

 

Stu

0 Kudos