0

I'm new to Python and need some help please.

I have a shapefile (Index.shp) that is used as an index for the world splitting it into 264 areas (polygon). I'm trying to create a Python script (for the ArcMap Hyperlink Script section of this Index.shp) that will add another shapefile to the current ArcMap project based on the polygon the user has clicked on with the hyperlink tool. The Index.shp has an attribute field called File_Name with the shapefile name to be opened.

I was able to get some information online, but I still can't figure out how to get it to work.

Here's my code to far:

import arcpy

#get the current map document
mxd = arcpy.mapping.MapDocument("CURRENT")  

#set the directory to bathy shapefiles
fc = "C:\GIS_Data\Bathymetry\Global"  

#get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

#create a new layer - [File_Name] = bathy shapefile name
bathy_file = (fc+ [File_Name] +'.shp')

newlayer = arcpy.mapping.Layer(bathy_file)

#add the layer to the map at the bottom of the TOC in data frame 0
arcpy.mapping.AddLayer(df, newlayer,"AUTO_ARRANGE")
  • I'm going to say this isn't possible. The "CURRENT" mxd document is only the current document when called from within ArcMap. This code will execute outside of ArcMap so there is no "CURRENT" mxd. You could probably do this by creating a toolbox tool or maybe a python add-in to do this. 1) Select the feature. 2) Click the tool 3) tool uses value from the selected feature to add data – JasonT Apr 22 '15 at 18:58
  • 1
    at any rate, I'd recommend instantiating bathy_file like this: bathy_file = os.path.join(fc,[file_name]+".shp"). Looks like you are missing a "\" in the above script. – mr.adam Apr 22 '15 at 20:03
1

Like I said in my comment, I don't think this is possible exactly how you describe it. This should work.

import os

# Change the following 3 variables - - - - - - - - - - - --  -
dir_fc = r'C:\dir\to\the\shapefiles'

# set the index name and attribute from that layer to be used for the filename
index_fc_name = "index"
index_fc_attribute = "field_for_file_name"

# End of user variables - - -- -  - -- - - - - - - - - - - - 


# get the current MXD
mxd = arcpy.mapping.MapDocument("CURRENT")

# get the 1st dataframe (assuming you are using 1)
df = arcpy.mapping.ListDataFrames(mxd)[0]

# Search Cursor only looks at selected objects
with arcpy.da.SearchCursor(index_fc_name,index_fc_attribute) as sc:
        for row in sc:
                # If multiple are selected, multiple will be added

                file_name = "{}.shp".format(row[0])
                fpath = os.path.join(dir_fc, file_name)

                newlayer = arcpy.mapping.Layer(fpath)   # Create the layer
                arcpy.mapping.AddLayer(df, newlayer)    # Add layer to data frame
arcpy.RefreshActiveView()       # Refresh the view

1st get this running in the python console window inside of ArcMap. After you get it running there, I would add the script to a toolbox, or better a python addin. Just use the Python Add In Wizard and lookup how to create the addin. It's pretty easy and only takes a few minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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