Using ArcPy FieldMappings?Looping multiple feature classes in python for ArcGIS 10Where clause problems when all parts are user input variablesArcPy merge datasets with slightly different fieldsarcpy fieldmappings: can't set output field name for fieldmapArcPy FieldMappings syntax using Merge toolCreating Multiple Field Indexes Using ArcpyWhen importing a shapefile, resulting field name in feature class contains schema and table nameAdding new records to attachment match table from select number of features from feature class using ArcPy?Scripting (Python) Feature Class content into Shapefiles following an UTM data structureCannot find results of MakeFeatureLayer, cannot use SelectFeatureByAttribute

Why colon to denote that a value belongs to a type?

Is there an explanation for Austria's Freedom Party virtually retaining its vote share despite recent scandal?

If a massive object like Jupiter flew past the Earth how close would it need to come to pull people off of the surface?

Terminology about G- simplicial complexes

Ticket sales for Queen at the Live Aid

Could I be denied entry into Ireland due to medical and police situations during a previous UK visit?

Crossing US border with music files I'm legally allowed to possess

What is a subpixel in Super Mario Bros, and how does it relate to wall clipping?

How do I subvert the tropes of a train heist?

What are these (utility?) boxes at the side of the house?

Apparent Ring of Craters on the Moon

Why doesn't the Earth's acceleration towards the Moon accumulate to push the Earth off its orbit?

How could Catholicism have incorporated witchcraft into its dogma?

Is there any use case for the bottom type as a function parameter type?

Why did this prime-sequence puzzle not work?

How to properly maintain eye contact with people that have distinct facial features?

Can a Beholder use rays in melee range?

Crossword gone overboard

How can I prevent interns from being expendable?

Do firearms count as ranged weapons?

Is floating in space similar to falling under gravity?

What does the behaviour of water on the skin of an aircraft in flight tell us?

How were these pictures of spacecraft wind tunnel testing taken?

Is this story about US tax office reasonable?



Using ArcPy FieldMappings?


Looping multiple feature classes in python for ArcGIS 10Where clause problems when all parts are user input variablesArcPy merge datasets with slightly different fieldsarcpy fieldmappings: can't set output field name for fieldmapArcPy FieldMappings syntax using Merge toolCreating Multiple Field Indexes Using ArcpyWhen importing a shapefile, resulting field name in feature class contains schema and table nameAdding new records to attachment match table from select number of features from feature class using ArcPy?Scripting (Python) Feature Class content into Shapefiles following an UTM data structureCannot find results of MakeFeatureLayer, cannot use SelectFeatureByAttribute






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















This is a follow up to Output field name not showing up in spatial join result in which I received no response.



I am trying to convert a shapefile to a feature class, I also have a GeoJSON file with the field names as I would like them to appear in the output feature class. The arcpy.FeatureClassToFeatureClass_conversion functionality accepts a field map, so I tried to build one by looping through a GeoPandas geodataframe of the GeoJSON file (to set my output field names) and by adding the shapefile to a fieldMappings object, and replacing the field map for each iteration:



import archook
archook.get_arcpy()
import arcpy
import geopandas

file_path = 'damage_results/Culverts/Culverts.shp'
original_fields = arcpy.ListFields(file_path)
original_fields = original_fields[2:]
gdf = geopandas.read_file('damage_results/Culverts/result_culverts.json')
new_field_names = gdf.columns[:-1].to_list()

fms = arcpy.FieldMappings()
fms.addTable(file_path)

for i,field in enumerate(original_fields):
print i
fm = fms.getFieldMap(i)

out_field = fm.outputField
out_field.name = new_field_names[i]
fm.outputField = out_field
fms.replaceFieldMap(i, fm)

for fm in fms:
print fm.outputField.name
arcpy.FeatureClassToFeatureClass_conversion(file_path, 'intermediate_damage.gdb',
'Culverts', field_mapping = fms)


The second for loop is to verify that the output field names have been written properly, and it seems from within the script that they are. However when I open the Culverts feature class the field names are unchanged from the shapefile.



Does the output field attribute in the FieldMap object work?










share|improve this question



















  • 1





    From my experience field mapping works on a feature layer only. So create it first, change names and export. I can post example that changes field visibility using mapping.

    – FelixIP
    May 15 at 4:28











  • have you ever tried it directly on a physical file?

    – user32882
    May 15 at 4:37











  • Nyet. Just found I didn't even use mapping. It was fieldInfo.

    – FelixIP
    May 15 at 5:26











  • I'm surprised this code even executes as file_path is not a valid full path to a shapefile?

    – Hornbydd
    May 17 at 9:14


















