[GIS] Create a World File from bounding box

georeferencingpythonrasterworld-file

What is the most pythonic way to create a Word File for a .png image, having only it's bounding box?

For example, given the following bounding box:

<LatLonBox>
  <north>30.46370000000000</north>
  <south>25.15370000000000</south>
  <east>89.06550000000000</east>
  <west>83.06550000000000</west>
</LatLonBox>

the result should be a text file reporting, in order:

  1. pixel size in the x-direction
  2. rotation about y-axis
  3. rotation about x-axis
  4. pixel size in the y-direction
  5. x-coordinate of the center of the upper left pixel
  6. y-coordinate of the center of the upper left pixel

Best Answer

Assuming a zero-based rotation factor for x and y, here you need the png file image dimensions in order to derive x and y pixel size.

Rudimentary example:

import xml.etree.ElementTree as etree

image_width = 500
image_height = 300
x_rotation = 0.0
y_rotation = 0.0

bbox_xml = etree.fromstring('''<LatLonBox>
<north>30.46370000000000</north>
<south>25.15370000000000</south>
<east>89.06550000000000</east>
<west>83.06550000000000</west>
</LatLonBox>''')

minx = float(bbox_xml.find('west').text)
miny = float(bbox_xml.find('south').text)
maxx = float(bbox_xml.find('east').text)
maxy = float(bbox_xml.find('north').text)

x_pixel_size = (maxx - minx) / image_width
y_pixel_size = ((maxy - miny) / image_height) * -1

print(x_pixel_size)
print(y_rotation)
print(x_rotation)
print(y_pixel_size)
print(minx)
print(miny)