Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

SpatialReference in Loop

$
0
0
Hey all,

I am trying to set the projection of a temp shapefile so I can add a length field. I am using the factory code to set it. I have a huge list of shapefiles in (GCS) and their corresponding ideal factory codes for State Plane CS. I am trying to loop through each and set the projection, but it only works the first time. The second time I get this error:

Traceback (most recent call last):
File "C:\Users\egreen\Desktop\BigFiles KEEP\CreatePROX_FINAL.py", line 67, in <module>
sr = arcpy.SpatialReference(ProjCode)
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 927, in __init__
self._arc_object.createFromFile(item)
RuntimeError: ERROR 999999: Error executing function.
the input is not a geographic or projected coordinate system

The error is misleading since it is not a bad factory code (or WKID). The second shapefile has the same factory code. I think the error is due to the variable sr already existing. I'm curious if I need to destroy sr somehow (I don't see a method for that though). Or am I misunderstanding this.

Here's my code:

Code:

import arcpy
import xml.etree.cElementTree as ET
import os
import xlrd
from arcpy import env
import os.path
import shutil
import zipfile
import glob

#set paths to documents
myPath = os.getcwd() + "\\ScriptFiles"
excel_path = myPath + "\\Inputs.xls"
wb = xlrd.open_workbook(excel_path)

# Load XLRD Excel Reader
sheetname = wb.sheet_names() #Read for XCL Sheet names
sh1 = wb.sheet_by_index(0) #Login

#flag for first row
Skip = True

### Set Geoprocessing environments
arcpy.env.scratchWorkspace = myPath + "\\Scratch.gdb"
arcpy.env.workspace = myPath + "\\PROXCreate.gdb"

Coordinate_System = "GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]" # provide a default value if unspecified
# Set overwrite option
arcpy.env.overwriteOutput = True

tmpRoads = "tmpRoads"
tmpBoundaries = "tmpBoundaries"

#Loop through Excel
for rownum in range(sh1.nrows):

    rows = sh1.row_values(rownum)
   
    #skip first row
    if Skip == True:
        Skip=False
    else:
        CountyName = rows[0]
        StateName = rows[2]
        InputRoadShpfl = rows[10]
        InputBoundaryShpfl = rows[11]
        ProjName = rows[9]
        ProjCode = rows[12]

        if arcpy.Exists(tmpRoads):
            arcpy.Delete_management(tmpRoads)
           
        if arcpy.Exists(tmpBoundaries):
            arcpy.Delete_management(tmpBoundaries)

        if (arcpy.Exists(myPath + "\\TigerFiles\\" + InputRoadShpfl)) and (myPath + "\\TigerFiles\\" + InputBoundaryShpfl):

            print "Executing shapefiles:" + InputBoundaryShpfl + " & " + InputRoadShpfl + "(" + StateName + "\\" + CountyName + ")"
         
            # Make a layer from the feature class
            #arcpy.MakeFeatureLayer_management(myPath + "\\TigerFiles\\" + InputRoadShpfl, tmpRoads)
            arcpy.CopyFeatures_management(myPath + "\\TigerFiles\\" + InputRoadShpfl, tmpRoads)
            arcpy.MakeFeatureLayer_management(myPath + "\\TigerFiles\\" + InputBoundaryShpfl, tmpBoundaries)

            # Process: Define Projection (3)
            #sr = arcpy.SpatialReference(102629)
            sr = arcpy.SpatialReference(ProjCode)
            #sr = arcpy.SpatialReference(ProjName)
            #sr.factoryCode = ProjCode
            sr.create()
            arcpy.DefineProjection_management(tmpRoads, sr)


Viewing all articles
Browse latest Browse all 2485

Trending Articles