1

I want to create a script that iterates through feature classes with different projections in a geodatabase and exports them into a new geodatabase, all with the same projections.

What I want is to create a script in toolbox with four display names:

  1. Input Geodatabase:
  2. Output Location:
  3. Convert to Coord System:
  4. Transformation Method:

This script will allow the user to choose different coordinate systems. I have been able to piece together the code and this is what I have:

# Import modules
import arcpy
import os

# Set environment settings
arcpy.env.workspace = r"F:\abc...il.gdb"

# Set local variables.  I want to take whats in arcpy.env.workspace and put it into a    
# geodatabse with new coordinate systems
outWorkspace = r"F:\abc...ished.gdb"

# outCS will be the old coordinate system.  

Problem 1: ArcMap 10.1 and above no longer # have a Coordinate System folder. # So coordinates have to be saved in Favorites. Therefore, coordinate systems # have to be sourced from a favorite folder where # coords have been manually added as favorites in the Shapefile Properties window.

outCS = r"C:\Users\User\AppData\Roaming\ESRI\Desktop10.1\ArcMap\Coordinate Systems\NAD 1983 HARN StatePlane Hawaii 3 FIPS 5103 (US Feet)"

# Use ListFeatureClasses to generate a list of shapefiles

fcList = arcpy.ListFeatureClasses()

# Copy shapefiles to a file geodatabase

for infc in fcList:
    arcpy.CopyFeatures_management(infc, r"F:\abc...ished.gdb" + os.sep + fc.strip(".shp"))

# Determine if the input has a defined coordinate system, can't project if it does 
# not,  Problem 2: What I want is to create a script 
# in toolbox with four display names: 1) Input Geodatabase: 2) Output Location:, 
# 3) Convert to Coord System: , 4) Transformation Method: . 
# I have to use Search Cursor I think to first find the coordinate system.

    desc = arcpy.Describe(arcpy.env.workspace)
    rows = arcpy.da.SearchCursor(arcpy.env.workspace, ["SHAPE@"], 
                             spatial_reference = desc.spatialReference)

# Now, the problem is I want the user to be able to choose the coord system, 
# so I can put it in the outFeatureClass below instead of typing "WGS 1984 UTM Zone 4N".

    if desc.spatialReference.Name == "Unknown":
        print('Undefined coordinate system: ' + infc)

    elif:
        # Determine the new output feature class path and name
        outFeatureClass = os.path.join(outWorkspace, infc.strip(".shp") + "WGS 1984 UTM Zone 4N")

        # Set transformation. Problem 4:  I want is to allow the user to choose this 
        # from the script this not have to type it as code.
        transform_method = "NAD_1983_To_WGS_1984_1"

        # New coord system
        arcpy.Project_management(infc, outFeatureClass, outCS, transform_method)

I am new at programming.

This is what I have :

# Import modules
import arcpy
import os

# Set environment settings
arcpy.env.workspace = r"F:\...Rail.gdb"

# Set local variables.  I want to take whats in arcpy.env.workspace and put it into a geodatabse
# with new coordinate systems

inputWorkspace = arcpy.GetParameterAsText(0)
outputName     = arcpy.GetParameterAsText(1)
outWorkspace = r"F:\...\Finished.gdb"

# n is index number for "Convert to Coord System" parameter
outCS = arcpy.GetParameterAsText(n) 

cs_dict = {
    "NAD 1983":r"path/to/NAD83/prj/file",
    "WGS 1984":r"path/to/WGS84/prj/file"
          }
outCSprj = cs_dict[outCSchoice]

# Use ListFeatureClasses to generate a list of shapefiles
fcList = arcpy.ListFeatureClasses()

# Copy shapefiles to a file geodatabase
for infc in fcList:
    arcpy.CopyFeatures_management(infc, r"F:\...Final_output.gdb" + os.sep + fc.strip(".shp"))

# Describe a feature class
    fc = "D:/St_Johns/data.gdb/roads"
    desc = arcpy.Describe(fc)

# Get the spatial reference 
    sr = desc.spatialReference

# Check if the feature class is in projected space
    if sr.type == "Projected":
        arcpy.Copy_management(fc,"D:/St_Johns/data.gdb/roads_UTM")

    elif:
        # Determine the new output feature class path and name
        outFeatureClass = os.path.join(outWorkspace, infc.strip(".shp") + "WGS 1984 UTM Zone 4N")

        # New coord system
        arcpy.Project_management(infc, outFeatureClass, outCS, transform_method)
  • 1
    Try adding a feature dataset to the output FGDB. Then simply import all of the FC's to the new feature dataset. – Aaron Apr 3 '15 at 11:17
2

Well, your questions seem to mostly be about user input. I would recommend using a dropdown menu in the tool dialog with coded values, and then a dictionary in the script itself that links each coded value with the full text string that you need.

For example, in your dropdown for "Convert to Coord System" list "NAD 1983" and "WGS 1984". Set this in the tool properties either in the parameter tab or by using the ToolValidator class.

In the script, use something like this:

outCSchoice = arcpy.GetParameterAsText(n) #n is index number for "Convert to Coord System" parameter

cs_dict = {
    "NAD 1983":r"path/to/NAD83/prj/file",
    "WGS 1984":r"path/to/WGS84/prj/file"
          }

outCSprj = cs_dict[outCSchoice]

A similar strategy could be used for the transformations.

Regarding the part about getting the spatial reference, don't use a Search Cursor, check this out. In fact, instead of having the user use a dropdown menu like I described above, you could have the parameter be a dataset or feature class. Then the user would just find a feature class/shapefile that is in the coordinate system they want everything converted to, and in the script you would just pull the spatial reference from that dataset using the desribe method, and then feed that spatial reference object into the Project operation.

2

@mr.adam covers a lot. One more suggestion; while coordinate systems are no longer in folders, they don't need to be actual files now.

See:

Geographic coordinate systems

Projected coordinate systems

You can then create your spatial reference object as such:

For NAD 83 UTM Zone 10:

By name:

SRbyName = arcpy.SpatialReference ("NAD 1983 UTM Zone 10N") #Replace underscores in name from pdf with spaces

By WKID:

SRbyWKID = arcpy.SpatialReference (26910)

both will get you the same result. No files needed.

To create a drop-down menu from a script added to a toolbox:

While adding (or changing the tool's properties), in the Parameters tab, set your input data Data Type to String and change the Filter to Value List:

enter image description here

Then add the legal values as you desire:

enter image description here

  • oh, that's very nice with the creating spatial references from EPSG codes, I didn't know you could do that. – mr.adam Apr 3 '15 at 18:42

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.