Move SHP to gdb and ArcView license (newbie frustration)!!!

3151
54
04-11-2012 07:58 AM
LucaMoiana
New Contributor
Hi there,
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.

My frustration is that i can???t even move my shp files to a created gdb, is that because of my ArcView license?

PLEASE PLEASE PLEASE HELP

# Import system modules
import arcpy
from arcpy import env
import os
 
# Set environment settings
env.workspace = "C:/Users/a391665/Documents/AAAAPERSONALE/rugareto"

# Execute CreateFileGDB
arcpy.CreateFileGDB_management("C:/park", "test4")
 
# Set local variables
outWorkspace = "c:/park/test4.gdb"


# Use ListFeatureClasses to generate a list of shapefiles in the
#  workspace shown above.
fcList = arcpy.ListFeatureClasses()
 
# Execute CopyFeatures for each input shapefile
for shapefile in fcList:
    # Determine the new output feature class path and name
    outFeatureClass = os.path.join(outWorkspace, shapefile.strip(".shp"))
    arcpy.CopyFeatures_management(shapefile, outFeatureClass)
Tags (2)
0 Kudos
54 Replies
MichaelVolz
Esteemed Contributor
Luca:

You get the error because you had already copied a feature class with that name from a file geodatabase in the directory that you were looping through.  If you only want to process shapefiles into the new file geodatabase, then you need to filter out data based on its source (only process shapefiles).  I hope this  helps.
0 Kudos
LucaMoiana
New Contributor
Ok, I can understand that but, why if I change the code as below, I get the resulting gdb without error?

BTW thank to you and the all forum thing I am learning A LOT

def fcs_in_workspace(workspace):
    arcpy.env.workspace = workspace
    for fc in arcpy.ListFeatureClasses():
        print os.path.join(workspace, fc)
        arcpy.FeatureClassToGeodatabase_conversion(fc, "c:\\park\\test4.gdb") 
        #outFeatureClass = os.path.join(outWorkspace, fc.strip(".shp"))
        #arcpy.CopyFeatures_management(fc, outWorkspace + "\\" + fc.strip(".shp"))     
        print os.path.join(workspace, fc, "TO GDB")


That's embarassing, I had some Python training (via webcourse) but apparently ain't enough...
This is my side project, to get new skills.
I did as suggested and it works fine for a while, then I get the following error (SHP are the ones I used to test the precious scripts):
0 Kudos
MichaelVolz
Esteemed Contributor
Luca:

I believe the ESRI documentation for FeatureClassToGeodatabase_conversion answers this question

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001200000021000000

�?�The name of the output feature classes will be based on the name of the input feature class name. For example, if the input is C:\base\streams.shp, the output feature class will be named streams.

�?�If the name already exists in the output geodatabase, a number will be appended to the end to make it unique, for example, "_1".

Using FeatureClassToGeodatabase_conversion works properly because when it hits an input feature class with the same name it appends a _1 to the name of the second instance and so on _2 for the third instance instead of throwing an error.
0 Kudos
LucaMoiana
New Contributor
Great, then I'll use that tool so I won't loose any data.
Thank you for your precious help.
Now I am gonna move to the next steps:
-deal with names starting with numbers;
-check shp projection.

Any reading suggestions??

have a great friday!

Luca:

I believe the ESRI documentation for FeatureClassToGeodatabase_conversion answers this question

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001200000021000000

�?�The name of the output feature classes will be based on the name of the input feature class name. For example, if the input is C:\base\streams.shp, the output feature class will be named streams.

�?�If the name already exists in the output geodatabase, a number will be appended to the end to make it unique, for example, "_1".

Using FeatureClassToGeodatabase_conversion works properly because when it hits an input feature class with the same name it appends a _1 to the name of the second instance and so on _2 for the third instance instead of throwing an error.
0 Kudos
LucaMoiana
New Contributor
OK, now I'm working on projection.
Following is my last code, it works fine as long as all the features are already in the indicated prj; but I get this error

Runtime error <class 'arcgisscripting.ExecuteError'>: Undefined geographic transformation.


when the projection is different. Do I have to specify the transformation method?
if so, how do I deal with transformation?

