Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I combine all three workspaces?

Only 1 workspace is being read while exporting data.

import os
import arcpy

# Set environment settings
arcpy.env.workspace = "C:/eis/eis shape/UPDM.gdb/P_PipeSystem"
arcpy.env.workspace = "C:/eis/eis shape/UPDM.gdb/OtherCompanies"
arcpy.env.workspace = "C:/eis/eis shape/UPDM.gdb/NEO"

arcpy.env.overwriteOutput = True

# Set local variables
outWorkspace = "C:/data"

try:
    # Use ListFeatureClasses to generate a list of inputs
    for infc in arcpy.ListFeatureClasses():

    # Determine if the input has a defined coordinate system, can't project              it if it does not
    dsc = arcpy.Describe(infc)

    if dsc.spatialReference.Name == "Unknown":
        print ('skipped this fc due to undefined coordinate system: ' + infc)
    else:
        # Determine the new output feature class path and name
        outfc = os.path.join(outWorkspace, infc)

        # Set output coordinate system
        outCS = arcpy.SpatialReference('WGS 1984')

        # run project tool
        arcpy.Project_management(infc, outfc, outCS)

        # check messages
        print(arcpy.GetMessages())

except arcpy.ExecuteError:
print(arcpy.GetMessages(2))

except Exception as ex:
print(ex.args[0])
share|improve this question
2  
You cannot,arcpy can only have one current workspace, turn your code into function that takes a workspace then call it three times, one for each input workspace. – Hornbydd 2 days ago
    
@Hornbydd When designing software, it's generally a bad idea to have a function modify global state. – jpmc26 yesterday
up vote 8 down vote accepted

You can have only one 'current' or 'active' workspace. The ListFeatureClasses() function is similar to 'dir' command in windows command line - you can list only one 'current' dir at time and if you need more, you need to switch.

Use for cycle to iterate over workspaces:

import os
import arcpy

# Set environment settings
workspaces = [
    "C:/eis/eis shape/UPDM.gdb/P_PipeSystem",
    "C:/eis/eis shape/UPDM.gdb/OtherCompanies",
    "C:/eis/eis shape/UPDM.gdb/NEO"
]

arcpy.env.overwriteOutput = True

# Set local variables
outWorkspace = "C:/data"

try:
    for ws in workspaces:
        arcpy.env.workspace = ws
        # Use ListFeatureClasses to generate a list of inputs
        for infc in arcpy.ListFeatureClasses():

            # Determine if the input has a defined coordinate system, can't project              it if it does not
            dsc = arcpy.Describe(infc)

            if dsc.spatialReference.Name == "Unknown":
                print ('skipped this fc due to undefined coordinate system: ' + infc)
            else:
                # Determine the new output feature class path and name
                outfc = os.path.join(outWorkspace, infc)

                # Set output coordinate system
                outCS = arcpy.SpatialReference('WGS 1984')

                # run project tool
                arcpy.Project_management(infc, outfc, outCS)

                # check messages
                print(arcpy.GetMessages())

except arcpy.ExecuteError:
print(arcpy.GetMessages(2))

except Exception as ex:
print(ex.args[0])
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.