6

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)
1
  • 1
    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
    Commented Jun 20, 2012 at 21:30

1 Answer 1

9

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/.

13
  • oK, I modified my code like this: Commented Jun 15, 2012 at 14:59
  • and I get this errore: Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 47) Commented Jun 15, 2012 at 15:16
  • 1
    +1 - Not just for the code solution, but also for the recommendation to check out the help documentation on the ESRI website. There is a ton of useful information and many code samples that would help people immensely, if they simply took the time to look. Commented Jun 17, 2012 at 8:55
  • 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 Commented Jun 18, 2012 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
    Commented Jun 27, 2012 at 7:06

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