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])
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 7, 2016 at 15:16
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. Commented Nov 7, 2016 at 15:23
  • @Hornbydd When designing software, it's generally a bad idea to have a function modify global state. 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])
answered Nov 7, 2016 at 15:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.