Select to view content in your preferred language

Name of local gdb?

919
1
04-30-2020 01:40 AM
AndersMark
Emerging Contributor

I want to read the name of the local gdb currently checked out from a map service. This is because I want to write some Pythoncode to access it. My problem is that the name will change and I cant know it in advance. 

So, what do I do?

1. Connect to a map service and drag the service to the layer area in ArcMap. 

2. Right click on the group layer (which has the same name as the service). 

3. Select "Edit Features/Create Local Copy for Editing"

4. Now I got a local copy with a random name. It can be something like: 

        "C:\temp\NNK\nnkautv1_plsql\fs129D23DB12304BF58962DB1A9F1037AE.gdb"

5. And now the question: How do I figure out this name and location?

Tags (3)
0 Kudos
1 Reply
AndersMark
Emerging Contributor

I will answer this myself since I solved the problem. Short code to loop through current layers and get name of the gdb for all of them. If the source is the same for all layers each layer will show the same gdb. 

The dataSource printed below contains beside name of the gdb, also name of the layer. To extract just the name of the gdb you can use a method like: 

import os
def getPath(source):
    # If there's a backslash, convert them to slash. Then you don't have to bother about backslash that also 
    # work as a escape character. 
    source = source.replace(os.sep, '/')
    s = source[:source.rfind(".gdb")+4]
    return s

mxd = arcpy.mapping.MapDocument("CURRENT")

for df in arcpy.mapping.ListLayers(mxd):
   if df.isFeatureLayer:
      print df.dataSource
      print df.datasetName

      gdbPath = getPath(df.dataSource)

      print gdbPath