1

I am trying to automate a workflow and I am working with the first 'piece' of the code which should check the feature classes for spatial reference and project them out if they do not equal the defined projection.

The following is the code I am using:

# Import arcpy and numpy
import os
import arcpy
import numpy

arcpy.env.workspace = r"C:\Users\GIS\Python\Distances.gdb"
outWorkspace = r"C:\Users\GIS\Python\Distances.gdb"

try:

    # Use ListFeatureClasses to generate a list of inputs
    for fc in arcpy.ListFeatureClasses():

        # Determine if the input has a defined coordinate system, can't project it if it does not
        dsc = arcpy.Describe(fc)

        if dsc.spatialReference.Name != "NAD 1983 StatePlane California III FIPS 0403 (US Feet)":
            # Determine the new output feature class path and name
            outfc = os.path.join(outWorkspace, fc + "_NAD1983")

            # Set output coordinate system
            outCS = arcpy.SpatialReference('NAD 1983 StatePlane California III FIPS 0403 (US Feet)')

            # run project tool
            arcpy.Project_management(fc, outfc, outCS)

            # check messages
            print(arcpy.GetMessages())

        else:

            print(fc)

except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

except Exception as ex:
    print(ex.args[0])

The code above just outputs all of the feature classes into the defined projection rather than discriminating between the ones that are properly projected and the ones that are not.

-Side Note- numpy is not used here but will be later.

1 Answer 1

3

Try printing out the "dsc.spatialReference.Name". When I did, none of my Spatial Reference Names had spaces and instead had underscores "_". I suspect that may be why none of your spatial references match "NAD 1983 StatePlane California III FIPS 0403 (US Feet)".

So it instead should be "NAD_1983_StatePlane_California_III_FIPS_0403_(US_Feet)".

3
  • This didn't work. Adding " _ " still output all of the feature classes with the extension _NAD1983 -- which is what it should do if all feature classes do not meet this classification.
    – Therow
    Feb 12, 2020 at 21:25
  • The only reason it would do that is because the "dsc.spatialReference.Name" doesn't match the string. Like I had said, try printing "dsc.spatialReference.Name" to the console and see what it returns. My guess is there's a typo that's causing the if block to run
    – MattM
    Feb 12, 2020 at 21:48
  • This was a very odd issue. I had run print on dsc previously which produced the following: "NAD_1983_StatePlane_California_III_FIPS_0403_Feet" *However, when trying to output the spatial reference it requires "NAD 1983 StatePlane California III FIPS 0403 (US Feet)" with arcpy.SpatialReference. I have no idea why there is a need for one over the other.
    – Therow
    Feb 12, 2020 at 22:59

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.