World raster file (tfw) to MAPINFO Raster TAB file conversion

Reference pint of raster is upper left corner.

There are two ways in which an image can know its geographic coordinates. The first option is to imbed special tags in a tif file with the GeoTIFF format. The second option is to have a world file, a 6-line ASCII file file such as

                   2.4384
                   0.0000
                   0.0000
                  -2.4384
              441794.4342
             5094101.4520

These are the 6 coefficients of a three-by-three transformation matrix. They indicate that a pixel is 2.4384 meters square, and that the center of the upper left pixel has an easting (x coordinate) of 441794.4342 and a northing of 5094101.4520. For fans of the matrix:

 
x = 2.4384 * column + 0.0 * row + 441794.4342
y = (-2.4384) * row + 0.0 * column + 5094101.4520

 

The second and third values are zero because there is no rotation or skew.

The convention is that a world file is named by jamming a w into the extension. For example, o47122d3.jpg might have a o47122d3.jgw file and o47122d3.tif might have a o47122d3.tfw file. Most of the tif files on the site are geotiff files with redundant tfw files.

TFW file example

50.00000000000000

0.00000000000000

0.00000000000000

-50.00000000000000

5360500.0

5157690.0

 

 

MapInfo Raster TAB file example

 

!table

!version 300

!charset WindowsLatin2

 

Definition Table

  File "clprh50g1.gif"

  Type "RASTER"

  (5360500,5157690) (0,0) Label "Pt 1",

  (5360550,5157690) (1,0) Label "Pt 2",

  (5360500,5157640) (0,1) Label "Pt 3"

  CoordSys Earth Projection 8, 1000, "m", 15, 0, 1, 5500000, 0

  Units "m"

 

 


Conversion calculation

 

TFW input

 

dx = 50.00000000000000

ro = 0.00000000000000

sk = 0.00000000000000

dy = -50.00000000000000

cx=5360500.0

cy=5157690.0

 

TAB output

 

raster image point 1 (pixel 0,0)

col1=0

row1=0

xp1=cx+col1*dx=5360500.0+0*50=5360500.0+0=5360500.0

yp1=cy+row1*dy=5157690.0+0*(-50)=5157690.0+0= 5157690.0

 

raster image point 2 (pixel 1,0)

col2=1

row2=0

xp2=cx+col2*dx=5360500.0+1*50=5360500.0+50=5360550.0

yp2=cy+row2*dy=5157690.0+0*(-50)=5157690.0+0= 5157690.0

 

raster image point 3 (pixel 0,1)

col3=0

row3=1

xp3=cx+col3*dx=5360500.0+0*50=5360500.0+0=5360500.0

yp3=cy+row3*dy=5157690.0+1*(-50)=5157690.0+50= 5157740.0

 

 

Map Basic Code (or download tfwread3.mbx here)

 

'* TFWJGWRead.MB

'*    A raster registration tool for opening raster images using .TFW and .jgw files

'*

'*************************************************************************

'* changed by me!"#, Zagreb, 08-11-2005 to suit MI 6.5

'*************************************************************************

'Include statements

'*************************************************************************

include "mapbasic.def"

dim sTFWFileName, sFileExtens as string

dim bjworld as logical

sTFWFileName = FileOpenDlg(ApplicationDirectory$() ,  "" ,  "*.*w" , 

"Select the TFW/JGW file needed to register image:" )

if ""=sTFWFileName then

  end program

end if

open file sTFWFileName for input as #1

dim f_dX1, f_dY1, f_dX2, f_dY2, f_X0, f_Y0, f_X1, f_Y1, f_X2, f_Y2 as float

input #1, f_dX1

input #1, f_dY1

input #1, f_dX2

input #1, f_dY2

input #1, f_X0

input #1, f_Y0

f_X1 = f_X0 + f_dX1

f_Y1 = f_Y0 + f_dY1

f_X2 = f_X0 + f_dX2

f_Y2 = f_Y0 + f_dY2

 

dim sBaseFileName as string

sBaseFileName = PathToFilename$(sTFWFileName)

sFileExtens = lcase$(right$(stfwfilename, 3))

do case sFileExtens

case "jgw"

bJworld = TRUE

case "tfw"

bjworld = False

case else

note "File is not recognised as a TFW/JGW type"

end program

end case

sBaseFileName = left$(sBaseFileName,len(sBaseFileName)-4)  'strip off the extension

dim sTabFileName as string

sTabFileName = PathToDirectory$(sTFWFileName) + sBaseFileName + ".TAB"

dim bContinue as logical

if FileExists(sTabFileName) then

  bContinue = Ask("Overwrite the existing " + sTabFileName + "?", "OK",

"Cancel")

  if not bContinue then

    end program

  end if

end if

Dim strCSys as string

strCSys = ChooseProjection$("", True)

open file sTabFileName for output as #2

print #2, "!table"

print #2, "!version 300"

print #2, "!charset WindowsLatin1"

print #2, ""

print #2, "Definition Table"

if bjworld then

print #2, "  File """ + sBaseFileName + ".jpg"""

else

print #2, "  File """ + sBaseFileName + ".tif"""

end if

print #2, "  Type ""RASTER"""

'careful with formats - watch out for comas and fullstops - i removed everything after a decimal point - even a decimal point

print #2, "  (" + format$(f_X0,"#") + "," + format$(f_Y0,"#") + ") (0,0) Label ""Pt 1"","

print #2, "  (" + format$(f_X1,"#") + "," + format$(f_Y1,"#") + ") (1,0) Label ""Pt 2"","

print #2, "  (" + format$(f_X2,"#") + "," + format$(f_Y2,"#") + ") (0,1) Label ""Pt 3"""

print #2, "  " + strCSys

close file #2

open table sTabFileName

map from TableInfo(0,TAB_INFO_NAME)