Select to view content in your preferred language

Create XY Event Layer / Save to Layer file / Copy features to shapefile

13008
12
Jump to solution
12-11-2013 06:51 AM
BenSciance
Deactivated User
I am attempting this simple workflow. 

1) Adding an XY event layer of lat/lon points with z values from a text file. 
2) Save the event layer to a layer file.
3) Copy the saved layer file to a new shapefile or feature class in geodatabase.

Now, I know the event layer that is created when you use the "create XY features" tool is a temporary file.  I am having issues at the 2nd and 3rd step.  The script completes, BUT the new layer file and shapefile are nowhere to be found.  I have defined the
workspace path to my local machine but the files are not there after the script completes.  Any advice would be greatly
appreciated.

Here is my code :

# MakeXYLayer.py
# Description: Creates an XY Event layer and saves it to a layer file then copies
# the layer file to a shapefile

# import system modules
import arcpy
from arcpy import env

# Set environment settings
env.workspace = "C:\Users\mbs7038\Documents\ArcGIS\matDEM"

try:
        # Set the local variables
        in_Table = "LatLonElev.txt"
        x_coords = "Lon"
        y_coords = "Lat"
        z_coords = "Elev"
        out_Layer = "ElevPts_layer"
        saved_Layer = r"C:\Users\mbs7038\Documents\ArcGIS\matDEM\ElevPts.lyr"

        # Set the spatial reference
        spRef = r("GCS_WGS_1984")

        # Make the XY event layer
        arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, out_Layer, spRef, z_coords)

        # Save to a layer file
        arcpy.SaveToLayerFile_management(out_Layer, saved_Layer)

        #===============================================================================
        # Copy features to shapefile

        arcpy.CopyFeatures_management("C:\Users\mbs7038\Documents\ArcGIS\matDEM\ElevPts.lyr", "C:\Users\mbs7038\Document         s\ArcGIS\matDEM\ElevPts.shp")
        #===============================================================================


except:
    # If an error occurred print the message to the screen
    print arcpy.GetMessages()

print 'script complete!!!!!'
Tags (2)
0 Kudos
12 Replies
JakeSkinner
Esri Esteemed Contributor
Yes, this worked for me.  Add some print statements to your code so you can see where it is failing at.  Ex:

import arcpy
from arcpy import env

# Set environment settings
env.workspace = r"C:\Users\mbs7038\Documents\ArcGIS\matDEM"

try:
    # Set the local variables
    in_Table = "LatLonElev.txt"
    x_coords = "Lon"
    y_coords = "Lat"
    z_coords = "Elev"
    out_Layer = "ElevPts_layer"
    saved_Layer = "ElevPts.lyr"

    # Set the spatial reference
    print "Set spatial reference"
    spRef = arcpy.SpatialReference("WGS 1984")

    # Make the XY event layer
    print "Make XY event layer"
    arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, out_Layer, spRef, z_coords)

    # Save to a layer file
    print "Saving layer file"
    arcpy.SaveToLayerFile_management(out_Layer, saved_Layer)

    #===============================================================================
    # Copy features to shapefile
    print "Copying features to shapefile"
    arcpy.CopyFeatures_management(saved_Layer, "ElevPts.shp")
    #===============================================================================


except:
    print arcpy.GetMessages()
0 Kudos
BenSciance
Deactivated User
There seems to be a problem when defining the spatial reference as an input variable

spRef = arcpy.SpatialReference(4326)


AND

spRef = arcpy.SpatialReference("WGS 1984")



Since the "Make XY event layer" script has a direct input for the spatial reference, I wrote the code for WGS 1984 coordinate
system directly in the script (4326) instead. 

 arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, out_Layer, 4326, z_coords)



After doing this, the entire script ran perfectly.  The print statements kept the processing steps more organized as well.  Thanks so much for your input!



import arcpy
from arcpy import env

# Set environment settings
env.workspace = r"C:\Users\mbs7038\Documents\ArcGIS\floodtest"

