I'm currently using a ArcGIS Pro and built in Jupyter Notebook to make changes to my feature layers (and some feature tables). Basically I'm hiding all the empty fields using field_info.setVisible().
Then I make new feature layer by overwriting it with this code. arcpy.management.MakeFeatureLayer(data_source, data_source.name, field_info=field_info)
Just for reference, field_info = arcpy.Describe(data_source).fieldInfo
When I run through all the layers with code, it resets the Layer ID from 0 and on.
I use these layers for AGOL (Web map) updates and the assigned Layer ID property is very important.
I can't seem to find any document regarding either maintaining the Layer ID or assigning Layer ID by code. All I found was the manual way of right clicking the layer > properties > set Layer ID.
This is extremely inconvenient as I have 20+ layers in each map and over 30 maps to work on.
Any help would be greatly appreciated.
Thanks!
I have not tried this by clobbering existing layers with the MakeFeatureLayer tool, but have used other tools/code to overwrite feature layers within my Pro project that I publish/overwrite existing services.
In my case, I check the Map layer properties to Allow assignment of unique numeric IDs for sharing web layers:
And, in the Geoprocessing options, Allow geoprocessing tools to overwrite exiting datasets, and un-check "Remove layers that reference data overwritten by geoprocessing tools":
This way, the layers are not removed, updated, then re-added, they are updated in place and the ID assignments do not change. I can then overwrite my feature service with the new data while maintaining item ID's.
In case this helps in your scenario,
R_
Thanks for your reply!
It seems like unless I specify MakeFeatureLayer, the layer on the map won't get updated.
Without it, something seems to be processing successfully, but no direct changes occur on the layer on the map.
To elaborate a little more, here's the snippet of hiding fields.
# Function to hide empty fields in a layer or table
def hide_empty_fields(data_source):
# Create a FieldInfo object
field_info = arcpy.Describe(data_source).fieldInfo
# Get a list of all fields in the data source
fields = [field.name for field in arcpy.ListFields(data_source)]
# Loop through the fields and check if they are empty across all rows
with arcpy.da.SearchCursor(data_source, fields) as cursor:
for field in fields:
is_empty = True
for row in cursor:
if row[fields.index(field)] not in [None, '', ' ']:
is_empty = False
break
if is_empty:
# Set the field visibility to "HIDDEN"
for i in range(field_info.count):
if field_info.getFieldName(i) == field:
field_info.setVisible(i, "HIDDEN")
cursor.reset() # Reset cursor to the beginning for the next field check
# Apply the updated field info to the existing layer or table
if isinstance(data_source, arcpy._mp.Layer):
arcpy.management.MakeFeatureLayer(data_source, data_source.name, field_info=field_info)
else:
arcpy.management.MakeTableView(data_source, data_source.name, field_info=field_info)
Unless I have those last two lines, the layers won't get updated, but that tool resets the Layer ID. (I've tried your recommendation)
Hard to follow your code as it has lost in indentation.
If you edit/post code following this post, it will maintain all the indentation, etc.
R_
def hide_empty_fields(data_source):
# Create a FieldInfo object
field_info = arcpy.Describe(data_source).fieldInfo
# Get a list of all fields in the data source
fields = [field.name for field in arcpy.ListFields(data_source)]
# Loop through the fields and check if they are empty across all rows
with arcpy.da.SearchCursor(data_source, fields) as cursor:
for field in fields:
is_empty = True
for row in cursor:
if row[fields.index(field)] not in [None, '', ' ']:
is_empty = False
break
if is_empty:
# Set the field visibility to "HIDDEN"
for i in range(field_info.count):
if field_info.getFieldName(i) == field:
field_info.setVisible(i, "HIDDEN")
cursor.reset() # Reset cursor to the beginning for the next field check
# Apply the updated field info to the existing layer or table
if isinstance(data_source, arcpy._mp.Layer):
arcpy.management.MakeFeatureLayer(data_source, data_source.name, field_info=field_info)
else:
arcpy.management.MakeTableView(data_source, data_source.name, field_info=field_info)