I am attempting to write a script that Adds XY data via arcpy.AddXY_management tool, then takes the data from the tool generated XY points and appends fields ("coordinateX" and "coordinateY") that already exist in the feature classes. After appending these fields, I'd like to delete the fields that were generated by the Add XY tool. This should iterate through all feature classes in a given file geodatabase. When I run the below code, I get this error:
"ERROR 000732: Input Features: Dataset C:\Users\ANCGIS\Desktop\ANC\Resources\Test\MartinezUtiliyData.gdb does not exist or is not supported
Failed to execute (AddXY)"
I assume this error has to do with my "full_path_to_fc" variable, but I am not sure what a work around would be. Is there a way to write this code in a way that would fall in line with what I need? Thanks.
import arcpy
import os
arcpy.env.overwriteOutput = True
arcpy.env.workspace = 'C:\Users\ANCGIS\Desktop\ANC\Resources\Test\MartinezUtiliyData.gdb'
fclist = arcpy.ListFeatureClasses(feature_type='Point')
full_path_to_fc = os.path.join(arcpy.env.workspace)
for fc in fclist:
arcpy.AddXY_management(full_path_to_fc)
with arcpy.da.UpdateCursor(full_path_to_fc, ["coordinateX", "coordinateY", "POINT_X", "POINT_Y"]) as rows:
for row in rows:
row[0] = row[2]
row[1] = row[3]
rows.updateRow(row)
arcpy.DeleteField_management(full_path_to_fc, 'POINT_X')
arcpy.DeleteField_management(full_path_to_fc, 'POINT_Y')
I assume you want to do this process for each FeatureClass in the database? But, you're right that the 'full_path_to_fc' is incorrect. You need to pass two arguments to the os.path.join function in order to get a combined path to the feature class. The below code should get you started.
I added a database variable and set the workspace to that. You don't need to use the 'AddXY_management', because you can access geometry values with tokens. All point FeatureClass names are in a list to iterate over. There were some slight indentation problems (line 9, 14, 15). I added a line to delete the "rows" generator object after it's exhausted.
import arcpy
import os
database = r'C:\Users\ANCGIS\Desktop\ANC\Resources\Test\MartinezUtiliyData.gdb'
arcpy.env.workspace = database
arcpy.env.overwriteOutput = True
#Python list of all point FC names
fclist = [x.name for x in arcpy.ListFeatureClasses(feature_type='Point')]
for fc in fclist:
full_path_to_fc = os.path.join(database, fc)#join database and fc name
with arcpy.da.UpdateCursor(full_path_to_fc, ["coordinateX","coordinateY","SHAPE@XY"]) as rows:
for row in rows:
row[0] = row[2][0]
row[1] = row[3][1]
rows.updateRow(row)
del rows
arcpy.DeleteField_management(full_path_to_fc, 'POINT_X')
arcpy.DeleteField_management(full_path_to_fc, 'POINT_Y')
Yes, I'd like to iterate through the entire geodatabase.
You need to pass two arguments to the os.path.join function in order to get a combined path to the feature class.
Could you expand on what this means?
os.path.join literally joins two strings together to make a complete file path. The documentation states,
Join one or more path components intelligently.
See here
put an 'r' in front of your path.... r'C:\Users\ANCGIS\Desktop\ANC\Resources\Test\MartinezUtiliyData.gdb'
In python 3 I get a path error because of the '\U' and the \T and \A in your path because it isn't 'raw' formatted. I suspect that the path just fails quietly and says it can't find the file in python 2.7
a = 'C:\Users\ANCGIS\Desktop\ANC\Resources\Test\MartinezUtiliyData'
File "<ipython-input-7-4ae2caca1c8f>", line 1
a = 'C:\Users\ANCGIS\Desktop\ANC\Resources\Test\MartinezUtiliyData'
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3:
truncated \uXXXX escape
# ---- Reason? \U is now reserved for Unicode with the backslash
# ---- Ok... I will skip 'U' and put stuff in a different folder....
a = 'C:\temp\A\MartinezUtiliyData'
a
'C:\temp\\A\\MartinezUtiliyData'
# ---- looks good on the surface except for the \ and \\ combo... must be ok
print(a)
C: emp\A\MartinezUtiliyData
# ---- I guess not... I don't have an ...emp folder... \t is the tab in python
Some other examples for people wanting to see what happens to paths...
If you set your work environment, you can even use built-in arcpy stuff..
import arcpy
a = r"C:\Data\Pro_base.gdb" # ---- setting a geodatabase to work with notice the 'r' !!!
arcpy.env.workspace = a # ---- setting the workspace to that gdb
fcs = arcpy.ListFeatureClasses() # a basic list featureclasses without any fluff
fcs
Out[12]:
['Carp_5x5km',
'Carp_5x5km_label',
'xy',
'mesh_sample',
'connecttoline',
'xy1000',
'small',
'xy1000_closest1',
'Line',
'Points',
'PointsOnLine',
'Curve',
'PointsOnCurve',
'connecttocurve']