/[drupal]/contributions/modules/carto/wms-parser.php
ViewVC logotype

Contents of /contributions/modules/carto/wms-parser.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (hide annotations) (download) (as text)
Fri Jan 13 07:54:00 2006 UTC (3 years, 10 months ago) by openwereld
Branch: MAIN
Branch point for: DRUPAL-4-6, DRUPAL-4-7
File MIME type: text/x-php
wms-parser.php is used by carto.module for parsing the wms capabilities of
WMS nodes. This file is meant to be moved to the mappingwidgets project.
1 openwereld 1.1 <?php
2    
3     // CubeWerx requires at least 10M for wms capabilities
4     // ini_set( 'memory_limit', '10M' );
5    
6     /**
7     *
8     * inherited: +style, +crs, =latlonboundingbox, =boundingbox, +authorityurl
9     * =dimension, =attribution, =min/maxscaledenominator, =queryable, =cascaded
10     * =opaque
11     * attribution: title, onlineresource->xlink:href, logourl(format,onlineresource)
12     * style: name, title, legendurl(format,onlineresource->xlink:href)
13     */
14     class CapabilitiesParser
15     {
16     var $parser;
17    
18     var $version = NULL; // wms version
19     var $root = NULL; // root element name (depends on wms version)
20     var $path = ''; // current path (excluding root)
21    
22     var $layers = array();
23     var $layer_level = 0;
24     var $layer_props;
25     var $inAttribution = false;
26     var $inStyle = false;
27    
28     var $element = ''; // current element
29     var $cbLayer = NULL;
30     var $cbLayerParams = NULL;
31     var $numLayers = 0;
32    
33     function CapabilitiesParser( )
34     {
35     $this->parser = xml_parser_create( );
36     xml_parser_set_option( $this->parser, XML_OPTION_CASE_FOLDING, 0 );
37     xml_set_element_handler( $this->parser, array(&$this,"_startElement"), array(&$this,"_endElement") );
38     xml_set_character_data_handler( $this->parser, array(&$this,"_characterData") );
39     $this->layer_level = 0;
40     $this->layer_props = array();
41     $this->layer_props[ $this->layer_level ] = array( );
42     $this->layers = array( );
43     }
44    
45     function free_parser( )
46     {
47     xml_parser_free( $this->parser );
48     }
49    
50     function inSomething()
51     {
52     return $this->inAttribution || $this->inStyle !== false;
53     }
54    
55     function parse( $capabilities, $cbLayer = NULL, $cbLayerParams = NULL )
56     {
57     $this->cbLayer = $cbLayer;
58     $this->cbLayerParams = $cbLayerParams;
59     return xml_parse( $this->parser, $capabilities, TRUE );
60     }
61    
62     function _startElement( $parser, $name, $attrs )
63     {
64     if( ! $this->root )
65     {
66     $this->root = $name;
67     $this->version = $attrs['version'];
68     }
69     else
70     {
71     $this->path .= '/' . $name;
72     }
73     $this->element = $name;
74    
75     switch( $name )
76     {
77     case 'Attribution':
78     $this->inAttribution = true;
79     break;
80     case 'Style':
81     $this->inStyle = true;
82     // $this->layer_props[ $this->layer_level ]['Style'] = array( );
83     break;
84     case 'OnlineResource':
85     if( $this->inStyle !== false )
86     {
87     $this->layer_props[ $this->layer_level ]['Style'][ $this->inStyle ] = $attrs['xlink:href'];
88     }
89     break;
90     case 'Layer':
91     // the previous layer is a parent layer of the new one
92     if( ! isset($this->layer_props[ $this->layer_level ]['layer_id']) )
93     {
94     $this->layer_props[ $this->layer_level ]['layer_id'] = $this->numLayers++;
95     if( $this->cbLayer )
96     eval( $this->cbLayer . "(\$this->layer_props[\$this->layer_level], \$this->cbLayerParams );" );
97     else
98     {
99     // print $this->layer_level . ' : ' . var_export($this->layer_props[ $this->layer_level ],true) . '<br>';
100     $this->layers[] = $this->layer_props[ $this->layer_level ];
101     }
102     }
103     else
104     {
105     // adjust layer of $this->layer_props[ $this->layer_level ]
106     // count child layers?
107     }
108     $this->layer_level += 1;
109     // copy layer_props for new layer level and merge new attrs
110     // @todo: merge only inherited props!
111     $inherited_values = array();
112     $inherited_properties = array( 'Style', 'SRS', 'LatLonBoundingBox', 'BoundingBox', 'queryable', 'opaque', 'cascaded' );
113     foreach( $inherited_properties as $prop )
114     {
115     if( isset($this->layer_props[ $this->layer_level - 1 ][$prop]) )
116     $inherited_values[$prop] = $this->layer_props[ $this->layer_level - 1 ][$prop];
117     }
118     $this->layer_props[ $this->layer_level ] = array_merge( $inherited_values, $attrs );
119    
120     // print var_export( array($name,$attrs,$this->layer_level), true ) . '<br>';
121     break;
122     case 'LatLonBoundingBox';
123     if( $this->layer_level > 0 )
124     $this->layer_props[ $this->layer_level][ $name ] = $attrs;
125     break;
126     case 'BoundingBox':
127     if( $this->layer_level > 0 )
128     {
129     if( ! isset($this->layer_props[ $this->layer_level][ $name ]) )
130     $this->layer_props[ $this->layer_level][ $name ] = array();
131     $SRS = $attrs['SRS'];
132     unset( $attrs['SRS'] );
133     $this->layer_props[ $this->layer_level][ $name ][$SRS] = $attrs;
134     }
135     break;
136     default:
137     // print $this->path . ':' . var_export($attrs,true) . '<br>';
138     break;
139     }
140     // print $this->path . ': attrs = ' . var_export($attrs,true) . '<br>';
141     }
142    
143     function _characterData( $parser, $data )
144     {
145     if( $this->element != '' )
146     {
147     $text = trim($data);
148     if( $this->layer_level > 0 && ! $this->inSomething() )
149     {
150     switch( $this->element )
151     {
152     case 'Title':
153     case 'Name':
154     case 'Abstract':
155     // if not a style name!!
156     $this->layer_props[ $this->layer_level][ $this->element ] = $text;
157     break;
158     case 'SRS':
159     if( isset($this->layer_props[ $this->layer_level][ $this->element ])
160     && $this->layer_props[ $this->layer_level][ $this->element ] != '' )
161     {
162     $this->layer_props[ $this->layer_level][ $this->element ] .= ' ';
163     }
164     $this->layer_props[ $this->layer_level][ $this->element ] .= $text;
165     break;
166     }
167     }
168     else if( $this->inStyle !== false )
169     {
170     switch( $this->element )
171     {
172     case 'Name':
173     $this->inStyle = $text;
174     break;
175     }
176     }
177     // print $this->path . ':<b>' . $this->element . '=' . $text . '|</b><br>';
178     // print '---> ' . $this->layer_level . '/' . var_export($this->layer_props[ $this->layer_level],true) . '<br>';
179     $this->element = '';
180     }
181     }
182    
183     function _endElement( $parser, $name )
184     {
185     $this->path = substr( $this->path, 0, -(1+strlen($name)) );
186     switch( $name )
187     {
188     case 'Layer':
189     if( ! $this->layer_props[ $this->layer_level ]['layer_id'] )
190     {
191     $this->layer_props[ $this->layer_level ]['layer_id'] = $this->numLayers++;
192     if( $this->cbLayer )
193     eval( $this->cbLayer . "(\$this->layer_props[\$this->layer_level], \$this->cbLayerParams );" );
194     else
195     {
196     // print $this->layer_level . ' : ' . var_export($this->layer_props[ $this->layer_level ],true) . '<br>';
197     $this->layers[] = $this->layer_props[ $this->layer_level ];
198     }
199     }
200     $this->layer_level -= 1;
201     array_pop( $this->layer_props );
202     break;
203     case 'Attribution':
204     $this->inAttribution = false;
205     break;
206     case 'Style':
207     $this->inStyle = false;
208     break;
209     default:
210     break;
211     }
212     }
213    
214     } // CapabilitiesParser
215    
216     ?>

  ViewVC Help
Powered by ViewVC 1.1.2