1

This is my first arcpy script attempting to use lists.

Input: 4 dif Excel files, each with a dif spatial reference

Output: 4 dif feature classes (in same geodatabase), each with original spatial reference converted to NAD 1983.

Issue: I can make 4 dif (but similar) scripts and have Python work for each input BUT is there a way to have Python go through my input list? The problem is that for the second step, arcpy.MakeXYEventLayer_management, I don't know how to rotate through the spatial references depending on which Excel file I am using. Trying to use user suggested idea of sub-lists.

Updated Pasted script from Notepad:

# Import arcpy module
import arcpy
from arcpy import env

# Set workspace
env.workspace = r"E:\GIS\1.gdb\\"

# Set the spatial reference
sr1 = arcpy.SpatialReference("WGS 1984")
sr2 = arcpy.SpatialReference("NAD 1927")
sr3 = arcpy.SpatialReference("NAD 1983")


# Set variables
textPath1 = r"E:\GIS\Excel_files_by_datum\\"                                            
textPath2 = r"E:\GIS\2015Q1\\"
textPath3 = r"E:\GIS\1.gdb\\"
input = [["WGS84.xlsx", sr1],["NAD27.xlsx",sr2],["NAD83.xlsx",sr3],["Long_0_or_blank.xlsx",sr3]]
out_Layer = "XY_Temp"
Layername = input[0:5]

#Run processes

for ss in input:
    try:
        print "Converting Excel file to table"
        # Convert Excel to table
        Table1 = arcpy.ExcelToTable_conversion(textPath1+ ss[0],textPath3 + Layername)

        print "Making XY event layer"
        # This is to take the xy values of the table and transform to points
        # Description: Creates an XY layer and exports it to a layer file
        XY_event = arcpy.MakeXYEventLayer_management(Table1,"LONGITUDE","LATITUDE",out_Layer, ss[0])

        print "Copying features"
        # Description: Creates geodatabase feature class from the xy layer
        FeatureCopy = arcpy.CopyFeatures_management(XY_event, Layername + "_temp")

        print "Making feature layer"
        # Description: Transforms xy event layer into feature layer so I can select from it
        FeatureLayer = arcpy.MakeFeatureLayer_management(FeatureCopy, Layername + "_temp2")

        print "Converting from original datum to NAD83"
        # Converts file from input datum to NAD83
        Transformed = arcpy.Project_management(FeatureLayer,textPath3 + Layername + "_to_NAD83",ss[2])

        print "Adding xy coordinates to feature layer table"
        # Adds new NAD83 xy coordinates to feature layer table
        arcpy.AddXY_management(Transformed)

        print "Deleting temp file"
        # Deletes temp FeatureCopy file
        arcpy.Delete_management(FeatureCopy)

        print "Deleting original table"
        # Deletes table from before the NAD 83 xy coordinates were added
        arcpy.Delete_management(Table1)

        print "Project complete"

    except:
        print arcpy.GetMessages()
  • Modified input list to include NAD27 file – WolverineTime May 28 '15 at 16:42
  • Updated code to include user suggested 'sub-list' idea – WolverineTime May 28 '15 at 17:32
3

Honestly, there's quite a bit wrong. I tried to do a quick cleanup, but admit I didn't test it:

# Import arcpy module
import arcpy
import os

# Set workspace
arcpy.env.workspace = "E:\\GIS\\1.gdb"

# Set the spatial reference
sr1 = arcpy.SpatialReference("WGS 1984")
sr2 = arcpy.SpatialReference("NAD 1927")
sr3 = arcpy.SpatialReference("NAD 1983")

# Set variables
textPath1 = "E:\\GIS\\Excel_files_by_datum"
textPath2 = "E:\\GIS\\2015Q1"
textPath3 = "E:\\GIS\\1.gdb"

input_data = [["WGS84.xlsx", sr1],["NAD27.xlsx", sr2],["NAD83.xlsx", sr3],["Long_0_or_blank.xlsx", sr3]]

