<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: AddLayer and .txt files in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296579#M22909</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 25 Apr 2014 00:28:49 GMT</pubDate>
    <dc:creator>curtvprice</dc:creator>
    <dc:date>2014-04-25T00:28:49Z</dc:date>
    <item>
      <title>AddLayer and .txt files</title>
      <link>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296577#M22907</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&amp;nbsp;&amp;nbsp; 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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But I've already run into a problem. Although I can add these txt files with the ArcMap Add data button,&amp;nbsp; I haven't been able to get AddLayer to add a txt file.&amp;nbsp; Is it possible? Or is some other method used?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What I tried in the python window was&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Apr 2014 22:41:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296577#M22907</guid>
      <dc:creator>PaulHuffman</dc:creator>
      <dc:date>2014-04-24T22:41:30Z</dc:date>
    </item>
    <item>
      <title>Re: AddLayer and .txt files</title>
      <link>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296578#M22908</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You actually don't need to add the file through the Python console.&amp;nbsp; Instead, you directly use the MakeXYEventLayer method.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Maybe try something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy&amp;nbsp; csvDir = r"Path\To\Your\csvDirectory" shpDir = r"Path\To\Your\shp\output\csvDirectory"&amp;nbsp; csvfiles = [os.path.join(csvDir, i) for i in os.listdir(csvDir) if i.endswith(".csv")]&amp;nbsp; for file in csvfiles: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeXYEventLayer_management(file, "lonFieldName", "latFieldName", "temp_outlayer",arcpy.SpatialReference(4326)) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management("temp_outlayer",os.path.join(shpDir,os.path.basename(file))) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("temp_outlayer")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;mxd = arcpy.mapping.MapDocument(r"CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "*")[0]&amp;nbsp; arcpy.env.workspace = shpDir shpList = arcpy.ListFeatureClasses()&amp;nbsp; for featureClass in shpList: &amp;nbsp;&amp;nbsp;&amp;nbsp; newLayer = arcpy.mapping.Layer(featureClass) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(df,newLayer) &amp;nbsp;&amp;nbsp;&amp;nbsp; del newLayer&amp;nbsp; del mxd del df&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Looking back I realized I made the csvfiles list using specifically the ".csv" extension.&amp;nbsp; This should work for the ".txt" extension too as long as your text files are appropriately comma delineated.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Apr 2014 23:19:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296578#M22908</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2014-04-24T23:19:29Z</dc:date>
    </item>
    <item>
      <title>Re: AddLayer and .txt files</title>
      <link>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296579#M22909</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Apr 2014 00:28:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296579#M22909</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-04-25T00:28:49Z</dc:date>
    </item>
    <item>
      <title>Re: AddLayer and .txt files</title>
      <link>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296580#M22910</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks, Matt,&amp;nbsp; MakeXYEvent is going to work, I think.&amp;nbsp; I just had to remember to import os at the start.&amp;nbsp; Don't know yet how MakeXYEvent is going to handle the first line which is column headers. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&amp;nbsp; A simple addition of file.replace(" ","") didn't fix this for me.&amp;nbsp; I suppose I'll also need to strip off ".txt" as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Added &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
file = file.replace(" ","")
&amp;nbsp;&amp;nbsp;&amp;nbsp; file = file.replace("-","")
&amp;nbsp;&amp;nbsp;&amp;nbsp; file = file.replace(".txt","")
&amp;nbsp;&amp;nbsp;&amp;nbsp; print file
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;to the loop,&amp;nbsp; 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.&amp;nbsp; Update: That bad file was just a notepad file I made to capture some text. Deleted that and I'm on my way again. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:13:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296580#M22910</guid>
      <dc:creator>PaulHuffman</dc:creator>
      <dc:date>2021-12-11T14:13:33Z</dc:date>
    </item>
    <item>
      <title>Re: AddLayer and .txt files</title>
      <link>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296581#M22911</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If there is a limit on the length of a shapefile name, I haven't ran into it yet.&amp;nbsp; There &lt;/SPAN&gt;&lt;STRONG&gt;IS&lt;/STRONG&gt;&lt;SPAN&gt;, however, a length limit to the field names of 10 characters.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sounds like you've figured out a lot of those hiccups you run into yourself.&amp;nbsp; Good catch on me forgetting to import the os module!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Apr 2014 18:12:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296581#M22911</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2014-04-25T18:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: AddLayer and .txt files</title>
      <link>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296582#M22912</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Finished!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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 &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeXYEventLayer_management(file, "LONG", "LAT", "temp_outlayer",arcpy.SpatialReference(4326))
&amp;nbsp;&amp;nbsp;&amp;nbsp; #remove spaces, -, and .txt in file name
&amp;nbsp;&amp;nbsp;&amp;nbsp; file = file.replace(" ","")
&amp;nbsp;&amp;nbsp;&amp;nbsp; file = file.replace("-","")
&amp;nbsp;&amp;nbsp;&amp;nbsp; file = file.replace(".txt","")
&amp;nbsp;&amp;nbsp;&amp;nbsp; print file
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management("temp_outlayer",os.path.join(shpDir,os.path.basename(file)))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("temp_outlayer")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is what I wrote to add them to the closed map project from a Python console, not the ArcMap python window.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; newLayer = arcpy.mapping.Layer(featureClass)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(df,newLayer)
&amp;nbsp;&amp;nbsp;&amp;nbsp; del newLayer
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
mxd.saveACopy(r"c:\avdata\FallChinookRedds\2013\fallchinook2013-2.mxd")
del mxd
del df
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:13:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addlayer-and-txt-files/m-p/296582#M22912</guid>
      <dc:creator>PaulHuffman</dc:creator>
      <dc:date>2021-12-11T14:13:36Z</dc:date>
    </item>
  </channel>
</rss>

