Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

What I need to do is move all the files into an mxd to a newly created gdb, chechking prj, and update mxd reference.

I have ArcGIS 10 SP4, license ArcView

I studied a bit of Python and get familiar with basic, then I copied some written code and pasted all together, attached is the code.

With some help from ESRI forum I ended up with the following script where I get an error in the arpy.SpatialReference line.

Please help.

# Import system modules
import arcpy
from arcpy import env
import os
import traceback
# Load required toolboxes...
arcpy.AddToolbox("C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\ArcToolbox\\Toolboxes\\Data Management Tools.tbx")


# Set environment settings
env.workspace = "C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto\\GIS\\test"
print("env.workspace completed successfully")

#check GDB exist
if os.path.exists("c:\\park\\test4.gdb"):
    arcpy.Delete_management("c:\\park\\test4.gdb")

# Execute CreateFileGDB
arcpy.CreateFileGDB_management("C:\\park", "test4")
print("CreateFileGDB completed successfully")

# Set local variables
outWorkspace = "c:\\park\\test4.gdb"
print("outWorkspace completed successfully")

# Iterate
def fcs_in_workspace(workspace):
    arcpy.env.workspace = workspace
    for fc in arcpy.ListFeatureClasses():
            dsc = arcpy.Describe(fc)
            if dsc.spatialReference.name == "WGS_1984_UTM_Zone_32N":               
                print os.path.join(workspace, fc, "prj: WGS_1984_UTM_Zone_32N")
                arcpy.FeatureClassToGeodatabase_conversion(fc, "c:\\park\\test4.gdb")
                print os.path.join(workspace, fc, "importaed in GDB")
            elif dsc.spatialReference.name == "Unknown":
                print os.path.join(workspace, fc, "UNKNOWN")
                #HERE I'D LIKE TO IMPORT THE SHP IN GDB ADDING "_UNKN" TO THE FEATURE NAME                
            else:
                print os.path.join(workspace, fc, "DIFFERENT PRJ!")
                # Determine the new output feature class path and name
                outfc = os.path.join(outWorkspace, fc)
                # Set output coordinate system
                print "PRJ CREATED!"
                # Create a spatial reference object using a projection file
                outCS = arcpy.SpatialReference('WGS_1984_UTM_Zone_32N')
                print "outCS CREATED!"
                arcpy.Project_management(fc, outfc, outCS)
                print "PROJECTED!"
    for ws in arcpy.ListWorkspaces():
        fcs_in_workspace(os.path.join(workspace, ws))

fcs_in_workspace("C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto\\GIS\\test")
share|improve this question
1  
In regards to your problem in creating the spatial reference object (arcpy.SpatialReference), you need to input a projection file (e.g., r'c:\path\to\file\WGS 1984.prj'), not a string. –  Jason May 9 '12 at 16:58
    
Also, I'm not sure that when you call arcpy.Project_management that you should be using the aforementioned spatial reference object outCS. I think the tool wants you to point it to a file, not a spatial reference object. I could be wrong, though. –  Jason May 9 '12 at 17:02
add comment

1 Answer 1

up vote 5 down vote accepted

I re-wrote your script to add the bit where you convert your shapefiles with unknown spatial reference into feature classes in your geodatabase. Set the variable outCS to the path of the projection file you want. Try this out:

# Import system modules
import arcpy
import os
import traceback
# Load required toolboxes...
arcpy.AddToolbox(r'C:\Program Files (x86)\ArcGIS\Desktop10.0\ArcToolbox\Toolboxes\Data Management Tools.tbx')


# Set environment settings
ws = r'C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\GIS\test'
arcpy.env.workspace = ws
print("env.workspace completed successfully")

#check GDB exist
gdb_dir = r'c:\park'
gdb_nam = 'test4.gdb'
gdb_full_path = os.path.join(gdb_dir,gdb_nam)
if os.path.exists(gdb_full_path):
    arcpy.Delete_management(gdb_full_path)

# Execute CreateFileGDB
arcpy.CreateFileGDB_management(gdb_dir, gdb_nam)
print("CreateFileGDB completed successfully")

# Iterate
def fcs_in_workspace(workspace,outWorkspace):
    arcpy.env.workspace = workspace
    for fc in arcpy.ListFeatureClasses():
            dsc = arcpy.Describe(fc)
            if dsc.spatialReference.name == "WGS_1984_UTM_Zone_32N":
                print '%s %s %s' % (workspace, fc, "prj: WGS_1984_UTM_Zone_32N")
                arcpy.FeatureClassToGeodatabase_conversion(fc, outWorkspace)
                print '%s %s %s' % (workspace, fc, "imported in GDB")

            elif dsc.spatialReference.name == "Unknown":
                #HERE I'D LIKE TO IMPORT THE SHP IN GDB ADDING "_UNKN" TO THE FEATURE NAME
                print os.path.join(workspace, fc, "UNKNOWN")
                fc_unkn = fc+'_unkn'
                arcpy.FeatureClassToGeodatabase_conversion(fc_unkn, outWorkspace)
                print '%s %s %s' % (workspace, fc_unkn, "imported in GDB")

            else:
                print os.path.join(workspace, fc, "DIFFERENT PRJ!")
                # Determine the new output feature class path and name
                outfc = os.path.join(outWorkspace, fc)
                # Set output coordinate system
                print "PRJ CREATED!"
                # Create a spatial reference object using a projection file
                outCS = r'C:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Projected Coordinate Systems\UTM\WGS 1984\Northern Hemisphere\WGS_1984_UTM_Zone_32N.prj'
                arcpy.Project_management(fc, outfc, outCS)
                print "PROJECTED!"
##     for ws in arcpy.ListWorkspaces():
##         fcs_in_workspace(os.path.join(workspace, ws))

fcs_in_workspace(ws,gdb_full_path)
share|improve this answer
    
Hi guys! Thanks a lot for your help. I run the new code but I get this error message after print ("PRJ CREATED!"): Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000622: Failed to execute (Project). Parameters are not valid. ERROR 000628: Cannot set input into parameter out_coor_system. –  Luca Moiana May 16 '12 at 7:45
    
@LucaMoiana, If the featureclass you want to project does not have the same underlying coordinate system as the projection you want to put it in (in this case, WGS 1984) you need to specify a transformation method. See this: help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… –  Jason May 16 '12 at 15:02
    
Hi Guys, Sorry to be back here after a long time. I decide to change my focus a bit so I don't want to start from a folder but from a MXD. My problem, or I should say my first problem, is to identify the .SHP in the TOC... HOW DO i DO? –  Luca Moiana Jun 14 '12 at 15:38
    
Hi Luca, I don't have any experience using arcpy to access shapefiles in a map document, sorry... –  Jason Jun 14 '12 at 16:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.