2















This is a follow up to Output field name not showing up in spatial join result in which I received no response.



I am trying to convert a shapefile to a feature class, I also have a GeoJSON file with the field names as I would like them to appear in the output feature class. The arcpy.FeatureClassToFeatureClass_conversion functionality accepts a field map, so I tried to build one by looping through a GeoPandas geodataframe of the GeoJSON file (to set my output field names) and by adding the shapefile to a fieldMappings object, and replacing the field map for each iteration:



import archook
archook.get_arcpy()
import arcpy
import geopandas

file_path = 'damage_results/Culverts/Culverts.shp'
original_fields = arcpy.ListFields(file_path)
original_fields = original_fields[2:]
gdf = geopandas.read_file('damage_results/Culverts/result_culverts.json')
new_field_names = gdf.columns[:-1].to_list()

fms = arcpy.FieldMappings()
fms.addTable(file_path)

for i,field in enumerate(original_fields):
print i
fm = fms.getFieldMap(i)

out_field = fm.outputField
out_field.name = new_field_names[i]
fm.outputField = out_field
fms.replaceFieldMap(i, fm)

for fm in fms:
print fm.outputField.name
arcpy.FeatureClassToFeatureClass_conversion(file_path, 'intermediate_damage.gdb',
'Culverts', field_mapping = fms)


The second for loop is to verify that the output field names have been written properly, and it seems from within the script that they are. However when I open the Culverts feature class the field names are unchanged from the shapefile.



Does the output field attribute in the FieldMap object work?










share|improve this question



















  • 1





    From my experience field mapping works on a feature layer only. So create it first, change names and export. I can post example that changes field visibility using mapping.

    – FelixIP
    May 15 at 4:28











  • have you ever tried it directly on a physical file?

    – user32882
    May 15 at 4:37











  • Nyet. Just found I didn't even use mapping. It was fieldInfo.

    – FelixIP
    May 15 at 5:26











  • I'm surprised this code even executes as file_path is not a valid full path to a shapefile?

    – Hornbydd
    May 17 at 9:14














2












2








2








This is a follow up to Output field name not showing up in spatial join result in which I received no response.



I am trying to convert a shapefile to a feature class, I also have a GeoJSON file with the field names as I would like them to appear in the output feature class. The arcpy.FeatureClassToFeatureClass_conversion functionality accepts a field map, so I tried to build one by looping through a GeoPandas geodataframe of the GeoJSON file (to set my output field names) and by adding the shapefile to a fieldMappings object, and replacing the field map for each iteration:



import archook
archook.get_arcpy()
import arcpy
import geopandas

file_path = 'damage_results/Culverts/Culverts.shp'
original_fields = arcpy.ListFields(file_path)
original_fields = original_fields[2:]
gdf = geopandas.read_file('damage_results/Culverts/result_culverts.json')
new_field_names = gdf.columns[:-1].to_list()

fms = arcpy.FieldMappings()
fms.addTable(file_path)

for i,field in enumerate(original_fields):
print i
fm = fms.getFieldMap(i)

out_field = fm.outputField
out_field.name = new_field_names[i]
fm.outputField = out_field
fms.replaceFieldMap(i, fm)

for fm in fms:
print fm.outputField.name
arcpy.FeatureClassToFeatureClass_conversion(file_path, 'intermediate_damage.gdb',
'Culverts', field_mapping = fms)


The second for loop is to verify that the output field names have been written properly, and it seems from within the script that they are. However when I open the Culverts feature class the field names are unchanged from the shapefile.



Does the output field attribute in the FieldMap object work?










share|improve this question
















This is a follow up to Output field name not showing up in spatial join result in which I received no response.



I am trying to convert a shapefile to a feature class, I also have a GeoJSON file with the field names as I would like them to appear in the output feature class. The arcpy.FeatureClassToFeatureClass_conversion functionality accepts a field map, so I tried to build one by looping through a GeoPandas geodataframe of the GeoJSON file (to set my output field names) and by adding the shapefile to a fieldMappings object, and replacing the field map for each iteration:



import archook
archook.get_arcpy()
import arcpy
import geopandas

