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
mxd = arcpy.mapping.MapDocument("CURRENT")
fc = "C:\GIS_Data\Bathymetry\Global"
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
bathy_file = (fc+ [File_Name] +'.shp')
newlayer = arcpy.mapping.Layer(bathy_file)
arcpy.mapping.AddLayer(df, newlayer,"AUTO_ARRANGE")
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.
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!
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"