Page 1 of 1

reading binary files - for SRTM-hgt files

Posted: Fri Jun 23, 2006 2:29 am
by wildcolour
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi

I am very new to programing and trying to teach myself - learning a few things, but this has me stuck.

Nasa have produced a file type called hgt.  it is a matix of two-byte integers (of about 1000 by 1000 bytes)

"Height files have the extension .HGT and are signed two byte integers.
The bytes are in Motorola "big-endian" order with the most significant byte
first, directly readable by systems such as Sun SPARC, Silicon Graphics and
Macintosh. DEC Alpha and most PCs use Intel ("little-endian") order so some
byte-swapping may be necessary."

I would like to be able to enter a location of the file (eg row number 5 column number 6(and 7) and return a numeric value.  The finding of the location I think I can work out.

My problem is that once I find the byte I want to do some math, but the things I have tried always return a zero value.

and here is my code that does not do what I want it to.

http://www.wildwalks.com/hgt/hgt.php

Code: Select all

<?php
// get contents of a file into a string
$location = 0;
$filename = "S34E150.hgt";
$handle = fopen($filename, "r");

//move to a particular column
$rubbish = fread($handle, $location);

// get 2 bytes
$byte1 = fread($handle, 1);
$byte2 = fread($handle, 1);

// tell me what you found
echo "hello <br/>";
echo $byte1 . " byte1<br/>";
echo $byte2 . " byte2<br/>";

// try this bindec function and see what it does
$byte1dec = bindec($byte1);
$byte2dec = bindec($byte2);
echo $byte1dec . " byte1 dec<br/>";
echo $byte2dec . " byte2 dec<br/>";

//try this unpack function and see what it does
$unpack1 = unpack("i",$byte1);
echo $unpack1[0] . " byte1 unpacked<br/>";

// if the byte to dec worked than this would give me the height value.
echo ($byte1dec*256 + $byte2dec) ;
fclose($handle);
?>
Thanks :)


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]