file_path = 'damage_results/Culverts/Culverts.shp'
original_fields = arcpy.ListFields(file_path)
original_fields = original_fields[2:]
gdf = geopandas.read_file('damage_results/Culverts/result_culverts.json')
new_field_names = gdf.columns[:-1].to_list()

fms = arcpy.FieldMappings()
fms.addTable(file_path)

for i,field in enumerate(original_fields):
print i
fm = fms.getFieldMap(i)

out_field = fm.outputField
out_field.name = new_field_names[i]
fm.outputField = out_field
fms.replaceFieldMap(i, fm)

for fm in fms:
print fm.outputField.name
arcpy.FeatureClassToFeatureClass_conversion(file_path, 'intermediate_damage.gdb',
'Culverts', field_mapping = fms)


The second for loop is to verify that the output field names have been written properly, and it seems from within the script that they are. However when I open the Culverts feature class the field names are unchanged from the shapefile.



Does the output field attribute in the FieldMap object work?







arcpy field-mapping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 15 at 8:00









PolyGeo

54k1782251




54k1782251










asked May 15 at 3:34









user32882user32882

1,26311127




1,26311127







  • 1





    From my experience field mapping works on a feature layer only. So create it first, change names and export. I can post example that changes field visibility using mapping.

    – FelixIP
    May 15 at 4:28











  • have you ever tried it directly on a physical file?

    – user32882
    May 15 at 4:37











  • Nyet. Just found I didn't even use mapping. It was fieldInfo.

    – FelixIP
    May 15 at 5:26











  • I'm surprised this code even executes as file_path is not a valid full path to a shapefile?

    – Hornbydd
    May 17 at 9:14













  • 1





    From my experience field mapping works on a feature layer only. So create it first, change names and export. I can post example that changes field visibility using mapping.

    – FelixIP
    May 15 at 4:28











  • have you ever tried it directly on a physical file?

    – user32882
    May 15 at 4:37











  • Nyet. Just found I didn't even use mapping. It was fieldInfo.

    – FelixIP
    May 15 at 5:26











  • I'm surprised this code even executes as file_path is not a valid full path to a shapefile?

    – Hornbydd
    May 17 at 9:14








1




1





From my experience field mapping works on a feature layer only. So create it first, change names and export. I can post example that changes field visibility using mapping.

– FelixIP
May 15 at 4:28





From my experience field mapping works on a feature layer only. So create it first, change names and export. I can post example that changes field visibility using mapping.

– FelixIP
May 15 at 4:28













have you ever tried it directly on a physical file?

– user32882
May 15 at 4:37





have you ever tried it directly on a physical file?

– user32882
May 15 at 4:37













Nyet. Just found I didn't even use mapping. It was fieldInfo.

– FelixIP
May 15 at 5:26





Nyet. Just found I didn't even use mapping. It was fieldInfo.

– FelixIP
May 15 at 5:26













I'm surprised this code even executes as file_path is not a valid full path to a shapefile?

– Hornbydd
May 17 at 9:14






I'm surprised this code even executes as file_path is not a valid full path to a shapefile?

– Hornbydd
May 17 at 9:14











1 Answer
1






active

oldest

votes


















4














In your second loop where you



print fm.outputfield.name


add another line to



print fm.inputfield.name


to check if your fieldmap object has input fields. You want every FieldMap object to have both an input and output field name, otherwise the fieldmappings object is not used by the tool.



The documentation has some good examples on this idea, fieldmapping docs.



Below is an example of how I use fieldmappings to append from a shapefile to a featureclass.



address_fms = arcpy.FieldMappings()
try:
# Start loop to make and add fieldmaps to fieldmappings object
for i in range(61):
str_i = str(i)
## Assign a fieldmap object to a variable
str_i = arcpy.FieldMap()
## Attribute the object's InputField
str_i.addInputField(intable, inflds[i])
## Assign the outputField object to a variable
fm_name = str_i.outputField
## Attribute the outputField object's name attribute
fm_name.name = outflds[i]
## Assign the field object with a populated name value
## to the fieldmap object
str_i.outputField = fm_name
# Add them to fieldmappings object so it works
address_fms.addFieldMap(str_i)





