Python code for exporting multiple layers?

5211
8
12-09-2016 08:51 AM
TheoFaull
Occasional Contributor III

Hi everyone,

I have 10 layers in my MXD. Each layer's attribute table has a text field called 'Contractor'. This field has a few different values in it, but I'm only concerned with ones that contain the word 'TLG' or 'TLG[extra bits of text]'

I want to export each layer to create 10 new shapefiles in a seperate folder. But only exporting the records where 'Contractor' = 'TLG' or 'TLG%'

At the moment I go into each layer, select all the records that include TLG and right click layer>export data (selected records only).

This is a manual slow process which I repeat every week or so when updating our clients with shapefile data.

ArcMap 10.4.

Thanks!!!

(I would like to use the geoprocessing>python console window if possible. I've used that before, so it's familiar to me!)

Tags (2)
0 Kudos
8 Replies
DarrenWiens2
MVP Honored Contributor
MichaelVolz
Esteemed Contributor

Once you have created your python script from the above samples, you can then use Task Scheduler to have the script automatically run each week.  This can be done either on your desktop or a more robust server machine if you have access to one.

TheoFaull
Occasional Contributor III

OK so far I've managed to export all records from my LITTER_BIN shapefile that contain "TLG" in the 'contractor' field, into a new shapefile (called LITTER_BINS_new)

EDIT: damn this is annoying, how can I paste python code in the correct viewing format into this post? No option in advanced editor!

>>> import arcpy
... from arcpy import env
...
... env.workspace = "K:/Data/Estates_Services/GROUNDS MAINTENANCE/GROUP GROUNDS MAINTENANCE"
... arcpy.Select_analysis("LITTER_BINS.shp", "U:\Tasks\TLG_data_updates\TLG_update_14122016/LITTER_BINS_new.shp", '"Contractor" = \'TLG\'')

Questions now:

1. How can I make the code select+export records from multiple shapefiles/into new multiple shapefiles? Not just one at a time?

2. How can I make the code select records where 'Contractor' = TLG or TLG%. The wildcard appears to be * in python (right?) but if I write '"Contractor" = \'TLG*\'', it exports a blank shapefile with no records.

Thanks

0 Kudos
MitchHolley1
MVP Regular Contributor

Is the 'Contractor' field a string type?  You may want to use FieldDelimiters to be safe: AddFieldDelimiters—ArcPy Functions | ArcGIS for Desktop

Something like this may work. 

field = 'Contractor'
datasource = r'path to datasource'
qry = arcpy.AddFieldDelimiters(datasource, field) + " LIKE '%TLG%'‍‍‍‍‍‍"‍‍‍‍‍‍
0 Kudos
roemhildtg
Occasional Contributor III

You can do this entirely in model builder...without coding, but if you want to do it with coding the same concepts apply.

Basic Steps:

  1. Iterate feature classes in workspace
    1. Make Feature Class (with definition query)
    2. Export to shapefile

Details:

Model builder has a special tool caled Iterate Feature Classes in workspace. You access it by using Insert > Iterators > Iterate Feature Classes. It accepts a workspace as input. Then you just chain the feature class to Make Feature Layer (with definition query) and then to Feature class to feature class.

You can set definition query in the Make feature layer tool:

You can use %Name% in your parameter to access the name of the input layer:

In python you can do this with:

import arcpy
arcpy.env.workspace = 'C:/workspace'
output_folder = 'C:/Output'
fc_list = arcpy.ListFeatureClasses()

for fc in fc_list:
    arcpy.MakeFeatureClass_management(fc, fc + "_filtered", where_clause="Contractor LIKE 'TLG%'")
    arcpy.FeatureClassToShapefile(fc+ "_filtered", output_folder)
TheoFaull
Occasional Contributor III

Ok, I found a way to export multiple shapefiles to multiple, new shapefiles. Using "TLG" as a means of selection.

>>> import arcpy
... from arcpy import env
...
... env.workspace = "K:\Data\Estates_Services\GROUNDS MAINTENANCE\GROUP GROUNDS MAINTENANCE"
... arcpy.Select_analysis("LITTER_BINS.shp", "U:\Tasks\TLG_data_updates\TLG_update_14122016/LITTER_BINS.shp", '"Contractor" = \'TLG\'')
... arcpy.Select_analysis("AMENITY_GRASS.shp", "U:\Tasks\TLG_data_updates\TLG_update_14122016/AMENITY_GRASS.shp", '"Contractor" = \'TLG\'')
... arcpy.Select_analysis("AMENITY_GRASS_SHELTERED.shp", "U:\Tasks\TLG_data_updates\TLG_update_14122016/AMENITY_GRASS_SHELTERED.shp", '"Contractor" = \'TLG\'')
... arcpy.Select_analysis("HEDGES_1SIDE_ONLY.shp", "U:\Tasks\TLG_data_updates\TLG_update_14122016/HEDGES_1SIDE_ONLY.shp", '"Contractor" = \'TLG\'')
... arcpy.Select_analysis("HEDGES_1SIDE_TOP.shp", "U:\Tasks\TLG_data_updates\TLG_update_14122016/HEDGES_1SIDE_TOP.shp", '"Contractor" = \'TLG\'')
... arcpy.Select_analysis("HEDGES_2SIDES_TOP.shp", "U:\Tasks\TLG_data_updates\TLG_update_14122016/HEDGES_2SIDES_TOP.shp", '"Contractor" = \'TLG\'')
... arcpy.Select_analysis("LEAF_CLEARANCE.shp", "U:\Tasks\TLG_data_updates\TLG_update_14122016/LEAF_CLEARANCE.shp", '"Contractor" = \'TLG\'')
... arcpy.Select_analysis("MEADOW_GRASS.shp", "U:\Tasks\TLG_data_updates\TLG_update_14122016/MEADOW_GRASS.shp", '"Contractor" = \'TLG\'')
... arcpy.Select_analysis("ROUGH_GROUND.shp", "U:\Tasks\TLG_data_updates\TLG_update_14122016/ROUGH_GROUND.shp", '"Contractor" = \'TLG\'')
... arcpy.Select_analysis("SHELTERED_GRITTING_AREAS.shp", "U:\Tasks\TLG_data_updates\TLG_update_14122016/SHELTERED_GRITTING_AREAS.shp", '"Contractor" = \'TLG\'')

I just copy pasted the line 10 times!

Now I still want to know how to make "TLG", "TLG%"... Everytime I use the %, it exports a new shapefile with no records in it!

0 Kudos
TheoFaull
Occasional Contributor III

And someone please tell me how to insert python code into a post on these forums... 😕

0 Kudos
BrittneyWhite1
Esri Contributor