8

I have a few digital images that are not georeferenced. E.g.:

Img01.tif  
Img02.tif  
...
Img99.tif  

On the other hand, I have a text file, which contains georeferencing information for these images. E.g., I have a file c:\tfws\list.txt which contains the following information:

Img01.tfw 0.25 0 0 - 0.25 456258,125 4569852,125  
Img02.tfw 0.25 0 0 - 0.25 456586,125 4570001,125   
Img03.tfw 0.25 0 0 - 0.25 456952,125 4570300,125  
...
Img99.tfw 0.25 0 0 - 0.25 458412,125 4575123,125  

I need to generate a TFW file for each image in the text file. E.g. create a file named C:\tfws\img??.tfw containing the following information:

0.25  
0  
0  
-0.25  
456258,125  
4569852,125  

Does anyone know of a tool able to generate these TFW files automatically?

6
  • 2
    Welcome to GIS-SE! I'm having trouble understanding your question. Would you be able to provide more details, please? Are you using any GIS software already?
    – PolyGeo
    Commented May 16, 2013 at 22:49
  • It might help if you can show a sample of the data that you would like to convert to TFW file. Also, it might help if you tell us what you've already tried, what worked for you and what didn't. You can just edit your question (click edit below the question) to add these very useful details.
    – BradHards
    Commented May 17, 2013 at 0:31
  • 1
    i think this is just a programming question and not within the scope of GISSE..
    – vinayan
    Commented May 23, 2013 at 2:37
  • 2
    I think it's borderline because some knowledge of the world file format is required and I can imagine other GIS users running into something similar, even if the solution in this case is simple text manipulation.
    – blah238
    Commented May 23, 2013 at 3:59
  • 3
    I think question is relevant for gis.se. There's a bad gotcha here - the question is tagged with geotiff, but asks about tfw files. Geotiff embeds registration into the tiff file itself. When there's a conflict between embedded registration and the registration in the tfw file, strange things can happen. Commented May 23, 2013 at 15:42

2 Answers 2

6

It isn't nice code, but for your specific circumstance, here is some python that should save typing them out by hand:

f = open('list.txt', 'r')
lines = f.readlines()
for line in lines:
    lineparts = line.split(' ')
    outfile = open(lineparts[0], 'w')
    for i in range(1, 4):
        outfile.write(lineparts[i] + '\n')
    outfile.write(lineparts[4] + lineparts[5] + '\n')
    for i in range(6, 8):
        outfile.write(lineparts[i] + '\n')

(real code would sanity check, but I assume you can check the results)

8
  • (1) Did you notice the whitespace between the leading minus signs and the values: - 0.25? It would be bad to split those with a newline! (2) It seems you just write out to a single file which includes the intended names of the world files. What's needed is output to the worldfiles separately.
    – whuber
    Commented May 23, 2013 at 12:54
  • 1
    (1) I did notice, but somehow read the output as requiring a line with a - on it. Tried again. (2) It outputs separate files (since outfile gets open()ed inside the lines loop) - I did test this.
    – BradHards
    Commented May 23, 2013 at 14:06
  • Good--I didn't catch that. I was thrown off by the outputting of eight fields; I had mistakenly thought one of them was the file name. How do you get eight fields in addition to the file name? I find only seven. Did you actually open one of the output files to check your code?
    – whuber
    Commented May 23, 2013 at 14:43
  • There are 8 fields - the name, and 7 values. range(6,8) produces [6, 7]. range(1,4) produces [1,2,3].
    – BradHards
    Commented May 23, 2013 at 20:30
  • Thanks--I was not aware of that strange definition of range, but in retrospect it's clear from the details of your code so I should have been able to figure that out. Incidentally, don't your inner for-loop bodies need some more indentation?
    – whuber
    Commented May 28, 2013 at 16:44
4

AWK is perhaps the most effective tool for such text conversion (although if you're familiar with Perl you might prefer it, out of habit): it was designed for exactly this kind of one-off quick reformatting work.

Here is the full AWK code to perform the requested operation on the input specified:

BEGIN {OFS="\n"}
{print $2, $3, $4, $5 $6, $7, $8 > $1}

(The absence of a comma between $5 and $6 is no misprint: it concatenates the minus sign - with the following value 0.25.)

This can be typed directly into AWK, but it's better to put it into a file called, say, tfw.awk. It would then be invoked from a command line prompt like

tfw list.txt

or

awk -f tfw.awk list.txt

(if the system environment has not been set up to recognize .awk files as executable).

It will execute instantaneously, leaving behind the specified files (and overwriting them if they already exist).

Every Unix system comes with a copy of AWK and there is a free GNU version for Windows. I just downloaded the GNU distribution and ran it on a Windows 64 workstation; it works fine.

1
  • Sorry that not has been able to review it before, I have enough work. Thank you very much, works perfectly.
    – Rodericus
    Commented Jun 24, 2013 at 11:35

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.