Lyr.replaceDataSource

1321
5
02-14-2018 04:00 AM
DanielTuck
New Contributor

Guys, I'm having problems with Lyr.replaceDataSource - it doesn’t seem to update as I’d hoped. See some testing below...

If it had worked we should have seen C:\Scratch\CarPositions\Import\239PM\temp\p1500_1510.txt on 2nd output line.

Any help appreciated. Cheers Dan

Richard Fairhurst‌?

import arcpy
... import os
... mxd = arcpy.mapping.MapDocument("CURRENT")
... df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
... lyr = arcpy.mapping.ListLayers(mxd, "*", df)[0]
... for file in os.listdir(r"C:\Scratch\CarPositions\Import\239PM\temp"):
... print file
... lyr.replaceDataSource(r"C:\Scratch\CarPositions\Import\239PM\temp", "TEXT_WORKSPACE", str(file))
... TextElement = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "MapTitleText")[0]
... TextElement.text = 'Paramics Vehicle Data: ' + str(file)[1:-11]+':'+str(file)[3:-9]+' - '+str(file)[6:-6]+':'+str(file)[8:-4]
... arcpy.RefreshActiveView()
... arcpy.RefreshTOC()
... print lyr.dataSource
...
p1500_1510.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1510_1520.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1520_1530.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1530_1540.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1540_1550.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1550_1600.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1600_1610.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1610_1620.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1620_1630.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1630_1640.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1640_1650.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1650_1700.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1700_1710.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1710_1720.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1720_1728.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1720_1730.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1730_1739.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1730_1740.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1740_1750.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
P1750_1800.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1800_1805.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1805_1810.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1810_1815.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1815_1820.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1820_1825.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1825_1830.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1830_1840.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1840_1850.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1850_1900.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1900_1910.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1910_1920.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1920_1930.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1930_1940.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1940_1950.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt
p1950_2000.txt
C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt

Tags (1)
0 Kudos
5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Dan... the code sample here

http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/listlayoutelements.htm

shows looking for the text element then replacing it with a new value... not sure that is what you are trying to do

0 Kudos
DanielTuck
New Contributor

Hi Dan, The text element part is working. What I'm trying to do is loop through a folder of CSV's and set the data source of the 1st layer in my TOC to each of the CSV files in \239PM\temp. There would then be an ExportToJPG line but I've left it out while I try and fix the main problem which is that this line...

lyr.replaceDataSource(r"C:\Scratch\CarPositions\Import\239PM\temp", "TEXT_WORKSPACE", str(file))

doesn't update from 

C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt  to  C:\Scratch\CarPositions\Import\239PM\temp\p1500_1510.txt

Dan,

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

not sure I follow since I don't do the mapping thing often

a = r"C:\Scratch\CarPositions\Import\239PM\proc1\p1600_1615.txt"
b = r"p1500_1510.txt"
c = "\\".join(a.split("\\")[:-1]) + "\\{}".format(b)

'C:\\Scratch\\CarPositions\\Import\\239PM\\proc1\\p1500_1510.txt'
0 Kudos
DanielTuck
New Contributor

Hi everyone, Just to clarify as I haven't said what the purpose of this script was for.

I have a folder of text files with OSGRs and I want to produce a plot of each one by setting the data source of the 1st layer in the TOC to each file. With some help from Dan Patterson yesterday I have this script below that creates a new layer for each text file. Not quite as I had planned but it does work.

... import os
... mxd = arcpy.mapping.MapDocument("CURRENT")
... df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
... inputSRS = arcpy.SpatialReference(27700) #BNG
... for file in os.listdir(r'C:\Scratch\CarPositions\Import\239PM\temp'):
...    print file
...    arcpy.MakeXYEventLayer_management(file, 'ADJ_X', 'ADJ_Y', str(file), inputSRS)
...    lyr = arcpy.mapping.ListLayers(mxd, "*", df)[0]
...    arcpy.ApplySymbologyFromLayer_management(file, "TEMPLATE")
...    TextElement = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "MapTitleText")[0]
...    TextElement.text = 'Paramics Vehicle Data: ' + str(file)[1:-11]+':'+str(file)[3:-9]+' - '+str(file)[6:-6]+':'+str(file)[8:-4]
...    arcpy.RefreshActiveView()
...    arcpy.RefreshTOC()
...    print lyr.dataSource
...    arcpy.mapping.ExportToJPEG(mxd, r"C:\TempXX\p" + str(file)[1:-4] + ".jpg", resolution=600)
...    lyr.visible=False

0 Kudos
PaulSmith8
New Contributor II

Hi Daniel,

I have had a look at your Original issue and I can confirm the same results. Simplifying this down to the most basic code and components possible. I created a simple X,Y,Comment text file with 1 row of data and added to a map as XY Event and saved out to layer file. I then duplicated the source text file and renamed. While the code ran successfully and the new source validated (I tested with and without Valid Flag and changed names etc.) it did not update the layer source. Appears to be a bug in replaceDataSource when datasource is a text file.

I'm using 10.5

import arcpy

lyr = arcpy.mapping.Layer(r"H:\Scratch\mylayer.lyr")
print lyr.dataSource
lyr.replaceDataSource(r"H:\Scratch\perm","TEXT_WORKSPACE","p1500_1510_temp.txt")
print lyr.dataSource‍‍‍‍‍‍

H:\Scratch\perm\p1500_1510_perm.txt
H:\Scratch\perm\p1500_1510_perm.txt   << should have been updated to H:\Scratch\perm\p1500_1510_temp.txt