Need help building my first script

7172
22
Jump to solution
03-28-2016 09:19 AM
AntoineCantin
New Contributor III

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,

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
AntoineCantin
New Contributor III

import arcpy

import os

#delete any data tha might be stored in memory

arcpy.Delete_management("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"

fcs=arcpy.ListFeatureClasses()

for fc in fcs:

    #create an empty list to be populated with the original shapefile's field names

    fieldnames=[]

    #List all fields in the shapefile and populate the fieldnames list with their names

    fields=arcpy.ListFields(fc)

    for field in fields:

        fieldnames.append(field.name)

    #define layer name for each shapefile

    fclayer=fc.rstrip(".shp")

    #create output files for the clip and the union layers

    fcClip=os.path.join("in_memory",fclayer+"clip")

    fcUnion=fclayer+"union.shp"#os.path.join("in_memory",fclayer+"union")

    #clip the shapefile using the hydro layer

    arcpy.Clip_analysis(fc,hydroshp,fcClip)

    #create a union of the clipped hydro and the shapefile

    arcpy.Union_analysis([fc,fcClip],fcUnion,"ALL")

    #delete any unnecessary fields from your union shapefile

    unionfields=arcpy.ListFields(fcUnion)

    for ufield in unionfields:

        if ufield.name not in fieldnames:

            arcpy.DeleteField_management(fcUnion,ufield.name)

    #Create layer file so you can perform a selection on it

    arcpy.MakeFeatureLayer_management(fcUnion,fclayer+"_sansO")

    arcpy.SelectLayerByLocation_management(fclayer+"_sansO","WITHIN",fcClip)

    #If selection > 0, delete features

    if int(arcpy.GetCount_management(fclayer+"_sansO").getOutput(0)) > 0:

        arcpy.DeleteFeatures_management(fclayer+"_sansO")

    #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")

print "done!!"

Someone helped me, here's what we have come up with!

View solution in original post

22 Replies
DarrenWiens2
MVP Honored Contributor

I think you're looking for the Delete Field tool.

If you need more help, please include your code within code tags so we can copy/paste/more easily read. Posting Code blocks in the new GeoNet

DanPatterson_Retired
MVP Esteemed Contributor

and featureclasstofeatureclass allows you to remove fields during the fieldmapping process Feature Class to Feature Class—Help | ArcGIS for Desktop

AntoineCantin
New Contributor III

Would it looks like that?

arcpy.FeatureClassToFeatureClass_conversion("Affectations_Union","C:\Users\Antoine\Desktop\test","layerName_shape.shp",,removeAll ())

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

according to this FieldMappings—Help | ArcGIS for Desktop in scripting, I think that would be correct, but I am not at a machine to test at present

0 Kudos
AntoineCantin
New Contributor III

Don't seem to work, I get this message:

"Parsing error SyntaxError: invalid syntax (line 1)" !?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

fms = arcpy.FieldMappings()

fms.removeAll()

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

DanPatterson_Retired
MVP Esteemed Contributor

or you can't have ,, in a row, something has to be inbetween

AntoineCantin
New Contributor III

I tried this:

fms = arcpy.FieldMappings()

fms.removeAll()

arcpy.FeatureClassToFeatureClass_conversion("Lac","C:/Users/Antoine/Desktop/test","Lac_empty.shp","fms")

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.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

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.