0

I have a gdb of point features. I want to iterate through the gdb, select the point features that are within a polygon shapefile, and export the selected feature results to a new gdb. However, I only want to retain geometry and uniqueid fields for the new selected features that I want to output to my new gdb.

 # Import system modules
 import arcpy
 import os
 # Set environment settings
 arcpy.env.workspace = "C:/data/input.gdb"
 # Set local variables
 out_workspace = "c:/output/output.gdb"
 # Boundary polygons for select by location
 boundaries = "C:/data/boundaries.shp"
 # Use ListFeatureClasses to generate a list of shapefiles in the workspace 
 fc_list = arcpy.ListFeatureClasses()
 # Execute CopyFeatures for each input shapefile
 for feature in fc_list:
 out_featureclass = os.path.join(out_workspace, feature + "_withinbounds")
 ### HERE IS WHERE I NEED TO FILTER ATTRIBUTES
 within_features = arcpy.SelectLayerByLocation_management(feature, "WITHIN", boundaries, "", "NEW_SELECTION")
 arcpy.CopyFeatures_management(within_features, out_featureclass)

Where would I put this code and how would I filter and retain just those attributes before exporting the new features using CopyFeatures_management?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 30, 2021 at 19:06
3
  • 1
    Are you using ArcGIS Pro or ArcMap when trying to do this? Have you considered using Delete Field? Have you considered using a field mapping? Commented Nov 30, 2021 at 21:06
  • I am doing this strictly in a standalone Python script (I think I am using the arcpy version from ArcGIS Pro in my env, but running the code in Spyder IDE). So, how would I use delete fields or field mapping prior to exporting my selected output feature? Commented Nov 30, 2021 at 21:21
  • There's an answer at gis.stackexchange.com/a/34478/115 which does it with ArcMap. The code should be the same with ArcGIS Pro but I just found a better option which I'll put into an answer. Commented Nov 30, 2021 at 21:28

1 Answer 1

1

To do this I think you should use the Delete Field tool in your script:

You can specify either the fields to delete or the fields to keep.

...

  • To keep fields, use the Field(s) parameter to specify the fields to keep, and set the Method parameter to the Keep Fields option.

I think you should use it after Copy Features so that you do not change the data that you are copying from.

answered Nov 30, 2021 at 21:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.