Select to view content in your preferred language

Is it possible to do a geoprocess automatically with all the layers of a folder?

1907
19
07-23-2018 03:50 AM
Jose_AlbertoNúñez
Emerging Contributor

Hi

I have 200 shapefile in a folder. all fo them are similar. I need select by atribute and them aply eliminate tool and save the shapes files. 

Is it possible to do automatically with all the layers from model builder or arcpy? How can I do?

thnak you very much

Tags (1)
0 Kudos
19 Replies
DanPatterson_Retired
MVP Emeritus

Do your process once so you can get the scripting syntax for each step.

Check the documentation on iterators in Modelbuilder if you aren't used to python.

Create a basic model to see if it works once.  I would suggest having a separate destination folder from the input folder it you are going to iterate all shapefiles in one folder.

Perhaps get a model started and people can leap in to see whether a model or script would be simpler in your case

0 Kudos
Jose_AlbertoNúñez
Emerging Contributor

Thank you @Dan Patterson,

.I tried it with Iterate Files in model builder but i can not connect output with the next step (Selec layer by atribute). I use connect icon but only can use precondition option. How can I upload a screenshot to show you?

0 Kudos
DanPatterson_Retired
MVP Emeritus

The easiest is to do a screen grab of the model, then insert it with the 'camera' (insert image) option into the post

0 Kudos
Jose_AlbertoNúñez
Emerging Contributor

Dan Patterson

That is I want to do with 190 files. I did with one shp example, its name is "Rod_inter_Masa_156_16.shp"

I want select polygons ❤️ ha, them use eliminate tool, by merging them with neighboring polygons that have the largest area or the longest shared border and finally save the result in a folder.

Doy you know how can I automate it?

Thank you


# Import arcpy module
import arcpy


# Local variables:
Rod_inter_Masa_156_16 = "Rod_inter_Masa_156_16"
Rod_inter_Masa_156_16__2_ = "Rod_inter_Masa_156_16"
Rod_inter_Masa_156_16_Elimin = "C:\\Users\\info\\Documents\\ArcGIS\\Default.gdb\\Rod_inter_Masa_156_16_Elimin"
prueba_shp = "C:\\Prueba_subrodal2\\prueba.shp"

# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(Rod_inter_Masa_156_16, "NEW_SELECTION", "\"Superfic_1\" <= 3")

# Process: Eliminate
arcpy.Eliminate_management(Rod_inter_Masa_156_16__2_, Rod_inter_Masa_156_16_Elimin, "LENGTH", "", "")

# Process: Copy Features
arcpy.CopyFeatures_management(Rod_inter_Masa_156_16_Elimin, prueba_shp, "", "0", "0", "0")

0 Kudos
Jose_AlbertoNúñez
Emerging Contributor

I tried to do it in model builder but unsuccessfully. I can not connect file.shp with select layer by attribute...

0 Kudos
DanPatterson_Retired
MVP Emeritus

Completely untested... but basically, set your workspace to the geodatabase which contains your data.

Set an output folder (note the 'raw' formatting)

cycle through the featureclasses, do a select by attributes, then eliminate.

import arcpy
import os

arcpy.env.workspace = "C:\\Users\\info\\Documents\\ArcGIS\\Default.gdb

fcs = arcpy.ListFeatureClasses()
out_folder = r"C:\SomeFolderForOutput"

for fc in featureclasses:

    out_name = "{}\\{}_elim.shp".format(out_folder, fc) 
    arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", "\"Superfic_1\" <= 3")

    arcpy.Eliminate_management(fc, out_name, "LENGTH", "", "")

Jose_AlbertoNúñez
Emerging Contributor

Dan Patterson‌ thank you very much. 

I did it:

import arcpy

from arcpy import env
env.overwriteOutput = True
import os

arcpy.env.workspace = r"E:\PLANFOR\PD_G000053VA_N\Carto\A_Monre\Tipos_masa\Prueba_subrodal\Rodales_tipmas.gdb"
fcs = arcpy.ListFeatureClasses()
out_folder = r"E:\PLANFOR\PD_G000053VA_N\Carto\A_Monre\Tipos_masa\Prueba_subrodal\Salida"


for fc in fcs:

out_name = "{}\\{}_elim.shp".format(out_folder, fc)
arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", "\"Superfic_1\" <= 3")

arcpy.Eliminate_management(fc, out_name, "LENGTH", "", "")

but it is there an error: 

I do not know why.

0 Kudos
XanderBakker
Esri Esteemed Contributor

In order to use "arcpy.SelectLayerByAttribute_management" the input must be a layer. See the help:

The input must be a feature layer or a table view. The input cannot be a feature class or table.

So you will have to use Make Feature Layer—Help | ArcGIS Desktop before arcpy.SelectLayerByAttribute_management

In  ArcGIS Pro the reference to the featureclass can be used and a layer is automatically created for you.

DanPatterson_Retired
MVP Emeritus

good catch... that's what you get for coding blind

0 Kudos