Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a huge set of compressed GeoTIFF files. Someone forgot to set the projection while creating the GeoTIFF files and I would like to add it now.

If I use

gdalwarp -t_srs source.tif target.tif

the result is an uncompressed file. I would like to avoid the (de)compressing if possible, since I expect it will degrade the final result due to the recompression and because it will require more time to decompress and compress each file again.

I assume that the projection information is stored in the header.

Is there an easy way to just update the projection information in a GeoTIFF file?

share|improve this question
    
ok..didn't get your question first time..removing ans.. –  vinayan Jun 6 '12 at 9:56
add comment

2 Answers 2

up vote 8 down vote accepted

Make a tiny Python script named "UpdateSRS.py":

import sys
from glob import glob
from osgeo import gdal, osr

# Define your projection here
sr = osr.SpatialReference()
# For example, UTM Zone 11
sr.SetUTM(11)
sr_wkt = sr.ExportToWkt()

file_list = set()
for arg in sys.argv[1:]:
    file_list.update(glob(arg))
for file in file_list:
    ds = gdal.Open(file, gdal.GA_Update)
    if ds:
        print('Updating projection for ' + file)
        res = ds.SetProjection(sr_wkt)
        if res != 0:
            print('Setting projection failed ' + str(res))
        ds = None # save, close
    else:
        print('Could not open with GDAL: ' + file)

Run it from a command prompt with arguments, something like this:

python UpdateSRS.py *.tif

You will need to update the logic to generate sr_wkt. It could be as simple as just pasting the WKT for the projection, if you know it.

share|improve this answer
    
Just was I was looking for. I wasn't sure if GDAL through Python would allow the projection to be updated without changing the image part of the file, but it does :) Thanks a lot for your help and answer! –  Chau Jun 6 '12 at 11:54
add comment

You can also use .prj files with projection info. In this way you can avoid any modification of source tiffs and this is the fastest way for large amount of data I believe.

PS using .prj file outside Esri software can be tricky and is not supported everywhere. So, check if it is supported in the software where you are planning to use your rasters.

share|improve this answer
1  
For GeoTIFF, this is a hack, which might work for Esri software. Using the GTiff GDAL driver, a .prj file is ignored, since this metadata needs to be in the header. –  Mike T Jun 6 '12 at 11:18
    
Oh, thats a useful info! Have not realized that it is not fully supported in GDAL. There is some way to use it by specifying in -s_srs parameter for some tools but anyway I agree - it is quite unreliable outside Esri world. I have updated my answer, thanks! –  Alex Markov Jun 6 '12 at 11:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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