How to access multiple workspaces in different locations

3084
4
Jump to solution
09-23-2015 03:14 PM
JohnPapageorgiou
New Contributor III

Hi,

New to python.  I'm trying to automate a task of selecting features from several shapefiles in multiple directories.  Conceptually I'm thinking of the following: activate all necessary workspace directories, convert necessary feature classes or shapefiles to feature layers, use the select by location tool.  This all to be done with the intent of batch selection and export of selected features.  My first stumbling block is actually being able to set up multiple workspaces simultaneously and just list their feature feature classes.  I can do it for one workspace a time but not multiple.  I feel like this is a crucial step if I want to proceed with anything else.  Let me know if I'm way off and point me in the right direction.  Any help is appreciated.

thanks,

John

0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Regular Contributor II

You have two things you need to look into for your issue. First is the esri related problem. You have to understand the workflow of iterating through a workspace to locate files one at a time so that you can perform your query against them and export the needed records.

It sounds like you understand the bulk of the workflow (i.e. making the layers, performing the selection, checking to see if anything was selected, and exporting the selected records to a new table) so I'll skip the part.

The latter part is not related to Esri and is more of a programming technique question. You'll want to look into using the threading or multiprocessing modules in python. I've added links to these modules below. It sounds to me like you need to create a workflow where you can use a single thread to locate the paths to your data, upload this data to a queue and have multiple threads or processes pull jobs from this queue for processing.

Python Queue - A thread-safe FIFO implementation

https://pymotw.com/2/Queue/

multiprocessing - Process-Based "threading" interface

https://docs.python.org/2/library/multiprocessing.html

Multiprocessing Basics

https://pymotw.com/2/multiprocessing/basics.html

threading - Higher-level threading interface

https://docs.python.org/2/library/threading.html

Python Multithreaded Programming

http://www.tutorialspoint.com/python/python_multithreading.htm

threading - Manage concurrent threads

https://pymotw.com/2/threading/

View solution in original post

4 Replies
IanMurray
Frequent Contributor

You could create an empty list and append each file path plus its workspace to the list for each workspace you need, using arcpy.da.walk on each workspace.  That way you have a full file path for each shapefile or feature class in  a list, which you could iterate over creating feature layers and then selecting them by location.

EmmanuelAina
New Contributor

Please, how do I do this? Assuming I have folders, a, b, c, and d.

0 Kudos
FreddieGibson
Regular Contributor II

You have two things you need to look into for your issue. First is the esri related problem. You have to understand the workflow of iterating through a workspace to locate files one at a time so that you can perform your query against them and export the needed records.

It sounds like you understand the bulk of the workflow (i.e. making the layers, performing the selection, checking to see if anything was selected, and exporting the selected records to a new table) so I'll skip the part.

The latter part is not related to Esri and is more of a programming technique question. You'll want to look into using the threading or multiprocessing modules in python. I've added links to these modules below. It sounds to me like you need to create a workflow where you can use a single thread to locate the paths to your data, upload this data to a queue and have multiple threads or processes pull jobs from this queue for processing.

Python Queue - A thread-safe FIFO implementation

https://pymotw.com/2/Queue/

multiprocessing - Process-Based "threading" interface

https://docs.python.org/2/library/multiprocessing.html

Multiprocessing Basics

https://pymotw.com/2/multiprocessing/basics.html

threading - Higher-level threading interface

https://docs.python.org/2/library/threading.html

Python Multithreaded Programming

http://www.tutorialspoint.com/python/python_multithreading.htm

threading - Manage concurrent threads

https://pymotw.com/2/threading/

JohnPapageorgiou
New Contributor III

My apologies to both Ian and Freddie.  When I asked my question I think I was headed down the wrong path because I was new at this.  Both of your answers were correct, but I didn't end up needing to to work with multiple workspaces.  However, If I do I think both of your answers would be very helpful in the future.  The good news is that I managed to write my script and it works.  Thank you for your helpful input and your very fast responses.

0 Kudos