2

What I need to do is to move all the shapefiles in an mxd into a newly created gdb, checking 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.

# 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")
2
  • 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. Commented May 9, 2012 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. Commented May 9, 2012 at 17:02

2 Answers 2

5

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)
4
  • 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. Commented May 16, 2012 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#//… Commented May 16, 2012 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? Commented Jun 14, 2012 at 15:38
  • Hi Luca, I don't have any experience using arcpy to access shapefiles in a map document, sorry... Commented Jun 14, 2012 at 16:53
0

I think it may be possible to accomplish your aim of copying all shapefiles referenced as layers in an MXD into a file geodatabase in a single step using the Package Map (Data Management) tool:

Packages a map document and all referenced data sources to create a single compressed .mpk file.

The tool has I think been available since ArcGIS Desktop 10.0 and has a Convert data to file geodatabase option.

The *.mpk file will then contain a zipped up file geodatabase that can be unpacked by dragging it onto a map, or various other methods.

The tool can be used within a Python script:

arcpy.PackageMap_management(in_map, output_file, {convert_data}, {convert_arcsde_data}, {extent}, {apply_extent_to_arcsde}, {arcgisruntime}, {reference_all_data}, {version}, {additional_files}, {summary}, {tags})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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