[GIS] Create FeatureDataSet in geodatabase with Arcpy from a table

arcpyfeature-datasetfile-geodatabasetable

I create an empty file geodatabase (gdb), and I would like to import in this gdb several feature dataset, at least 120 different dataset. I would like to import those dataset from a table like an excel table.

I try to do this with the python command with something like :

arcpy.CreateFileGDB_management("C:/data/arcgis/gdb", "gdbTemp.gdb")
<Result 'C:/data/arcgis/gdb\\gdbTemp.gdb'>

>>> import arcpy
from arcpy import env

env.workspace = "C:/data/arcgis/gdb"
arcpy.CreateFeatureDataset_management("C:/data/arcgis/gdbTemp.gdb", 

From the last line I don't know how to :

  • import my spreadsheet
  • tell I want for each line of the spreadsheet a new feature Dataset in my geodatabase

My actual spreadsheet looks like that, Its is very basic with only the name of the dataset (I want to go step by step).

enter image description here

So, I would like to create for each row a new dataset in the geodatabase.

I found something for an other post, It seems to be a good starting point :

fdList = [...]

Here I could make a command that get back from the spreadsheet the name of all my future dataset.

Then with a loop, add them on the geodatabase :

for fd in fdList:
    arcpy.CreateFeatureDataset_management(""C:/data/arcgis/gdbTemp.gdb"", fd, "I:/python/MultipleFD2GDB/2229.prj")

I need to do this with the python command (is this arcpy?)

Best Answer

Below is the code you require:

import arcpy
xls = r"C:\Scratch\Book1.xls"
table =r"C:\Scratch\tempxls.dbf"
arcpy.ExcelToTable_conversion(xls,table)
with arcpy.da.SearchCursor(table,["Feature"]) as cursor:
    for row in cursor:
        fd = row[0]
        print "Creating FeatureDataset: " + fd
        arcpy.CreateFeatureDataset_management(r"C:\Scratch\fGDB_Scratch.gdb", fd)

# Clean up
arcpy.Delete_management(table)

You need to read the Excel file, I did this by converting it into a temporary dBase file which I delete at the end. The code then steps through the temporary table reading each row and using that as the name for the FeatureDataset. In my example I do not set a spatial reference.

I don't think (but could be wrong) that you can read an Excel table with a search cursor in arcpy. This answer to Reading Excel sheet in ArcPy script? discusses reading an Excel sheet using another Python module.