19

I have a folder which contains a large number of files in GeoTIFF format.

I need to generate both PRJ and TFW files for each of these images.

Is there some way to do this?

1
  • 1
    using what software? What is available to you? Commented Nov 14, 2019 at 19:14

5 Answers 5

22

The listgeo utility that comes with libgeotiff is a nice command-line utility that can extract the TWF file from GeoTIFF files.

For example, I have a directory of GeoTIFFs, and I have libgeotiff installed as part of OSGeo4w. You can run the OSGeo4w shell, and do this:

$ listgeo -tfw BN24_GeoTif_1-01.tif
World file written to 'BN24_GeoTif_1-01.tfw'.

It would be nice if the same utility could also extract the PRJ file too.

1
  • "It would be nice if the same utility could also extract the PRJ file too." -> But it doesn't.
    – user99665
    Commented Jan 16, 2018 at 15:49
18

The slickest way to generate TFWs is to write a script in Python or Java using GDAL, which would be a handful of lines of code.

Creation of old-style (pre ArcGis 9) .prj files are not supported GDAL, only reading (see here). New-style (based on WKT) files are supported for creation, but it's not guaranteed they cover all cases. But either way, in a supreme case of displacement activity, I have written a Python script that does what you need. There's no error checking or anything, but it works for the directory of tiffs I had to hand, YMMV.

# Written by MerseyViking (mersey dot viking at gmail dot com), 2011.
# Released into the public domain - May 8, 2011
# I accept no responsibility for any errors or loss of data, revenue, or life this script may cause. Use at your own risk.

import osgeo.gdal as gdal
import osgeo.osr as osr
import os
import glob
import sys

def generate_tfw(path, gen_prj):
    for infile in glob.glob(os.path.join(path, '*.tif')):
        src = gdal.Open(infile)
        xform = src.GetGeoTransform()

        if gen_prj == 'prj':
            src_srs = osr.SpatialReference()
            src_srs.ImportFromWkt(src.GetProjection())
            src_srs.MorphToESRI()
            src_wkt = src_srs.ExportToWkt()

            prj = open(os.path.splitext(infile)[0] + '.prj', 'wt')
            prj.write(src_wkt)
            prj.close()

        src = None
        edit1=xform[0]+xform[1]/2
        edit2=xform[3]+xform[5]/2

        tfw = open(os.path.splitext(infile)[0] + '.tfw', 'wt')
        tfw.write("%0.8f\n" % xform[1])
        tfw.write("%0.8f\n" % xform[2])
        tfw.write("%0.8f\n" % xform[4])
        tfw.write("%0.8f\n" % xform[5])
        tfw.write("%0.8f\n" % edit1)
        tfw.write("%0.8f\n" % edit2)
        tfw.close()

if __name__ == '__main__':
    generate_tfw(sys.argv[1], sys.argv[2])

Call it from the command line like this:

python gen_tfw.py <path_to_tiff_directory> [prj]

The second parameter can be prj to generate WKT-style prj files, or anything else to just generate .TFWs.

If you can't use Python scripts for whatever reason, you can use:

gdal_translate -co "TFW=YES" in.tif out.tif

But that will copy the image data too, so you'll have to delete the original. And of course, it won't generate .prj files of either flavour. But presuming all your tiffs are in the same projection, you could just hand-craft a .prj file and duplicate it for all the source images.

3
  • 2
    Note that generate_tfw does not work properly on rotated rasters, which fortunately are less common. This can be fixed with a bit of matrix multiplication.
    – Mike T
    Commented Feb 28, 2015 at 23:27
  • Hey man, i'm trying to use the tool, but i dont know how to do, exaclty. Can you please make more detailed explanation how to use it? I just need to generate the tfw for the TIFs, in folders and subfolders. If it cant run on subfolders, thats fine. I can run it several times. Thanks
    – raphael
    Commented May 5, 2017 at 19:00
  • In QGIS open menu "Raster", choose "Conversion" then choose "Translate - Convert format". Window appear - in upper line choose your geotiff and under Advanced parameters, hit + sign to add command. Under Name enter word tfw, under Value enter yes. Specify output destination, name of the file and voila ... your geotiff file is converted to plain tiff and tfw part. Now you can edit tiff in image editor of your choice and has it geolocated as long as you don't change number of pixels in x or y axis
    – user29347
    Commented Nov 13, 2019 at 8:29
2

If one wants further edit tiff in photo retouching app and retain it's geolocation, Export of raster layer with tfw creation and Rendered image enabled is the easiest way, IMO. For slightly different options there is a way through menu Raster / Conversion / Translate.

enter image description here

0

Not really - if you know the projection of the files then you can look up the content of the prj file at http://spatialreference.org and then use a shell script to copy a template to each a .prj file for each image.

Georeferencing them will need to be done on a per image basis as the .tfw file will be different for each image (unless they are all of the same place). http://warper.geothings.net/ may be the way to go if you don't have access to a desktop GIS to do this in.

4
  • 2
    Except that GeoTIFF files often have both a projection and world file embedded in the metadata .. so all this information is known.
    – Mike T
    Commented May 8, 2011 at 20:42
  • 1
    in which case why would you write it out as separate files?
    – Ian Turton
    Commented May 8, 2011 at 21:02
  • 1
    Presumably the software Joe is using doesn't understand GeoTIFFs with embedded metadata. Commented May 9, 2011 at 8:17
  • yes had the same issue metadata needed extracted to be .tfw for the gis web app to 'know' the locations of each tile - use own script to extracted it (excel - macro at the time.)
    – Mapperz
    Commented May 9, 2011 at 14:06
0

Using Geotools in Java you can use this code:

// read geotiff file (using org.geotools.gce.geotiff.GeoTiffReader)
GeoTiffReader reader = new GeoTiffReader(geotiff);
// get transformation
AffineTransform transformation = reader.getMetadata().getModelTransformation();
// create org.geotools.data.WorldFileWriter (world file is created automatically!)
WorldFileWriter worldFileWriter = new WorldFileWriter(myWorldFile, transformation);

If you want to get the projection you can use this code:

// get the projection string from CRS
CoordinateReferenceSystem crs = reader.getCoordinateReferenceSystem();
String projectionString = crs.toWKT();

Just write the content of projectionString to your projection file.

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.