Skip to content

Instantly share code, notes, and snippets.

@greencoder
Last active August 22, 2021 21:35
Show Gist options
  • Save greencoder/89feb44742d0fd34fa24 to your computer and use it in GitHub Desktop.
Save greencoder/89feb44742d0fd34fa24 to your computer and use it in GitHub Desktop.
Create an extent for a NOAA radar image using the world file
import io
import PIL.Image
import urllib2
import sys
world_file_url = 'http://radar.weather.gov/ridge/Conus/RadarImg/northeast_radaronly.gfw'
radar_file_url = 'http://radar.weather.gov/ridge/Conus/RadarImg/northeast_radaronly.gif'
# Parse the radar image to find the height and width
radar_file = urllib2.urlopen(radar_file_url)
radar_image_file = io.BytesIO(radar_file.read())
image = PIL.Image.open(radar_image_file)
img_width = image.width
img_height = image.height
# Parse the world file
world_file_contents = urllib2.urlopen(world_file_url).read()
parts = world_file_contents.split('\n')
lng_pixel_width = float(parts[0])
rotation_x = float(parts[1])
rotation_y = float(parts[2])
lat_pixel_width = float(parts[3])
lng_upper_left = float(parts[4])
lat_upper_left = float(parts[5])
# Calculate the extent
west = lng_upper_left
north = lat_upper_left
east = west + (img_width * lng_pixel_width)
south = north + (img_height * lat_pixel_width)
print 'Image Height: %d' % img_height
print 'Image Width: %d' % img_width
print 'West: %s' % west
print 'South: %s' % south
print 'East: %s' % east
print 'North: %s' % north
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment