Monday 9 September 2019

arcgis desktop - Copying feature classes from Personal Geodatabase to File Geodatabase using ArcPy?


I have huge amount of personal geodatabases that contain dataset and feature classes (one dataset with four feature classes and one feature classes outside of the dataset in each mdb).


I want to create a file geodatabase for each of mdb and copy the dataset and feature classes in the file geodatabases.



For creating file geodatabse i use the below code but i don't know how to copy the datasets and feature classes to the file geodatabase programmatically.


import arcpy,os
for file in os.listdir(r"X:\test\2"):
if file.endswith(".mdb"):
arcpy.CreateFileGDB_management(r"X:\test\2",file[:-4],"CURRENT")

Answer



The following script converts all of the feature datasets and feature classes of a personal geodatabase to a new file geodatabase.




import arcpy, os


# Set the in and out workspaces
inws = r'C:\temp'
outws = r'C:\temp\outws'

# List the Access Databases
arcpy.env.workspace = inws
mdbs = arcpy.ListWorkspaces(workspace_type = "Access")

count = 1
for m in mdbs:

# Define output FGDB name
fgdb_name = os.path.basename(m).split(".")[0] + ".gdb"

# Create a new FGDB
arcpy.CreateFileGDB_management(outws, fgdb_name)

# Copy any FCs that are directly in personal geodatabase
arcpy.env.workspace = os.path.join(inws, m)
fcs = arcpy.ListFeatureClasses()
for fc in fcs:

arcpy.CopyFeatures_management(fc, os.path.join(outws, fgdb_name, fc))

# Report on processing status
print "%s of %s personal databases converted to FGDB" % (count, len(mdbs))
count += 1

# List the feature dataset
arcpy.env.workspace = os.path.join(inws, m)
fds = arcpy.ListDatasets()
for f in fds:

# Determine FDS spatial reference
desc = arcpy.Describe(f)
sr = desc.spatialReference

# Copy FDS to FGDB
arcpy.CreateFeatureDataset_management(os.path.join(outws, fgdb_name), f, spatial_reference = sr)

# Copy the FCs to new FDS
arcpy.env.workspace = os.path.join(inws, m, f)
fcs = arcpy.ListFeatureClasses()

for fc in fcs:
arcpy.CopyFeatures_management(fc, os.path.join(outws, fgdb_name, f, fc))

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...