ArcMap 10.3 - Hyperlink Script to Open Shapefile

4227
3
04-22-2015 11:29 AM
HeidiHinz
New Contributor

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 Scrip 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.  Any help would be greatly appreciated...

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")

0 Kudos
3 Replies
Zeke
by
Regular Contributor III

Couple things I see quickly...

If you use Windows path separators, either use two instead of one or, my preference, precede the string with r

fc = "C:\GIS_Data\Bathymetry\Global"

change to...

fc = r"C:\GIS_Data\Bathymetry\Global"

or

c = "C:\\GIS_Data\\Bathymetry\\Global"

I don't see where you're getting [File_Name]. It should be set to a variable when you do get it. You need code to get this value.

There's also no path separator between the directory and the file name in

bathy_file = (fc+ [File_Name] +'.shp')

instead,

bathy_file = (fc+ '\\' + [File_Name] +'.shp')

or

import os

bathy_file = os.path.join(fc, [File_Name]) + '.shp'

Didn't really go through the rest of the code, but that's a start.

0 Kudos
MikeMcGibney
New Contributor

Did you ever get this code to work properly? I am trying to figure out how to do the same thing. Any and all advice is welcomed! Thank you!

0 Kudos
DanPatterson_Retired
MVP Emeritus

before I look any further

fc = "C:\GIS_Data\Bathymetry\Global" 

won't work, since backslashes are interpreted as escape characters in python, you have to get it into raw notation by putting that ever-so-little-but-important r in front of the path ...

fc = r"C:\GIS_Data\Bathymetry\Global"