Select to view content in your preferred language

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

12318
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
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
The problem was with the coordinate system.  Replace:

spRef = r("GCS_WGS_1984")


With:

spRef = arcpy.SpatialReference("WGS 1984")


Also, a good suggestion is to add print statements throughout the code to pinpoint where the code is failing.

View solution in original post

0 Kudos
12 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Ben,

When specifying absolute paths, be sure to specify an 'r' before hand.  Ex:

env.workspace = r"C:\Users\mbs7038\Documents\ArcGIS\matDEM"


However, once you have the env.workspace set, you can read/write data from this workspace without specifying the entire path.  Ex:

arcpy.CopyFeatures_management(saved_Layer, "ElevPts.shp")


Try the following:

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
    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(saved_Layer, "ElevPts.shp")
    #===============================================================================


except:
# If an error occurred print the message to the screen
    print arcpy.GetMessages()
0 Kudos
BenSciance
Deactivated User
Hi Jake Thank you for the input. 

I included the full filepath for variables in the original code just to double check everything was in order, I know it makes the code messy and unorganized, just a temporary thing while working this issue out. 

I made the changes you suggested, specifically, adding the "r" in front of the filepath for defining the env.workspace. 

The script completed, yet no new layer files and shapefiles were added to the workspace.

Any other possible workarounds or ideas as to why this is occurring?    

Thank you

Ben
0 Kudos
JakeSkinner
Esri Esteemed Contributor
As a test, try setting your env.workspace to another directory, i.e. r"C:\temp".
0 Kudos
BenSciance
Deactivated User
Hi Jake,  I changed the workspace to the temp folder, still no luck.  I guarantee the code is missing something very simple, as is the usual case from my experience.  I can give it a go in ArcMap, just will take longer than desired. 

If you have any other suggestions I'll give it a shot.

Thanks

Ben
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Can you upload a sample of your .txt file you are working with?
0 Kudos
BenSciance
Deactivated User
Sure.

Columns are as follows:  serial date number, date, Lat, Lon, and Z value.
0 Kudos
DanPatterson_Retired
MVP Emeritus
try saving it to a comma-separated values file (.csv)
0 Kudos
JakeSkinner
Esri Esteemed Contributor
The problem was with the coordinate system.  Replace:

spRef = r("GCS_WGS_1984")


With:

spRef = arcpy.SpatialReference("WGS 1984")


Also, a good suggestion is to add print statements throughout the code to pinpoint where the code is failing.
0 Kudos
BenSciance
Deactivated User
Still no luck.  Did it work when you ran it?
0 Kudos