forked from leonardoxc/leonardoxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CL_hgt.php
133 lines (106 loc) · 3.31 KB
/
CL_hgt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?
//************************************************************************
// Leonardo XC Server, https://github.com/leonardoxc/leonardoxc
//
// Copyright (c) 2004-2010 by Andreadakis Manolis
//
// This program is free software. You can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// $Id: CL_hgt.php,v 1.8 2012/10/08 07:25:39 manolis Exp $
//
//************************************************************************
/*
An HGT file covers an area of 1�x1�. Its south western corner can
be deduced from its file name: for example,
n51e002.hgt covers the area between
N 51� E 2� and N 52� E 3�,
and s14w077.hgt covers
S 14� W 77� to S 13� W 76�.
The file size depends on the resolution.
If this is 1", there are 3601 rows of 3601 cells each;
*** If it is 3", there are 1201 rows of 1201 cells each.
The rows are laid out like text on a page, starting with the northernmost row, with each row reading
from west to east.
Each cell has two bytes, and the elevation at that cell is 256*(1st byte) + (2nd byte).
It follows that a 3" HGT file has a file length of 2 x 1201 x 1201.
*/
// This is the file used for 3d elevation
$openDEMfiles=array();
global $openDEMfiles,$CONF_DEMpath;
class hgt {
function getHeight($lat,$lon) {
global $openDEMfiles,$CONF_DEMpath;
if ($lat>=0) {
$latSouth=floor($lat);
//$latNorth=$latSouth+1;
$latD=1-$lat+$latSouth;
$latStr="N";
} else {
$latSouth=ceil(abs($lat));
// $latNorth=$latSouth-1;
$latD=-$lat-$latSouth+1;
$latStr="S";
}
if ($lon>=0){
$lonWest=floor($lon);
//$lonEast=$lonWest+1;
$lonD=$lon-$lonWest;
$lonStr="E";
} else {
$lonWest=ceil(abs($lon));
//$lonEast=$lonWest-1;
$lonD=$lonWest+$lon;
$lonStr="W";
}
// find the file to use!
$demFile=sprintf("%s%02d%s%03d.hgt.zip",$latStr,$latSouth,$lonStr,$lonWest);
if ( ! isset($openDEMfiles[$demFile]) ) {
// echo "Getting DEM file: $demFile<BR>";
if (!is_file($CONF_DEMpath.'/'.$demFile)) {
// echo "#not found ".$CONF_DEMpath.'/'.$demFile."#";
return 0;
}
require_once(dirname(__FILE__).'/lib/pclzip/pclzip.lib.php');
$archive = new PclZip($CONF_DEMpath.'/'.$demFile);
$list=$archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING);
if ( $list == 0) {
//
// echo "unzip error<br>";
return 0;
// die("Error : ".$archive->errorInfo(true));
} else {
// echo $list[0]['content'];
}
$openDEMfiles[$demFile]=$list[0]['content'];
}
// find x,y inside the file
// 1 degree is 1201 points
$x=floor($lonD*1201);
$y=floor($latD*1201);
// point offeset in file
$pointOffset=($x+$y*1201)*2;
// echo "$latD $lonD $x $y $pointOffset<BR>";
$alt=ord($openDEMfiles[$demFile][$pointOffset])*256+ord($openDEMfiles[$demFile][$pointOffset+1]);
if ($alt>10000) $alt=0;
return $alt;
}
} // end of class
/* usage :
$lat=40.4879667;
$lon=23.1730500;
// height =476 !!
$alt=hgt::getHeight($lat,$lon);
echo "$lat,$lon -> $alt<BR>";
or test multiple lat/lon
for ($dlat=0;$dlat<100;$dlat++)
for ($dlon=0;$dlon<100;$dlon++) {
$lat=40.0+$dlat/100;
$lon=23.1+$dlon/100;
$alt=hgt::getHeight($lat,$lon);
//echo "$lat,$lon -> $alt<BR>";
echo "$alt, ";
}
*/
?>