Hi,
I am trying to create a python script with 3 parameters;
1) Dataset Folder which contains the shapefile datasets to be reprojected
2) Target Projection Dataset whose spatial reference will be used as specified projection in the reprojection
3) Projected Dataset Folder containing projected datasets
check the two formats in the help topic for spatial reference
dataset = "c:/data/landbase.gdb/Wetlands"
spatial_ref = arcpy.Describe(dataset).spatialReference
sr = arcpy.SpatialReference("c:/coordsystems/NAD 1983.prj")
do note the subtle but difference in implementation and case in your case you are mixing the two.
This bit is not going to work -
for fc in fcList: if fc != spatialRef:
Consider what you are doing here.
fc is an text item from your list of features.
But spatialRef from the template describe object is a spatial reference object.
So they will never be equal.
Inside the loop you need to do another Describe of each fc, get the SR of that, then compare.
I have updated the script with the above suggestion, but it still doesn't work. I even tried template = filepathname, but that didn't work either. is there any other suggestion/comments? Thanks.
import arcpy
# Set Workspace
arcpy.env.workspace = arcpy.GetParameterAsText(0)
#Access shapefiles
fcList = arcpy.ListFeatureClasses()
try:
# Projection template
template = arcpy.GetParameterAsText(1)
# Output folder
outFolder = arcpy.GetParameterAsText(2)
# Get the spatial reference
spatialRef = arcpy.Describe(template).SpatialReference
#Loop through shapfiles in folder and reproject
for fc in fcList:
fcspatialRef = arcpy.Describe(fc).SpatialReference
if fcspatialRef != spatialRef:
arcpy.Project_management(fc, outFolder + "\\" + fc, spatialRef)
# Return any errors
except:
arcpy.AddMessage(arcpy.GetMessages ())
# Print shapefile list
for fc in fcList:
print fc
The block after try: should also be indented as well. How about telling us what errors you are getting instead saying "it doesn't work".
Not many clues there.
Oh Sorry Neil. The code in the script is indented thanks to IDLE.
What is happening is all the shapefiles (8) are being reprojected, instead of the 6 shapefiles which have different projections to the template projection shapefile. 1 other file is in the same projection, so it shouldn't be projected.
Case matters in Python.
It is:
spatialRef = arcpy.Describe(template).spatialReference
Not:
spatialRef = arcpy.Describe(template).SpatialReference
Thanks Darren,
I changed "Spatial" to "spatial" and I also had to place .Name on the end and change "spatialRef" in Project tool to "template". The script is now working.
It's confusion because sometimes I see "Spatial" and "spatial"
Bad Joke Friday:
"You're extra Spatial!"