1
    # Script to use Layers in MXD source file paths to locate and redefine projections

import arcpy, glob, os, sys, arcgisscripting
from arcpy import env, mapping
path = os.getcwd()
env.workspace = path
env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument("N:\MXDs\_ThomaKL\MXD GPS Valves\GPS_Shapefile_Paths.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE"):
        print lyr.dataSource

print 'Reading files from ' + lyr.dataSource
os.chdir(path)
sr = arcpy.SpatialReference(103133)

x=0
z=x+1
NoProjCount=0
FileList= arcpy.mapping.ListLayers()
for File in FileList:
    desc = arcpy.Describe(File)
    SR = desc.spatialReference
    if SR.name == "Unknown":
        print "Projection of " + str(File) + " is " + SR.name + " so defining projection."
        f = open('NoProjection.txt', 'a')
        f.write(str(File)+"\n")
        f.close()
        arcpy.DefineProjection_management(File, sr) 
        NoProjCount=NoProjCount+1
    else:    
        print File + " is projected " + str(SR.name)

I get this after I run the Script enter image description here

Traceback (most recent call last): File "N:\MXDs_ThomaKL\MXD GPS Valves\DefineProjWDTest", line 22, in FileList= arcpy.mapping.ListLayers() File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) TypeError: ListLayers() takes at least 1 argument (0 given)

NoProjection.txt shows a list of featureclasses that is in the folder where the featureclasses is Located.

I have a python script that I have been working on were the script goes into a MXD that has several shapefiles that are in different locations on my network. Again I am trying to use the location of the shapefiles in the MXD to tell python where to look for them and then redefine their projections.. I feel like I am very close but I have hit a road block with it and I was wondering if anyone could help me finish this to get it to work..

Update

    # Script to use Layers in MXD source file paths to locate and redefine projections

import arcpy, glob, os, sys, arcgisscripting

mxd = arcpy.mapping.MapDocument("N:\MXDs\_ThomaKL\MXD GPS Valves\GPS_Shapefile_Paths.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE"):
        print lyr.dataSource

from arcpy import env, mapping
path = lyr.dataSource
env.workspace = path
env.overwriteOutput = True

print 'Reading files from ' + path

sr = arcpy.SpatialReference(103133)

x=0
z=x+1
NoProjCount=0
FileList= arcpy.mapping.ListLayers(lyr)
for lyr in FileList:
    desc = arcpy.Describe(lyr)
    SR = desc.spatialReference
    if SR.name == "Unknown":
        print "Projection of " + str(lyr) + " is " + SR.name + " so defining projection."
        f = open('NoProjection.txt', 'a')
        f.write(str(lyr)+"\n")
        f.close()
        arcpy.DefineProjection_management(lyr, sr) 
        NoProjCount=NoProjCount+1
    else:    
        print lyr + " is projected " + str(SR.name)

I realized i had some pieces ordered wrong so I decided to make some changes to my original code, good news is that it worked the bad news is that it only worked on the very last layer in the MXD, so all the other layers in the MXD still showed up with "no projection". it doesn't give me any errors, but again i am trying to get it to go through the list of layers in the folders and define their projections, as i said it only does the very last one that it reads not the entire list.

  • That looks fine, are the projections not being set? What's in NoProjection.txt? – Michael Stimson Dec 16 '15 at 22:49
  • You say "I have hit a road block" but you have not told us what that road block is (error message or other symptoms), nor asked a specific question. As a new user be sure to take the Tour to learn about our focussed Q&A format. – PolyGeo Dec 16 '15 at 22:51
  • @MichaelMiles-Stimson well that is good, it does seem to run okay, as for the NoProjection.txt, I can not find the txt file to look at it, I wondering i might need to place the script file in the folder with the MXD for it to actually define the projections of the shapefiles? I just thought about that after you said that, i moved the file for safety. I'll add a image of the error I get. – popboarder Dec 16 '15 at 23:00
  • @PolyGeo you are right sorry let me get that image of the Python Shell. – popboarder Dec 16 '15 at 23:01
  • 1
    Please include errors as text rather than pictures so that they can become available to future searches. – PolyGeo Dec 16 '15 at 23:20
2

You have a mistake here, that I think manifests itself as that error:

mxd = arcpy.mapping.MapDocument("N:\MXDs\_ThomaKL\MXD GPS Valves\GPS_Shapefile_Paths.mxd")

which should be:

mxd = arcpy.mapping.MapDocument(r"N:\MXDs\_ThomaKL\MXD GPS Valves\GPS_Shapefile_Paths.mxd")

or

mxd = arcpy.mapping.MapDocument("N:\\MXDs\\_ThomaKL\\MXD GPS Valves\\GPS_Shapefile_Paths.mxd")

or

mxd = arcpy.mapping.MapDocument("N:/MXDs/_ThomaKL/MXD GPS Valves/GPS_Shapefile_Paths.mxd")

I am also not so sure about the underscore in the _ThomaKL folder name. If the above does not work try renaming that folder and changing your code to match.

  • The error is on line FileList= arcpy.mapping.ListLayers() which should be FileList= arcpy.mapping.ListLayers(mxd), you are right about the raw paths and escape characters though. – Michael Stimson Dec 16 '15 at 23:21
  • @MichaelMiles-Stimson From the output you are right; I wonder how the code (if run precisely as presented) got that far? – PolyGeo Dec 16 '15 at 23:23
  • It ran through the first loop (listlayers correct) just fine but when it got to the 2nd it fell over on the missing parameter. Theoretically, you're right, it shouldn't have even got that far due to escape characters, I think though it's just luck as \M and \G aren't escape characters so were passed as-is. It would have been a different story if it was a path like d:\Toby\.. as \T is TAB. – Michael Stimson Dec 16 '15 at 23:50
  • @MichaelMiles-Stimson yes I did try changing the FileList= arcpy.mapping.ListLayers() to FileList= arcpy.mapping.ListLayers(mxd) and that gave me an error on Line 24 because I have "for File in FileList:" and desc = arcpy.Describe(File). I am thinking that needs to be changed to mxd as well as the parts that reference (File) after that. – popboarder Dec 17 '15 at 16:07
2

As the error states, arcpy.mapping.ListLayers () requires an argument. As per its documentation it requires the map document object (or a layer object) as its input.

arcpy.mapping.ListLayers (mxd)

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.