1

The purpose of my code is to create a loop that copies shapefiles from a folder to my folder and change its projection 'GCS_WGS_1984'. Each shapefile starts with the year follow by the month and '_rfe.shp' (ex. 200701_rfe.2007) which is why I made the loop the way it is. When the first stage of the loop starts I get a error saying that in my arcpy.DefineProjection_management function, my projection is not valid.

How can I get my projection to be 'GCS_WGS_1984'?

        Traceback (most recent call last):
  File "C:\Users\Elvis\Desktop\Spring\Geog376\lab05\lab05.py", line 32, in <module>
    arcpy.DefineProjection_management(shp, projection)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 8184, in DefineProjection
    raise e
ExecuteError: ERROR 000622: Failed to execute (Define Projection). Parameters are not valid.
ERROR 000628: Cannot set input into parameter coor_system.

    #import modules
    import arcpy
    import os


     # set workspace(This is where all your processing will be done)
     #arcpy.env.workspace = 'S://376/spring14/eherrer2/'

arcpy.env.workspace = 'C://workspace/'

     workspace = 'C://workspace/'
     otherFolder = 'C://folder/'

    backup = []    
    for shapefiles in range(1,13):
        arcpy.CopyFeatures_management(outPut+'2007'+str(shapefiles).zfill(2)+'_rfe.shp',inPut+'2007'+str(shapefiles).zfill(2)+'_rfe.shp')
        shp = '2007'+str(shapefiles).zfill(2)+'_rfe.shp'
        describeShp = arcpy.Describe(shp)
        shpSR = describeShp.SpatialReference

        projection = 'GCS_WGS_1984'
        arcpy.DefineProjection_management(shp, projection)
        print 'GCS {0}'.format (shpSR.GCSname)
4

Try replacing 'GCS_WGS_1984' with the factory code, which is a bombproof method of assigning a spatial reference. This is how you would put it together:

    ...
    for shapefiles in range(1,13):
        arcpy.CopyFeatures_management(outPut+'2007'+str(shapefiles).zfill(2)+'_rfe.shp',inPut+'2007'+str(shapefiles).zfill(2)+'_rfe.shp')
        shp = '2007'+str(shapefiles).zfill(2)+'_rfe.shp'
        describeShp = arcpy.Describe(shp)
        shpSR = describeShp.SpatialReference

        projection = arcpy.SpatialReference(4326)
        arcpy.DefineProjection_management(shp, projection)
        print 'GCS {0}'.format (shpSR.GCSname)

Also, you should properly format your workspace:

arcpy.env.workspace = 'C:/path/to/your/ws'
  • 2
    Thank you Aaron. I messed around with your code and found out that you can just put the factory code in the Define Projection function. arcpy.DefineProjection_management(shp, 4326) – Trey Mar 12 '14 at 13:44
  • how did you find out 'GCS_WGS_1984' has the SpatialRefernce code of 4326? I tried using the describe and spatial reference objects on a feature but it keeps giving me back just the objects name... – ziggy Mar 25 '16 at 16:10
3

You haven't defined a workspace. So your script doesn't know where to find the shp variable. Add a workspace or set the full path to your shp variable.

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.