I am writing the following code to make a report describing a feature class. SO far, I got their names and shape type, but now I need to get SRS/coordinate system for each of the feature class. I think I can use

spatial_ref = arcpy.Describe(dataset).spatialReference

but what would the parameter be if I use this in my code?

def data_report(workspace):

elements = defaultdict(list)

walk = arcpy.da.Walk(workspace, topdown=True)

for dirpath, dirnames, filenames in walk:
    for dirname in dirnames:  # 'FeatureDataset'
        desc = arcpy.da.Describe(os.path.join(dirpath, dirname))

    for filename in filenames:
        desc = arcpy.da.Describe(os.path.join(dirpath, filename))

report = []

for element_type, element_list in elements.items():
    report += [f"{element_type}:"]
    for element_description in element_list:

        if element_type == "FeatureClass":
            report +=  [f"\t\t{element_description['baseName']}"] # Here I just output the path, but there's lots more info in the Describe object
            report +=  [f"\t\t\tShape type is: {element_description['shapeType']}"] 
  • One possible solution is that I can use List function to make a list of the feature class, but is there any way I can do that without making the list? – Yoshi Feb 18 at 22:18
  • 2
    Please do not comment on your own question (Edit the Question instead). The DA Describe returns a dictionary; compiling a list of dictionaries (or feature classes) doesn't seem to be getting you closer to your goal. – Vince Feb 19 at 0:36

Assuming that you are simply asking how to get a string (text) representation of your spatial_ref spatial reference object that you can append to your report...

Here is the documentation for the spatial reference class. If you examine the Properties section, you will find various properties that return a string (text). Eg, "name". You can use one of these, or join several of them together if you need more information about the spatial reference.


spatial_ref = arcpy.Describe(dataset).spatialReference
spatialRefName = spatial_ref.name

or simply:

spatialRefName = arcpy.Describe(dataset).spatialReference.name
|improve this answer|||||
  • Sorry I meant what should I put into (dataset) ? I already have desc = arcpy.da.Describe(os.path.join(dirpath, dirname)), so should I use (os.path.join(dirpath, dirname)) again? – Yoshi Feb 20 at 4:13
  • report += [f"\t\t\t{} has an unknown spatial reference".format(fc)] ^ SyntaxError: f-string: empty expression not allowed – Yoshi Feb 20 at 20:21
  • This is how I tried to do in in report +=, but I got the error message – Yoshi Feb 20 at 20:22
  • You are mixing f-strings and format() strings. It should be either [f"\t\t\t{fc} has an unknown spatial reference"] (with f prefix and variable in {}) OR ["\t\t\t{} has an unknown spatial reference".format(fc)] (without f prefix and variable in format()). They are two different and incompatible ways of interpolating/formatting variable values into literal strings. – Son of a Beach Feb 20 at 21:09

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.