Batch Re-projection script syntax error

4715
13
01-21-2015 10:32 AM
SalmanAhmed
New Contributor III

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.

 

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"

0 Kudos
13 Replies
SusanJones
Occasional Contributor II

HiSalman

First, Try setting your workspace  before using the arcpy.ListDatasets()

eg,

arcpy.env.workspace = sourceWorkspace

Second, use the correct  arcpy.ListDatasets() arguements (wild card naming and feature type

eg,

listDataSet = arcpy.ListDatasets(wild_card = "*", feature_type = "Feature")

Susan

0 Kudos
SalmanAhmed
New Contributor III

Hi

I actually tried setting up the workspace already although I think its not necessary as long as you give a path in the ListDataset() function. Anyways even doing that hasn't made any difference. I get the same syntax error.

Regarding the arguments, since I need to change projection for all the files in that folder irrespective of feature type, I don't need to set any arguments like starting letter or feature type. Is that not so?

Salman

0 Kudos
SusanJones
Occasional Contributor II

Hi Salman

The arcpy.ListDataset() is calling for a wildcard.

Not sure if it is optional, but it wouldn't hurt to specifiy the wildcard ("*")

See if it works

Susan

0 Kudos
SusanJones
Occasional Contributor II

Hi again

Also, I don't think arcpy.ListDataset() is expecting a workspace.

Perhaps, spectify the workspace before hand under the arcpy.env.workspace parameter.

Susan

0 Kudos
SalmanAhmed
New Contributor III

no luck still error.png

0 Kudos
SusanJones
Occasional Contributor II

Hi Salman

First, What is the value of outputDataset?

Second, Output_Coordinate_System is expecting either a projection file (with a prj extension) or a coordinate system object. Not a shp extension.

Make sure the Shapefile has a projection, and then set the targetProjection to point to it's prj file.

Susan

0 Kudos
SalmanAhmed
New Contributor III

replacing the shape file with the prj file doesn't change anything. Same syntax error. And I thought it was enough to just point to the shape file to get its projection.

0 Kudos
SalmanAhmed
New Contributor III

Ah! sorry I just realized I can't put a path in that function's argument. Ok but anyways the setting up workspace option is not working either.

import arcpy

... sourceWorkspace = "C:\\Users\\tx8p90\\Desktop\\Lesson2"

... targetProjection = "C:\\Users\\tx8p90\\Desktop\\python excercise data\\Lesson1\\contourlines.shp"

... arcpy.env.workspace = sourceWorkspace

... listDataSet = arcpy.ListDatasets()

... try:

...     for x in listDataset:

...         outputDataset = sourceWorkspace + "\\projected_" + x

...         arcpy.Project_management(x, outputDataset, targetProjection)

...         print "Reprojection successful"

... except:

...     print arcpy.GetMessages()

...     print "failed"

What could be the problem then?

0 Kudos
SusanJones
Occasional Contributor II

Hi Salman

Please test the following:

tmpDir = "D:\\Tmp" ##tmpDir = r"d:\Tmp"

arcpy.env.workspace = tmpDir

print arcpy.env.workspace

ld = arcpy.ListDatasets("*")

print ld

What is returned in the ld variable ?

Susan

0 Kudos