Select to view content in your preferred language

Load shapefile in arcmap using python script

13599
3
03-21-2012 10:20 AM
rrjs67
by
Emerging Contributor
I am a novice in Python. Please help me to load the shapefile into  arcmap and generate straight line distances for each of them in a  folder.  Thanks  Raj
0 Kudos
3 Replies
DarrenWiens2
MVP Honored Contributor
This will add the feature class specified in "fc" to the current map document. Run as a script from inside ArcMap.
import arcpy

fc = "H:/GIS_Data/TEMP.gdb/polygons"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

arcpy.MakeFeatureLayer_management(fc, "poly")
addLayer = arcpy.mapping.Layer("poly")
arcpy.mapping.AddLayer(df,addLayer)

What do you mean by "generate straight line distances for each of them in a folder"?
0 Kudos
rrjs67
by
Emerging Contributor
Yes sir, I want to generate straight line distances to each of them to an output folder.By the way, my files are point shapefile.
When I run the above script, nothing is loading to mxd document.
Thank you very much for your help.
Raj
0 Kudos
rrjs67
by
Emerging Contributor
I am getting the error message for the code:
Traceback (most recent call last):
  File "H:\Desktop\Load_shp.py", line 4, in <module>
    mxd = arcpy.mapping.MapDocument("CURRENT")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\mixins.py", line 444, in __init__
    super(MapDocumentMethods, self).__init__(mxd)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 47, in __init__
    for arg in args))
RuntimeError: Object: CreateObject cannot open map document


However, after spending awhile on this, I could write small script that loads the point shp file.
the code is:
import arcpy
# get the map document
mxd = arcpy.mapping.MapDocument(r"C:\Research-gravity\Huff\Santhi\SGravity\StLine.mxd")
# Set the workspace
arcpy.env.workspace = r"C:\Research-gravity\Huff\Santhi\SGravity"
# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
# create a new layer
newlayer = arcpy.mapping.Layer(r"C:\Research-gravity\Huff\Santhi\SGravity\points.shp")
# add the layer to the map at the bottom of the TOC in data frame 0
arcpy.mapping.AddLayer(df, newlayer,"AUTO_ARRANGE")
# save the mxd file
mxd.save()
del mxd

Now I need to calculate straight line distances for the point shape file. could you please help me?
I greatly appreciate your help.
Thanks
Raj
0 Kudos