5

I am writing a code to loop through all the files in a folder and reproject them, using a raster as the spatial reference. I have a suspicion that there is a different way to get the coordinate system from a raster. This is my code so far:

#Import system modules
import arcpy
import os

#Set environment
arcpy.env.workspace = "D:/Project/Joined"
arcpy.env.overwriteOutput = True

#Get list of files in Joined folder
fileList = arcpy.ListFeatureClasses()
print fileList

#Set raster as basis for coordinate system
base = "D:/Project/Data/Development.tif"
base2 = arcpy.Describe(base)
out_coor_system = arcpy.SpatialReference(base2)

#Loop through shapefiles in fileList and reproject 
for fc in fileList:
    outName = fc + '_rprj.shp'
    new = arcpy.Project_management(fc, outName, out_coor_system)

This is the error I am receiving:

File "D:/Project/Data/Reproject.py", line 16, in <module>
    out_coor_system = arcpy.SpatialReference(base2)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\mixins.py", line 949, in __init__
    self._arc_object.createFromFile(item)
RuntimeError: ERROR 999999: Error executing function.

From what I've read, this is a pretty generic error for Python IDLE to throw.

I made the code print out out_coor_system and I am getting: <geoprocessing spatial reference object object at 0x0D2CE830> printed out to me. Could this be the problem?

I also wanted to add that the projection I am trying to use is a custom one that my organization uses. I don't know if that's relevant (it seems it shouldn't be) but I wanted to throw that in.

4
  • Welcome to GIS SE. Your code looks OK and I am not sure there is another, except if you look for a solution without arcpy. Could you explain what you have in mind ? On the other hand, I would be very cautious with this kind of code because you do not handle the datum transform, which could cause a systematic error (5 to 100 m) if your projections are based on different datums.
    – radouxju
    Commented Feb 23, 2016 at 19:05
  • @radouxju -- I'm not sure I have anything in mind, however I did see some GDAL modules referenced in other answers about rasters so I thought there was maybe something there. I also don't think there is a datum transformation needed, according to the documentation if it is required, the error will say so. I'll add the error I'm getting from this code to the question, it might help illuminate the problem. Commented Feb 23, 2016 at 19:09
  • No, that is a generic error that ESRI throws. I've seen the same in ArcMap and the same in ArcObjects. It is highly unusual in Python code that doesn't involve ESRI. (Python is generally much, much better at giving useful information in errors.) Also, IDLE is just an editor. when you run a script, the engine that runs it would just be called "Python."
    – jpmc26
    Commented Feb 24, 2016 at 4:23
  • Also, <geoprocessing spatial reference object object at 0x0D2CE830> is just because ESRI failed to define a good repr (the function on Python objects that is called to get a string when the object is fed into print). All it means that there is a non-None object there; it could have any data. You would have to examine the attributes of that object to learn more about its contents.
    – jpmc26
    Commented Feb 24, 2016 at 4:26

4 Answers 4

5

You're codes not quite right. You're trying to create a spatial reference object from a describe object. Here's a fix:

#Set raster as basis for coordinate system
base = "D:/Project/Data/Development.tif"
out_coor_system = arcpy.Describe (base).spatialReference

#Loop through shapefiles in fileList and reproject 
for fc in fileList:
    outName = fc + '_rprj.shp'
    new = arcpy.Project_management (fc, outName, out_coor_system)
3

This worked for me:

out_coor_system = arcpy.Describe(base).spatialReference
2

if the out_coor_system is giving you an object, wouldn't you need to specify something like out_coor_system.name? because projections are specified by giving the projection name in the arc.Project_management args.I don't think the function is able to evaluate the object as it stands now.

2
  • Interesting idea. I tried it and got this error: ExecuteError: ERROR 000622: Failed to execute (Project). Parameters are not valid. ERROR 000628: Cannot set input into parameter out_coor_system. Commented Feb 23, 2016 at 19:54
  • Ah! I guess the .name wasn't the right subscription. As per @freshhmints & @Emil Brundage answer below. It looks like the .spatialReference singles out the name of the spatialReference from the raster describe object.
    – yanes
    Commented Feb 23, 2016 at 22:25
2

Thank you to everyone who helped me with this. I ended up creating a spatial reference object from the raster. What eventually worked was this:

#Set raster as basis for coordinate system
base = "D:/Project/Data/Development.tif"
sr = arcpy.CreateSpatialReference_management("", base)   

#Loop through shapefiles in fileList and reproject 
for fc in fileList:
    outName = fc[:-4] + '_rprj.shp'
    new = arcpy.Project_management(fc, outName, sr)

To be honest, I'm not sure why this worked. If anyone has any insight to offer, that would be great.

2
  • I don't see why the solution's suggested above didn't work for you. Your solution is also a different approach to the problem, you created a projection by copying it from a an existing raster. Where as the x = arcpy.Describe (base).spatialReference saves the projection from the given raster under the variable x. I believe both should work. Did it give you error when you tried @Emil's code?
    – yanes
    Commented Feb 24, 2016 at 17:44
  • It was the same error I had been getting. Would it have had anything to do with the projection being custom? Commented Feb 26, 2016 at 14:09

Your Answer

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

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