Hi, I'm trying to build a script, my first, so that it would create another version of some polygons in a shapefile (per example zoning)by erasing some polygons contained in a different shapefile (called hydro). I don't have a Pro license, so I use the "Union then delete" method instead of the erase tool. I don't want the attributes from the hydro table. I want to duplicate the script to apply those modification to other shapefiles. So here is what I found for the moment:
# Import the system modules
from arcpy import env
# Set the current workspace
# (to avoid having to specify the full path to the feature classes each time)
env.workspace = "c:/data/data.gdb"
# For first layer
#Turn off all fields of hydro.shp so when union is done, the attributes table will only keep the fields from "zoning", leaving out the turned off fields of "hydro"
#Can't find script lines for that
#union layerName with hydro.shp
arcpy.Union_analysis (["layerName", "hydro", "layerName_union", "ALL")
#select FID_hydro <> -1
arcpy.SelectLayerByAttribute_management ("layerName_union", "NEW_SELECTION", " [FID_hydro] <> '-1' ")
#If selection > 0, delete FID_hydro <> -1
if int(arcpy.GetCount_management(layerName_union).getOutput(0)) > 0:
#export data layerName_union as shapefile
# Repeat for other layers
Anyone know how to use script to turn off all the field of a layer?
Would those steps work? Is there something missing?
Solved! Go to Solution.
#delete any data tha might be stored in memory
#set workspace to where all your shapefiles are
arcpy.env.workspace="C:/Users/Antoine/Desktop/MRCL - Local/Ketar_shp"
#define where the hydro shapefile is located, ideally in a different folder than the others
hydroshp="C:/Users/Antoine/Desktop/MRCL - Local/Ketar_shp/hydro/hydro.shp"
for fc in fcs:
#create an empty list to be populated with the original shapefile's field names
#List all fields in the shapefile and populate the fieldnames list with their names
for field in fields:
#define layer name for each shapefile
#create output files for the clip and the union layers
#clip the shapefile using the hydro layer
#create a union of the clipped hydro and the shapefile
#delete any unnecessary fields from your union shapefile
for ufield in unionfields:
if ufield.name not in fieldnames:
#Create layer file so you can perform a selection on it
#If selection > 0, delete features
if int(arcpy.GetCount_management(fclayer+"_sansO").getOutput(0)) > 0:
#export data layerName_union as shapefile (works)
arcpy.FeatureClassToFeatureClass_conversion(fclayer+"_sansO","C:/Users/Antoine/Desktop/MRCL - Local/Ketar_shp/shapes_sansO.gdb",fclayer+"_sansO")
Someone helped me, here's what we have come up with!
fms = arcpy.FieldMappings()
Create the output feature class, using the FieldMappings object arcpy.FeatureClassToFeatureClass_conversion( in_file, arcpy.env.workspace, out_file, field_mapping=fms)
guess-ta-coding from the help files
I tried this:
fms = arcpy.FieldMappings()
I get this message now:
"Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\conversion.py", line 1685, in FeatureClassToFeatureClass raise e ExecuteError: ERROR 999999: Error executing function. An invalid SQL statement was used. An invalid SQL statement was used. Failed to execute (FeatureClassToFeatureClass). "
When I try to use the FeatureClassToFeatureClass tool, in the UI, I get another error message. I tried to solve it with this solution. Still not working.
the syntax I posted was field_mapping=fms)
You assigned it a text value "fms" which will surely fail. Check the exact syntax in the help files and what the defaults are if you omit parameters. You can't just omit parameters by using ,, or changing object properties ( a fieldmap is a type of object which is not a string object). These errors aren't caught by IDE's, the clues are in the reported message, unfortunately they are interpreted with respect to the object expected and not by what the user expect to be given to solve the problem.