1

Is it possible to create a python script to import shapefiles/feature classes, from different sources into specific feature datasets in geodatabase, but in one script?

I managed to write the script to make multiple feature datasets in geodatabase. This is the code:

>>> import arcpy 
    arcpy.CreateFileGDB_management("D:/GIS_Temp", "TEST.gdb")
    fdList = ["Datase_A", "Dataset_B", "Dataset_C"]
    for fd in fdList:
    arcpy.CreateFeatureDataset_management("D:/GIS_Temp/TEST.gdb", fd, "D:/GIS_Temp/Projection.prj")

And now i would like to import shapefiles/feature classes into specific datasets. For example, from "folder A", to "Dataset_A", "folder B" to "Dataset_B", and so on.

I could do it one script by one, like this:

>>> import arcpy
    arcpy.env.workspace = 'D:/GIS_Temp/Folder_A'
    arcpy.FeatureClassToGeodatabase_conversion(["File_X", "File_Y"],
    'D:/GIS_Temp/Test.gdb/Dataset_A')

But is it possible to do all it in one script. I am really new to python scripting, so I really don't know to combine those separate scripts and do the task.

If I use this code, i can get shapefiles into root of database, but not in feature dataset:

>>> import arcpy
    from arcpy import env
    import os
    env.workspace = "D:/GIS_Temp/Folder_A/"
    fcList = arcpy.ListFeatureClasses()
    for fc in fcList:
    arcpy.CopyFeatures_management(fc, "D:/GIS_Temp/Test.gdb/" + os.sep + fc.rstrip(".shp"))

And if I add name of dataset like this, it won't run:

arcpy.CopyFeatures_management(fc, "D:/GIS_Temp/Test.gdb/Dataset_A" + os.sep + fc.rstrip(".shp"))

This could work:

>>> import arcpy
    arcpy.env.workspace = 'D:/GIS_Temp/Folder_A'
    arcpy.FeatureClassToGeodatabase_conversion(["File_X", "File_Y"],
    'D:/GIS_Temp/Test.gdb/Dataset_A')

>>> import arcpy
    arcpy.env.workspace = 'D:/GIS_Temp/Folder_B'
    arcpy.FeatureClassToGeodatabase_conversion(["File_X", "File_Y"],
    'D:/GIS_Temp/Test.gdb/Dataset_B')

but only from one folder, to one dataset. I would like to write ONE code that would allow me to import defined shapefiles/classes, into specific feature datasets.

6
  • Yes, but be aware that feature class names need to be unique within the database... so you can't have two feature classes called 'roads' for example. Your code is very close but I think you need to look at arcpy.ListFeatureClasses help.arcgis.com/en/arcgisdesktop/10.0/help./index.html#//… to get the list of shapefiles from your input folders and you can sort this yourself. Note: you need an indent after for fd in fdList: or it won't work. Apr 26, 2016 at 22:07
  • I am familiar that that code, and it works for me, but only to import shapefiles.
    – Dean7
    Apr 26, 2016 at 22:17
  • So what's the problem? I see you're using an interpreter, are you stuck on creating a python file and running that? Apr 26, 2016 at 22:20
  • I tried this code from link, and it works only to import shapefiles into root of geodatabase, not in specific dataset. I don't know how to at edit second script, which will import data specfic folders into defined dataset, so I do not have to write code by code, like in second example.
    – Dean7
    Apr 26, 2016 at 22:26
  • Does it give any error message? Do your shapefiles have spaces in their names? Apr 26, 2016 at 22:37

3 Answers 3

1

I suspect that your script is failing because the output already exists, to avoid this set overwrite to True, which is easier than check and delete, but may not be what you need in the long run.

Putting together your scraps into a contiguous codeblock:

# set overwrite = True so it won't crash if 
# the output already exists
arcpy.env.overwriteOutput = True

# setup your from/to as lists of the same length
fdList  = ["Datase_A", "Dataset_B", "Dataset_C"]
folList = ["c:\\path\\Folder A", "c:\\path\\Folder B", "c:\\path\\Folder C"]

# create a range [0,1,2] to index the lists
workRange = range(len(fdList))

for thisIndex in workRange:
    # step through the lists one by one
    fd = fdList[thisIndex]
    arcpy.env.workspace = folList[thisIndex]

    arcpy.CreateFeatureDataset_management("D:\\GIS_Temp\\TEST.gdb", fd, "D:\\GIS_Temp\\Projection.prj")
    for impFC in arcpy.ListFeatureClasses():
        fcName,fcExt = os.path.splitext(impFC) # split into name and extension
        fcName.replace(" ","_") # get rid of spaces.

        # os.path.join is your friend here, it will join the components 
        # of paths with your OS path separator
        arcpy.FeatureClassToFeatureClass_conversion(os.path.join(folList[thisIndex],impFC),os.path.join("D:\\GIS_Temp\\TEST.gdb",fd),fcName)

will match up your folders to feature datasets and import each feature class.. beware that names must be unique as I said in the comments; this code does not check for that.

