I'm trying to get the data source from a bunch of lyr files pointing to various raster formats. Using the lyr.dataSource method on a feature layer after creating a layer object I can get the data source for TIFs fine but Esri GRID format rasters are seen as being directories. My goal is to re-path the lyr files to the same raster that has been copied to another location using the lyr.replaceDatasource method.
import arcpy lyr = r”\\gis\test.lyr”
layer = arcpy.mapping.Layer(lyr)
print layer.dataSource
Runtime error Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\arcobjects\_base.py", line 78, in _get(attr_name, self.__class__.__name__))
NameError: The attribute 'dataSource' is not supported on this instance of Layer.
Is there a clever way to get the full data source for the lyr file?
Solved! Go to Solution.
keep digging
Layer properties—Help | ArcGIS Desktop
layer (Read Only) | The Describe object of the layer within a .lyr file. |
See
Describe object properties—Help | ArcGIS Desktop http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/layer-properties.htm
then
desc = arcpy.Describe("C:/Data/your_layer.lyr")
Layer properties—Help | ArcGIS Desktop
desc.dataElement.catalogPath
if I remember correctly
Thanks Dan. I just tested and it gives the path of the lyr file itself and not the path of the data it's pointing to.
keep digging
Layer properties—Help | ArcGIS Desktop
layer (Read Only) | The Describe object of the layer within a .lyr file. |
Thanks Dan. I think I've sorted.
lyr = r'\\gis\test.lyr'
flyr = arcpy.mapping.Layer(lyr)
for l in arcpy.mapping.ListLayers(flyr):
if l.supports("DATASOURCE"):
if l.isRasterLayer:
print l.dataSource
.
..returns the data source I need!
good... things are more or less the same in ArcGIS Pro's arcpy
Please mark a response as Correct if it answers your question.
Man alive, give me chance!