Python - how to select certain shapfiles in folder for projection

3953
8
04-09-2015 05:29 PM
MarkWisniewski
New Contributor III

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

Tags (1)
0 Kudos
8 Replies
DanPatterson_Retired
MVP Emeritus

check the two formats in the help topic for spatial reference 

dataset = "c:/data/landbase.gdb/Wetlands"
spatial_ref = arcpy.Describe(dataset).spatialReference

  • Using a projection file (.prj)

    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. 

0 Kudos
NeilAyres
MVP Alum

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.

MarkWisniewski
New Contributor III

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

0 Kudos
NeilAyres
MVP Alum

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.

0 Kudos
MarkWisniewski
New Contributor III

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.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Case matters in Python.

It is:

spatialRef = arcpy.Describe(template).spatialReference

Not:

spatialRef = arcpy.Describe(template).SpatialReference

MarkWisniewski
New Contributor III

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"

0 Kudos
BlakeTerhune
MVP Regular Contributor

Bad Joke Friday:

"You're extra Spatial!"

0 Kudos