Programmatically get the data source from a lyr file pointing to an Esri GRID format raster uing Python 2.7 and ArcMap 10.5

977
7
Jump to solution
11-16-2018 12:50 AM
MatthewHowe
New Contributor III

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.

  1. 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?

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

keep digging

Layer properties—Help | ArcGIS Desktop 

layer
(Read Only)

The Describe object of the layer within a .lyr file.

View solution in original post

7 Replies
DanPatterson_Retired
MVP Esteemed Contributor
MatthewHowe
New Contributor III

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.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

keep digging

Layer properties—Help | ArcGIS Desktop 

layer
(Read Only)

The Describe object of the layer within a .lyr file.

MatthewHowe
New Contributor III

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!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

good... things are more or less the same in ArcGIS Pro's arcpy

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Please mark a response as Correct if it answers your question.

0 Kudos
MatthewHowe
New Contributor III

Man alive, give me chance!

0 Kudos