# Created by Leo from: C:\Python23\Tom\leo\pyro.leo # << demmod declarations >> import struct,string import Image # -- end -- << demmod declarations >> # << demmod methods >> class demdata: # << class demdata methods >> (1 of 2) def __init__(self): self.tom = open('c:\\python23\demdata\W140N40.DEM','rb') #gif = open('c:\\python23\demdata\W140N40.GIF','rb') bmp = Image.open('c:\\python23\demdata\W140N40.GIF').convert('RGB') self.imgtxt = bmp.tostring() self.imgsize = (bmp.size[0], bmp.size[1]) temp_file = open('c:\\python23\demdata\W140N40.HDR','r') temp_file.readline() temp_file.readline() self.N_ROWS = int(string.split(temp_file.readline())[1]) self.N_COLS = int(string.split(temp_file.readline())[1]) temp_file.readline() temp_file.readline() temp_file.readline() temp_file.readline() temp_file.readline() temp_file.readline() self.long_num = float(string.split(temp_file.readline())[1]) #self.start_long = long_num self.lat_num = float(string.split(temp_file.readline())[1]) #self.start_lat = lat_num self.x_scale = float(string.split(temp_file.readline())[1]) self.y_scale = float(string.split(temp_file.readline())[1]) #Latitude_Minimum = lat_num - (N_ROWS * y_scale) #Latitude_Maximum = lat_num + y_scale #Longitude_Minimum = long_num - x_scale #Longitude_Maximum = long_num + (N_COLS * x_scale) temp_file.close() # << class demdata methods >> (2 of 2) def TD(self,coords): # where Everything is happening #hi_num = 0# there?s a master list of elevation for each tile of the DEM data #lo_num = 4328# so I could use some kind of look up table but this way is just as easy new_line = [] data_char = '' data_line_num = (coords[2] * 16) # width of box 8 to 1 data to gif ratio 16 bit variable in a string for x in range((coords[1] * 8),(coords[1] + coords[3]) * 8): # iterate over height data_start = (x * 9600) + (coords[0] * 16) # find start position of rectangle in file (the number 9600 bytes per line # should probably be in a variable) self.tom.seek(data_start)# move to proper position in file data_char = self.tom.read(data_line_num)# read proper number of bytes for i in range(0, len(data_char) - 1, 2):# iterate over the characters by 2s # its a 16 bit signed integer which means that there has got to be # a more elegant way of doing this but I was lazy data_num = struct.unpack('>h',data_char[i:i+2])[0] #data_num = (ord(data_char[i]) << 8) | ord(data_char[i+1]) #if data_num == 55537:#sea level # data_num = 0 #if data_num >= 32767: # data_num = -(data_num ^ 65535) if data_num == -9999:#sea level data_num = 0 new_line.append(data_num) return new_line # -- end -- << class demdata methods >> # -- end -- << demmod methods >>