Select to view content in your preferred language

Need help building my first script

8844
22
Jump to solution
03-28-2016 09:19 AM
AntoineCantin
Deactivated User

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
22 Replies
AdrianWelsh
MVP Honored Contributor

Hi Antoine,

To reiterate what others said, consider using the code block in the forum post:

use Advanced Editor in the thread and click on the double arrows, go to syntax then Python:

I like the commenting you have done in your code, it helps for others to see. Have you tried running your code to see if it works? You may want to how you format your path strings. There are many different ways to do this, here are some examples:

In your DeleteFeatures_Management, you do not have quotes around your layer name. From looking at other lines of code in your block, you will need those quotes.

AntoineCantin
Deactivated User
# 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
#Copy hydro.shp and delete all field of hydro_copy.shp so when union is done, the attributes table will only keep the fields from "zoning", leaving out the turned off fields of "hydro 
arcpy.opyFeatures_management("hydro.shp", "C:/output/hydro_copy.shp")
arcpy.DeleteField_management("C:/output/hydro_copy.shp",["STREET_NAM", "LABEL", "CLASS"])

 #union layerName with hydro.shp
acpy.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:
arcpy.DeleteFeatures_management(layerName_union)

#export data layerName_union as shapefile
  arcpy.FeatureClassToFeatureClass_conversion("layerName_union","C:/Chemin/vers/dossier","layerName_noHydro.shp")

#ReaRepeat for other layers
#Repeat for other layers

Is this better? Maybe it's my Chrome browser but the editor isn't friendly to use.

0 Kudos
AntoineCantin
Deactivated User
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!