Make XY Event Layer for all files in directory

1025
2
Jump to solution
03-03-2021 05:31 PM
Pokatko
New Contributor

Hi there, 

I am trying to create xy event layer that display data from csv tables as points, set new projected system, copy features to shapefiles. I am new to Python, the script below is working BUT only for 1 file, the last one printed, and I don't know how to do that for all files in directory. Thank you.

import arcpy
import os

mydir = r"I:\gas\export\gps_test"
for filename in os.listdir(mydir):
    if filename.endswith(".csv") or filename.endswith(".xlsx"):
        print(os.path.join(mydir, filename))
else:
    continue

# Set environment settings
arcpy.env.workspace = mydir


try:
    # Set the local variables
    in_table = filename
    out_layer = "gps_" + filename
    saved_layer = mydir
    x_coords = "Easting"
    y_coords = "Northing"
    z_coords = "Elevation"

    # Set the spatial reference
    spRef = arcpy.SpatialReference("NZGD 1949 Hawkes Bay Circuit")


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

    # Print the total rows
    print(arcpy.GetCount_management(out_layer))

    # Save to a layer file
    arcpy.SaveToLayerFile_management(out_layer, saved_layer)

    # Copy features to shapefile
    arcpy.CopyFeatures_management(out_layer, out_layer.strip(".shp"))

except Exception as err:
    print(err.args[0])

 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - Esri Community

your try - except block isn't within the loop

import arcpy
import os

mydir = r"I:\gas\export\gps_test"
# Set environment settings
arcpy.env.workspace = mydir
for filename in os.listdir(mydir):
    if filename.endswith(".csv") or filename.endswith(".xlsx"):
        print(os.path.join(mydir, filename))
    else:
        continue
    # Set the local variables
    in_table = filename
    out_layer = "gps_" + filename
    saved_layer = mydir
    x_coords = "Easting"
    y_coords = "Northing"
    z_coords = "Elevation"
    # Set the spatial reference
    spRef = arcpy.SpatialReference("NZGD 1949 Hawkes Bay Circuit")
    # Make the XY event layer
    arcpy.MakeXYEventLayer_management(in_table, x_coords, y_coords, out_layer, spRef, z_coords)

    # Print the total rows
    print(arcpy.GetCount_management(out_layer))
    # Save to a layer file
    arcpy.SaveToLayerFile_management(out_layer, saved_layer)
    # Copy features to shapefile
    arcpy.CopyFeatures_management(out_layer, out_layer.strip(".shp"))

quick format... dumped the try-except block.  If it fails, you will know about it and you will get the same error message in any event


... sort of retired...

View solution in original post

2 Replies
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - Esri Community

your try - except block isn't within the loop

import arcpy
import os

mydir = r"I:\gas\export\gps_test"
# Set environment settings
arcpy.env.workspace = mydir
for filename in os.listdir(mydir):
    if filename.endswith(".csv") or filename.endswith(".xlsx"):
        print(os.path.join(mydir, filename))
    else:
        continue
    # Set the local variables
    in_table = filename
    out_layer = "gps_" + filename
    saved_layer = mydir
    x_coords = "Easting"
    y_coords = "Northing"
    z_coords = "Elevation"
    # Set the spatial reference
    spRef = arcpy.SpatialReference("NZGD 1949 Hawkes Bay Circuit")
    # Make the XY event layer
    arcpy.MakeXYEventLayer_management(in_table, x_coords, y_coords, out_layer, spRef, z_coords)

    # Print the total rows
    print(arcpy.GetCount_management(out_layer))
    # Save to a layer file
    arcpy.SaveToLayerFile_management(out_layer, saved_layer)
    # Copy features to shapefile
    arcpy.CopyFeatures_management(out_layer, out_layer.strip(".shp"))

quick format... dumped the try-except block.  If it fails, you will know about it and you will get the same error message in any event


... sort of retired...
Pokatko
New Contributor

Ooh I see! Thank you Dan. 

0 Kudos