Hi everyone,
Could someone share with me code that I could use to get the Data Type for a layer. In this example below, I would want returned "CAD Polyline Feature Class".
Thanks!
maybe skip Arc* altogether and just use windows file explorer
Thanks Joshua. I think that opens a whole new can of worms I don’t really want to get into ideally.
Thanks Dan. I’m going through the links you sent to see if there’s a way I can workaround with the describe object to get at what I’m looking for as it seems I can’t directly get at what I want using arcpy (according to Joshua).
The short answer is that you can't get that specific value from within ArcPy.
The Data Type value of the Data Source tab comes from IFeatureLayer.DataSourceType, i.e., it is a layer value/description and not a dataset value/description. Unfortunately, ArcPy does not expose that layer property through the Describe object.
If you are willing to install a Python com package, like GitHub - enthought/comtypes: A pure Python, lightweight COM client and server framework, based on the ctypes Python FFI … , then you can use ArcObjects to access the value.
The code below, when pasted in the interactive Python window, will enumerate all the feature layers in the MXD and print their names and layer-based DataSourceType.
<SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">import</SPAN> os <SPAN class="keyword token">from</SPAN> comtypes<SPAN class="punctuation token">.</SPAN>client <SPAN class="keyword token">import</SPAN> CreateObject<SPAN class="punctuation token">,</SPAN> GetModule mxd <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN> <SPAN class="comment token"># path to MXD file (if run outside ArcMap)</SPAN> <SPAN class="comment token"># Function for casting COM objects to interfaces</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">CType</SPAN><SPAN class="punctuation token">(</SPAN>obj<SPAN class="punctuation token">,</SPAN> interface<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"""Casts obj to interface and returns comtypes POINTER or None"""</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> newobj <SPAN class="operator token">=</SPAN> obj<SPAN class="punctuation token">.</SPAN>QueryInterface<SPAN class="punctuation token">(</SPAN>interface<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> newobj <SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> None <SPAN class="comment token"># Path to ArcGIS Desktop COM files</SPAN> comDirectory <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetInstallInfo<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'InstallDir'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'com'</SPAN> <SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># References to needed ArcGIS Desktop COM components</SPAN> esriSystem <SPAN class="operator token">=</SPAN> GetModule<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>comDirectory<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'esriSystem.olb'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> esriFramework <SPAN class="operator token">=</SPAN> GetModule<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>comDirectory<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'esriFramework.olb'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> esriArcMapUI <SPAN class="operator token">=</SPAN> GetModule<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>comDirectory<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'esriArcMapUI.olb'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> esriCarto <SPAN class="operator token">=</SPAN> GetModule<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>comDirectory<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'esriCarto.olb'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Create filter for layers that support IGeoFeatureLayer</SPAN> pUID <SPAN class="operator token">=</SPAN> CreateObject<SPAN class="punctuation token">(</SPAN>esriSystem<SPAN class="punctuation token">.</SPAN>UID<SPAN class="punctuation token">)</SPAN> pUID<SPAN class="punctuation token">.</SPAN>Value <SPAN class="operator token">=</SPAN> <SPAN class="string token">"{40A9E885-5533-11d0-98BE-00805F7CED21}"</SPAN> <SPAN class="comment token"># Section for running within ArcGIS Desktop application</SPAN> <SPAN class="comment token"># Confirm code is being run within ArcMap (Interactive, Addin, Toolbox)</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> pApp <SPAN class="operator token">=</SPAN> CreateObject<SPAN class="punctuation token">(</SPAN>esriFramework<SPAN class="punctuation token">.</SPAN>AppRef<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">assert</SPAN> pApp<SPAN class="punctuation token">.</SPAN>Name <SPAN class="operator token">==</SPAN> <SPAN class="string token">'ArcMap'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Code must be run within ArcMap'</SPAN> pMxDoc <SPAN class="operator token">=</SPAN> CType<SPAN class="punctuation token">(</SPAN>pApp<SPAN class="punctuation token">.</SPAN>Document<SPAN class="punctuation token">,</SPAN> esriArcMapUI<SPAN class="punctuation token">.</SPAN>IMxDocument<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">except</SPAN> WindowsError<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">assert</SPAN> <SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Code must be run within an ArcGIS Desktop application'</SPAN> <SPAN class="comment token">## Section for running outside of ArcGIS Desktop application</SPAN> <SPAN class="comment token">## Open MXD file</SPAN> <SPAN class="comment token">#try:</SPAN> <SPAN class="comment token"># pMapDoc = CreateObject(esriCarto.MapDocument, interface=esriCarto.IMapDocument)</SPAN> <SPAN class="comment token"># assert pMapDoc.IsMapDocument(mxd), 'Valid MXD required'</SPAN> <SPAN class="comment token"># pMapDoc.Open(mxd)</SPAN> <SPAN class="comment token"># pMap = pMapDoc.Map(0)</SPAN> <SPAN class="comment token">#except:</SPAN> <SPAN class="comment token"># assert False, 'Unable to open MXD and retrieve map'</SPAN> <SPAN class="comment token"># Loop over layers that support IGeoFeatureLayer (first line from in ArcMap, second line outside of ArcMap)</SPAN> pEnumLayer <SPAN class="operator token">=</SPAN> pMxDoc<SPAN class="punctuation token">.</SPAN>FocusMap<SPAN class="punctuation token">.</SPAN>Layers<SPAN class="punctuation token">(</SPAN>pUID<SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># pEnumLayer = pMap.Layers(pUID, True)</SPAN> results <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"LayerName, DataSourceType"</SPAN><SPAN class="punctuation token">]</SPAN> pEnumLayer<SPAN class="punctuation token">.</SPAN>Reset<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> pLayer <SPAN class="operator token">=</SPAN> pEnumLayer<SPAN class="punctuation token">.</SPAN>Next<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">while</SPAN> pLayer<SPAN class="punctuation token">:</SPAN> pFeatureLayer <SPAN class="operator token">=</SPAN> CType<SPAN class="punctuation token">(</SPAN>pLayer<SPAN class="punctuation token">,</SPAN> esriCarto<SPAN class="punctuation token">.</SPAN>IFeatureLayer2<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Append results and move to next layer</SPAN> results<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{}, {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>pLayer<SPAN class="punctuation token">.</SPAN>Name<SPAN class="punctuation token">,</SPAN> pFeatureLayer<SPAN class="punctuation token">.</SPAN>DataSourceType<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> pLayer <SPAN class="operator token">=</SPAN> pEnumLayer<SPAN class="punctuation token">.</SPAN>Next<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> res <SPAN class="keyword token">in</SPAN> results<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>res<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
from arcpy you can use Describe to find out what object types you have
Describe—Data Access module | Documentation
then burrow in further
CAD FeatureClass properties—ArcPy Functions | Documentation
only to discover that CAD featureclasses return FeatureClass as the DataType
but
Dataset properties—ArcPy Functions | Documentation
allows you to determine whether a dataset type
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.