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
import arcpy
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
#Source: http://help.arcgis.com/En/Arcgisdesktop/10.0/Help/index.html#//00080000000s000000
arcpy.Union_analysis (["layerName", "hydro", "layerName_union", "ALL")
#select FID_hydro <> -1
#Source: http://help.arcgis.com/En/Arcgisdesktop/10.0/Help/index.html#//001700000071000000)
arcpy.SelectLayerByAttribute_management ("layerName_union", "NEW_SELECTION", " [FID_hydro] <> '-1' ")
#If selection > 0, delete FID_hydro <> -1
#Source: http://help.arcgis.com/EN/ARCGISDESKTOP/10.0/HELP/index.html#//001700000036000000
if int(arcpy.GetCount_management(layerName_union).getOutput(0)) > 0:
arcpy.DeleteFeatures_management(layerName_union)
#export data layerName_union as shapefile
#Source: http://resources.arcgis.com/en/help/main/10.1/index.html#//001200000020000000
arcpy.FeatureClassToFeatureClass_conversion("layerName_union","C:/Chemin/vers/dossier","layerName_noHydro.shp")
# 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?
Thanks,
Solved! Go to Solution.
I copied the wrong one. I tried yours at first, partly worked. I mean, a layer was saved as a new shape, but had the same fields as the original.
This is because it was an empty fms object, so it used the default - which is a complete field map that keeps all the fields.
@ Antoine Cantin on 28-Mar-2016 4:51 PM
ahhh perhaps...
fm = arcpy.FieldMap() fms = arcpy.FieldMappings() fms.removeAll() fm.outputField = "out_stuff" fms.addFieldMap(fm) #Create the output feature class, using the FieldMappings object arcpy.FeatureClassToFeatureClass_conversion( in_file, arcpy.env.workspace, out_file, field_mapping=fms)
might work but I would have to try it though... parsed from 2nd example here
When I try it exactly like you posted I get an error message, but my guess is that I'm doing something wrong... What is the fm.outputField? Should I replace "out_stuff" by something?
the field map output field... and it would be the name of the field you want to output... the example in the help topic seems reasonable, however, it might be best to go with Darren's deleting multiple fields... and providing all their names, or excluding the one you want to keep, ... than providing the name of the field or fields you want to keep.
As a lazy coder, I would only use FieldMap and FieldMappings as a last resort. They are complete outliers in the context of arcpy.
Agree... sigh... It is so easy within arcmap...hide the fields, right-click, data, export data OR delete the unneeded after... unfortunately, it is part of the automagically solutions that take so long and are so difficult to code simply
Sometimes one can use ModelBuilder for those tedious field copying tasks. Drop in FCtoFC then at least you have a GUI look at the input features and can remove the fields you don't want.
Test it and examine your output to see if it's the output you meant to produce.
Then export that simple model to python.
Saves typing a lot of possibly cumbersome fields names, screwing around with syntax, etc...
Antoine, there are a few typo errors in your code that you have posted (just briefly glancing through this, I see "opyFeatures_management" which is missing the letter "c". Also, for your path strings, you should put an "r" in front of the path to make it the raw path:
r"C:\Users\Antoine\Desktop\test"
since it is reading it differently (looking at the "\t", etc.)
What python editor are you using? You might consider something like PyCharm or PyScripter (both free) that will find errors like this and give suggestions.
Thanks! I corrected it and just downloaded PyCharm