0

I am trying to obtain the spatial reference for lists of feature classes using the following code;

def spatialFileListing():
    for root, dirs, files in os.walk(inputLocation):
        for d in dirs:
            workspace = os.path.join(root, d)
            arcpy.env.workspace = workspace
            fcList = arcpy.ListFeatureClasses()
            for fc in fcList:
                fcPath = os.path.join(workspace, fc)
                desc = arcpy.Describe(fcPath)
                descSR = desc.SpatialReference.Name
                print(descSR)

The below error message is returned;

"AttributeError: DescribeData: Method SpatialReference does not exist".

However, when I substitute for a fixed path to a shapefile e.g.:

fcPath = r"C:\Fixed\Path\To\Shapefile\A_Shapefile.shp"

The spatial reference is returned. E.g.:

"ETRS_1989_UTM_Zone_31N"

I'm sure it is something to do with the name / structure of the feature class being returned within the for loop, but don't know how to resolve it.

  • At the moment you are presenting a copy/paste from your code rather than a code snippet but, in any event I think you should print(fcPath) before you try to Describe it so that you are certain which data item is throwing the error. – PolyGeo Aug 7 '18 at 8:43
  • Thanks, I'll add that and find out what is causing the problem. – French Franc Aug 7 '18 at 9:16
1

Dont use dirs, root will give you correct path:

def spatialFileListing():
    for root, dirs, files in os.walk(inputLocation):
        workspace = root
        arcpy.env.workspace = workspace
        fcList = arcpy.ListFeatureClasses()
        for fc in fcList:
            fcPath = os.path.join(workspace, fc)
            desc = arcpy.Describe(fcPath)
            try:
                descSR = desc.SpatialReference.Name
                print(descSR)
            except AttributeError:
                print('Failed on fc: ',fc)
| improve this answer | |
  • Thanks very much. This returns one spatial reference then fails with the same error message; "AttributeError: DescribeData: Method SpatialReference does not exist". – French Franc Aug 7 '18 at 9:01
  • def main(): spatialFileListing() if name == 'main': main() – French Franc Aug 7 '18 at 9:07
  • Maybe you have feature classes without spatial reference. Add a print statement after for fc in fcList and check that feature class – BERA Aug 7 '18 at 9:08
  • If that is the case, I would need some code to deal with that situation wouldn't I? – French Franc Aug 7 '18 at 9:09
  • 1
    Of course. Capture the fact there is not a spatial reference and then move on to the next feature class. Thanks. I can deal with that. It's a case of working out how best to capture and then move on using something like "continue" or "break". Thanks. I'll work it out and post the code that works. – French Franc Aug 7 '18 at 9:15
1

The below code works. I realise I still need to include a try: except statement to catch anything untoward.

def spatialFileListing():
    for root, dirs, files in os.walk(inputLocation):
        workspace = root
        arcpy.env.workspace = workspace
        fcList = arcpy.ListFeatureClasses()
        for fc in fcList:
            spatial_ref = arcpy.Describe(fc).spatialReference
            if spatial_ref.name == "Unknown":
                print("{0} has an unknown spatial reference".format(fc))
            else:
                print("{0} : {1}".format(fc, spatial_ref.name))
| improve this answer | |

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.