I am trying to calculate a world file for an image given the following information.

  • Pixel size
  • Rotation around top-left
  • Top-left corner

The calculation is as follows:

  var world_file =
    { A: scale[0]
    , D: -(Math.tan(source.getRotation()) * scale[0])
    , B: -(Math.tan(source.getRotation()) * scale[1])
    , E: -scale[1]
    , C: tl[0]
    , F: tl[1]

There is a problem when the image is rotated nearly precisely 90 degrees. The value of Math.tan tends to infinity as the argument tends to pi/2. This makes the B and D values way too big.

For example when

scale = [0.9267397812797137, 0.9267397812797137]
source.getRotation = 1.5762954120038497 (nearly precisely pi/2 = 90 degrees)
tl = [..] (irrelevant)

both the D and B parameter get very large incorrect values.

However, I don't really see how to fix this as it seems an inevitable part of using a world file. Do my images have to already approximately rotated before positioned precisely with the world file?

  • Are you sure about your formulas? Rotation should also change the A and E scales? – user30184 Nov 11 '18 at 22:18
  • Yes, they work correctly for all other cases (about 3000) that have been processed in this manner. – user154228 Nov 11 '18 at 22:27
  • So if you have an image with pixel size of (1,1) and you rotate 45 degrees clockwise you get A, B, C, and D as 0.707106781, -0.707106781, -0.707106781, -0.707106781, respectively? – user30184 Nov 12 '18 at 6:56
  • Have you resolved your problem or do you want more help? – user30184 Nov 14 '18 at 7:26
  • No I haven't resolved it. I think I was wrong to say pixel size as the calculation is correct for cases where the rotation is not nearly 90 degrees. – user154228 Nov 14 '18 at 8:30

A rotated world file https://en.wikipedia.org/wiki/World_file can be created by the following formulae.


pixel width                                         pixX
pixel heigth                                        pixY
rotation angle in degrees (clockwise=negetive)      d
x of the top left corner of the top left pixel)     X
y of the top left corner of the top left pixel)     Y


row 1   pixX * Cos(d)
row 2   pixX * Sin(d)
row 3   pixY * Sin(d)
row 4  -pixY * Cos(d)
row 5   X
row 6   Y

You can certainly use other formalae if they yield the same result. As an example a world file


when rotated by exactly 90 degrees clockwise will come as

  • This doesn't answer the question at all sorry. The calculation is correct already, the problem is what to do when the rotation is 90 degrees. – user154228 Nov 15 '18 at 10:49
  • How I understand your equation is that the A and E components remain the same even if you rotate the image. Is that true? – user30184 Nov 15 '18 at 17:49
  • Yes. That's correct as given by the pseudo-code in the original question. – user154228 Nov 16 '18 at 22:12
  • But that makes invalid world files. Rotation splits the offset vectors into two components but pixels do not get any bigger. When you start to rotate the image the original pixX and pixY terms must get smaller. At 90 and 270 degrees they are both zero. – user30184 Nov 17 '18 at 9:58
  • Are you still struggling with this? – user30184 Nov 19 '18 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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