AddLayer and .txt files

793
5
Jump to solution
04-24-2014 03:41 PM
PaulHuffman
Occasional Contributor III
I just got handed 30 comma delimited txt files from someone's old GPS unit and asked if I could put them on a map for their report due tomorrow.   Sure, I could plow through these files one at a time, import the txt files to ArcMap, display XY data, convert to a feature class, and make the deadline. But wouldn't it be more fun and elegant to loop through all the txt files with a python script? Might not save any time, but I might learn something and I'd prefer to miss the deadline to teach these people that their lack of planning does not make my emergency.

But I've already run into a problem. Although I can add these txt files with the ArcMap Add data button,  I haven't been able to get AddLayer to add a txt file.  Is it possible? Or is some other method used?

What I tried in the python window was
mxd = arcpy.mapping.MapDocument("CURRENT") addLayer = (r"C:\avdata\FallChinookRedds\2013\cent2don 10-14-13.txt") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] arcpy.mapping.AddLayer(df, addLayer)


but just got a bunch of red runtime error stuff, nothing added to the map document. Don't know if I have a syntax error for the python window or AddLayer is not going to work with the txt file.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MattEiben
Occasional Contributor
You actually don't need to add the file through the Python console.  Instead, you directly use the MakeXYEventLayer method.
Maybe try something like this:

import arcpy  csvDir = r"Path\To\Your\csvDirectory" shpDir = r"Path\To\Your\shp\output\csvDirectory"  csvfiles = [os.path.join(csvDir, i) for i in os.listdir(csvDir) if i.endswith(".csv")]  for file in csvfiles:     arcpy.MakeXYEventLayer_management(file, "lonFieldName", "latFieldName", "temp_outlayer",arcpy.SpatialReference(4326))     arcpy.CopyFeatures_management("temp_outlayer",os.path.join(shpDir,os.path.basename(file)))     arcpy.Delete_management("temp_outlayer")


If you ran that above script in the background and the feature classes weren't added to you Dataframe, you could do something like this afterwards to add them all to your Table of Contents:

mxd = arcpy.mapping.MapDocument(r"CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "*")[0]  arcpy.env.workspace = shpDir shpList = arcpy.ListFeatureClasses()  for featureClass in shpList:     newLayer = arcpy.mapping.Layer(featureClass)     arcpy.mapping.AddLayer(df,newLayer)     del newLayer  del mxd del df


Looking back I realized I made the csvfiles list using specifically the ".csv" extension.  This should work for the ".txt" extension too as long as your text files are appropriately comma delineated.

View solution in original post

0 Kudos
5 Replies
MattEiben
Occasional Contributor
You actually don't need to add the file through the Python console.  Instead, you directly use the MakeXYEventLayer method.
Maybe try something like this:

import arcpy  csvDir = r"Path\To\Your\csvDirectory" shpDir = r"Path\To\Your\shp\output\csvDirectory"  csvfiles = [os.path.join(csvDir, i) for i in os.listdir(csvDir) if i.endswith(".csv")]  for file in csvfiles:     arcpy.MakeXYEventLayer_management(file, "lonFieldName", "latFieldName", "temp_outlayer",arcpy.SpatialReference(4326))     arcpy.CopyFeatures_management("temp_outlayer",os.path.join(shpDir,os.path.basename(file)))     arcpy.Delete_management("temp_outlayer")


If you ran that above script in the background and the feature classes weren't added to you Dataframe, you could do something like this afterwards to add them all to your Table of Contents:

mxd = arcpy.mapping.MapDocument(r"CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "*")[0]  arcpy.env.workspace = shpDir shpList = arcpy.ListFeatureClasses()  for featureClass in shpList:     newLayer = arcpy.mapping.Layer(featureClass)     arcpy.mapping.AddLayer(df,newLayer)     del newLayer  del mxd del df


Looking back I realized I made the csvfiles list using specifically the ".csv" extension.  This should work for the ".txt" extension too as long as your text files are appropriately comma delineated.
0 Kudos
curtvprice
MVP Esteemed Contributor
One thing that can wrinkle this process is the data types ArcGIS guesses for your fields. For best results, you want the first row of data to have the lat long specified with a decimal point. Then the lat long fields will come in as type Double and not something else.
0 Kudos
PaulHuffman
Occasional Contributor III
Thanks, Matt,  MakeXYEvent is going to work, I think.  I just had to remember to import os at the start.  Don't know yet how MakeXYEvent is going to handle the first line which is column headers.

The script bombed when it hit the first txt file name that includes spaces, so it couldn't make a shapefile name with that character.  A simple addition of file.replace(" ","") didn't fix this for me.  I suppose I'll also need to strip off ".txt" as well.

Added
file = file.replace(" ","")
    file = file.replace("-","")
    file = file.replace(".txt","")
    print file

to the loop,  and it ran through the first 9 files, created shapefiles, then hit a problem in the tenth file. Says field LONG and LAT do not exist. Looking into this file now.  Update: That bad file was just a notepad file I made to capture some text. Deleted that and I'm on my way again.

I thought some of these file names were going to be too long as well, but I got shapefiles. Aren't we limited to 8 characters for a shapefile name?
0 Kudos
MattEiben
Occasional Contributor
If there is a limit on the length of a shapefile name, I haven't ran into it yet.  There IS, however, a length limit to the field names of 10 characters. 

Sounds like you've figured out a lot of those hiccups you run into yourself.  Good catch on me forgetting to import the os module!
0 Kudos
PaulHuffman
Occasional Contributor III
Finished!

For the first step, txt files are read in, filenames are striped of blanks, -, and ".txt, then copied to a shapefile in a for loop
import arcpy, os

csvDir = r"c:\avdata\FallChinookRedds\2013\moretextfiles"
shpDir = r"c:\avdata\FallChinookRedds\2013\Shapefiles"

csvfiles = [os.path.join(csvDir, i) for i in os.listdir(csvDir) if i.endswith(".txt")]

for file in csvfiles:
    arcpy.MakeXYEventLayer_management(file, "LONG", "LAT", "temp_outlayer",arcpy.SpatialReference(4326))
    #remove spaces, -, and .txt in file name
    file = file.replace(" ","")
    file = file.replace("-","")
    file = file.replace(".txt","")
    print file
    arcpy.CopyFeatures_management("temp_outlayer",os.path.join(shpDir,os.path.basename(file)))
    arcpy.Delete_management("temp_outlayer")


This is what I wrote to add them to the closed map project from a Python console, not the ArcMap python window.

import arcpy, os

mxd = arcpy.mapping.MapDocument(r"c:\avdata\FallChinookRedds\2013\fallchinook2013.mxd")
print mxd
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]

arcpy.env.workspace = r"c:\avdata\FallChinookRedds\2013\Shapefiles"
shpList = arcpy.ListFeatureClasses()
print shpList

for featureClass in shpList:
    newLayer = arcpy.mapping.Layer(featureClass)
    arcpy.mapping.AddLayer(df,newLayer)
    del newLayer
    
mxd.saveACopy(r"c:\avdata\FallChinookRedds\2013\fallchinook2013-2.mxd")
del mxd
del df


Eventually, I'll run these shapefiles through some other scripts that add and populate some fields, get the date out of the file name and into a field, drop some other fields, and append into a geodatabase.
0 Kudos