0

Anyone know how i can fix this problem below?

I am trying to merge all the layers under the same name in different subfolders.

import arcpy
import os

# parameters
input_folder = arcpy.GetParameterAsText(0)
output_geodatabase = arcpy.GetParameterAsText(1)

# validate output geodatabase name
output_geodatabase = os.path.join(os.path.dirname(output_geodatabase), 
os.path.basename(output_geodatabase).split('.')[0] + '.gdb')

# turn overwrite on
arcpy.env.overwriteOutput = True

# make output geodatabase if it doesn't exist
if not os.path.exists(output_geodatabase):
    arcpy.AddMessage("Creating output geodatabase...")
    # make sure name is valid
    arcpy.CreateFileGDB_management(os.path.dirname(output_geodatabase), 
os.path.basename(output_geodatabase))


# Making an empty list to put filepaths to feature classes in
arcpy.AddMessage("Scanning input directory...")
merge_dict = {}
# Walking workspace recursively checking type, and appending filepath to list
for dirpath, dirnames, filenames in arcpy.da.Walk(input_folder, 
datatype="FeatureClass"):
    for filename in filenames:
        desc = arcpy.Describe(os.path.join(dirpath, filename))
        if not filename in merge_dict:
            merge_dict[filename] = [os.path.join(dirpath, filename)]
        else:
            cur = merge_dict[filename]
            cur.append(os.path.join(dirpath, filename))
            merge_dict[filename] = cur
arcpy.AddMessage(" -> Found {} different file groups to 
merge".format(len(merge_dict.keys())))



arcpy.AddMessage("Starting merge processes...")
i = 1
tot = len(merge_dict.keys())
for k in merge_dict.keys():
    arcpy.AddMessage(' -> {}/{} {}'.format(i, tot, k))
    # get dataset name
    datasetName = os.path.basename(os.path.dirname(merge_dict[k][0]))
    # if output dataset with same name doesn't exist, make it
    if not arcpy.Exists(os.path.join(output_geodatabase, datasetName)):
        # get spatial reference
        sr = arcpy.Describe(os.path.dirname(merge_dict[k][0])).spatialReference
        arcpy.CreateFeatureDataset_management(output_geodatabase, datasetName, sr)
    # now merge files
    outputMergedFile = os.path.join(output_geodatabase, datasetName, k)
    arcpy.Merge_management(merge_dict[k], outputMergedFile)

    i += 1

arcpy.AddMessage('Done!')

Below is the error from ArcGIS Pro:

Failed script Merge Data...
 Traceback (most recent call last):
  File "C:\Users\Admin\Desktop\Python Script\merge_data.py", line 48, in <module>
    sr = arcpy.Describe(os.path.dirname(merge_dict[k][0])).spatialReference
AttributeError: DescribeData: Method spatialReference does not exist
 Failed to execute (MergeData).
  • 1
    You are trying to describe the GDB to get a spatial reference. Run Describe on a featureclass. – user2856 Aug 21 '18 at 2:31
  • can i ask how i can change to that? the script is done by my friend :/ and i dont know how to edit this without any python knowledge – Darius Aug 21 '18 at 2:35
2

Try changing:

sr = arcpy.Describe(os.path.dirname(merge_dict[k][0])).spatialReference

to this:

sr = arcpy.Describe(merge_dict[k][0]).spatialReference

The os.path.dirname() is being passed a feature class (I think?) in which case it will be returning the workspace. You want to use the feature class itself, not the workspace to get the spatial reference.

See: http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/featureclass-properties.htm

which says that it also includes: http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/dataset-properties.htm

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.