0

I have conducted some image manipulation on GeoTIFFs in a program and the resulting TIFFs now have no spatial information attached. Their x,y origins are (0,0). Since these processed images are of the same exact area as the original images, can I simply copy the metadata from the original images to the processed images?

I have seen some gdal solutions, but I'm looking for a solution in Arcpy.

1

For the sake of explanation let's call your original geotiff source.tif and the manipulated version target.tif. If you use the tool arcpy.management.ExportRasterWorldFile in conjunction with source.tif it will create a world file called source.tfw that contains the tiff's georeferencing information. Assuming that target.tif has the same pixel size, extent and projection as the source version you can rename the world file to target.tfw and place it in the same directory as the target tiff and applications such as ArcMap, gdal, etc. will then implicitly know to obtain spatial reference information for the target raster from the world file.

However, world files don't contain the projection of the raster and that must be defined in separate step, which can be done with arcpy.management.DefineProjection. The script below executes both of these processes. To adapt the code to your project set the variable source_tif to the raster that has spatial reference information defined and set target_folder to a folder containing the tiffs that need their spatial reference defined. Note that any file with a .tif extension in the target folder will be modified by the script:

import os
import re
import arcpy
import shutil
from os import path
from arcpy import management

source_tif = 'P:/ath/to/src/geotiff/src_raster.tif'
target_folder = 'P:/ath/to/target/folder'

def transferRasterSpatialInfo():
    """Transfer spatial reference information from a a source raster to a set of
    other rasters that have the same spatial reference values, but for which that
    information is not defined"""

    # create world file from source raster
    management.ExportRasterWorldFile(source_tif)
    src_world_file = re.sub('\.tif$', '.tfw', source_tif)

    # get source raster projection
    src_tiff_srs = arcpy.Describe(source_tif).spatialReference

    for tiff in os.listdir(target_folder):
        if re.match('.+\.tif$', tiff):
            # rename and copy world file to target tiffs
            world_file_name = re.sub('\.tif$', '.tfw', tiff)
            new_world_file = path.join(target_folder, world_file_name)
            shutil.copy(src_world_file, new_world_file)


            # define projection for target tiffs
            tiff_path = path.join(target_folder, tiff)
            management.DefineProjection(tiff_path, src_tiff_srs)

transferRasterSpatialInfo()
3
  • Works great, thanks Grant! I had to import arcpy and add 'r' before the image path strings to get it working. – jm90 Aug 30 '15 at 8:44
  • Glad it worked for you and I added import arcpy to the code block. The file paths should work as shown if you use forward slashes (E:/xample/file/path), but you're right that raw string with back slashes works as well (r'E:\xample\file\path') – Grant Humphries Aug 30 '15 at 17:11
  • Completely missed you had forward slashes there - my fault. Thanks again. – jm90 Aug 30 '15 at 22:34

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