1

I am making a script which creates a file geodatabase with a feature dataset named "Data". The user inputs the location of where the geodatabse should be created and the name of the geodatabase.

import os, arcpy, time ,sys
sr = arcpy.SpatialReference("WGS 1984 UTM Zone 37N")

#Create geodatabase
out_folder_path = raw_input('Enter location to create file geodatabase :')
out_name = raw_input('Enter name of file geodatabase :')
GDB = arcpy.CreateFileGDB_management(out_folder_path, out_name)

#Create feature datasets
out_dataset_path =  GDB
out_name0 = "Data"
fd0 = arcpy.CreateFeatureDataset_management(out_dataset_path, out_name0, sr)

How would one reference this feature dataset created above later on in a script, as an output/input location or to set the env.workspace?

I have tried for example:

arcpy.env.workspace = fd0
points = arcpy.ListFeatureClasses()
print points

This yields an error:

RuntimeError: Object: Error in accessing environment

To use as an output location I have tried:

    arcpy.ASCII3DToFeatureClass_3d (file,"XYZ", fd0 + "test", "MULTIPOINT", 1,sr,5,"TXT")

This gives the error:

TypeError: unsupported operand type(s) for +: 'Result' and 'str'

  • The question title says "geodatabase" but the question is about a feature dataset. Feature datasets are not workspaces. You also list multiple errors, but don't provide the code associated with the second error. – Vince Aug 23 '16 at 13:59
3

The issue is not your use of the arcpy workspace.

The issue is that you're expecting the CreateFeatureDataset tool to return the path of the created feature dataset. But ESRI has an obnoxious habit of being inconsistent (i.e., some tools do simply return the string that you would expect them to); and it's not helped by the fact that printing the result does in fact print the path that you want.

This tool is actually returning a Result object. Which means that if you want to extract the full path of the feature dataset from the result, you would need to change it to this:

result = arcpy.CreateFeatureDataset_management(out_dataset_path, out_name0, sr)
fd0 = result.getOutput(0)

Then, everything else can remain as it is.

  • Esri can definitely be inconsistent (especially with naming things!) but to be fair I think all arcpy commands return a result object. Incidentally you can test this using type(fd0) which returns class 'arcpy.arcobjects.arcobjects.Result' – Stephen Lead Aug 24 '16 at 5:01
  • @StephenLead, what I was referring to that returns strings is ListFeatureClasses (and the like), which return lists of strings--vs ListFields, which returns a list of field objects. – Tom Aug 24 '16 at 15:17
  • yep, that one is definitely inconsistent – Stephen Lead Aug 24 '16 at 23:00
1

I have had this problem before. Once you have created the GDB, you need to set this as the workspace.

import os, arcpy, time ,sys
sr = arcpy.SpatialReference("WGS 1984 UTM Zone 37N")

#Create geodatabase
out_folder_path = raw_input('Enter location to create file geodatabase :')
out_name = raw_input('Enter name of file geodatabase :')
GDB = arcpy.CreateFileGDB_management(out_folder_path, out_name)

#Set the GDB as the workspace
ws = str(str(GDB) + "\\")
arcpy.env.workspace = ws

#Create feature datasets
out_name0 = "Data"
fd0 = arcpy.CreateFeatureDataset_management(ws, out_name0, sr)

By adding the "\\" to the WS variable, any new feature classes or datasets created will be within the GDB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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