5

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])
2
  • 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
    Commented Nov 7, 2016 at 15:23
  • @Hornbydd When designing software, it's generally a bad idea to have a function modify global state.
    – jpmc26
    Commented Nov 7, 2016 at 17:52

1 Answer 1

9

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])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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