#Run processes
for data in input_data:
    try:
        layer_name = data[0].split(".")[0]
        out_layer = str(layer_name) + "_XY_Temp"

        print "Converting Excel file to table"
        # Convert Excel to table
        Table1 = arcpy.ExcelToTable_conversion(os.path.join(textPath1, str(data[0])), os.path.join(textPath3, layer_name))

        print "Making XY event layer"
        # This is to take the xy values of the table and transform to points
        # Description: Creates an XY layer and exports it to a layer file
        XY_event = arcpy.MakeXYEventLayer_management(Table1, "LONGITUDE", "LATITUDE", out_layer, data[1])

        print "Copying features"
        # Description: Creates geodatabase feature class from the xy layer
        FeatureCopy = arcpy.CopyFeatures_management(XY_event, layer_name + "_temp")

        print "Making feature layer"
        # Description: Transforms xy event layer into feature layer so I can select from it
        FeatureLayer = arcpy.MakeFeatureLayer_management(FeatureCopy, layer_name + "_temp2")

        print "Converting from original datum to NAD83"
        # Converts file from input datum to NAD83
        Transformed = arcpy.Project_management(FeatureLayer, os.path.join(textPath3, layer_name + "_to_NAD83"), sr3)

        print "Adding xy coordinates to feature layer table"
        # Adds new NAD83 xy coordinates to feature layer table
        arcpy.AddXY_management(Transformed)

        print "Deleting temp file"
        # Deletes temp FeatureCopy file
        arcpy.Delete_management(FeatureCopy)

        print "Deleting original table"
        # Deletes table from before the NAD 83 xy coordinates were added
        arcpy.Delete_management(Table1)

        print "Project complete"

    except:
        arcpy.GetMessages()
  • Thank you! It gave me properly named outputs! But I must have done something wrong because when I went to add the feature classes to the map they were all in their original spatial references. Was arcpy.Project_management the wrong tool to use to transform to NAD83? – WolverineTime May 28 '15 at 18:25
  • 1
    @WolverineTime yeah, sorry. I had deleted my answer because I don't like posting untested code. I've updated the code to project to NAD83. I had misread at first. – ianbroad May 28 '15 at 18:27
  • 1
    thank you so much!!! It worked perfectly. I will have to study your code so I can get this right myself next time. Cheers – WolverineTime May 28 '15 at 18:38
2

I think The best way would be to embed your spatial references into your list as sub-lists, i.e.:

input = [["WGS84.xlsx",sr1],["NAD83.xlsx",sr2],["WGS84.xlsx",sr3],["Long_0_or_blank.xlsx",sr1]]

Then update your script to (note use of ss[]):

for ss in input:
    try:
        # ...
        Table1 = arcpy.ExcelToTable_conversion(textPath1 + ss[0], textPath3 + Layername)
        # ...
        Transformed = arcpy.Project_management(FeatureLayer, textPath3 + Layername + "_to_NAD83", ss[1])
  • 1
    If you copy/pasted @slibby 's code, remove the asterisks at the end of the line. – recurvata May 28 '15 at 16:47
  • 1
    @WolverineTime please update the code in your original post to reflect what you currently have. – ianbroad May 28 '15 at 17:09
  • 1
    The posted code looks fine as far as indents. Check that all indents line up properly in your actual code. Sometimes it can make a difference if you mix tabs and spaces, even though both can be 4 spaces, for example. – recurvata May 28 '15 at 17:12
  • 1
    you may want to update your except block, because it should be printing errors. In the Transform line, you just have [0], not ss[0], so I assuming there is a syntax error (you can't concatenate a textpath1 which is a string with [0] which is a list) or something of the sort.. – mr.adam May 28 '15 at 17:42
  • 1
    Ok, I had to look it up, and I think you got no messages because it was a python error, not an arcpy error. To improve the messages, I'd recommend taking the except block from this documentation: help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… Scroll down to the section called traceback and take that except code. You'll have to import the traceback and sys modules at the top of the script too, but this except block will be super helpful in any future scripts too. – mr.adam May 28 '15 at 18:34

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.