1

I am trying to write a script to make a tool that will run in Arcmap 10.7.1.

I have a file with multiple different shapefiles with multiple different projections. Basically, it has to take the spatial reference of a specified shapefile and reproject it to all the others. When I run the script (hard-coding the 'arcpy.GetParameterAsText()' variables to test the functionality of the script), I get ERROR 000628: Cannot set input into parameter out_coor_system.. How can I get around that and make the script functional?

Here is the code I have:

#Import modules
import arcpy, os

#Set workspace directory
from arcpy import env

#Define workspace
inWorkspace = arcpy.GetParameterAsText(0)
env.workspace = inWorkspace
env.overwriteOutput = True

#Define local shape file to reproject to
targetFeature = arcpy.GetParameterAsText(1)

#Describe the input feature class
inFc = arcpy.Describe(targetFeature)

#Define the spatial reference of the input feature class
sRef = inFc.spatialReference.name

#List the feature classes in the workspace
fcList = arcpy.ListFeatureClasses()


#Reproject all the feature classes:
for fc in fcList:
    desc = arcpy.Describe(fc)
    if desc.spatialReference.name != sRef:
        print "Projection of " + str(fc) + " is " + desc.spatialReference.name + ", so re-defining projection now:"
        newFc = arcpy.Project_management(fc, desc.name + "_projected", sRef)
        arcpy.AddMessage(arcpy.GetMessages())
        newFc = arcpy.Describe(newFc)
        print str(fc) + " has been reprojected to " + str(newFc.spatialReference.name)

I'm trying to add the "_projected" to the original file name, to signify that it has been reprojected. Also, I'm using IDLE as my IDE.

1
  • 2
    Something off topic that may help is string.format() so the first line printed in your loop would look like 'Projection of {0} is {1}, so re-defining projection now:'.format(fc,desc.spatialReference.name) it's a little bit easier to read and you don't need to str(variable) to insert it in, python does the hard work for you; also with string.format() you can specify the number of decimal places in the output, reuse a single parameter or even use parameters out of order (like '{0} {2} {1}') read more docs.python.org/3.4/library/string.html Oct 25, 2019 at 3:06

2 Answers 2

4

In your arcpy.Project_management() you are passing the name of the spatial reference, not the spatial reference itself. Although the documentation for the out_coor_system parameter states

Valid values are a Spatial Reference object, a file with a .prj extension, or a string representation of a coordinate system.

I'm not sure if the name of a spatial reference system qualifies as "a string representation of a coordinate system".

So change this line:

sRef = inFc.spatialReference.name

to this:

sRef = inFc.spatialReference

This would also require you to change:

    if desc.spatialReference.name != sRef:

to this:

    if desc.spatialReference.name != sRef.name:
2
  • Thanks for the info, I made all of those changes. After running the new code, however, I am getting the ERROR 999999: Error executing function. Create output feature class failed, failed to execute(Project). Can you see something else that might cause that exception to be raised? Code is the same as my original entry, just with those changes.
    – ihb
    Oct 25, 2019 at 2:55
  • 3
    Instead of desc.name + "_projected", try fc + "_projected". I don't think the Describe() for a feature class includes a name property. (If your fc is a shapefile, you'll need to cater for the ".shp" in its name by removing it and then adding it at the end). Oct 25, 2019 at 3:14
0

I figured out that in order to get the original code to work, you have to change the order of the output feature class notation.

Instead of: newFc = arcpy.Project_management(fc, desc.name + "_projected", sRef)

You must use this instead: newFc = arcpy.Project_management(fc, "projected_" + fc, sRef)

It then runs.

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.