NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/Chameleon/trunk/Chameleon/KeyMap/KeyMap.widget.php

Revision 13 (checked in by jcleary, 17 years ago)

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * KeyMap Widget class
4  *
5  * @project     CWC2
6  * @revision    $Id:
7  * @purpose     KeyMap Widget class
8  * @author      DM Solutions Group (sfournier@dmsolutions.ca)
9  * @copyright
10  * <b>Copyright (c) 2002, DM Solutions Group Inc.</b>
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */
29
30
31 include_once(dirname(__FILE__)."/../Widget.php");
32 include_once(dirname(__FILE__)."/../NavTool.php");
33 include_once(dirname(__FILE__)."/../geomath.php");
34 /**
35  * display a Key Map
36  */
37 class KeyMap extends NavTool
38 {
39     var   $mszAnchorName = "keyMapAnchor";
40     var   $mszKeyMapImageName = "KeyMapImage";
41
42     function KeyMap()
43     {
44         parent::NavTool();
45
46         $this->SetNavCommand("KEY_MAP");
47         $this->moButton->mszAnchorName = $this->mszAnchorName;
48
49         $this->mnPriority = PRIORITY_HIGH;
50
51         $this->maAttributes["WIDTH"] = new IntegerAttribute( "WIDTH", false, 0 );
52         $this->maAttributes["HEIGHT"] = new IntegerAttribute( "HEIGHT", false, 0 );
53         $this->maAttributes["IMAGE"] = new StringAttribute( "IMAGE", false );
54         $this->maAttributes["COLOR"] = new RGBColorAttribute( "COLOR", false );
55         $this->maAttributes["OUTLINECOLOR"] = new RGBColorAttribute( "OUTLINECOLOR", false );
56         $this->maAttributes["MINX"] = new FloatAttribute( "MINX", false );
57         $this->maAttributes["MINY"] = new FloatAttribute( "MINY", false );
58         $this->maAttributes["MAXX"] = new FloatAttribute( "MAXX", false );
59         $this->maAttributes["MAXY"] = new FloatAttribute( "MAXY", false );
60         $this->maAttributes["SRS"] = new StringAttribute( "SRS", false );
61
62         // set the description for this widget
63         $this->szWidgetDescription = <<<EOT
64 The KeyMap widget displays a key map (or reference map) that displays the users
65 currently viewed extents within a larger reference frame.
66 EOT;
67         $this->mnMaturityLevel = MATURITY_TECHNICALRELEASE;
68     }
69
70     /**
71      * process the url changes
72      */
73     function  ParseURL()
74     {
75         parent::ParseURL();
76         $szCmd = "";
77         if ($this->getVar("NAV_CMD"))
78           $szCmd = trim($this->getVar("NAV_CMD"));
79
80
81         if ($szCmd == "KEY_MAP")
82         {
83             $oMap = $this->moMapObject->oMap;
84
85             $szInputCoords = "";
86             $szInputCoords = $this->getVar("NAV_INPUT_COORDINATES");
87             $szInputType = $this->getVar("NAV_INPUT_TYPE");
88
89             if(strlen($szInputCoords) <= 0)
90             {
91                 $szInputCoords = ($oMap->reference->width/2).",".($oMap->reference->height/2);
92                 $szInputType = "POINT";
93             }
94
95             if ($szInputType == "POINT")
96             {
97                 $aPixPos = explode(",", $szInputCoords);
98                 $this->keymapNavigate ($aPixPos[0], $aPixPos[1] );
99             }
100             else
101             {
102                 $_SESSION['gErrorManager']->setError(ERR_WARNING,
103                    trim($this->moMLT->get("1", "ERROR: Invalid input type specified in"))." KeyMap.php.");
104             }
105         }
106         
107         // return success
108         return true;
109     }
110
111     /**
112      * render the keymap widget
113      */
114     function DrawPublish()
115     {
116         if (!$this->mbVisible)
117             return "<!-- KeyMap Widget hidden -->";
118
119         if ( PHP_OS == "WINNT" || PHP_OS == "WIN32" )
120             $szGDModule = "php_gd2.dll";
121         else
122         {
123             $szGDModule = "php_gd2.so";
124         }
125
126         if (!extension_loaded("gd"))
127             dl($szGDModule);
128
129         $oMap = $this->moMapObject->oMap;
130
131         $nWidth = $oMap->reference->width;
132         $nHeight = $oMap->reference->height;
133         
134         if ($nWidth <= 0)
135           $szWidthHTMLTag = "";
136         else
137           $szWidthHTMLTag = "WIDTH=".$nWidth;
138
139          if ($nHeight <= 0)
140           $szHeightHTMLTag = "";
141         else
142           $szHeightHTMLTag = "HEIGHT=".$nHeight;
143
144         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
145           $szOnLoad = "onLoad=\"KeyMapLoaded()\"";
146         else
147           $szOnLoad = "";
148           
149         $szCoreWebPath = $_SESSION["gszCoreWebPath"];
150         $nMapSessionMode = $_SESSION["gnMapSessionMode"];
151         $szKeyMapImageName = $this->mszKeyMapImageName;
152         $oApp = GetChameleonApplication();
153         $szAPixel = $_SESSION['gszCoreWebPath'].'/skins/default/images/a_pixel.gif';
154         $WEBCOMMON = WEBCOMMON;
155         $szSID = SID;
156         $szReturn = <<<EOT
157 <table cellpadding="0" cellspacing="0" border="0">
158 <tr>
159     <td bgcolor="000000"><img src="{$szAPixel}" width="1" height="1" id="keymapFrameTL" name="keymapFrameTL"></td>
160     <td bgcolor="000000"><img src="{$szAPixel}" width="1" height="1"></td>
161     <td bgcolor="000000"><img src="{$szAPixel}" width="1" height="1" id="keymapFrameTR" name="keymapFrameTR"></td>
162   </tr>
163   <tr>
164     <td bgcolor="000000"><img src="{$szAPixel}" width="1" height="1"></td>
165     <td><a name="mapanchor"  HREF="javascript:KeyMapClicked()"><IMG SRC="{$WEBCOMMON}/wrapper/drawmap.php?map_session_mode={$nMapSessionMode}&REQUEST=KEYMAP&{$szSID}" {$szWidthHTMLTag} {$szHeightHTMLTag} NAME="{$szKeyMapImageName}" {$szOnLoad} border="0" onmouseover="KeyMapCaptureMouse()" onmouseout="KeyMapReleaseMouse()"></a></td>
166     <td bgcolor="000000"><img src="{$szAPixel}" width="1" height="1"></td>
167   </tr>
168   <tr>
169     <td bgcolor="000000"><img src="{$szAPixel}" width="1" height="1" id="keymapFrameBL" name="keymapFrameBL"></td>
170     <td bgcolor="000000"><img src="{$szAPixel}" width="1" height="1"></td>
171     <td bgcolor="000000"><img src="{$szAPixel}" width="1" height="1" id="keymapFrameBR" name="keymapFrameBR"></td>
172   </tr>
173 </table>
174 EOT;
175
176         return $szReturn;
177     }
178
179     /**
180      * SetMap($oMapObject)
181      *
182      * Set the map object.
183      *
184      * @param oMapObject
185      */
186     function SetMap(&$oMapObject)
187     {
188         $this->moMapObject =& $oMapObject;
189         
190         if (isset($_SESSION['KEYMAP_INIT']) && $_SESSION['KEYMAP_INIT'] == "1")
191             return;
192             
193         $oRef =& $this->moMapObject->oMap->reference;
194         $oExt =& $oRef->extent;
195         if (isset($this->maParams["MINX"]))
196         {
197             $oExt->set( "minx"$this->maParams["MINX"] );
198         }
199         if (isset($this->maParams["MAXX"]))
200         {
201             $oExt->set( "maxx"$this->maParams["MAXX"] );
202         }
203         if (isset($this->maParams["MINY"]))
204         {
205             $oExt->set( "miny"$this->maParams["MINY"] );
206         }
207         if (isset($this->maParams["MAXY"]))
208         {
209             $oExt->set( "maxy"$this->maParams["MAXY"] );
210         }
211         if (isset($this->maParams["WIDTH"]))
212         {
213             $oRef->set("width",$this->maParams["WIDTH"]);
214         }
215         if (isset($this->maParams["HEIGHT"]))
216         {
217             $oRef->set("height",$this->maParams["HEIGHT"]);
218         }
219         if (isset($this->maParams["IMAGE"]))
220         {
221             $oApp = GetChameleonApplication();
222             $szPath = $oApp->getRemoteFile( $this->maParams['IMAGE'], "png" );
223             $szPath = $oApp->resolvePath2( $szPath, $_SESSION['gszAppPath'] );
224             $oRef->set("image",$szPath);
225         }
226         if (isset($this->maParams["COLOR"]))
227         {
228             $aColor = split( " ", $this->maParams["COLOR"] );
229             if (count($aColor) != 3)
230             {
231                 if(isset( $_SESSION["gErrorManager"] ))
232                 {
233                     $_SESSION["gErrorManager"]->setError( ERR_WARNING, "invalid color parameter in KeyMap widget" );
234                 }
235             }
236             else
237             {
238                 $oRef->color->setRGB( $aColor[0], $aColor[1], $aColor[2] );
239             }
240         }
241         if (isset($this->maParams["OUTLINECOLOR"]))
242         {
243             $aColor = split( " ", $this->maParams["OUTLINECOLOR"] );
244             if (count($aColor) != 3)
245             {
246                 $_SESSION["gErrorManager"]->setError( ERR_WARNING, "invalid outline color parameter in KeyMap widget" );
247             }
248             else
249             {
250                 $oRef->outlinecolor->setRGB( $aColor[0], $aColor[1], $aColor[2] );
251             }
252         }
253         if (isset($this->maParams["SRS"]))
254         {
255             //TODO: enable this, but also add init= if SRS starts with epsg:
256             //$oRef->setprojection($this->maParams["SRS"]));
257             $_SESSION["KEYMAP_SRS"] = $this->maParams["SRS"];
258         }
259         $_SESSION['KEYMAP_INIT'] = '1';
260      }
261
262     /**
263      * return javascript functions required by the keymap
264      */
265     function GetJavascriptFunctions()
266     {
267         $oMap = $this->moMapObject->oMap;
268
269         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
270           $bCWCJSAPI = 1;
271         else
272           $bCWCJSAPI = 0;
273
274         $nWidth = $oMap->reference->width;
275         $nHeight = $oMap->reference->height;
276         
277         $szJsFunctionName = "KeyMapTrackMouseXY";
278         $szFunction = <<<EOT
279 function {$szJsFunctionName}(e)
280 {
281     if (navigator.appName == "Microsoft Internet Explorer")
282     {
283         gnMouseX = event.clientX + document.body.scrollLeft;
284         gnMouseY = event.clientY + document.body.scrollTop;
285     }
286     else
287     {
288         gnMouseX = e.pageX;
289         gnMouseY = e.pageY;
290     }
291     return true;
292 }
293 EOT;
294
295         $aReturn[$szJsFunctionName] = $szFunction;
296         
297         $szJsFunctionName = "KeyMapCaptureMouse";
298         $szFunction = <<<EOT
299 function {$szJsFunctionName}()
300 {
301     document.onmousemove=KeyMapTrackMouseXY;
302 }
303 EOT;
304         $aReturn[$szJsFunctionName] = $szFunction;
305
306         $szJsFunctionName = "KeyMapReleaseMouse";
307         $szFunction = <<<EOT
308 function {$szJsFunctionName}()
309 {
310     document.onmousemove=null;
311 }
312 EOT;
313         $aReturn[$szJsFunctionName] = $szFunction;
314
315
316         $szJsFunctionName = "KeyMapClicked";
317         $szFunction = <<<EOT
318 function {$szJsFunctionName}( )
319 {
320     if (gnMouseX == -1 || gnMouseY == -1)
321         return;
322         
323     var nAnchorX = CWCDHTML_FindObjectPosX( CWCDHTML_GetImage('keymapFrameTL') );
324     var nAnchorY = CWCDHTML_FindObjectPosY( CWCDHTML_GetImage('keymapFrameTL') );;
325     
326     nAnchorX +=1;
327     nAnchorY +=1;
328     
329     nMapX = gnMouseX - nAnchorX;
330     nMapY = gnMouseY - nAnchorY;
331     
332     szCoord= nMapX + "," + nMapY;
333     
334     {$this->mszHTMLForm}.NAV_INPUT_COORDINATES.value=szCoord;
335     {$this->mszHTMLForm}.NAV_INPUT_TYPE.value='POINT';
336     {$this->mszHTMLForm}.NAV_CMD.value='KEY_MAP';
337     
338     if ({$bCWCJSAPI})
339     {
340         goCWCJSAPI.NAV_INPUT_COORDINATES = szCoord;
341         goCWCJSAPI.NAV_INPUT_TYPE = 'POINT';
342         goCWCJSAPI.NAV_CMD='KEY_MAP';
343         goCWCJSAPI.UpdateNavTools();
344     }
345     else
346     {
347         {$this->mszHTMLForm}.submit();
348     }
349 }
350 EOT;
351
352         $aReturn[$szJsFunctionName] = $szFunction;
353
354         if ($bCWCJSAPI)
355         {
356             
357           $url = //$_SESSION["gszCoreWebPath"].
358                      WEBCOMMON."/wrapper/drawmap.php?map_session_mode=".
359                      $_SESSION["gnMapSessionMode"].
360                      "&run_query=0&REQUEST=KEYMAP&".SID;
361             
362             $szJsFunctionName = "KeyMapWRegisterForEvent";
363             $szFunction = <<<EOT
364 /**
365  * {$szJsFunctionName}
366  * called to register and even when the map extents chnages (JSAPI)
367  */
368 function {$szJsFunctionName}()
369 {
370     goCWCJSAPI.RegisterEvent(MAP_EXTENT_CHANGED, "KeyMapWMapExtentsChanged");
371 }
372 EOT;
373
374        $aReturn[$szJsFunctionName] = $szFunction;
375
376        $szJsFunctionName = "KeyMapWMapExtentsChanged";
377        $szFunction = <<<EOT
378 /**
379  * {$szJsFunctionName}
380  * called when the mapextents are changed to update the scalebar images(JSAPI)
381  */
382 function {$szJsFunctionName}()
383 {
384      var sImgName = "{$this->mszKeyMapImageName}";
385      var url = "{$url}";
386      var d = new Date();
387      var unique = d.getTime() + '' + Math.floor(1000 * Math.random());
388      url = url + "&UniqId="+unique;
389      document.images[sImgName].src = url;
390 }
391 EOT;
392        $aReturn[$szJsFunctionName] = $szFunction;
393
394        $szJsFunctionName = "KeyMapLoaded";
395        $szFunction = <<<EOT
396 /**
397  * {$szJsFunctionName}
398  * called when the image is finished loading
399  */
400 function {$szJsFunctionName}()
401 {
402     // Get all errors from ErrorManager on server side.
403     //goCWCJSAPI.oErrorManager.GetServerErrors();
404 }
405 EOT;
406        $aReturn[$szJsFunctionName] = $szFunction;
407       }
408        return $aReturn;
409    }
410
411
412     /**
413      * return javascript variables required by the keymap
414      */
415     function GetJavascriptVariables()
416     {
417         $aReturn = parent::GetJavascriptVariables();
418
419         //?TODO should we create another function for global code
420         $szVariable = "KeyMapWidget".$this->mnId;
421         $szValue = "if (navigator.appName != \"Microsoft Internet Explorer\") document.captureEvents(Event.MOUSEMOVE);\n";
422         $aReturn[$szVariable] = $szValue;
423
424         $szVariable = "gnMouseX";
425         $szValue = "var $szVariable = -1;\n";
426         $aReturn[$szVariable] = $szValue;
427
428         $szVariable = "gnMouseY";
429         $szValue = "var $szVariable = -1;\n";
430         $aReturn[$szVariable] = $szValue;
431
432         return $aReturn;
433     }
434     
435     function GetJavascriptIncludeFunctions()
436     {
437         $aReturn = parent::GetJavascriptIncludeFunctions();
438
439         $szVar = 'cwc_dhtml.js';
440         $aReturn[$szVar] = '<script language="JavaScript" src="'.$_SESSION['gszCoreWebPath'].
441                             '/widgets/js/cwc_dhtml.js" type="text/javascript"></script>';
442         return $aReturn;
443     }
444
445
446     /**
447      * return an array of javascript functions needed by keymap widget
448      * and called when the page is loaded.
449      * @return array of name = function values
450      */
451     function GetJavascriptOnLoadFunctions()
452     {
453         $aReturn = parent::GetJavascriptOnLoadFunctions();
454
455         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
456         {
457              $szJsFunctionName = "KeyMapWRegisterForEvent";
458              $szFunction = "$szJsFunctionName();\n";
459              $aReturn[$szJsFunctionName] = $szFunction;
460         }
461
462         return $aReturn;
463     }
464
465     /**
466      * return variables required by KeyMap
467      */
468     function GetHTMLHiddenVariables()
469     {
470         $aReturn = parent::GetHTMLHiddenVariables();
471         
472         $szVariable = "NAV_INPUT_TYPE";
473         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"\">\n";
474         $aReturn[$szVariable] = $szValue;
475
476         $szVariable = "NAV_INPUT_COORDINATES";
477         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"\">\n";
478         $aReturn[$szVariable] = $szValue;
479
480         return $aReturn;
481     }
482
483     /**
484      * This function takes the keymap click co-ordinates and recenters the map
485      * accordingly.
486      *
487      * @param $nX integer - The keymap X click position in pixels.
488      * @param $nY integer - The keymap Y click position in pixels.
489      **/
490     function keymapNavigate ( $nX, $nY )
491     {
492         // get local map object
493         $oMap = $this->moMapObject->getMapObj();
494         $oRef = $oMap->reference;
495         $oExt = $oRef->extent;
496
497         $nWidth = $oRef->width;
498         $nHeight = $oRef->height;
499         
500                   
501 /* -------------------------------------------------------------------- */
502 /*      get the click geographic coordinates and then project the       */
503 /*      value to the map projection.                                    */
504 /* -------------------------------------------------------------------- */
505         $nGeoClickX = Pix2Geo($nX, 0, $nWidth, $oExt->minx,
506                                      $oExt->maxx, 0);
507         $nGeoClickY = Pix2Geo($nY, 0, $nHeight, $oExt->miny,
508                                      $oExt->maxy, 1);
509         
510         $oPoint = ms_newpointobj();
511         $oPoint->setxy($nGeoClickX, $nGeoClickY);
512         
513         //TODO: this may not be necessary if MapServer supports a projection
514         // object in the ReferenceObj
515         if (isset($_SESSION["KEYMAP_SRS"]))
516         {
517             $szKeyProjection = $_SESSION['KEYMAP_SRS'];
518             if (stristr( $szKeyProjection, "epsg" ))
519                 $szKeyProjection = "init=".strtolower($szKeyProjection);
520                 
521             if (strcasecmp( $szKeyProjection, $oMap->getProjection()) != 0)   
522             {
523                 $oKeyProjection = ms_newProjectionObj( $szKeyProjection );
524                 $oMapProjection = ms_newProjectionObj( $oMap->getProjection() );
525                 $oPoint->project( $oKeyProjection, $oMapProjection );
526             }
527         }
528         
529         //calculate the new extents of the map
530         $dfDeltaX = $oMap->extent->maxx - $oMap->extent->minx;
531         $dfDeltaY = $oMap->extent->maxy - $oMap->extent->miny;
532         
533         $dfNewMinX = $oPoint->x - ($dfDeltaX/2);
534         $dfNewMinY = $oPoint->y - ($dfDeltaY/2);
535         $dfNewMaxX = $oPoint->x + ($dfDeltaX/2);
536         $dfNewMaxY = $oPoint->y + ($dfDeltaY/2);
537         // recenter to the click position
538         $oMap->setExtent($dfNewMinX, $dfNewMinY, $dfNewMaxX, $dfNewMaxY);
539     }
540
541     /**
542      * This function calculates the corresponding map click given a keymap click.
543      *
544      * @param $nMapPix
545      * @param $nMapMinGeo
546      * @param $nMapMaxGeo
547      * @param $nMapMinGeoMax
548      * @param $nMapMaxGeoMax
549      * @param $nKeyPixClick
550      * @param $nKeyPix
551      * @param $bX
552      * @return integer - The map click position in pixels.
553      **/
554     function keymapToMap ( $nMapPix, $nMapMinGeo, $nMapMaxGeo, $nMapMinGeoMax,
555                            $nMapMaxGeoMax, $nKeyPixClick, $nKeyPix,
556                            $bX )
557     {
558        // calculate the difference in extents
559         $nMapGeo = $nMapMaxGeo - $nMapMinGeo;
560         $nMapGeoMax = $nMapMaxGeoMax - $nMapMinGeoMax;
561
562         // calculate the pixel value of the maximum map extents (i.e. width or
563         // height of map at maximum extents)
564         $nMapPixMax = ( $nMapPix * $nMapGeoMax ) / $nMapGeo;
565
566         // calculate the map click distance
567         $dClickDistance =  ( $nKeyPixClick * $nMapPixMax ) / $nKeyPix;
568
569         // calculate the value to translate the origin
570         if ($bX)
571             $dDeltaOrigin = (($nMapMinGeo - $nMapMinGeoMax) * $nMapPix) / $nMapGeo;
572         else
573             $dDeltaOrigin = (($nMapMaxGeoMax - $nMapMaxGeo) * $nMapPix) / $nMapGeo;
574
575         // calculate the return value
576         $dReturnValue = $dClickDistance - $dDeltaOrigin;
577
578         return $dReturnValue;
579     }
580 }
581 ?>
582
Note: See TracBrowser for help on using the browser.