I am having trouble setting up a basic loop. Most of the examples I'm finding online are either rudimentary "this is what a loop does" examples or don't seem to apply to what I'm doing. It's entirely probable that my workflow is just still not right. Please take a look at my code and help me become less of an amateur!
I have multiple processes to run on multiple files of the same type. So I build out the workflow in ModelBuilder and export that to a python script. In this case, I'm running the Minimum Bounding Geometry tool on a multipoint feature, converting the resultant vertices to points, then adding XY fields. It works great running in ModelBuilder or as a script on a single file, and the code is below in bold; however, I want to run this tool on a whole folder's worth of files.
import arcpy
# Local variables:
N2E100 = "D:\\Broomfield.Lidar\\Projection\\ReProject.gdb\\N2E100"
N2E100_MinimumBoundingGeomet = "D:\\Broomfield.Lidar\\GeometryResults.gdb\\N2E100_MinimumBoundingGeomet"
N2E100_MinimumBoundingGeomet1 = "D:\\Broomfield.Lidar\\GeometryResults.gdb\\N2E100_MinimumBoundingGeomet1"
# Process: Minimum Bounding Geometry
arcpy.MinimumBoundingGeometry_management(N2E100, N2E100_MinimumBoundingGeomet, "RECTANGLE_BY_AREA", "ALL", "", "NO_MBG_FIELDS")
# Process: Feature Vertices To Points
arcpy.FeatureVerticesToPoints_management(N2E100_MinimumBoundingGeomet, N2E100_MinimumBoundingGeomet1, "ALL")
# Process: Add XY Coordinates
arcpy.AddXY_management(N2E100_MinimumBoundingGeomet1)
I'm quite familiar with the syntax of loops and how they work in theory, but it's putting the loops into practice that I struggle with.
Solved! Go to Solution.
Hi Alex,
Are you trying to run this on every feature class within a single geodatabase? Or, are you trying to run this on feature classes in different geodatabases?
Below is an example on how you can do this for every feature class within a single geodatabase. You can use the arcpy.ListFeatureClasses function to create a list of all the feature classes. You can then run a 'for' loop on each feature class within this list.
import arcpy from arcpy import env env.workspace = r"D:\Broomfield.Lidar\Projection\ReProject.gdb" lstFCs = arcpy.ListFeatureClasses() for fc in lstFCs: arcpy.MinimumBoundingGeometry_management(fc, fc + "_MinimumBoundingGeomet", "RECTANGLE_BY_AREA", "ALL", "", "NO_MBG_FIELDS") arcpy.FeatureVerticesToPoints_management(fc, fc + "_MinimumBoundingVertices", "ALL") arcpy.AddXY_management(fc + "_MinimumBoundingVertices")
Hi Alex,
Are you trying to run this on every feature class within a single geodatabase? Or, are you trying to run this on feature classes in different geodatabases?
Below is an example on how you can do this for every feature class within a single geodatabase. You can use the arcpy.ListFeatureClasses function to create a list of all the feature classes. You can then run a 'for' loop on each feature class within this list.
import arcpy from arcpy import env env.workspace = r"D:\Broomfield.Lidar\Projection\ReProject.gdb" lstFCs = arcpy.ListFeatureClasses() for fc in lstFCs: arcpy.MinimumBoundingGeometry_management(fc, fc + "_MinimumBoundingGeomet", "RECTANGLE_BY_AREA", "ALL", "", "NO_MBG_FIELDS") arcpy.FeatureVerticesToPoints_management(fc, fc + "_MinimumBoundingVertices", "ALL") arcpy.AddXY_management(fc + "_MinimumBoundingVertices")
Perfecto! Thanks!