Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Possible Duplicate:
How to move SHP to gdb with ArcView license?

Let's cut it short, I have numerous mxd with layer coming from .SHP and various servers WMS that I need to move to a gdb. What I want is to open the mxd and run a script that select all .SHP layers in the TOC create a gdb, export the .SHP, and create a new MXD with the new path pointing to gdb.

Following is the code I wrote, but I can't have it to select only the .SHP, I tried with describe dataType but they all come out as "FeatureLayer"...need help!

# Import system modules 
import arcpy, datetime, os, traceback 
from arcpy import env 

# Load required toolboxes... 
arcpy.AddToolbox("C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\ArcToolbox\\Toolboxes\\Data Management Tools.tbx")


# Get the active map document 
import arcpy.mapping 
mxd = arcpy.mapping.MapDocument ("CURRENT") 

# List broken links 
# arcpy.mapping.ListBrokenDataSources(mxd) 

# Set environment settings 
ws = "C:\\PARK" 
arcpy.env.workspace = ws 
print("env.workspace completed successfully") 
installdir = arcpy.GetInstallInfo("desktop") 

#check GDB exist 
gdb_nam = "test.gdb" 
gdb_full_path = os.path.join(ws,gdb_nam) 
if os.path.exists(gdb_full_path): 
    arcpy.Delete_management(gdb_full_path) 
print("GDB checked") 

# Execute CreateFileGDB 
arcpy.CreateFileGDB_management(ws, gdb_nam) 
print("CreateFileGDB completed successfully") 
outWorkspace = gdb_full_path 

# Iterate 

list = [] 

for df in arcpy.mapping.ListDataFrames (mxd): 
    for fc in arcpy.mapping.ListLayers (mxd, "", df):
        if (fc.datasetName.endswith(".shp")):
                    list.append(fc) 
for fc in fclist: 
    arcpy.FeatureClassToGeodatabase_conversion(fc, gdb_full_path)
share|improve this question

marked as duplicate by Mapperz Jul 20 '12 at 13:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
You mention in your opening line that you need to move WMS to a GDB. You are aware that you can't move a WMS, right? –  nagytech Jun 20 '12 at 21:30

1 Answer 1

Code

If the layer is a shapefile, Layer.datasetName will return the file name.

Please read the documentation in the following link to help you understand more about the datasetName parameter of the Layer object:

dataSource (Read Only) : Returns the complete path for the layer's data source. It includes the workspacePath and the datasetName properties combined. Not all layers support the datasetName property (for example, Web services), so it is good practice to test for this ahead of time using the supports method.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/

import arcpy, os
#import datetime, os, traceback
import arcpy.mapping
#from arcpy import env

#arcpy.AddToolbox('C:\Program Files (x86)\ArcGIS\Desktop10.0\ArcToolbox\Toolboxes\Data Management Tools.tbx')

project_root = 'C:/temp/'
working_mxd_directory = os.path.join(project_root, 'documents/')
blank_mxd_filename = project_root + 'templates/Blank.mxd'
working_mxd_file_names = ['Document_1.mxd', 'Document_2.mxd']

shapefiles_encountered = []

for working_mxd_file_name in working_mxd_file_names:
    current_mxd = arcpy.mapping.MapDocument(os.path.join(working_mxd_directory, working_mxd_file_name))
    print('I> Working on MXD: ' + current_mxd.filePath)

    layer_list = []
    for data_frame in arcpy.mapping.ListDataFrames(current_mxd):
        for layer in arcpy.mapping.ListLayers(current_mxd, "", data_frame):
            if (layer.supports("dataSource") and layer.dataSource.upper().endswith("SHP")):
                layer_list.append(layer)
                print 'I> Layer is a shapefile, adding to list: ' + layer.name
            else:
                print 'W> Layer not a shapefile: ' + layer.name    

    for layer in layer_list:
        # TODO: If shapefiles_encountered.contains(layer) foo, then bar

For starters, I would recommend you put the full layer object into your new 'list' array, then loop through that and do what you want.

After you've created / populated your GDBs, you have three options for creating your new map documents.

  1. Use the existing map document, change the data sources and save the map document as a copy.
  2. The second is to create new map documents from scratch and on the fly. I'm not sure if you can create a new empty map document directly from python/ArcPy.
  3. (Reccomended) You will need to have an empty template that you can copy in your file system. For this, you will have to access the file system using python.

You will need to consider your business rules for what happens when you come across the same shapefile in two different map documents. Do you duplicate the information or what?

Notes

I haven't used ESRI in ages, so I can't offer you a full solution to your problem. Unless someone else comes along to help you, I recommend you try to make sense of the Geoprocessing and ArcPy documentation on the ESRI site. There are quite a few people in the world who 'practice GIS', and only a percentage of those willing to write code for you.

The link posted above should get you to the general area of the ESRI ArcPy documentation. There is also a general tutorial for python here: http://docs.python.org/tutorial/.

share|improve this answer
    
oK, I modified my code like this: –  Luca Moiana Jun 15 '12 at 14:59
    
and I get this errore: Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 47) –  Luca Moiana Jun 15 '12 at 15:16
    
The sample code wasn't meant to be taken literally, sorry for the confusion. I'm assuming you've copied / pasted the '....' bit which isn't python syntax. Please see the edited version. –  nagytech Jun 15 '12 at 21:34
1  
Hi Argon, As you coould easily guess I am a newbie to Python, I just take a couple of online classes, and I am using this problem to learn more. I was exactly looking for the hints you just gave on the List and on the new mxd. I'll work on that and maybe post the code back...I also need to learn to use stackexchange. Thanks a lot –  Luca Moiana Jun 18 '12 at 13:12
1  
Sorry mate, I gave you a bum steer with 'datasetName' it should be 'dataSource'. The ESRI documentation is misleading the way it implies the datasetName includes the extension. –  nagytech Jun 27 '12 at 7:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.