# 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
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.