Thanks from the new python junkie

# Import system modules
import arcpy
from arcpy import env
import os
import traceback

    
# Set environment settings
env.workspace = "C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto\\GIS"
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")
            else:
                print os.path.join(workspace, fc, "DIFFERENT PRJ!")
                # Determine the new output feature class path and name
                #outfc = os.path.join(workspace, fc)
                # Set output coordinate system
                #prjFile = "C:\\Program Files\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Projected Coordinate Systems\\UTM\\WGS 1984\\Northern Hemisphere\\WGS 1984 UTM Zone 32N.prj"
                print "PRJ CREATED!"
                # Create a spatial reference object using a projection file
                outCS = arcpy.SpatialReference("C:\\Program Files\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Projected Coordinate Systems\\UTM\\WGS 1984\\Northern Hemisphere\\WGS 1984 UTM Zone 32N.prj")
                print "outCS CREATED!"
                arcpy.Project_management(fc, "c:\\park\\test4.gdb", 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")
0 Kudos
MichaelVolz
Esteemed Contributor
Luca:

I would first check to see if the prj file is correct.  Take one of the feature classes in your file geodatabase and set it to the indicated projection within ArcMap or ArcCatalog.  If this fails, it might indicate an issue with your prj file.

Also, are these feature classes already in a projection.  If so, maybe you do not need to use the arcpy.SpatialReference method, but go directly to arcpy.Project_management.  I have a python script where I reproject multiple feature classes in a loop which already have a projection and I just use the arcpy.Project_management method to accomplish this task.
0 Kudos
LucaMoiana
New Contributor
Luca:

I would first check to see if the prj file is correct.  Take one of the feature classes in your file geodatabase and set it to the indicated projection within ArcMap or ArcCatalog.  If this fails, it might indicate an issue with your prj file.

Also, are these feature classes already in a projection.  If so, maybe you do not need to use the arcpy.SpatialReference method, but go directly to arcpy.Project_management.  I have a python script where I reproject multiple feature classes in a loop which already have a projection and I just use the arcpy.Project_management method to accomplish this task.


Hi!
Sorry for the late reply but I am out of office.
The prj file is correct and I can correctly use it in the toolbox project tool, I tried to convert the very same file and it worked just fine.
The shp files already have projection, but I do need to set a Spatial reference, as in the help script example:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000007m000000
0 Kudos
MichaelVolz
Esteemed Contributor
Luca:

I just looked at the script that I use to reproject feature classes and I pass a string representation of the out_coor_system, as opposed to the .prj file that you are using.

Maybe you can get the string representation of the out_coor_system instead of the .prj file and see if that works.  My out_coor_system is different from yours so it would not help you.

If you say using the .prj file in an out-of-the-box tool worked, but the python script did not work then you might want to log an incident with ESRI Technical support as I would think both instances are running the same behind the scenes code.

Also in my script, I need to add the appropriate toolboxes in order for the python script to run as below:
# Load required toolboxes...
arcpy.AddToolbox("C:/Program Files (x86)/ArcGIS/Desktop10.0/ArcToolbox/Toolboxes/Data Management Tools.tbx")

Your path to the toolbox might be slightly different depending upon your operating system.

I hope this info helps.
0 Kudos
LucaMoiana
New Contributor
Really getting nervous on this...
I loaded

# Load required toolboxes...
arcpy.AddToolbox("C:/Program Files (x86)/ArcGIS/Desktop10.0/ArcToolbox/Toolboxes/Data Management Tools.tbx")


But nothing changes.

Following is my new code, the scripts stops at
# Create a spatial reference object using a projection file
                outCS = arcpy.SpatialReference('WGS 1984 UTM Zone 32N')

And the error is:

Runtime error <type 'exceptions.RuntimeError'>: ERROR 999999: Error executing function.


# 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")
            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
                #prjFile = "C:\\Program Files\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Projected Coordinate Systems\\UTM\\WGS 1984\\Northern Hemisphere\\WGS 1984 UTM Zone 32N.prj"
                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")
0 Kudos
MathewCoyle
Frequent Contributor
You need to reference a projection file, not a projection name.
0 Kudos