Arcpy: AddField is not adding a field and not giving any error

692
2
11-17-2021 11:58 AM
ChrisCowin
New Contributor III

Hello, writing a script to generate all the extents of a map series so they can be displayed in the extent map.

Here is my relevant code:

import arcpy

p = arcpy.mp.ArcGISProject("G:\\Game_program\\Web_maps\\Controlled_Hunt\\Controlled_Hunt.aprx")
arcpy.env.addOutputsToMap = True
gdb = "G:\\Game_program\\Web_maps\\Controlled_Hunt\\Controlled_Hunt.gdb"
fc = 'Map_Series_Extents'


arcpy.CreateFeatureclass_management(gdb, fc, "POLYGON",
has_m = "DISABLED", has_z = "DISABLED",
spatial_reference = "G:\\Game_program\\Web_maps\\Controlled_Hunt\\Controlled_Hunt.gdb\\Divided_ControlledHunt_tobeMapped_2022")

arcpy.management.AddField(fc, "Name", "TEXT")

 

For whatever reason the field just stopped getting added and there are no errors that come up when I run the code. I know it worked at some point.

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

strange... but you use "fc" try using the full path

full_path =   "G:\\Game_program\\Web_maps\\Controlled_Hunt\\Controlled_Hunt.gdb\\Map_Series_Extents"


... sort of retired...
0 Kudos
ChrisCowin
New Contributor III

E: after looking at it more i thought the copy features would insert a polygon object into the existing Feature Class not just completely overwrite it. 

Same result when it is in the same spot as my code, but works when I run it independently. But then when I run the rest of my code it goes away.. I'm not deleting anything, I have no idea why this is happeneing and its so frustrating. Here is all of my code for when I'm manually making the feature class:

import arcpy

p = arcpy.mp.ArcGISProject("G:\\Game_program\\Web_maps\\Controlled_Hunt\\Controlled_Hunt.aprx")
arcpy.env.addOutputsToMap = True
gdb = "G:\\Game_program\\Web_maps\\Controlled_Hunt\\Controlled_Hunt.gdb"
fc = 'Map_Series_Extents'

# arcpy.CreateFeatureclass_management(gdb, fc, "POLYGON",
# has_m = "DISABLED", has_z = "DISABLED",
# spatial_reference = "G:\\Game_program\\Web_maps\\Controlled_Hunt\\Controlled_Hunt.gdb\\Divided_ControlledHunt_tobeMapped_2022")

# arcpy.AddField_management("G:\\Game_program\\Web_maps\\Controlled_Hunt\\Controlled_Hunt.gdb\\Map_Series_Extents", "Name", "TEXT")


l = p.listLayouts()[2]

if not l.mapSeries is None:
ms = l.mapSeries
if ms.enabled:
for pageNum in range(1, ms.pageCount + 1):
# Loop through the pages
ms.currentPageNumber = pageNum

# Get the Map Name
pageName = ms.pageNameField

# Get the Map Frame
mf = l.listElements("mapframe_element", "Map Frame")[0]

# Generate extent from the Map Frame
extent = mf.camera.getExtent()
polygon = extent.polygon

# Insert the polygon into a feature class
arcpy.CopyFeatures_management(polygon, gdb + '\\' + fc)

# Add field to polygon
with arcpy.da.UpdateCursor(gdb + '\\' + fc, ['Name']) as cursor:
for row in cursor:
if not row[0]:
row[0] = pageName
cursor.updateRow(row)

0 Kudos