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

root/Chameleon/trunk/Chameleon/ows.php

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

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  *
4  * @project     Chameleon
5  * @revision    $Id: ows.php,v 1.2 2005/02/23 14:54:30 pspencer Exp $
6  * @purpose     OWS services
7  * @author      DM Solutions Group (pspencer@dmsolutions.ca)
8  * @copyright
9  * <b>Copyright (c) 2002, DM Solutions Group Inc.</b>
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  */
28
29 /**
30  * This module contains generic OWS services like loading contexts
31  */
32  
33 //access ms error messages 'cause stuff can go wrong loading contexts ...
34 include_once( 'ms_error.php' );
35
36 /**
37  * load a context into a map object.  Optionally append the layers
38  * to the context.
39  *
40  * @param szContextURL string the url to the context to load
41  * @param oMap object the map object to load the context into
42  * @param bAllowResize boolean if true, the map will be resized
43  * @param bAppendLayers boolean if true, just append new layers.  If false,
44  *        then remove existing layers and initialize MAP level information
45  *        such as map size, projection etc and metadata
46  * @return boolean true if the context loaded ok, false otherwise
47  */
48 function loadContext( $szContextURL, $oMap, $bAllowResize = true, $bAppendLayers = false )
49 {
50     $szLocalContext = retrieveContext( $szContextURL );
51     
52     //do all the work inside a temporary map object
53     //to avoid screwing up stuff for chameleon ...
54     $oTmpMap = ms_newMapObj( "" );
55     $bResult = $oTmpMap->loadMapContext( $szLocalContext );
56     if ($oTmpMap->numlayers == 0)
57     {
58         $szText = "ERROR: Invalid Context file";
59         $_SESSION['gErrorManager']->setError(ERR_WARNING, $szText);
60         return false;
61     }
62     else
63     {
64         //remove existing layers if not appending
65         if (!$bAppendLayers)
66         {
67             for ($i = 0; $i < $oMap->numlayers; $i++)
68             {
69                 $oLayer = $oMap->getLayer( $i );
70                 $oLayer->set( "status", MS_DELETE );
71             }
72         }
73
74         //import the newly defined layers
75         $nLayers = $oTmpMap->numlayers;
76         //echo "adding ".$nLayers." layers<BR>";
77         for ($i=0; $i<$nLayers; $i++)
78         {
79             ms_newLayerObj($oMap, $oTmpMap->getlayer($i));
80         }
81
82         //only modify map-level metadata and properties if not
83         //appending ...
84         if (!$bAppendLayers)
85         {
86             $oMap->set( "name", $oTmpMap->name );
87             $oMap->setProjection( $oTmpMap->getprojection(), MS_TRUE );
88             $oMap->extent->setExtent( $oTmpMap->extent->minx, $oTmpMap->extent->miny, $oTmpMap->extent->maxx, $oTmpMap->extent->maxy );
89             $oMap->setMetadata( "original_extents", "");
90             $oMap->setMetadata( "original_projection", "" );
91             $oMap->web->set( "minscale", $oTmpMap->web->minscale );
92             $oMap->web->set( "maxscale", $oTmpMap->web->maxscale );
93             
94             //TODO: check the list of metadata in each mapserver release ...
95             $aMetaData = array( "wms_abstract", "wms_accesscontraints",
96                                 "wms_addresstype", "wms_address",
97                                 "wms_city", "wms_stateorprovince",
98                                 "wms_postcode", "wms_country",
99                                 "wms_contactelectronicmailaddress",
100                                 "wms_contactfacsimiletelephone",
101                                 "wms_contactperson", "wms_contactorganization",
102                                 "wms_contactposition", "wms_contactvoicetelephone",
103                                 "wms_encoding","wms_fees", "wms_group_title",
104                                 "wms_keywordlist", "wms_onlineresource",
105                                 "wms_resx", "wms_resy", "wms_srs",
106                                 "wms_timeformat", "wms_title" );
107             
108             foreach($aMetaData as $szTag)
109             {
110                 $oMap->setmetadata( $szTag, $oTmpMap->getmetadata( $szTag ) );
111             }
112             
113         }
114
115         if ($bAllowResize)
116         {
117             $oMap->set( "width", $oTmpMap->width );
118             $oMap->set( "height", $oTmpMap->height );
119         }
120         
121         // Get mapserver error stack and display them as HTML comments.
122         $oError = ms_GetErrorObj();
123         while($oError && $oError->code > 0)
124         {
125             if (isset($this->moErrorManager))
126             {
127                 $szMsg = getMapServerErrorMessage( $oError );
128                 $_SESSION['gErrorManager']->setError(ERR_WARNING, $szMsg);
129             }
130             echo "<!-- ".$szMsg." -->";
131
132             $oError = $oError->next();
133         }
134     }
135     return true;
136 }
137
138 /**
139  * retrieve a remote context to a local file
140  * @param szURL string the context to retrieve
141  * @return string the local filename or false if it failed to download
142  */
143 function retrieveContext( $szURL )
144 {
145     if (strcasecmp( "http", substr($szURL, 0, 4 )) != 0)
146     {
147          return $szURL;
148     }
149     else
150     {
151         //at this point we need to fetch the file
152         $aszFile = @file( $szURL );
153         if ($aszFile === false)
154         {
155             $_SESSION["gErrorManager"]->setError(ERR_ERROR, "Error retrieving remote file ($szFilename), please ensure the URL is correct" );
156             $szLocalFile = false;
157         }
158         else
159         {
160             $szLocalFile = tempnam($_SESSION['gszTmpPath'], "remote").".cml";
161             $fh = @fopen($szLocalFile, "w");
162             if ($fh === false)
163             {
164                 $szLocalFile = false;
165                 $_SESSION["gErrorManager"]->setError(ERR_ERROR, "Error creating local copy ($szLocalFile) of remote file ($szFilename), please ensure permissions are correctly set." );
166             }
167             else
168             {
169                 $nbLine = count($aszFile);
170
171                 for($i=0; $i<$nbLine; $i++)
172                 {
173                     fwrite($fh, $aszFile[$i]);
174                 }
175                 fclose($fh);
176             }
177         }
178     }
179     return $szLocalFile;
180 }
181 ?>
Note: See TracBrowser for help on using the browser.