Python Script to Project Shapefiles in a folder

4987
10
Jump to solution
10-25-2015 02:05 PM
Highlighted
New Contributor III

I am very, very new to Python and I am trying to write a script that will allow me to Project shapefiles into a coordinate system that is assigned to one particular shapefile in a folder. So I have 4 files and one has that which I want to re-Project the others into. What I am struggling with is the output CS parameter required by the Project_management tool when called in the script. I get an error message saying that the output Coordinate System value and the outputFC parameter is not valid. I'm wondering where I'm going wrong.

Any help at all would be so greatly appreciated.

  Sincerely,

Natasha

SR6.jpg

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
MVP Esteemed Contributor

The line

     outFC = arcpy.os.path.join(outputFCpath, fc)

Joins the output path/folder and the input feature class name, so the output will not overwrite the input. This will create a new <fc>.shp in the output folder I set up as a variable at the top of the script.

....Once you feel you have your answer, make sure to mark one of the comments as "answered" and any others you find as helpful.  This will help others find the solution, and will close this thread. thanks.

View solution in original post

10 Replies
Highlighted
MVP Esteemed Contributor

ok...even if it succeeds in your if else block it isn't going to project the file and you aren't giving it a coordinate system, just a name, also, you should add an arcpy overwriteoutputs line so you can experiment and overwrite stuff otherwise you will get a whole load of other errors.

Highlighted
New Contributor III

Dan I found what you were talking about in the Project 2 sample script:

Project—Help | ArcGIS for Desktop

Thankyou

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

it was only a matter of time  keep at it

Highlighted
MVP Esteemed Contributor

Natasha,

It looks like you are printing "hooray" if the don't match (!=) and trying to project them if they do.   Try changing the != to ==  for the first part.

Then, as Dan mentioned, you have issues with you output, in fact the path set in the variable output isn't used.  But, that is all part of the learning process.  My guess is that is the reason you are creating the script, and not just running it once manually, then double clicking the command in the results tab and changing the input and output shape file?

Highlighted
New Contributor III

​ Hmm thankyou Rebecca. I am trying hard to get this.

So I'll try changing the syntax but I'm still not sure where the output

CS is supposed to be iterated.

The print statements were just supposed to iterate which shapefiles were

supposed to be projected, or not.​

I'm still unclear as to how this section is useful but I suppose it is

because I at least have a set of files that I know should be re projected.

So where should I put the == statement

What I want to accomplish is putting together a script that would

reproject shapefiles in a folder in different projections to one which I

have set.

I appreciate any continued help!

Thankyou Rebecca

On Sun, Oct 25, 2015 at 8:10 PM, Rebecca Strauch, GISP <geonet@esri.com>

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

Natasha,

A tip for the future, when posting a question, it will help others help you if you follow the tips here Posting Code blocks in the new GeoNet​    But since it was a short script, I went ahead and started playing with the code myself.  Dan had some good comments (and some help sources that you should look thru), but I've re-written the code...keeping the main flow of your script....but have the actual project comment commented out.  I haven't quite got the correct syntax for that yet, but this may help you see some of the other issues you have.

UPDATED:  this should work.  Update your paths, and the targetFC of course.  You had several this that needed fixing.  Read thru this and hopefully you will see what it all means, but ask questions if it doesn't make sense.  Keep in mind that shapefiles have a .prj file to assign the projection, and if this is missing the command will have problem projecting.  A missing projection will cause issues for any feature class, but shapefiles in particular are a bit more simplistic, so to speak.

import arcpy

#setup pathc

targetFC = r"c:\__projIn\gmu.shp"

filesToExamine = r"c:\__projIn"

outputFCpath =   r"c:\__ProjOut"

# get SR for targetFC

targetDescribe = arcpy.Describe(targetFC)

targetSR = targetDescribe.spatialReference

targetSRName = targetSR.Name

print targetSRName

# get a list of shapefiles

arcpy.env.workspace = filesToExamine

fcList = arcpy.ListFeatureClasses()

# loop through the list

for fc in fcList:

  fcDescribe = arcpy.Describe(fc)

  fcSR = fcDescribe.spatialReference

  fcSRName = fcSR.Name

  print fcSRName

  if fcSR.Name == targetSR.Name:

    print "hooray"

  else:

    print "No"

    outFC = arcpy.os.path.join(outputFCpath, fc)

    arcpy.Project_management(fc, outFC, targetSR)

Highlighted
New Contributor III

Rebecca I am looking through this reply and I very much appreciate you

response and I am so new to Python that I am thoroughly confused.

I also appreciate Dans replies and I am learning from all of it!

outFC = arcpy.os.path.join(outputFCpath, fc)

the above line confuses me as I don't know where to put os and Ive not

understood it nor have we used it in our syntax however I will try to

incorporate it in my code and accommodate what you so generously have

helped try to write.

Also, I will go through the documentation that you and Dan have

advised me to do both in posting and in realizing the skillset.

Thankyou very much, very much.

Cheers,

~Natasha

I'm trying right now and again, I appreciate your assistance.

On Sun, Oct 25, 2015 at 8:53 PM, Rebecca Strauch, GISP <geonet@esri.com>

Highlighted
MVP Esteemed Contributor

The line

     outFC = arcpy.os.path.join(outputFCpath, fc)

Joins the output path/folder and the input feature class name, so the output will not overwrite the input. This will create a new <fc>.shp in the output folder I set up as a variable at the top of the script.

....Once you feel you have your answer, make sure to mark one of the comments as "answered" and any others you find as helpful.  This will help others find the solution, and will close this thread. thanks.

View solution in original post

Highlighted
New Contributor III

Rebecca this is really helpful and I so appreciate it.

Thanks so much for this information.

I will follow your direction as per information on the GeoNet site.

Again. Many thanks.

On Sun, Oct 25, 2015 at 9:44 PM, Rebecca Strauch, GISP <geonet@esri.com>