1 |
<?php |
---|
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 |
include_once(dirname(__FILE__)."/../Widget.php"); |
---|
32 |
|
---|
33 |
|
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 |
class TrueNorth extends CWCWidget |
---|
39 |
{ |
---|
40 |
var $mszTrueNorthImage = ""; |
---|
41 |
var $mszImage = ""; |
---|
42 |
var $mnWidth = ""; |
---|
43 |
var $mnHeight = ""; |
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 |
function TrueNorth() |
---|
51 |
{ |
---|
52 |
|
---|
53 |
parent::CWCWidget(); |
---|
54 |
|
---|
55 |
|
---|
56 |
$this->maAttributes["TRUENORTHIMAGE"] = new StringAttribute("TRUENORTHIMAGE", true); |
---|
57 |
|
---|
58 |
|
---|
59 |
$this->mnPriority = PRIORITY_LAST; |
---|
60 |
|
---|
61 |
|
---|
62 |
$this->szWidgetDescription = <<<EOT |
---|
63 |
The TrueNorth widget just display a bitmap rotated indicating the real north. |
---|
64 |
EOT; |
---|
65 |
$this->mnMaturityLevel = MATURITY_BETA; |
---|
66 |
} |
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 |
function InitDefaults() |
---|
72 |
{ |
---|
73 |
parent::InitDefaults(); |
---|
74 |
$oApp = GetChameleonApplication(); |
---|
75 |
|
---|
76 |
if (isset( $this->maParams["TRUENORTHIMAGE"]) && $this->maParams['TRUENORTHIMAGE'] != '') |
---|
77 |
$this->mszTrueNorthImage = $oApp->findFile( $this->maParams["TRUENORTHIMAGE"] ); |
---|
78 |
if (!$this->mszTrueNorthImage) |
---|
79 |
{ |
---|
80 |
$_SESSION['gErrorManager']->setError( ERR_CRITICAL, "TrueNorth: ".$this->maParams["TRUENORTHIMAGE"]." cannot be found." ); |
---|
81 |
$this->mszTrueNorthImage = ''; |
---|
82 |
} |
---|
83 |
} |
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 |
|
---|
95 |
|
---|
96 |
function ParseURL() |
---|
97 |
{ |
---|
98 |
parent::ParseURL(); |
---|
99 |
|
---|
100 |
if ($this->mszTrueNorthImage == '') |
---|
101 |
{ |
---|
102 |
$_SESSION['gErrorManager']->setError( ERR_CRITICAL, "TrueNorth: invalid or no image specified." ); |
---|
103 |
return false; |
---|
104 |
} |
---|
105 |
|
---|
106 |
if ( PHP_OS == "WINNT" || PHP_OS == "WIN32" ) |
---|
107 |
$szGDModule = "php_gd2.dll"; |
---|
108 |
else |
---|
109 |
$szGDModule = "php_gd2.so"; |
---|
110 |
|
---|
111 |
if (!extension_loaded("gd")) |
---|
112 |
dl($szGDModule); |
---|
113 |
|
---|
114 |
$oMap = $this->moMapObject->oMap; |
---|
115 |
|
---|
116 |
|
---|
117 |
$szTmpMapProj = $oMap->getProjection(); |
---|
118 |
|
---|
119 |
|
---|
120 |
|
---|
121 |
if ($szTmpMapProj === false) |
---|
122 |
return false; |
---|
123 |
|
---|
124 |
$projInObj = ms_newprojectionobj( $szTmpMapProj ); |
---|
125 |
$projOutObj = ms_newprojectionobj("init=epsg:4326"); |
---|
126 |
|
---|
127 |
|
---|
128 |
$oRect = ms_newrectobj(); |
---|
129 |
$oRect->setExtent($oMap->extent->minx, $oMap->extent->miny, |
---|
130 |
$oMap->extent->maxx, $oMap->extent->maxy); |
---|
131 |
|
---|
132 |
if (strcasecmp("init=epsg:4326", $szTmpMapProj) != 0) |
---|
133 |
$oRect->project($projInObj, $projOutObj); |
---|
134 |
|
---|
135 |
|
---|
136 |
$nMinY = $this->pixel_to_geo($oMap->height/2-10, 0, $oMap->height, |
---|
137 |
$oRect->miny, $oRect->maxy); |
---|
138 |
|
---|
139 |
$nMaxY = $this->pixel_to_geo($oMap->height/2+10, 0, $oMap->height, |
---|
140 |
$oRect->miny, $oRect->maxy); |
---|
141 |
|
---|
142 |
$poPoint1 = ms_newpointobj(); |
---|
143 |
$poPoint2 = ms_newpointobj(); |
---|
144 |
$poPoint1->setXY(($oRect->maxx - $oRect->minx)/2+$oRect->minx, $nMaxY); |
---|
145 |
$poPoint2->setXY($poPoint1->x, $nMinY); |
---|
146 |
|
---|
147 |
if (strcasecmp("init=epsg:4326", $szTmpMapProj) != 0) |
---|
148 |
{ |
---|
149 |
|
---|
150 |
$poPoint1->project($projOutObj, $projInObj); |
---|
151 |
$poPoint2->project($projOutObj, $projInObj); |
---|
152 |
} |
---|
153 |
|
---|
154 |
|
---|
155 |
$nTop = ($poPoint1->y-$poPoint2->y); |
---|
156 |
$nDeltaX = ($poPoint1->x-$poPoint2->x); |
---|
157 |
$nDeltaY = ($poPoint1->y-$poPoint2->y); |
---|
158 |
$nBottom = sqrt( $nDeltaX*$nDeltaX + $nDeltaY*$nDeltaY); |
---|
159 |
if ($nBottom == 0) |
---|
160 |
{ |
---|
161 |
$nDeg = 90; |
---|
162 |
} |
---|
163 |
else |
---|
164 |
{ |
---|
165 |
$nDeg = rad2deg(asin($nTop/$nBottom)); |
---|
166 |
} |
---|
167 |
|
---|
168 |
if ($poPoint1->x-$poPoint2->x<0) |
---|
169 |
$szFileName = $this->drawSymbolRotated(90-$nDeg); |
---|
170 |
else |
---|
171 |
$szFileName = $this->drawSymbolRotated(-1*(90-$nDeg)); |
---|
172 |
|
---|
173 |
$this->mszImage = "<IMG src=\"".$_SESSION['gszTmpWebPath']."/". |
---|
174 |
basename($szFileName)."\" width=\"".$this->mnWidth. |
---|
175 |
"\" height=\"".$this->mnHeight."\">\n"; |
---|
176 |
|
---|
177 |
|
---|
178 |
return true; |
---|
179 |
} |
---|
180 |
|
---|
181 |
function drawSymbolRotated($nDeg) |
---|
182 |
{ |
---|
183 |
$szSufix = strtoupper(substr($this->mszTrueNorthImage, -3)); |
---|
184 |
if ($szSufix=="PNG") |
---|
185 |
{ |
---|
186 |
$oImg = imagecreatefrompng($this->mszTrueNorthImage); |
---|
187 |
} |
---|
188 |
else |
---|
189 |
{ |
---|
190 |
if ($szSufix=="GIF") |
---|
191 |
{ |
---|
192 |
$oImg = imagecreatefromgif($this->mszTrueNorthImage); |
---|
193 |
} |
---|
194 |
else |
---|
195 |
{ |
---|
196 |
if ($szSufix=="JPG") |
---|
197 |
{ |
---|
198 |
$oImg = imagecreatefromjpeg($this->mszTrueNorthImage); |
---|
199 |
} |
---|
200 |
else |
---|
201 |
{ |
---|
202 |
$oImg = imagecreatefrompng($this->mszTrueNorthImage); |
---|
203 |
} |
---|
204 |
} |
---|
205 |
} |
---|
206 |
|
---|
207 |
$this->mnWidth = imagesx($oImg); |
---|
208 |
$this->mnHeight = imagesy($oImg); |
---|
209 |
|
---|
210 |
$oMap = ms_newMapObj(""); |
---|
211 |
$oMap->selectOutputFormat("PNG24"); |
---|
212 |
$oMap->set("width", $this->mnWidth); |
---|
213 |
$oMap->set("height", $this->mnHeight); |
---|
214 |
$oMap->setExtent(0, 0, $this->mnWidth, $this->mnHeight); |
---|
215 |
|
---|
216 |
$oLayer = ms_newLayerObj($oMap); |
---|
217 |
$oLayer->set("transform", MS_FALSE); |
---|
218 |
$oLayer->set("type", MS_LAYER_POINT); |
---|
219 |
|
---|
220 |
$oImage = $oMap->prepareimage(); |
---|
221 |
|
---|
222 |
$oClass = ms_newClassObj($oLayer); |
---|
223 |
|
---|
224 |
$nSymbol = ms_newSymbolObj($oMap, $this->mszTrueNorthImage); |
---|
225 |
$oStyle = ms_newStyleObj($oClass); |
---|
226 |
$oStyle->set("symbol", $nSymbol); |
---|
227 |
|
---|
228 |
$oi = $oClass->createLegendIcon($this->mnWidth, $this->mnHeight); |
---|
229 |
|
---|
230 |
$oImage->pasteImage($oi,0, $this->mnWidth/2, $this->mnHeight/2, $nDeg); |
---|
231 |
|
---|
232 |
$szTmpDir = $_SESSION['gszTmpImgPath']; |
---|
233 |
if (substr($szTmpDir, -1) == "/") |
---|
234 |
$szTmpDir = substr($szTmpDir, 0, -1 ); |
---|
235 |
|
---|
236 |
$szFileName = str_replace("\\", "/", tempnam($szTmpDir, "TN").".png"); |
---|
237 |
|
---|
238 |
$oImage->saveImage($szFileName); |
---|
239 |
|
---|
240 |
return $szFileName; |
---|
241 |
} |
---|
242 |
|
---|
243 |
function drawPublish() |
---|
244 |
{ |
---|
245 |
return $this->mszImage; |
---|
246 |
} |
---|
247 |
|
---|
248 |
|
---|
249 |
|
---|
250 |
|
---|
251 |
|
---|
252 |
|
---|
253 |
|
---|
254 |
|
---|
255 |
|
---|
256 |
|
---|
257 |
|
---|
258 |
|
---|
259 |
|
---|
260 |
function pixel_to_geo($nPixPos, $dfPixMin, $dfPixMax, $dfGeoMin, $dfGeoMax, |
---|
261 |
$nInversePix = "") |
---|
262 |
{ |
---|
263 |
|
---|
264 |
$dfWidthGeo = $dfGeoMax - $dfGeoMin; |
---|
265 |
$dfWidthPix = $dfPixMax - $dfPixMin; |
---|
266 |
|
---|
267 |
|
---|
268 |
$dfPixToGeo = $dfWidthGeo / $dfWidthPix; |
---|
269 |
|
---|
270 |
|
---|
271 |
if (!$nInversePix) |
---|
272 |
$dfDeltaPix = $nPixPos - $dfPixMin; |
---|
273 |
else |
---|
274 |
$dfDeltaPix = $dfPixMax - $nPixPos; |
---|
275 |
|
---|
276 |
|
---|
277 |
$dfDeltaGeo = $dfDeltaPix * $dfPixToGeo; |
---|
278 |
$dfPosGeo = $dfGeoMin + $dfDeltaGeo; |
---|
279 |
|
---|
280 |
|
---|
281 |
return ($dfPosGeo); |
---|
282 |
|
---|
283 |
|
---|
284 |
} |
---|
285 |
|
---|
286 |
|
---|
287 |
|
---|
288 |
|
---|
289 |
|
---|
290 |
|
---|
291 |
|
---|
292 |
|
---|
293 |
|
---|
294 |
|
---|
295 |
|
---|
296 |
|
---|
297 |
|
---|
298 |
function geo_to_pixel ($nGeoPos, $dfPixMin, $dfPixMax, $dfGeoMin, |
---|
299 |
$dfGeoMax, $nInverseGeo = "") |
---|
300 |
{ |
---|
301 |
|
---|
302 |
$dfWidthGeo = abs($dfGeoMax - $dfGeoMin); |
---|
303 |
$dfWidthPix = abs($dfPixMax - $dfPixMin); |
---|
304 |
|
---|
305 |
|
---|
306 |
$dfGeoToPix = $dfWidthPix / $dfWidthGeo; |
---|
307 |
|
---|
308 |
|
---|
309 |
if (!$nInverseGeo) |
---|
310 |
$dfDeltaGeo = $nGeoPos - $dfGeoMin; |
---|
311 |
else |
---|
312 |
$dfDeltaGeo = $dfGeoMax - $nGeoPos; |
---|
313 |
|
---|
314 |
|
---|
315 |
$dfDeltaPix = $dfDeltaGeo * $dfGeoToPix; |
---|
316 |
$dfPosPix = $dfPixMin + $dfDeltaPix; |
---|
317 |
|
---|
318 |
|
---|
319 |
return round ($dfPosPix); |
---|
320 |
|
---|
321 |
} |
---|
322 |
|
---|
323 |
} |
---|
324 |
?> |
---|
325 |
|
---|