0

I am trying to convert the feature classes in data.gdb to a new PCS per the code on this webpage:

import arcpy
import os
arcpy.env.workspace = "G:/W135thStCopy/data.gdb"
arcpy.env.overwriteOutput = True
outWorkspace = "G:/W135thStCopy/data_WebMerc.gdb"
try:
    for infc in arcpy.ListFeatureClasses():
        dsc = arcpy.Describe(infc)

        if dsc.spatialReference.Name == "Unknown":
            print ('skipped this fc due to undefined coord sys: ')
            print (infc)

        else:
            #Determine the new outpurt feature class
            outfc = os.path.join(outWorkspace, infc)

            #Set output coord sys
            outCS = arcpy.SpatialReference('WGS 1984 Web Mercator Auxiliary Sphere')
            break
            #Run project tool
            arcpy.Project_management(infc, outfc, outCS)

            #Check messages
            print(arcpy.GetMessages())

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

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

ERROR 999999: Error executing function.

The error seems to be at the call to SpatialReference() since I don't get the error when I insert the break above that line. I have tried removing the underscores in the coordinate system name and still get the error. How can I fix this?

  • Just to confirm, what do you get when you print outfc? – smiller May 15 at 15:40
  • I also get an error immediately at that SpatialReference definition. Testing. – smiller May 15 at 15:41
  • 1
    try with the EPSG code instead of the name (e.g. outCS = arcpy.SpatialReference(3857)). Does it give you the same error? – umbe1987 May 15 at 15:42
  • This line worked without error (added parenthesis based on pro.arcgis.com/en/pro-app/tool-reference/…) testcs = arcpy.SpatialReference("WGS 1984 Web Mercator (Auxiliary Sphere)"). – smiller May 15 at 15:42
  • 1
    Moreover, there does not seem to be any name like the one you used ("WGS 1984 Web Mercator Auxiliary Sphere") in this official PCS naming document linked by the same arcpy.SpatialReference help page. To my understanding you should use something like "WGS_1984_Web_Mercator_Auxiliary_Sphere". – umbe1987 May 15 at 15:47
1

According to at least one reference, the spatial reference name has "Auxiliary Sphere" in parentheses.

This line worked without error:

testcs = arcpy.SpatialReference("WGS 1984 Web Mercator (Auxiliary Sphere)")

You could also use the EPSG code instead e.g. outCS = arcpy.SpatialReference(3857)

  • I changed the SpatialReference parameter to 3857: #Set output coord sys outCS = arcpy.SpatialReference(3857) and got: ERROR 000208: Error creating output feature class Failed to execute (Project). Looking up the error, arcpy is complaining about the name: "The name that has been specified is likely invalid. Correct the name and try again..." resources.arcgis.com/en/help/main/10.2/index.html#//… Seems strange because the field names in the infc should be valid. I'll try the ValidateFieldName method.. – Regulus May 15 at 16:11
  • Double-check the outfc to ensure it is not accidentally passing the old path. – smiller May 15 at 16:17
  • ... outCS = arcpy.SpatialReference(3857) ... print ('outfc printed after call to SpatialReference' + outfc) returns this: outfc printed after call to SpatialReference G:\W135thStCopy\data_WebMerc.gdb\Street_Light_Pedestrial ERROR 000208: Error creating output feature class Failed to execute (Project). So it starts at the 5th-to-last feature class. There are 24 feature classes in the gdb. It does not print after ... arcpy.Project_management(infc, outfc, outCS) so that's where the error occurs. – Regulus May 15 at 16:35
  • What is the name of the FC at which it fails? – smiller May 15 at 16:45
  • Street_Light_Pedestrial. It is the 19th FC in the GDB and its name is the only one that is printed by the diagnostic print. – Regulus May 15 at 16:51
0

You'de better use EPSG codes in this context to be less prone to errors.

Try with outCS = arcpy.SpatialReference(3857) and you should be good to go.

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.