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

root/Chameleon/trunk/Chameleon/LayerFeatures/LayerFeatures.widget.php

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

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * LayerFeatues Widget class
4  *
5  * @project     GeoDan OWT Chart
6  * @revision    $Id: LayerFeatures.widget.php,v 1.11 2004/10/14 18:19:15 pspencer Exp $
7  * @purpose     LayerFeatures Widget class puts the features of a layer
8  *              into a shared resource for the use in a table widget
9  * @author      DM Solutions Group (dev@dmsolutions.ca)
10  * @copyright
11  * <b>Copyright (c) 2004 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__)."/../LayerAttributes.php" );
33
34 /**
35  * LayerFeatures Widget
36  *
37  * @desc a widget that extracts the features of a map layer and inserts it
38  *       into a SharedResource.
39  */
40 class LayerFeatures extends CWCWidget
41 {
42     /* the name of the shared resource to dump the results into */
43     var $mszSharedResourceName;
44    
45     // the name of the layer template mapfile
46     var $mszLayerTemplateFile;
47    
48     /**
49      * LayerFeatures
50      *
51      * Constructor method for the LayerFeatures widget.
52      */
53     function LayerFeatures()
54     {
55         // invoke constructor of parent
56         parent::CWCWidget();
57        
58         // set the priority to low so that it is processed almost last
59         $this->mnPriority = PRIORITY_LAST;
60
61         // set the description for this widget
62         $this->szWidgetDescription = <<<EOT
63 The LayerFeatures widget gets the contents of a mapfile layer and adds an array to
64 a SharedResource widget in a format suitable for feeding a table widget.
65 The layer to process can be passed in the url as "selectedlayers" or be
66 set in the layer's metadata.
67 EOT;
68         $this->maAttributes["SHAREDRESOURCENAME"] =
69             new StringAttribute( "SHAREDRESOURCENAME", true );
70            
71         $this->maAttributes["LAYERTEMPLATEFILE"] =
72             new StringAttribute( "LAYERTEMPLATEFILE", true );     
73         $this->mnMaturityLevel = MATURITY_BETA;
74     }
75
76     /**
77      * initialize respectable defaults
78      */
79     function InitDefaults()
80     {
81         parent::InitDefaults();
82
83         if ( isset( $this->maParams["SHAREDRESOURCENAME"] ) )
84         {
85             $this->mszSharedResourceName =
86                 $this->maParams["SHAREDRESOURCENAME"];
87         }
88         if ( isset( $this->maParams["LAYERTEMPLATEFILE"] ) )
89         {
90             $this->mszLayerTemplateFile =
91                 $this->maParams["LAYERTEMPLATEFILE"];
92         }               
93     }
94    
95     /**
96      * return an array of HTML variables to include in the page
97      */
98     function GetHTMLHiddenVariables()
99     {
100         $aReturn = array();
101         $szVariable = 'WFS_FILTER';
102         $szValue = "<INPUT TYPE=hidden NAME=$szVariable VALUE=\"".
103                                         $this->getVar("WFS_FILTER")."\">\n";
104         //$szValue = "<INPUT TYPE=text NAME=$szVariable VALUE=\"%3Cor%3E%3CPropertyIsEqualTo%3E%3CPropertyName%3EDIRNAAM%3C%2FPropertyName%3E%3CLiteral%3ELimburg%3C%2FLiteral%3E%3C%2FPropertyIsEqualTo%3E%3CPropertyIsEqualTo%3E%3CPropertyName%3EDIRNAAM%3C%2FPropertyName%3E%3CLiteral%3EZeeland%3C%2FLiteral%3E%3C%2FPropertyIsEqualTo%3E%3C%2For%3E\">\n";
105         $aReturn[$szVariable] = $szValue;
106         $szVariable = 'WFS_SELECTED_LAYER';
107         $szValue = "<INPUT TYPE=hidden NAME=$szVariable VALUE=\"".
108                                         $this->getVar("WFS_SELECTED_LAYER")."\">\n";
109         $aReturn[$szVariable] = $szValue;                   
110         $szVariable = 'LF_CLASS_FILTER';
111         $szValue = "<INPUT TYPE=hidden NAME=$szVariable VALUE=\"".
112                                             $this->getVar("LF_CLASS_FILTER")."\">\n";
113         $aReturn[$szVariable] = $szValue;       
114         return $aReturn;
115     }
116    
117     function GetJavascriptOnLoadFunctions()
118     {
119         $aReturn = array();
120         $aReturn = parent::GetJavascriptOnLoadFunctions();
121         $szJsFunctionName = "readSRtoJS";
122         $szFunction = "$szJsFunctionName();\n";
123         $aReturn[$szJsFunctionName] = $szFunction;
124         return $aReturn;
125     }   
126    
127     function GetJavascriptVariables()
128     {
129         $aReturn = array();
130         $aReturn = parent::GetJavascriptVariables();
131         $aReturn['gaxSRFeatures'] = "var gaxSRFeatures = new Array();\n";
132         return $aReturn;
133     }
134    
135     /**
136      * Get all the javascript functions
137      * shared resource.
138      */   
139     function GetJavascriptFunctions()
140     {
141         $aReturn = array();
142        
143         // loop and build the javascript array
144         $szJSArray = '';
145         $nCount = count( $this->maSharedResourceWidgets[$this->
146                                         mszSharedResourceName]->maszContents );
147         for( $i=0; $i<$nCount; $i++ )
148         {
149             // create a new field array
150             $szJSArray .= 'gaxSRFeatures['.$i.'] = new Array();'."\n";
151            
152             // loop for each field
153             $j=0;
154             foreach( $this->maSharedResourceWidgets[$this->
155                                         mszSharedResourceName]->maszContents[$i] as
156                      $key=>$value )
157             {
158                 // create a new key value array for this field and populate
159                 $szJSArray .= 'gaxSRFeatures['.$i.']['.$j.'] = new Array();'."\n";
160                 $szJSArray .= 'gaxSRFeatures['.$i.']['.$j.'][0] = "'.$key.'";'."\n";
161                 $szJSArray .= 'gaxSRFeatures['.$i.']['.$j.'][1] = "'.$value.'";'."\n";
162                 $j++;
163             }
164         }
165            
166         // add an on load function to make the shared resource into a javascript var
167         $szJsFunctionName = "readSRtoJS";
168         $szFunction = <<<EOT
169 /**
170  * {$szJsFunctionName}
171  * Convert the shared resource into a javascript array
172  */
173 function {$szJsFunctionName}()
174 {
175     // convert the SR to JS array
176     {$szJSArray}
177
178     // return
179     return;
180 }
181 EOT;
182
183         $aReturn[$szJsFunctionName] = $szFunction;
184
185        return $aReturn;
186     }
187    
188        
189     /**
190      * Parse the layer and create the necessary entries in the
191      * shared resource.
192      */
193     function ParseURL()
194     {
195         // set the timeout
196         set_time_limit( 0 );
197
198         // set shared resource variable
199         $srName = $this->mszSharedResourceName;
200        
201         // set the map object
202         $oMap = $this->moMapObject->oMap;
203        
204         // check of the layer has been passed on the url
205         if( $this->isVarSet( "WFS_SELECTED_LAYER" ) &&
206              strlen( $this->getVar("WFS_SELECTED_LAYER") ) > 0 )
207         {
208             $szSelectedLayer = $this->getVar("WFS_SELECTED_LAYER");
209         }
210         else
211         {
212             // loop through the layers and check for metadata
213             for( $i=0; $i<$oMap->numlayers; $i++ )
214             {
215                 // get layer object and check metadata
216                 $oLayer = $oMap->getlayer( $i );
217                 if ( $oLayer->getmetadata("selected") == 1 )
218                 {
219                     $szSelectedLayer = $oLayer->name;
220                     break;
221                 }
222             }
223
224             // return an empty array to the shared resource
225             if ( !isset( $szSelectedLayer ) )
226             {
227                 $this->maSharedResourceWidgets[$srName]->maszContents = array();
228                 return true;
229             }
230         }
231        
232         // check if the layer name exists
233         if ( strlen( $szSelectedLayer ) <= 0 )
234         {
235             $this->maSharedResourceWidgets[$srName]->maszContents = array();
236             return true;           
237         }
238        
239         // get the layer object
240         if ( $this->isVarSet( 'LF_CLASS_FILTER' ) && ($this->getVar('LF_CLASS_FILTER') != ""))
241                                 {
242                 $oLayer = $this->getWMSLayer( $szSelectedLayer );
243         }
244        
245         // set wfs_filter
246         if ( $this->isVarSet( "WFS_FILTER" ) &&
247              strlen( $this->getVar("WFS_FILTER") ) > 0 )
248         {
249             $oLayer->setmetadata( 'wfs_filter',
250                                     urldecode( $this->getVar("WFS_FILTER") ) );
251         }
252
253         // build array of attributes to return
254         $aszAttributes = array();
255        
256         // get and re-order the results for the shared resource
257         $aszTmpResults = array();
258         $aszResults = GetLayerFeatures( $oMap, $oLayer, $aszAttributes );
259                
260         // get the layer's features and push them into shared resource
261         $this->maSharedResourceWidgets[$srName]->maszContents = $aszResults;
262                
263         // return success
264         return true;
265     }
266
267     function getWMSLayer( $szSelectedLayer )
268     {
269         // init vars
270         $oMap = $this->moMapObject->oMap;
271         $szTempLayerName = 'layer_features_widget_tmp';
272         $szClassItem = '';
273         $szClassExpression = '';
274        
275         // get the class item and expression
276         if ( $this->isVarSet( 'LF_CLASS_FILTER' ) )
277         {
278             $aszClass = explode( ",", urldecode( $this->getVar( 'LF_CLASS_FILTER' ) ) );
279             if ( isset( $aszClass[0] ) )
280             {
281                 $szClassItem = $aszClass[0];
282             }
283             if ( isset( $aszClass[1] ) )
284             {
285                 $szClassExpression = $aszClass[1];
286             }
287         }
288        
289         // check if the temp layer exists
290         $aszMapLayers = $oMap->getAllLayerNames();
291         if ( in_array( $szTempLayerName, $aszMapLayers ) )
292         {
293             // get layer
294             $oLayer =  $oMap->getLayerByName( $szTempLayerName );
295         }
296         else
297         {
298             // create layer from template layer
299             $oTemplateMap = ms_newMapObj( $this->mszLayerTemplateFile );
300             $oTemplateLayer = $oTemplateMap->getLayer( 0 );
301             $oLayer = ms_newLayerObj( $oMap, $oTemplateLayer );
302             $oLayer->set( 'name', $szTempLayerName );             
303            
304         }
305        
306         // update necessary info
307         $oLayer->set( 'classitem', $szClassItem );
308         $oLayer->setmetadata( 'wms_name', $szSelectedLayer );
309         $oLayer->setmetadata( 'wms_title', $szSelectedLayer );
310         $oLayer->setmetadata( 'wms_sld_body', 'AUTO' );       
311        
312         // get the hilite class and set expression
313         $nCount = $oLayer->numclasses;
314         for( $i=0; $i<$nCount; $i++ )
315         {
316             $oClass = $oLayer->getclass($i);
317             if ( strtolower( $oClass->name ) == 'hilite' )
318             {
319                 $oClass->setexpression( $szClassExpression );
320                 break;
321             }
322         }
323        
324         // return ponter to new layer
325         return $oLayer;
326     }   
327 }
328
329 ?>
Note: See TracBrowser for help on using the browser.