try:
    # Set the local variables
    #INPUTS
    in_Table = "TEST.txt"
    x_coords = "Lon"
    y_coords = "Lat"
    z_coords = "wlevel"

    #OUTPUTS
    out_Layer = "TEST_Layer"
    #saved_Layer = "TEST1.lyr"
    fc=r"C:\Users\mbs7038\Documents\ArcGIS\floodtest\AWESOME.gdb\TEST1"

    #===========================================================================
    # Set the spatial reference----DID NOT WORK WHEN DEFINING spRef.
    # USE COORDINATE SYSTEM CODE FOR "WGS 84" (4326) IN THE SCRIPT FOR MAKE XY EVENT LAYER
    #print "Set spatial reference"
    #spRef = arcpy.SpatialReference(4326)
    #===========================================================================

    #===========================================================================
    # Make the XY event layer
    # MakeXYEventLayer_management (table, in_x_field, in_y_field, out_layer, {spatial_reference}, {in_z_field})
    print "Make XY event layer"
    arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, out_Layer, 4326, z_coords)
    #===========================================================================

    #===========================================================================
    # Save to a layer file----DO NOT NEED TO PROCESS THIS STEP.  CAN SKIP TO COPY FEATURES
    #print "Saving layer file"
    #arcpy.SaveToLayerFile_management(out_Layer, saved_Layer)
    #===========================================================================

    #===========================================================================
    # Copy features to feature class
    print "Copying features to feature class"
    arcpy.CopyFeatures_management(out_Layer,fc)
    #===========================================================================


except:
    print arcpy.GetMessages()
 
0 Kudos
JoshuaAhmed
Deactivated User

Hi all,

I have a similar script where I am going through a file and converting all .txt files into events layers, and finally into points files. The points are X and Y coords. I have tried changing all manner of things yet I still get a Runtime error: object: error in executing tool message. Any ideas where the script is going wrong. I know it's something in the MakeXYEventLayer_management but cannot figure out where... Any help will be welcomed.

-------------------------------------------------------------------

import arcpy, sys, os

from arcpy import env

from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

arcpy.env.overwriteOutput = True #Set the script to be able to overwrite files

Path_table = "J:/Annual_time_series/NDVI_images/Beni1/Centrelines/" #path

for root, dirs, files in os.walk(Path_table):

        for name in files:

            beni3_reclass = os.path.join(root,name) #name

            if beni3_reclass.endswith('_CL.txt') == True:

                tilename = name[0:-4] #changed to keep my filenames

                print tilename + " being processed."

                table = Path_table + name

                in_x_field = "cl_x"

                in_y_field = "cl_y"

                out_layer = Path_table + tilename + "_layer"

                #saved_layer = Path_table + tilename + ".lyr"

                #Convert txt file table to an events layer

                arcpy.MakeXYEventLayer_management (table, in_x_field, in_y_field, out_layer, {"J:/Annual_time_series/NDVI_images/coordinate_system/WGS 1984 UTM Zone 19N.prj"}, {})

                print arcpy.GetMessages()

               

                #save to featureclass - this makes it a permenant shape points file

                Path_points = "J:/Annual_time_series/NDVI_images/Beni1/Centrelines/CL_coords"

                in_features = out_layer

                out_featureclass = Path_points + tilename + "_CL_pts.shp"

                arcpy.CopyFeatures_management (in_features, out_feature_class, {}, {}, {}, {})

-------------------------------------------------------------------------

The error message is:

Traceback (most recent call last):

  File "E:/CLtxt_to_points.py", line 22, in <module>

    arcpy.MakeXYEventLayer_management (table, in_x_field, in_y_field, out_layer, {"J:/Annual_time_series/NDVI_images/coordinate_system/WGS 1984 UTM Zone 19N.prj"}, {})

  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 6348, in MakeXYEventLayer

    raise e

RuntimeError: Object: Error in executing tool

Thanks,

Josh

0 Kudos