5
  • If i run this code, i get this error: "Runtime error Traceback (most recent call last): File "<string>", line 24, in <module> TypeError: FeatureClassToGeodatabase() takes at most 2 arguments (3 given)"
    – Dean7
    Apr 26, 2016 at 23:16
  • You're right, I got my tools mixed up. It should be FeatureClassToFeatureClass_conversion. Apr 26, 2016 at 23:20
  • There is still the same error. But if i remove "fcName", from last line it seems to be working.
    – Dean7
    Apr 26, 2016 at 23:32
  • That doesn't sound right, unless you haven't changed the tool. There's quite a few that sound similar that do almost exactly the same thing with different arguments... have a read of help.arcgis.com/En/Arcgisdesktop/10.0/Help/index.html#//… about FeatureClassToFeatureClass_conversion. Is the result what you expect? Apr 26, 2016 at 23:36
  • I tried both "FeatureClassToGeodatabase", and "FeatureClassToFeatureClass_conversion". and gives me errors, unless i remove "fcname"
    – Dean7
    Apr 26, 2016 at 23:46
1

This could work, based on answer Michael wrote, but there are still some problems:

>>> fdList  = ["Datase_A", "Dataset_B", "Dataset_C"]
    folList = ["D:\\GIS_Temp\Folder A", "D:\\GIS_Temp\\Folder B", "D:\\GIS_Temp\\Folder C"]
    workRange = range(len(fdList))
    for thisIndex in workRange: 
    fd = fdList[thisIndex]
    arcpy.env.workspace = folList[thisIndex]
    arcpy.CreateFeatureDataset_management("D:\\GIS_Temp\\TEST.gdb", fd, "D:\\GIS_Temp\\Projection.prj")
    for impFC in arcpy.ListFeatureClasses():
    fcName,fcExt = os.path.splitext(impFC) 
    fcName.replace(" ","_")
    arcpy.FeatureClassToFeatureClass_conversion(os.path.join(folList[thisIndex],impFC),os.path.join("D:\\GIS_Temp\\TEST.gdb",fd),fcName)

and then i get this error:

Runtime error Traceback (most recent call last): File "", line 11, in File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\conversion.py", line 1547, in FeatureClassToFeatureClass raise e ExecuteError: ERROR 999999: Error executing function. Failed to execute (FeatureClassToFeatureClass).

And if I use same code, but with last line changed:

FeatureClassToGeodatabase_conversion(os.path.join(folList[thisIndex],impFC),os.path.join("D:\\GIS_Temp\\TEST.gdb",fd),fcName)

i get this error:

Runtime error Traceback (most recent call last): File "", line 11, in TypeError: FeatureClassToGeodatabase() takes at most 2 arguments (3 given)

5
  • Does D:\\GIS_Temp\\TEST.gdb exist? Is it empty? I can't see if you've set overwrite so you could be getting an error because that name is already in use within the database. It could be the database has gone a little confused... it might be better to delete it and re-make it with ArcCatalog unless there's other feature classes you want to keep in it. Apr 27, 2016 at 0:38
  • Whole code with "FeatureClassToFeatureClass" seems to be working. But, in that case i get an error, that my shapefiles have invalid characters. Is there a workaround to that, so the code should also fix spaces in names, and replace it with underscore. For example, my shapefile is called "Point B", so the code returns with error. Is there part of code that should fix it?
    – Dean7
    Apr 27, 2016 at 1:10
  • It's in my code.. fcName.replace(" ","_") # get rid of spaces. Apr 27, 2016 at 1:11
  • It still gives this error: "Runtime error Traceback (most recent call last): File "<string>", line 12, in <module> File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\conversion.py", line 1547, in FeatureClassToFeatureClass raise e ExecuteError: ERROR 000354: The name contains invalid characters Failed to execute (FeatureClassToFeatureClass)."
    – Dean7
    Apr 27, 2016 at 1:29
  • I am sorry, as you asked me before, I just now realized that shapefiles do have spaces in their names, but this code is not fixing it. Code just works if i manually rename the spaces first, so i asked, is there a code, that will fix this. that code can be standalone, it is not necessary to be part of this code.
    – Dean7
    Apr 27, 2016 at 1:35
0

To sum up, this code works, as Michael helped:

>>> arcpy.env.overwriteOutput = True
    fdList  = ["Dataset_A", "Dataset_B", "Dataset_C"]
    folList = ["D:\\GIS_Temp\Folder A", "D:\\GIS_Temp\\Folder B", "D:\\GIS_Temp\\Folder C"]
    workRange = range(len(fdList))
    for thisIndex in workRange:
    fd = fdList[thisIndex]
    arcpy.env.workspace = folList[thisIndex]
    arcpy.CreateFeatureDataset_management("D:\\GIS_Temp\\TEST.gdb", fd, "D:\\GIS_Temp\\Projection.prj")
    for impFC in arcpy.ListFeatureClasses():
    fcName,fcExt = os.path.splitext(impFC)
    fcName.replace(" ","_")
    arcpy.FeatureClassToFeatureClass_conversion(os.path.join(folList[thisIndex],impFC),os.path.join("D:\\GIS_Temp\\TEST.gdb",fd),fcName)

But if this code gives an error about names, this code, from Multiple rename shapefiles and feature classes in python helps, and it should be run before this code:

>>> import arcpy
    InWork =  ["D:\\GIS_Temp\Folder A", "D:\\GIS_Temp\\Folder B"]
    for ws in InWork:
    arcpy.env.workspace = ws
    datasets = arcpy.ListFeatureClasses()
    for fc in datasets:
    newName = fc.replace(' ','_')
    arcpy.Rename_management(fc, newName[:-4])

Your Answer

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

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