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

root/Chameleon/trunk/Chameleon/ROIFeatureTool/ROIFeatureTool.widget.php

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

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * ROIFeatureTool Widget class
4  *
5  * @project     CWC2
6  * @revision    $Id: ROIFeatureTool.widget.php,v 1.6 2004/12/03 03:31:56 pspencer Exp $
7  * @purpose     ROI Feature Tool Widget class.  Allows the user to select
8  * a region of interest from a feature in an existing layer. 
9  * @author      DM Solutions Group (spencer@dmsolutions.ca)
10  * @copyright
11  * <b>Copyright (c) 2002, DM Solutions Group Inc.</b>
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  */
30
31 include_once(dirname(__FILE__)."/../Widget.php");
32 include_once(dirname(__FILE__)."/../Button.php");
33
34 /**
35  * ROIFeatureTool
36  *
37  * @desc A widget to draw a Region of Interest from an existing feature.  User
38  * clicks on the map and the selected layer is queried by point to select a feature
39  * then the feature is used to create an ROI.
40  *
41  * Only works on polygon layers
42  */
43 class ROIFeatureTool extends CWCWidget
44 {
45     /* the button that is the interface for this widget */
46     var $moButton;
47     
48     /**
49      * construct a new ROI Feature Tool widget
50      */
51     function ROIFeatureTool()
52     {
53         // invoke constructor of parent
54         parent::CWCWidget();
55         
56         // set the description for this widget
57         $this->szWidgetDescription = <<<EOT
58 The v widget allows the user to draw a region
59 of interest on the map interface by selecting an existing
60 feature from a polygon layer.
61 EOT;
62         $this->moButton = new CWCButton( $this );
63         $this->mnMaturityLevel = MATURITY_BETA;
64     }
65     
66     /**
67      *
68      */
69     function InitDefaults()
70     {
71         parent::InitDefaults();
72         
73         $this->moButton->InitDefaults();
74         $this->moButton->SetOnClick('ROIFeatureTool'.$this->mnId);
75     }
76
77     /**
78      * pass URL to button
79      */
80     function  ParseURL()
81     {
82         return $this->moButton->ParseURL();
83     }
84
85     function GetHTMLHiddenVariables()
86     {
87         $aReturn = $this->moButton->GetHTMLHiddenVariables();
88                 
89         $szCmd = '';
90         if ($this->isVarSet('NAV_CMD'))
91             $szCmd = $this->getVar('NAV_CMD');
92         $szVariable = "NAV_CMD";
93         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"\">\n";
94         $aReturn[$szVariable] = $szValue;
95         
96         return $aReturn;
97     }
98     
99     /**
100      * GetJavascriptInitFunctions
101      *
102      * Functions to be called at the end of the load.
103      */
104     function GetJavascriptInitFunctions()
105     {
106         $aReturn = $this->moButton->GetJavascriptInitFunctions();
107
108         /*
109         $szJsFunctionName = "ROIFeatureToolInit";
110         $szFunction = "$szJsFunctionName();\n";
111         $aReturn[$szJsFunctionName] = $szFunction;
112         */
113         $aReturn['InitCWCROIManagerForm'] = 'goCWCROIManager.SetFormObject('.$this->mszHTMLForm.');';
114         
115         return $aReturn;
116     }
117
118     /**
119      * javascript include files
120      */
121     function GetJavascriptIncludeFunctions()
122     {
123         $aReturn = parent::GetJavascriptIncludeFunctions();
124         
125         $szVar = "cwc_roi.js";
126         $aReturn[$szVar] = '<script src="'.$_SESSION['gszCoreWebPath'].
127                             '/widgets/js/cwc_roi.js" type="text/javascript"></script>';
128         
129         $szVar = "cwc_dhtml.js";
130         $aReturn[$szVar] = '<script src="'.$_SESSION['gszCoreWebPath'].
131                             '/widgets/js/cwc_dhtml.js" type="text/javascript"></script>';
132         return $aReturn;
133     }
134
135     /**
136      * @desc javascript mousemove functions
137      */
138     function GetJavascriptOnMouseMoveFunctions()
139     {
140         $aReturn = parent::GetJavascriptOnMouseMoveFunctions();
141         
142         $aReturn['ROIFeatureTool'] = 'ROIFeatureToolMouseMove(e);';
143
144         return $aReturn;
145     }
146
147     /**
148      * @desc general javascript functions
149      */
150     function GetJavascriptFunctions()
151     {
152         $aReturn = parent::GetJavascriptFunctions();
153
154         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
155           $bCWCJSAPI = 1;
156         else
157           $bCWCJSAPI = 0;
158           
159         // add JSAPI mode functions 
160         if ($bCWCJSAPI)
161         {
162             $szJsFunctionName = "ROIFeatureToolWRegisterForEvent";
163             $szFunction = <<<EOT
164 /**
165  * {$szJsFunctionName}
166  * called to register events when the layers change (JSAPI)
167  */
168 function {$szJsFunctionName}()
169 {
170     goCWCJSAPI.RegisterEvent(MAP_EXTENT_CHANGED, "ROIFeatureToolWExtentChanged");
171     goCWCJSAPI.RegisterEvent(MAP_PROJECTION_CHANGED, "ROIFeatureToolWExtentChanged");
172 }
173 EOT;
174
175            $aReturn[$szJsFunctionName] = $szFunction;
176            $szJsFunctionName = "ROIFeatureToolWExtentChanged";
177            $szFunction = <<<EOT
178 /**
179  * {$szJsFunctionName}
180  * called when the extent is changed (JSAPI)
181  */
182 function {$szJsFunctionName}()
183 {
184     // re-draw the ROI
185     goCWCROIManager.SetGeoExtents( goCWCJSAPI.oMap.minx, goCWCJSAPI.oMap.miny,
186                                     goCWCJSAPI.oMap.maxx, goCWCJSAPI.oMap.maxy );
187     goCWCROIManager.Redraw();
188 }
189 EOT;
190             $aReturn[$szJsFunctionName] = $szFunction;
191
192         }
193         
194         $nId = $this->mnId;
195         $szName = 'ROIFeatureTool'.$nId;
196         $szFunction = <<<EOT
197 function {$szName}()
198 {
199     goCWCROIManager.SetROITool('{$nId}');
200 }
201 EOT;
202         $aReturn[$szName] = $szFunction;
203
204         $szName = 'ROIRectToolInit';
205         $szFunction = <<<EOT
206 function {$szName}()
207 {
208
209     //ROIRectToolCreateDHTMLLayers();
210 }
211 EOT;
212         $aReturn[$szName] = $szFunction;
213         
214         
215         /**
216          * TODO: this does not reconstitute the points associated with the
217          * ROI tool because this is complicated ;)  In reality, the points may
218          * not be at valid locations.  The only reason to reconstitute the points
219          * would be to enable editing, which can't happen
220          */
221         $szObjectJS = "";
222          if (isset($_SESSION['ROIRENDERER']))
223         {
224             $nROI = count($_SESSION['ROIRENDERER']);
225             for ($i=0; $i<$nROI; $i++)
226             {
227                 $aROI = $_SESSION['ROIRENDERER'][$i];
228                 if ($aROI['type'] == 'feature')
229                 {
230                     $szObjectJS = "    var jsObj;\n";
231                     $szObjectJS .= "    goCWCROIManager.SetMode(".
232                                    $aROI['mode'] . ");\n";
233                     $szObjectJS .= "    jsObj = new CWCFeatureROI();\n";
234                     $szObjectJS .= "    jsObj.edgeColor = \"".$aROI['edgecolor']."\";\n";
235                     $szObjectJS .= "    jsObj.edgeWidth = ".$aROI['edgewidth'].";\n";
236                     $szObjectJS .= "    jsObj.fillLayer = goCWCROIManager.bUseFill;\n";
237                     $szObjectJS .= "    jsObj.fillColor = \"".$aROI['fillcolor']."\";\n";
238                     $szObjectJS .= "    jsObj.fillOpacity = ".$aROI['fillopacity'].";\n";
239                     $szObjectJS .= "    jsObj.szLayer = \"".$aROI['selectedLayer']."\";\n";
240                     $szObjectJS .= "    jsObj.geoX = ".$aROI['aOrigCoords'][0].";\n";
241                     $szObjectJS .= "    jsObj.geoY = ".$aROI['aOrigCoords'][1].";\n";
242                     $szObjectJS .= "    goCWCROIManager.Insert( jsObj, $i );\n";
243                 }
244             }
245             if (strlen( $szObjectJS ) > 0)
246             {
247                 $szObjectJS .= "    goCWCROIManager.UpdateLayerVisibility();\n";
248             }
249         }
250         
251         
252         $szName = 'ROIFeatureToolInitObjects';
253         $szFunction = <<<EOT
254 function {$szName}()
255 {
256 {$szObjectJS}
257 }
258 EOT;
259         $aReturn[$szName] = $szFunction;
260         
261         
262         $szName = 'ROIFeatureToolMouseUp';
263         $szFunction = <<<EOT
264 function {$szName}(e)
265 {
266     ROIFeatureToolGetMousePosition(e);
267
268     if (gROIToolmouseX >=0 && gROIToolmouseY >=0)
269     {
270         if ({$this->mszHTMLForm}.SELECTED_LAYERS != null &&
271             {$this->mszHTMLForm}.SELECTED_LAYERS.value != "")
272         {
273             var szLayer = {$this->mszHTMLForm}.SELECTED_LAYERS.value;
274             var oROIFeatureObj = new CWCFeatureROI();
275             oROIFeatureObj.edgeColor = goCWCROIManager.szEdgeColor;
276             oROIFeatureObj.edgeWidth = goCWCROIManager.nEdgeWidth;
277             oROIFeatureObj.fillLayer = goCWCROIManager.bUseFill;
278             oROIFeatureObj.fillColor = goCWCROIManager.szFillColor;
279             oROIFeatureObj.fillOpacity = goCWCROIManager.nFillOpacity;
280             oROIFeatureObj.Set( gROIToolmouseX, gROIToolmouseY, szLayer );
281             goCWCROIManager.Add( oROIFeatureObj );
282             oROIFeatureObj = null;
283                     
284         }
285         else
286         {
287             alert( "you must select a layer before using the ROI Feature Tool." );
288         }
289         
290     }
291     return false;
292 }
293 EOT;
294         $aReturn[$szName] = $szFunction;
295         
296         $szName = 'ROIFeatureToolGetMousePosition';
297         $szFunction = <<<EOT
298 function {$szName}(e)
299 {
300     if (CWCIsNav4 || CWCIsNav6) // Netscape
301     {               
302         gROIToolmouseX = e.pageX;
303         gROIToolmouseY = e.pageY;
304     }
305     else if (CWCIsIE) // IE
306     {           
307         gROIToolmouseX = event.clientX + document.body.scrollLeft;
308         gROIToolmouseY = event.clientY + document.body.scrollTop;
309     }
310     else
311     {                              // Don't know
312         gROIToolmouseX = gROIToolmouseY = 0;
313     }
314     var oImage = CWCDHTML_GetImage('mapimage');
315     
316     if (oImage == null)
317     {
318         gROIToolmouseX = -1;
319         gROIToolmouseY = -1
320         return false;
321     }
322     var left = CWCDHTML_FindObjectPosX( oImage );
323     var top = CWCDHTML_FindObjectPosY( oImage );
324     
325     var width = oImage.width;
326     var height = oImage.height;
327     
328     // subtract offsets from page left and top
329     gROIToolmouseX = gROIToolmouseX - left;
330     gROIToolmouseY = gROIToolmouseY - top;
331     
332     if (gROIToolmouseX < 0 || gROIToolmouseX > width ||
333         gROIToolmouseY < 0 || gROIToolmouseY > height)
334     {
335         gROIToolmouseX = -1;
336         gROIToolmouseY = -1
337     }
338     return true;
339 }
340 EOT;
341         $aReturn[$szName] = $szFunction;
342         $szName = 'ROIFeatureToolMouseMove';
343         $szFunction = <<<EOT
344 function {$szName}(e)
345 {
346     ROIFeatureToolGetMousePosition(e);
347     
348     if (gROIToolmouseX >=0 && gROIToolmouseY >=0 &&
349         {$this->mszHTMLForm}.NAV_CMD != null &&
350         {$this->mszHTMLForm}.NAV_CMD.value == "ROI_TOOL" &&
351         {$this->mnId} == goCWCROIManager.GetROITool())
352     {
353         document.onmousedown = null;
354         document.onmouseup = ROIFeatureToolMouseUp;   
355     }
356     else
357     {
358         if (document.onmouseup == ROIFeatureToolMouseUp)
359             document.onmouseup = null;
360     }
361     return false;
362 }
363 EOT;
364         $aReturn[$szName] = $szFunction;
365
366         return $aReturn;
367     }
368
369     /**
370      * GetJavascriptVariables
371      *
372      * Return JS global variables and global code.
373      */
374     function GetJavascriptVariables()
375     {
376         $aReturn = array();
377
378         $szVariable = "gROIToolmouseX";
379         $szValue = " var $szVariable = 0;\n";
380         $aReturn[$szVariable] = $szValue;
381
382         $szVariable = "gROIToolmouseY";
383         $szValue = " var $szVariable = 0;\n";
384         $aReturn[$szVariable] = $szValue;
385         
386         $szVariable = "goROIFeatureObj";
387         $szValue = " var $szVariable = null;\n";
388         $aReturn[$szVariable] = $szValue;
389         
390
391         return $aReturn;
392     }
393     
394     /**
395      * return an array of javascript functions needed by the Ruler widget
396      * and called when the page is loaded.
397      * @return array of name = function values
398      */
399     function GetJavascriptOnLoadFunctions()
400     {
401         $aReturn = parent::GetJavascriptOnLoadFunctions();
402
403         $aReturn["ROIFeatureToolInitObjects"] = "    ROIFeatureToolInitObjects();\n";
404         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
405         {
406             $szJsFunctionName = "ROIFeatureToolWRegisterForEvent";
407             $szFunction = "$szJsFunctionName();\n";
408             $aReturn[$szJsFunctionName] = $szFunction;
409         }       
410
411         return $aReturn;
412     }
413     
414     /**
415      * draw this widget on the page
416      */
417     function DrawPublish($szPostEvent = "")
418     {
419         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
420           $bCWCJSAPI = 1;
421         else
422           $bCWCJSAPI = 0;
423
424         if (!$this->mbVisible)
425             return "<!-- ROIFeatureTool hidden -->";
426
427         $szReturn = $this->moButton->DrawPublish();
428
429         return $szReturn;
430     }
431 }
432 ?>
433
Note: See TracBrowser for help on using the browser.