share|improve this answer

























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "79"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f322775%2fusing-arcpy-fieldmappings%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    In your second loop where you



    print fm.outputfield.name


    add another line to



    print fm.inputfield.name


    to check if your fieldmap object has input fields. You want every FieldMap object to have both an input and output field name, otherwise the fieldmappings object is not used by the tool.



    The documentation has some good examples on this idea, fieldmapping docs.



    Below is an example of how I use fieldmappings to append from a shapefile to a featureclass.



    address_fms = arcpy.FieldMappings()
    try:
    # Start loop to make and add fieldmaps to fieldmappings object
    for i in range(61):
    str_i = str(i)
    ## Assign a fieldmap object to a variable
    str_i = arcpy.FieldMap()
    ## Attribute the object's InputField
    str_i.addInputField(intable, inflds[i])
    ## Assign the outputField object to a variable
    fm_name = str_i.outputField
    ## Attribute the outputField object's name attribute
    fm_name.name = outflds[i]
    ## Assign the field object with a populated name value
    ## to the fieldmap object
    str_i.outputField = fm_name
    # Add them to fieldmappings object so it works
    address_fms.addFieldMap(str_i)





    share|improve this answer





























      4














      In your second loop where you



      print fm.outputfield.name


      add another line to



      print fm.inputfield.name


      to check if your fieldmap object has input fields. You want every FieldMap object to have both an input and output field name, otherwise the fieldmappings object is not used by the tool.



      The documentation has some good examples on this idea, fieldmapping docs.



      Below is an example of how I use fieldmappings to append from a shapefile to a featureclass.



      address_fms = arcpy.FieldMappings()
      try:
      # Start loop to make and add fieldmaps to fieldmappings object
      for i in range(61):
      str_i = str(i)
      ## Assign a fieldmap object to a variable
      str_i = arcpy.FieldMap()
      ## Attribute the object's InputField
      str_i.addInputField(intable, inflds[i])
      ## Assign the outputField object to a variable
      fm_name = str_i.outputField
      ## Attribute the outputField object's name attribute
      fm_name.name = outflds[i]
      ## Assign the field object with a populated name value
      ## to the fieldmap object
      str_i.outputField = fm_name
      # Add them to fieldmappings object so it works
      address_fms.addFieldMap(str_i)





      share|improve this answer



























        4












        4








        4







        In your second loop where you



        print fm.outputfield.name


        add another line to



        print fm.inputfield.name


        to check if your fieldmap object has input fields. You want every FieldMap object to have both an input and output field name, otherwise the fieldmappings object is not used by the tool.



        The documentation has some good examples on this idea, fieldmapping docs.



        Below is an example of how I use fieldmappings to append from a shapefile to a featureclass.



        address_fms = arcpy.FieldMappings()
        try:
        # Start loop to make and add fieldmaps to fieldmappings object
        for i in range(61):
        str_i = str(i)
        ## Assign a fieldmap object to a variable
        str_i = arcpy.FieldMap()
        ## Attribute the object's InputField
        str_i.addInputField(intable, inflds[i])
        ## Assign the outputField object to a variable
        fm_name = str_i.outputField
        ## Attribute the outputField object's name attribute
        fm_name.name = outflds[i]
        ## Assign the field object with a populated name value
        ## to the fieldmap object
        str_i.outputField = fm_name
        # Add them to fieldmappings object so it works
        address_fms.addFieldMap(str_i)





        share|improve this answer















        In your second loop where you



        print fm.outputfield.name


        add another line to



        print fm.inputfield.name


        to check if your fieldmap object has input fields. You want every FieldMap object to have both an input and output field name, otherwise the fieldmappings object is not used by the tool.



        The documentation has some good examples on this idea, fieldmapping docs.



        Below is an example of how I use fieldmappings to append from a shapefile to a featureclass.



        address_fms = arcpy.FieldMappings()
        try:
        # Start loop to make and add fieldmaps to fieldmappings object
        for i in range(61):
        str_i = str(i)
        ## Assign a fieldmap object to a variable
        str_i = arcpy.FieldMap()
        ## Attribute the object's InputField
        str_i.addInputField(intable, inflds[i])
        ## Assign the outputField object to a variable
        fm_name = str_i.outputField
        ## Attribute the outputField object's name attribute
        fm_name.name = outflds[i]
        ## Assign the field object with a populated name value
        ## to the fieldmap object
        str_i.outputField = fm_name
        # Add them to fieldmappings object so it works
        address_fms.addFieldMap(str_i)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 17 at 0:50

























        answered May 15 at 5:44









        Luke TLuke T

        687




        687



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f322775%2fusing-arcpy-fieldmappings%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

            Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

            What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company