0
import arcpy
sourceWorkspace = "C:\\Users\\tx8p90\\Desktop\\Lesson2"
targetProjection = "C:\\Users\\tx8p90\\Desktop\\python excercise data\\Lesson1\\contourlines.shp"
listDataSet = arcpy.ListDatasets(sourceWorkspace)
try:
    for x in listDataSet:
        outputDataset = sourceWorkspace + "\\projected_" + x
        arcpy.Project_management(x, outputDataset, targetProjection)
        print "Reprojection successful"
except:
    print arcpy.GetMessages()
    print "failed"

I have started learning Python for ArcGIS last week and have a simple script to reproject all the datasets in a folder. The script says "syntax error- invalid syntax" but I can't find anything. Can someone please have a look? It seems something very basic but I'm pretty new so I don't see it at the moment. Also running this in ArcGIS prints "failed" right away. And the files are in place in the right folders. I have already checked.

Edit:

Ok just at least the syntax error has disappeared by just writing the code again by removing those 3 dots at the beginning of each line. But still I get "Failed" as output.

3
  • python.org/dev/peps/pep-0008
    – ianbroad
    Jan 22, 2015 at 2:08
  • 1
    Please edit your question and post your code as code instead of as an image (hint: use the {} button above the question when it's in edit mode).
    – user2856
    Jan 22, 2015 at 3:25
  • What is a .shp file doing as a targetProjection argument? According to the help, that is supposed to be a .prj file.
    – whuber
    Jan 23, 2015 at 20:41

3 Answers 3

2

listDataSet and listDataset are two different objects (capitalization matters in Python). It is looking for all the 'x' in 'listDataset', but 'listDataset' does not exist ('listDataSet' does, though. Try that).

1
  • Done. Doesn't change anything. Still same syntax error with pythonwin and Arcmap prints failed right away.
    – Salman
    Jan 23, 2015 at 13:35
2

Use describe object and get spatial reference (SR) from it. Specify it as a target projection in the tool, e.g.

d=arcpy.Describe(LayerWithTargetProjection)
SR=d.spatialReference
arcpy.Project_management(input_features, output_feature_class, SR)
3
  • Please expand your answer a bit, so everyone will have an easier time applying it. If I understand correctly, he should extract the srid from the file, not pass it directly to arcpy. Jan 21, 2015 at 23:29
  • I extendec my answer
    – FelixIP
    Jan 22, 2015 at 0:54
  • Shit! Even this changes nothing. Same invalid syntax error in Pythonwin and nothing happens in Arcmap after running the code. With this I don't even get the 'Failed' print out.
    – Salman
    Jan 23, 2015 at 13:40
0

Okay so I finally figured this out. It was a stupid mistake on my part. The files I had in the specified folders were actually feature classes and not datasets. Anyway it is not possible to create a dataset inside a folder. It can only be done inside a geodatabase. So for someone who knows their way around with ArcGIS, they could have noticed that I had just a folder in the code but I was calling ListDataSets() function which is wrong. Using ListFeatureClasses() solved the issue and the files were properly reprojected. Thanks for all the inputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.