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

root/Chameleon/trunk/Chameleon/ProjectionSelector/ProjectionSelector.widget.php

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

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * ProjectionSelector Widget Class
4  *
5  * @project     CWC2
6  * @revision    $Id: ProjectionSelector.widget.php,v 1.9 2005/04/12 18:44:18 pspencer Exp $
7  * @purpose     Display a projection selector dialog box.
8  * @author      DM Solutions Group (assefa@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 include_once(dirname(__FILE__)."/../Widget.php");
30 include_once(dirname(__FILE__)."/../Button.php");
31 include_once(dirname(__FILE__)."/../Popup.php");
32
33 /**
34  * ProjectionSelector
35  *
36  * @desc Display a widget that pops up a projection selection widget.
37  */
38 class ProjectionSelector extends CWCWidget
39 {
40     var $moButton;
41     var $moPopup;
42
43     /**
44      * ProjectionSelector
45      *
46      * Constctor method for the ProjectionSelector
47      */
48     function ProjectionSelector()
49     {
50         // invoke constructor of parent
51         parent::CWCWidget();
52
53         // set the description for this widget
54         $this->szWidgetDescription = <<<EOT
55 The ProjectionSelector widget allows the user to select a new projection
56 for the current context from those defined in a common resource called
57 "projection".  The structure of the common resource is: &ltprojection
58 name="human name" srs="epsg code"/&gt;.  This structure is shared with the
59 projection label widget if used.
60 EOT;
61
62         $this->moButton = new CWCButton( $this );
63
64         $this->moPopup = new CWCPopup( $this );
65         $this->moPopup->mszLink = $_SESSION['gszCoreWebPath']."/widgets/ProjectionSelector/ProjectionSelectorPopup.phtml";
66         $this->mnMaturityLevel = MATURITY_BETA;
67
68     }
69
70     function InitDefaults()
71     {
72         parent::InitDefaults();
73         //this widget should never belong to a toolset
74         $this->maParams["TOOLSET"] = "";
75         $this->moButton->InitDefaults();
76         $this->moButton->SetOnClick("clickProjectionSelectorPopup");
77     }
78    
79     function GetJavascriptVariables()
80     {
81         if ($this->mbVisible)
82             return $this->moButton->GetJavascriptVariables();
83         else return array();
84     }
85
86     function GetJavascriptInitFunctions()
87     {
88         if ($this->mbVisible)
89             return $this->moButton->GetJavascriptInitFunctions();
90         else return array();
91     }
92
93     function GetJavascriptIncludeFunctions()
94     {
95         if ($this->mbVisible)
96             $aReturn = $this->moButton->GetJavascriptIncludeFunctions();
97         else
98             $aReturn = array();
99         return $aReturn;
100     }
101
102
103     /**
104      * GetJavascriptFunctions
105      *
106      * Build and return the array of functions needed in the
107      * widget.
108      */
109     function GetJavascriptFunctions()
110     {
111         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
112           $bCWCJSAPI = 1;
113         else
114           $bCWCJSAPI = 0;
115
116         $aReturn = array();
117         $szJsFunctionName = "clickProjectionSelectorPopup";
118         $szButtonJS = $this->moPopup->DrawPublish();
119         $szFunction = <<<EOT
120 /**
121  * {$szJsFunctionName}
122  * popup a Projection Selector dialog
123  */
124 function {$szJsFunctionName}()
125 {
126     {$szButtonJS}
127     return;
128 }
129 EOT;
130         $aReturn[$szJsFunctionName] = $szFunction;
131         $szValidProj = "";
132         if (isset($this->maSharedResourceWidgets["projection"]->maszContents["PROJECTION"]))
133         {
134             $szSep = "";
135             foreach($this->maSharedResourceWidgets["projection"]->maszContents["PROJECTION"] as $aProj )
136             {
137                 if (isset($aProj["SRS"]))
138                 {
139                     $szValidProj .= $szSep."new Array(".
140                                     "\"".$aProj["NAME"]."\", ".
141                                     "\"".$aProj["SRS"]."\")";
142                     $szSep = ", ";
143                 }
144             }
145         }
146         $szJsFunctionName = "applyProjection";
147         $szFunction = <<<EOT
148 /**
149  * {$szJsFunctionName}
150  * apply the user's projection selection
151  */
152 function {$szJsFunctionName}( szProj )
153 {
154     aszValidProj = getProjectionList();
155     //validate szProj
156     bValid = false;
157     for (i=0;i<aszValidProj.length;i++)
158     {
159         if (aszValidProj[i][1] == szProj)
160         {
161             bValid = true;
162             break;
163         }
164     }
165     if (bValid)
166     {
167         if ($bCWCJSAPI)
168         {
169             goCWCJSAPI.oMap.SetProjection( szProj );
170             goCWCJSAPI.UpdateProjection();
171         }
172         else
173         {
174             {$this->mszHTMLForm}.MAP_PROJECTION.value = szProj;
175             {$this->mszHTMLForm}.submit();
176         }
177     }
178 }
179 EOT;
180         $aReturn[$szJsFunctionName] = $szFunction;
181
182         if (is_object( $this->moMapObject ))
183         {
184             $szProj = $this->moMapObject->oMap->getProjection();
185         }
186         else
187         {
188             $szProj = "init=EPSG:None";
189         }
190         if (substr($szProj, 0, 1) == '+')
191           $szProj = substr($szProj, 1 );
192         $szProj = substr( $szProj, 5 );
193                 $szJsFunctionName = "getProjectionList";
194         $szFunction = <<<EOT
195 /**
196  * {$szJsFunctionName}()
197  * return the list of valid projections
198  */
199 function {$szJsFunctionName}()
200 {
201     return new Array( {$szValidProj} );
202 }
203 EOT;
204         $aReturn[$szJsFunctionName] = $szFunction;
205
206         $szJsFunctionName = "getCurrentProjection";
207         $szFunction = <<<EOT
208 /**
209  * {$szJsFunctionName}()
210  * return the current projection
211  */
212 function {$szJsFunctionName}()
213 {
214     return "{$szProj}";
215 }
216 EOT;
217         $aReturn[$szJsFunctionName] = $szFunction;
218
219         if ($bCWCJSAPI)
220         {
221             $nValidProj = count($this->maSharedResourceWidgets["projection"]->maszContents["PROJECTION"]);
222             $szStrValidProj="";
223             if ($nValidProj > 0)
224             {
225                 $szStrValidProj = "goCWCJSAPI.oMap.validprojections = new Array(".$nValidProj.");\n";
226                 $iTmp =0;
227                 foreach($this->maSharedResourceWidgets["projection"]->maszContents["PROJECTION"] as $aProj )
228                 {       
229                     $szStrValidProj .= "goCWCJSAPI.oMap.validprojections[".$iTmp."]='".$aProj["SRS"]."';\n";
230                     $szStrValidProj .= "goCWCJSAPI.oMap.validprojectionNames[".$iTmp."]='".$aProj["NAME"]."';\n";
231                     $iTmp++;
232                 }
233             }
234             $szJsFunctionName = "ProjSelectWSetJSAPIProjList";
235             $szFunction = <<<EOT
236 /**
237  * {$szJsFunctionName}()
238  * return the current projection
239  */
240 function {$szJsFunctionName}()
241 {
242     {$szStrValidProj}
243 }
244 EOT;
245             $aReturn[$szJsFunctionName] = $szFunction;
246         }
247            
248         return $aReturn;
249     }
250
251     /**
252      * return an array of javascript functions needed by Scalebar widget
253      * and called when the page is loaded.
254      * @return array of name = function values
255      */
256      function GetJavascriptOnLoadFunctions()
257      {
258         if ($this->mbVisible)
259             $aReturn = $this->moButton->GetJavascriptOnLoadFunctions();
260         else
261             $aReturn = array();
262
263          if (isset($this->maSharedResourceWidgets["CWCJSAPI"]) &&
264              isset($this->maSharedResourceWidgets["projection"]->maszContents["PROJECTION"]))
265          {
266              $szJsFunctionName = "ProjSelectWSetJSAPIProjList";
267              $szFunction = "$szJsFunctionName();\n";
268              $aReturn[$szJsFunctionName] = $szFunction;
269          }
270
271          return $aReturn;
272      }
273      
274     /**
275      * GetHTMLHiddenVariables
276      *
277      * Return HTML hidden variables.
278      */
279     function GetHTMLHiddenVariables()
280     {
281         $aReturn = $this->moButton->GetHTMLHiddenVariables();
282        
283         if (is_object($this->moMapObject))
284             $szProj = $this->moMapObject->oMap->getProjection();
285         else
286             $szProj = "init=EPSG:None";
287         $szProj = substr( $szProj, 5 );
288         $szVariable = "MAP_PROJECTION";
289         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"\">";
290         $aReturn[$szVariable] = $szValue;
291         return $aReturn;
292     }
293
294     /**
295      * ParseURL
296      *
297      * Look for the MAP_PROJECTION being different from the
298      * current projection
299      */
300     function  ParseURL()
301     {
302         $oMap = &$this->moMapObject->oMap;
303
304         $szOriginalProjection = trim( $oMap->getMetaData( "original_projection" ));
305         if ($szOriginalProjection == "" && $oMap->getprojection() != "")
306         {
307             $oMap->setMetaData( "original_projection", $oMap->getprojection() );
308         }
309         $szOriginalProjection = trim( $oMap->getMetaData( "original_projection" ));
310
311         $szProj = "";
312         if ($this->isVarSet( "MAP_PROJECTION" ))
313             $szProj = $this->getVar( "MAP_PROJECTION" );
314         if ($szProj != "")
315         {
316             $szCurrentProjection = $this->moMapObject->oMap->getProjection();
317             $szCurrentProjection = substr( $szCurrentProjection, 5 );
318             if (strcasecmp( $szProj, $szCurrentProjection ) != 0)
319             {
320                 // Check all layers. If no projection use the current one.
321                 for( $i=0; $i< $oMap->numlayers; $i++)
322                 {
323                     $oLayer = $oMap->getLayer( $i );
324                     if ($oLayer->getProjection() == "")
325                     {
326                         $oLayer->setProjection($szOriginalProjection);
327                     }
328                 }
329              
330                 $aTmpExtent = array($oMap->extent->minx,$oMap->extent->miny,
331                                     $oMap->extent->maxx,$oMap->extent->maxy);
332                 $szTmpProjection = $oMap->getProjection();
333
334                 $oMap->setProjection($szTmpProjection, MS_TRUE);
335                 $oMap->setExtent( $aTmpExtent[0], $aTmpExtent[1],
336                                   $aTmpExtent[2], $aTmpExtent[3] );
337
338                 $nScale = intval($oMap->scale);
339
340                 $oOldProj = ms_newProjectionObj($szTmpProjection);
341
342                 $nCenterX = ($oMap->extent->minx + $oMap->extent->maxx) / 2.0;
343                 $nCenterY = ($oMap->extent->miny + $oMap->extent->maxy) / 2.0;
344
345                 $oCenter = ms_newPointObj( );
346                 $oCenter->setXY( $nCenterX, $nCenterY );
347
348
349                 if (strncasecmp(trim($szProj), "AUTO", 4) ==0)
350                 {
351                     $this->ReprojectAuto($szProj);
352                     return;
353                 }
354                
355                 //assuming it is an epsg
356                 $szNewProj = "init=".strtolower($szProj);
357
358                 $oNewProj = ms_newProjectionObj( $szNewProj );
359
360
361                 $oCenter->project( $oOldProj, $oNewProj );
362
363                 $oMap->setProjection( $szNewProj, MS_TRUE );
364
365                 $oMap->setExtent( $oCenter->x - 1, $oCenter->y - 1,
366                                   $oCenter->x + 1, $oCenter->y + 1 );
367
368                 $oPixelPos = ms_newPointObj();
369                 $nWidth = $oMap->width;
370                 $nHeight = $oMap->height;
371                 $oPixelPos->setXY( $nWidth/2, $nHeight/2 );
372
373                 $oMap->zoomScale( $nScale, $oPixelPos, $nWidth, $nHeight, $oMap->extent );
374             }
375         }
376
377         return true;
378     }
379
380     /**
381      * DrawPublish
382      *
383      * Return the HTML code using the name in the map file and the
384      * parameters of the CWC tag.
385      */
386     function DrawPublish()
387     {
388         if (!$this->mbVisible)
389             return "<!-- Projection Selector popup widget hidden -->";
390
391         $szResult = $this->moButton->DrawPublish();
392
393         return $szResult;
394     }
395 }
396 ?>
Note: See TracBrowser for help on using the browser.