Select to view content in your preferred language

Find and fix images ith Spatial Reference Undefined.

5754
19
Jump to solution
01-14-2016 02:02 PM
PaulHuffman
Frequent Contributor

I have a directory with 370 tifs.  I just found out that a few, an unknown number, have the projection undefined, but they draw aligned with the know projection tifs, so no mystery there.  I thought I could loop through a list of tifs in the folder and set the definition on the files missing, similar to https://community.esri.com/thread/13078  but wondered if I should add an "if" to check if the image is one of the ones with projection undefined. But what would be that test?  Do you think the test would speed up execution or not? 

0 Kudos
1 Solution

Accepted Solutions
PaulHuffman
Frequent Contributor

Yes,  the sample code was very helpful. Spent too much time looking through the Syntax and Properties section, when the sample had a clear example of the test.  And yes lower case w on workspace was also needed. I couldn't figure out why my raster list seemed to be empty when it hit line 20.

This code worked on a sample data folder.  Now I'll run it on the rest.  I'm not sure if this testing if the coordinate system is undefined decreased run time or if I could even notice a difference  with 370 files. Maybe 3000 files.

import sys, os, arcpy
from arcpy import env
# Load required toolboxes...  
#gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")  
         
#workspace  
env.workspace = "G:\\usgs_quads\\test"  
#coordsys = "C:\\Program Files\\ArcGISCoordinate Systems\\Projected Coordinate Systems\\National Grids\\British National Grid.prj"  
      
#List all features in  gp.workspace  
rasters = arcpy.ListRasters("*", "TIF")  
#raster = rasters.Next()


# get the coordinate system by describing a feature class
dsc = arcpy.Describe("YESMOWIT CANYON.tif")
coord_sys = dsc.spatialReference

  
for raster in rasters:
    # Create the spatial reference object
    spatial_ref = arcpy.Describe(raster).spatialReference

    # If the spatial reference is unknown
    if spatial_ref.name == "Unknown":
     arcpy.DefineProjection_management(raster, coord_sys)  
     print raster + " is defined."  

View solution in original post

19 Replies
DanPatterson_Retired
MVP Emeritus

since you don't appear to be afraid of scripting, I will refer you to SpatialReference—Help | ArcGIS for Desktop

which contains all the appropriate information of coordinate systems and their associated properties.  The code sample at the bottom will be of use to you

PaulHuffman
Frequent Contributor

I figure the code would be something like this, revised slightly from something I had laying around for fixing shapefiles, trying to remove the old style gp stuff. I'm still trying to figure out how I might test for projection undefined. but it might run just as fast without the test.

import arcgisscripting, sys, os, arcpy
from arcpy import env
# Load required toolboxes...  
#gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")  
         
#workspace  
env.Workspace = "G:\\usgs_quads\\test"  
#coordsys = "C:\\Program Files\\ArcGISCoordinate Systems\\Projected Coordinate Systems\\National Grids\\British National Grid.prj"  
      
#List all features in  gp.workspace  
rasters = arcpy.ListRasters("*", "TIF")  
#raster = rasters.Next()


# get the coordinate system by describing a feature class
dsc = arcpy.Describe("YESMOWIT CANYON.tif")
coord_sys = dsc.spatialReference

  
for raster in rasters:  
     arcpy.defineprojection(raster, coord_sys)  
     print fc + " is defined."  
DanPatterson_Retired
MVP Emeritus

you did check the code sample in my previous post from the help file?.   You want to get a list of them first ... before ... you define them.  Just to make sure we are on the same page

ps you don't need arcgisscripting and check to see whether workspace is capitalized in env.w...

PaulHuffman
Frequent Contributor

Yes,  the sample code was very helpful. Spent too much time looking through the Syntax and Properties section, when the sample had a clear example of the test.  And yes lower case w on workspace was also needed. I couldn't figure out why my raster list seemed to be empty when it hit line 20.

This code worked on a sample data folder.  Now I'll run it on the rest.  I'm not sure if this testing if the coordinate system is undefined decreased run time or if I could even notice a difference  with 370 files. Maybe 3000 files.

import sys, os, arcpy
from arcpy import env
# Load required toolboxes...  
#gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")  
         
#workspace  
env.workspace = "G:\\usgs_quads\\test"  
#coordsys = "C:\\Program Files\\ArcGISCoordinate Systems\\Projected Coordinate Systems\\National Grids\\British National Grid.prj"  
      
#List all features in  gp.workspace  
rasters = arcpy.ListRasters("*", "TIF")  
#raster = rasters.Next()


# get the coordinate system by describing a feature class
dsc = arcpy.Describe("YESMOWIT CANYON.tif")
coord_sys = dsc.spatialReference

  
for raster in rasters:
    # Create the spatial reference object
    spatial_ref = arcpy.Describe(raster).spatialReference

    # If the spatial reference is unknown
    if spatial_ref.name == "Unknown":
     arcpy.DefineProjection_management(raster, coord_sys)  
     print raster + " is defined."  
DanPatterson_Retired
MVP Emeritus

I wouldnt worry about runtime... there are even clever shortcuts like making multiple copies of *.tfw world files to match *.tif files... and assuming you want the files to remain as separate entities initially at least... but lets not go there. Just let arcpy do its work

0 Kudos
PaulHuffman
Frequent Contributor

Now I see where I picked up uppercase W on Workspace.  I was looking at some old code examples from wayyyyyy back in 2010, back when everything started with creating a gp object.  Then it was gp.Workspace.   That's so six years ago.

DanPatterson_Retired
MVP Emeritus

I hope you are ready for the transition to Python 3.x and arcgis pro ...

PaulHuffman
Frequent Contributor

A strange thing is happening today. I found another folder with a few undefined projection tifs in it.  I changed my script so env.workspace = this new folder.   But the script never gets any hits on the if spatial_ref.name == "Unknown": line.  I stuck in a line around line 12 to print out my raster list and verified that I am getting the tifs into the list.  Then I moved some of the known undefined spatial reference tifs into a test folder, and ran the script on the test folder.  Same deal. Is there a kind of "undefined"  that the spatial_ref.name == "Unknown": can't detect?  In ArcCatalog, the properties of this tifs reveal that the Spatial Reference is <Undefined>

0 Kudos
DanPatterson_Retired
MVP Emeritus

your last two lines don't appear to be indented properly, correct and see what it does.  Also, add a print statement above the if statement and actually print out the the name.  make sure you str it so that it will catch None etc