0

I have feature class that contains many fields.

I'm using MakeFeatureLayer tool in arcpy to make a new layer that would contain selected fields with different names.

Here's the code I've written to make the new layer:

arcpy.MakeFeatureLayer_management("Firebreak","Firebreaks","","","OBJECTID OBJECTID VISIBLE NONE;FIREBREAKSUBTYPE TYPE VISIBLE NONE;SHAPE Shape VISIBLE NONE;FNUMBER FNUMBER HIDDEN NONE;GPSSATAVG GPSSATAVG HIDDEN NONE;GPSCOLLECTUSED GPSCOLLECTUSED HIDDEN NONE;SURVEYER SURVEYER HIDDEN NONE;GLOBALID GLOBALID HIDDEN NONE;LVMDISTRICTCODE LVMDISTRICTCODE HIDDEN NONE;BLOCKKEY BLOCKKEY HIDDEN NONE;LASTCHANGEAPP LASTCHANGEAPP HIDDEN NONE;LASTCHANGEDATE LASTCHANGEDATE HIDDEN NONE;SHAPE.LEN SHAPE_Leng VISIBLE NONE")

The new layer contains the fields I selected, but unfortunately, those fields aren't renamed, i.e., fields of new layer have the same names as fields in original layer.

How can I make MakeFeatureLayer to rename the fields of new layer?

  • 1
    What makes you think that the field name of the layer created has not been altered by MakeFeatureLayer from what is stored in the data? – PolyGeo May 23 '18 at 9:03
  • The new layer's attribute table's fields don't contain the desired field names and their types haven't changed. Or I just didn't understand the point of your question... – Guuuson May 23 '18 at 9:57
  • 1
    Your question only talks about renaming fields not altering field types. I suspect that you are expecting MakeFeatureLayer to permanently alter a field name in your data whereas that tool will only change it in a layer which has that data as its source. You may or may not be looking for the AlterField tool. – PolyGeo May 23 '18 at 10:03
1

MakeFeatureLayer only creates a temporary, in-memory layer. If you wish to save that layer to a file, then you would do something like:

arcpy.MakeFeatureLayer_management("Firebreak","Firebreaks","","","OBJECTID OBJECTID VISIBLE NONE;FIREBREAKSUBTYPE TYPE VISIBLE NONE;SHAPE Shape VISIBLE NONE;FNUMBER FNUMBER HIDDEN NONE;GPSSATAVG GPSSATAVG HIDDEN NONE;GPSCOLLECTUSED GPSCOLLECTUSED HIDDEN NONE;SURVEYER SURVEYER HIDDEN NONE;GLOBALID GLOBALID HIDDEN NONE;LVMDISTRICTCODE LVMDISTRICTCODE HIDDEN NONE;BLOCKKEY BLOCKKEY HIDDEN NONE;LASTCHANGEAPP LASTCHANGEAPP HIDDEN NONE;LASTCHANGEDATE LASTCHANGEDATE HIDDEN NONE;SHAPE.LEN SHAPE_Leng VISIBLE NONE")

arcpy.CopyFeatures_management('Firebreaks', r'path\where\I\want\to\save.gdb\Firebreaks')

See CopyFeatures.

Also, as an alternative to the long field info string, you could use a FieldInfo object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.