Find and fix images ith Spatial Reference Undefined.

4876
19
Jump to solution
01-14-2016 02:02 PM
PaulHuffman
Occasional Contributor III

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
19 Replies
PaulHuffman
Occasional Contributor III

http://www.vidarholen.net/contents/interjections/ , indentation wasn't as important in VB, or Avenue, or AML, or CPL.  But here, changing the indentation of the last two lines from two to a tab of four spaces didn't change the result.  Still don't detect spatial reference == "unknown"  and hit the defineProjection and the print raster + " is defined".

I had a hard time figuring out the print statement, but finally discovered it was simpler than I thought.  Now I get a print out of spatial_ref string but I don't know what it means.  In this test folder,  ANDERSON LAKE.tif is defined as Washington State Plane S., while the rest are undefined.

for raster in rasters:
    # Create the spatial reference object
    spatial_ref = arcpy.Describe(raster).spatialReference
    #print "\n".join(raster).join(str(spatial_ref))
    #print "\n".join(raster),str(spatial_ref)
    #print "\n".join(raster)
    #print "\n".join(str(spatial_ref))
    #print (str(spatial_ref))
    print raster + " " + (str(spatial_ref))
    # If the spatial reference is unknown
    if spatial_ref.name == "Unknown":
        arcpy.DefineProjection_management(raster, coord_sys)  
        print raster + " is defined."  

ANDERSON LAKE.tif <geoprocessing spatial reference object object at 0x113B3A88>
INDIAN ROCK.tif <geoprocessing spatial reference object object at 0x113B3AA0>
JENNIES BUTTE.tif <geoprocessing spatial reference object object at 0x113B3908>
JUNGLE BUTTE.tif <geoprocessing spatial reference object object at 0x113B3A88>
0 Kudos
DanPatterson_Retired
MVP Emeritus

Interjection #4.  The result object in the print statement (line 9) is good, because it means that you obtained the spatial reference object.

SpatialReference—Help | ArcGIS for Desktop

From there, you can now navigate through and get its properties, like whether it if a geographic or projected coordinate system, its POSC code, its name and the like.

PS

use spaces not tabs, 4 is generally the rule, but Guido says there are no rules, print has changed in python 3, get used to it justt put everything in brackets and learn the python mini-formatting language)

PaulHuffman
Occasional Contributor III

Now I'm getting it. The print formatting stuff and the spatialReference object.  From spatialReference.name, I can see that the problem rasters are set to Lambert_Conformal_Conic_2SP,  but ArcDesktop thinks they are <Undefined>. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

Now do those files have a separate world file or is the coordinate system built-in? (assuming you are still talking about tiffs.... unfortunately tiff's are under the t's in this link...

Supported raster dataset file formats—Help | ArcGIS for Desktop

0 Kudos
PaulHuffman
Occasional Contributor III

The Stateplane and the undefined ones all have tfw files that contain  6 similar lines of numbers. The top and right numbers look like StatePlane,  and they draw exactly where they are supposed to on a Wash. State Plane Data Frame,  so I could just stop here and define the coordinate system of everything in the folder as State Plane.

I tried the trick in ArcCatalog to change the raster options to read the world file, but ArcCatalog still says they are undefined.

0 Kudos
PaulHuffman
Occasional Contributor III

Those Lambert_Conformal_Conic_2SP rasters stay stubbornly undefined.  I modified my script to skip the if test for spacial ref unknown, and tried to just define the projection for everything in the folder:

for raster in rasters:
    # Create the spatial reference object
    spatial_ref = arcpy.Describe(raster).spatialReference
    print("{0} : {1}".format(raster, spatial_ref.name))
    # If the spatial reference is unknown
    #if spatial_ref.name == "Unknown":
    arcpy.DefineProjection_management(raster, coord_sys)  
    print raster + " is defined."  

This runs, hits both print statements, the first print statement says the spatial ref is State_Plane but ArcCatalog still says they are undefined.  In this test folder, only Anderson Lake is known to ArcCatalog as defined State Plane.

ANDERSON LAKE.tif : NAD_1983_StatePlane_Washington_South_FIPS_4602_Feet
ANDERSON LAKE.tif is defined.
INDIAN ROCK.tif : NAD_1983_StatePlane_Washington_South_FIPS_4602_Feet
INDIAN ROCK.tif is defined.
JENNIES BUTTE.tif : NAD_1983_StatePlane_Washington_South_FIPS_4602_Feet
JENNIES BUTTE.tif is defined.
JUNGLE BUTTE.tif : NAD_1983_StatePlane_Washington_South_FIPS_4602_Feet
JUNGLE BUTTE.tif is defined.
0 Kudos
DanPatterson_Retired
MVP Emeritus

You do refresh ArcCatalog between runs?

0 Kudos
PaulHuffman
Occasional Contributor III

I've tried refreshing ArcCatalog, restarting ArcCatalog, restarting the PC, and changing the ArcCatalog options to "Use world file" and back.  State plane is a kind of Lambert_Conformal_Conic_2SP, so .spatialReference is getting part of the idea, but ArcCatalog is not. The bad tifs have a world file that is similar to the good tifs.  Maybe I can attach one here.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I am going to flag the expert on projections to help with this Melita Kennedy​ could you have a browse through this post and see if you can make any suggestions

0 Kudos
PaulHuffman
Occasional Contributor III

ANDERSON LAKE is a good tif,  INDIAN ROCK is a bad tif.

0 Kudos