Skip to content

Commit

Permalink
Fix #41: SRTM downloader not working
Browse files Browse the repository at this point in the history
  • Loading branch information
sque committed Apr 4, 2014
1 parent ae0e0f9 commit 033bd9f
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 65 deletions.
2 changes: 1 addition & 1 deletion globals/classes/geoimage.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function plotlink($width, $height, $point_a, $point_b, $antenna_a=0, $antenna_b=
$step_lat = ($point_b->lat - $point_a->lat) / ($width - 1);
$step_log = ($point_b->lon - $point_a->lon) / ($width - 1);
for ($i=0;$i<$width;$i++) {
$elevations[$i] = $srtm->get_elevation($point_a->lat + $step_lat * $i, $point_a->lon + $step_log * $i, FALSE);
$elevations[$i] = $srtm->get_elevation(new LatLon($point_a->lat + $step_lat * $i, $point_a->lon + $step_log * $i), FALSE);
if ($point_a->lat == '' || $point_a->lon == '' || $point_b->lat == '' || $point_b->lon == '' || $elevations[$i] === FALSE) {
imagestring ($image, 5, 10, 10, "Data error!", $black);
return $image;
Expand Down
126 changes: 100 additions & 26 deletions globals/classes/srtm.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,86 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


/**
* Class to handle SRTM Data
* @brief A Class for managing latitude and longtitude coordinates
*/
class LatLon {

/**
* @brief The latitude in decimal format
* @param float $lat
*/
public $lat;

/**
* @brief The longtitude in decimal format
* @param float $lot
*/
public $lon;

/**
* @brief Construct a new coordinates object
* @param float $lat
* @param float $lon
*/
public function __construct($lat, $lon) {
$this->lat = $lat;
$this->lon = $lon;
}

/**
* @brief Normalize coordinates
*/
public function normalize() {
if ($this->lon > 180)
$this->lon -= 360;
}

/**
* @brief Check if this coordinates coorespond souther
* of the equator
* @return boolean
*/
public function is_south() {
$this->normalize();
return ($this->lat <= 0);
}

/**
* @brief Check if this coordinates coorespond northen
* of the equator
* @return boolean
*/
public function is_north() {
$this->normalize();
return ($this->lat > 0);
}

/**
* @brief Check if this coordinates coorespond eastward
* of the Prime Meridian
* @return boolean
*/
public function is_east() {
$this->normalize();
return ($this->lon >= 0);
}

/**
* @brief Check if this coordinates coorespond westward
* of the Prime Meridian
* @return boolean
*/
public function is_west() {
$this->normalize();
return ($this->lon < 0);
}

}

/**
* @brief Class to handle SRTM Data
*/
class srtm {

Expand All @@ -28,45 +106,41 @@ function __construct($data_path='') {
$this->data_path = $data_path;
}

public static function get_filename($lat, $lon) {
if ($lat < 0) {
public static function get_filename(LatLon $latlon) {
if ($latlon->is_south()) {
$lat_dir = 'S';
$lat_adj = 1;
} else {
$lat_dir = 'N';
$lat_adj = 0;
}
if ($lon < 0) {
if ($latlon->is_west()) {
$lon_dir = 'W';
$lon_adj = 1;
} else {
$lon_dir = 'E';
$lon_adj = 0;
}

return $lat_dir . sprintf("%02.0f", (integer)($lat+$lat_adj)).$lon_dir.sprintf("%03.0f", (integer)($lon+$lon_adj)).'.hgt';
return $lat_dir
. sprintf("%02d", (integer)(abs($latlon->lat)+$lat_adj))
. $lon_dir
. sprintf("%03d", (integer)(abs($latlon->lon)+$lon_adj)).'.hgt';
}

function get_elevation($lat, $lon, $round=TRUE) {
if ($lat < 0) {
$lat_dir = 'S';
$lat_adj = 1;
} else {
$lat_dir = 'N';
$lat_adj = 0;
}
if ($lon < 0) {
$lon_dir = 'W';
$lon_adj = 1;
} else {
$lon_dir = 'E';
$lon_adj = 0;
}
$y = $lat;
$x = $lon;
function get_elevation(LatLon $latlon, $round=TRUE) {

$lat_adj = (integer)$latlon->is_south();
$lon_adj = (integer)$latlon->is_west();

$filename = $this->data_path.$lat_dir.sprintf("%02.0f", (integer)($lat+$lat_adj)).$lon_dir.sprintf("%03.0f", (integer)($lon+$lon_adj)).'.hgt';
if ($lat === '' || $lon === '' || !file_exists($filename)) return FALSE;
$y = abs($latlon->lat);
$x = abs($latlon->lon);

$filename = $this->data_path . $this->get_filename($latlon);
if (!file_exists($filename)) {
error_log("file not found '". $filename);
return FALSE;
}

$file = fopen($filename, 'r');
$offset = ( (integer)(($x - (integer)$x + $lon_adj) * 1200) * 2 + (1200 - (integer)(($y - (integer)$y + $lat_adj) * 1200)) * 2402 );
Expand All @@ -85,8 +159,8 @@ function get_elevation($lat, $lon, $round=TRUE) {
$$c = $m;
}

$fx = ($lon - ((integer)($lon * 1200) / 1200)) * 1200;
$fy = ($lat - ((integer)($lat * 1200) / 1200)) * 1200;
$fx = ($latlon->lon - ((integer)($latlon->lon * 1200) / 1200)) * 1200;
$fy = ($latlon->lat - ((integer)($latlon->lat * 1200) / 1200)) * 1200;

// normalizing data
$elevation = ($h1 * (1 - $fx) + $h2 * $fx) * (1 - $fy) + ($h3 * (1 - $fx) + $h4 * $fx) * $fy;
Expand Down
4 changes: 2 additions & 2 deletions includes/pages/nodes/nodes_plot_link.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ function output() {
if ($this->tpl['a_node'] != '' && $this->tpl['b_node'] != '') {
$this->tpl['a_node_azimuth'] = $geocalc->GCAzimuth($a_node_data['latitude'], $a_node_data['longitude'], $b_node_data['latitude'], $b_node_data['longitude']);
$this->tpl['b_node_azimuth'] = $geocalc->GCAzimuth($b_node_data['latitude'], $b_node_data['longitude'], $a_node_data['latitude'], $a_node_data['longitude']);
$this->tpl['a_node_geo_elevation'] = $srtm->get_elevation($a_node_data['latitude'], $a_node_data['longitude']);
$this->tpl['b_node_geo_elevation'] = $srtm->get_elevation($b_node_data['latitude'], $b_node_data['longitude']);
$this->tpl['a_node_geo_elevation'] = $srtm->get_elevation(new LatLon($a_node_data['latitude'], $a_node_data['longitude']));
$this->tpl['b_node_geo_elevation'] = $srtm->get_elevation(new LatLon($b_node_data['latitude'], $b_node_data['longitude']));
$this->tpl['a_node_elevation'] = $a_node_data['elevation'];
$this->tpl['b_node_elevation'] = $b_node_data['elevation'];

Expand Down
4 changes: 2 additions & 2 deletions includes/pages/nodes/nodes_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ function calculate_distance($a_node, $b_node) {
$lat2 = isset($b_node_i[0]['latitude'])?$b_node_i[0]['latitude']:1; // the same
$lon2 = isset($b_node_i[0]['longitude'])?$b_node_i[0]['longitude']:1; // the same

$a_node_el = str_replace(",", ".", $srtm->get_elevation($lat1, $lon1, FALSE));
$b_node_el = str_replace(",", ".", $srtm->get_elevation($lat2, $lon2, FALSE));
$a_node_el = str_replace(",", ".", $srtm->get_elevation(new LatLon($lat1, $lon1), FALSE));
$b_node_el = str_replace(",", ".", $srtm->get_elevation(new LatLon($lat2, $lon2), FALSE));

$dist = $geocalc->GCDistance($lat1, $lon1, $lat2, $lon2);

Expand Down
2 changes: 1 addition & 1 deletion install/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
</div>
<div class="footer">
Powered by: <a href="https://github.com/wind-project/wind"><b>WiND - Wireless Nodes Database project</b></a><br/>
&copy; 2005-2013 <a href="https://github.com/wind-project/wind/wiki/Team">WiND Contributors</a>
&copy; 2005-2014 <a href="https://github.com/wind-project/wind/wiki/Team">WiND Contributors</a>
</div>
</body>
</html>
Expand Down
2 changes: 1 addition & 1 deletion install/srtm_download.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

$base_url = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM3/';
$srtm_directory = dirname(__FILE__) . '/../files/srtm/';
$fname = srtm::get_filename((int)$_GET['lat'], (int)$_GET['lng']);
$fname = srtm::get_filename(new LatLon((int)$_GET['lat'], (int)$_GET['lng']));
$zip_fname = $fname . '.zip';
$regions = array(
'Africa',
Expand Down
17 changes: 5 additions & 12 deletions install/steps/step_srtm.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,12 @@
$bounds = $_SESSION['config']['gmap']['bounds'];

for($lat = floor($bounds['min_latitude']); $lat <= ceil($bounds['max_latitude']);$lat += 1) {
// Clamp
if ($lat > 90)
$lat = -90;
for($lng = floor($bounds['min_longitude']); $lng <= ceil($bounds['max_longitude']);$lng += 1) {
// clamp
if ($lng > 180)
$lng = -180;

$fname = srtm::get_filename($lat, $lng);
$coords = new LatLon($lat, $lng);
$fname = srtm::get_filename($coords);
$needed_files[$fname] = array(
'exists' => file_exists(dirname(__FILE__) . '/../../files/srtm/' . $fname),
'lat' => $lat,
'lng' => $lng
'exists' => file_exists(dirname(__FILE__) . '/../../files/srtm/' . $fname),
'latlon' => $coords
);
}
}
Expand All @@ -63,7 +56,7 @@

echo '<ul class="srtm">';
foreach($needed_files as $fname => $data) {
echo '<li data-lat="' . $data['lat']. '" data-lng="' . $data['lng']. '" class="' . ($data['exists']?'existing':'missing') .'">'
echo '<li data-lat="' . $data['latlon']->lat. '" data-lng="' . $data['latlon']->lon. '" class="' . ($data['exists']?'existing':'missing') .'">'
. '<span>'. $fname . '</span></li>';
}
echo '</ul>';
Expand Down
36 changes: 16 additions & 20 deletions tools/srtm/download_all.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<?php

/**
* Returns absolute path to srtm direactory based on configuration.
* @brief Returns absolute path to srtm direactory based on configuration.
* @return string
*/
function get_srtm_directory() {
Expand All @@ -20,7 +20,7 @@ function get_srtm_directory() {
}

/**
* Get bounds from configuration file
* @brief Get bounds from configuration file
*/
function get_bounds() {
global $config;
Expand All @@ -29,7 +29,7 @@ function get_bounds() {


/**
* Get all SRTM regions
* @brief Get all SRTM regions
*/
function get_regions() {
return array(
Expand All @@ -43,26 +43,20 @@ function get_regions() {
}

/**
* Get the list with all SRTM files to be downloaded
* @brief Get the list with all SRTM files to be downloaded
*/
function srtm_files($bound_sw, $bound_ne) {
// Calculate needed files
$needed_files = array();

for($lat = floor($bound_sw['lat']); $lat <= ceil($bound_ne['lat']);$lat += 1) {
// Clamp
if ($lat > 90)
$lat = -90;

for($lon = floor($bound_sw['lon']); $lon <= ceil($bound_ne['lon']);$lon += 1) {
// clamp
if ($lon > 180)
$lon = -180;

$fname = srtm::get_filename($lat, $lon);
$coords = new LatLon($lat, $lon);
$fname = srtm::get_filename($coords);
$needed_files[$fname] = array(
'exists' => file_exists(get_srtm_directory() . "/" . $fname),
'lat' => $lat,
'lon' => $lon
'latlon' => $coords
);
}
}
Expand All @@ -71,14 +65,13 @@ function srtm_files($bound_sw, $bound_ne) {

/**
* Download srtm files for a specific latitude and longitude.
* @param int $lat
* @param int $lng
* @param LatLon $latlon
*/
function download_srtm_for($lat, $lon) {
function download_srtm_for($latlon) {
global $config;
$base_url = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM3/';
$srtm_directory = get_srtm_directory() . "/";
$fname = srtm::get_filename($lat, $lon);
$fname = srtm::get_filename($latlon);
$zip_fname = $fname . '.zip';
$regions = array(
'Africa',
Expand Down Expand Up @@ -114,7 +107,8 @@ function download_srtm_for($lat, $lon) {
return true;
}
}
die("Cannot find tile for {$lat},{$lon}\" in any region.\n");
print("Cannot find tile for \"{$latlon->lat},{$latlon->lon}\" in any region.\n");
return false;
}

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -177,8 +171,10 @@ function download_srtm_for($lat, $lon) {
}
print "downloading... ";

if (download_srtm_for($info['lat'], $info['lon'])) {
if (download_srtm_for($info['latlon'])) {
print "OK\n";
} else {
print "SKIP\n";
}
}
print "Download finshed succesfully!\n";

0 comments on commit 033bd9f

Please sign in to comment.