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.

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()
share|improve this question
    
Modified input list to include NAD27 file –  WolverineTime 23 hours ago
    
Updated code to include user suggested 'sub-list' idea –  WolverineTime 22 hours ago

2 Answers 2

up vote 3 down vote accepted

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()
share|improve this answer
    
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 21 hours ago
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. –  ian 21 hours ago
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 21 hours ago

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])
share|improve this answer
    
Thank you @slibby! I'm trying that now. I received an invalid syntax error pointing to the right of the ]]** at the end of input. I have not done a sub-list before, so not familiar with that error. –  WolverineTime 23 hours ago
1  
If you copy/pasted @slibby 's code, remove the asterisks at the end of the line. –  recurvata 23 hours ago
1  
@WolverineTime please update the code in your original post to reflect what you currently have. –  ian 23 hours ago
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 22 hours ago
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 21 hours ago

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.