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

root/Chameleon/trunk/Chameleon/context.php

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

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * this function tests a file name and downloads it as a local file in the
4  * session if it is a remote file, then returns the local filename.  If the
5  * file is already local, then it will simply return the same name.
6  * @param szFilename string the filename to get
7  * @param szExtension string an optional extension to place on the local file if the
8  * file is downloaded.  If the file is not downloaded, the filename is not
9  * changed.
10  * @return string a local filename or FALSE if fetching the remote file failed.
11  */
12 function getRemoteFile( $szFilename, $szExtension = "" )
13 {
14     if (strlen($szFilename) < 7)
15     {
16         $szLocalFile = $szFilename;
17     }
18     else if (strcasecmp( "http://", substr($szFilename, 0, 7 )) != 0)
19     {
20         $szLocalFile = $szFilename;
21     }
22     else
23     {
24         //at this point we need to fetch the file
25         $aszFile = @file( $szFilename );
26         if ($aszFile === false)
27         {
28             $_SESSION["gErrorManager"]->seError(ERR_ERROR, "Error retrieving remote file ($szFilename), please ensure the URL is correct" );
29             $szLocalFile = false;
30         }
31         else
32         {
33             $szLocalFile = tempnam($_SESSION['gszTmpPath'], "remote").".".$szExtension;
34             $fh = @fopen($szLocalFile, "w");
35             if ($fh === false)
36             {
37                 $szLocalFile = false;
38                 $_SESSION["gErrorManager"]->setError(ERR_ERROR, "Error creating local copy ($szLocalFile) of remote file ($szFilename), please ensure permissions are correctly set." );
39             }
40             else
41             {
42                 $nbLine = count($aszFile);
43
44                 for($i=0; $i<$nbLine; $i++)
45                 {
46                     fwrite($fh, $aszFile[$i]);
47                 }
48                 fclose($fh);
49             }
50         }
51     }
52     return $szLocalFile;
53 }
54
55 /**
56  * this function loads a context. The name passed to this function
57  * can be either a filename on the local filesystem OR a URL to a remote
58  * context.  If it is a remote context, it will be downloaded first and then
59  * loaded.
60  *
61  * If the file is a context collection, then each context in the
62  * collection will be loaded in order.
63  *
64  * @param szContextName string the name (file or URL) of the context or
65  * collection to load.
66  */
67 function loadContext( $szContextName, $bAllowResize = true )
68 {
69     //echo "loading context $szContextName<BR>";
70     $szLocalContext = $this->getRemoteFile( $szContextName, ".cml" );
71
72     $oMap = ms_newMapObj( "" );
73     $bResult = $oMap->loadMapContext( $szLocalContext );
74     if ($oMap->numlayers == 0)
75     {
76         $szText = $oMLT->get( "4", "ERROR: Invalid Context file" );
77         $this->moErrorManager->setError(ERR_WARNING, $szText);
78
79     }
80     else
81     {
82         //remove existing layers
83         for ($i = 0; $i < $this->moMapSession->oMap->numlayers; $i++)
84         {
85             $oLayer = $this->moMapSession->oMap->getLayer( $i );
86             $oLayer->set( "status", MS_DELETE );
87         }
88
89         //import the newly defined layers
90         $nLayers = $oMap->numlayers;
91         //echo "adding ".$nLayers." layers<BR>";
92         for ($i=0; $i<$nLayers; $i++)
93         {
94             ms_newLayerObj($this->moMapSession->oMap, $oMap->getlayer($i));
95             $oLayer = $this->moMapSession->oMap->getLayer( $i );
96         }
97
98         $this->moMapSession->oMap->set( "name", $oMap->name );
99         $this->moMapSession->oMap->setmetadata( "wms_title", $oMap->getmetadata( "wms_title" ) );
100         if ($bAllowResize)
101         {
102             $this->moMapSession->oMap->set( "width", $oMap->width );
103             $this->moMapSession->oMap->set( "height", $oMap->height );
104         }
105         $this->moMapSession->oMap->setProjection( $oMap->getprojection(), MS_TRUE );
106         $this->moMapSession->oMap->extent->setExtent( $oMap->extent->minx, $oMap->extent->miny, $oMap->extent->maxx, $oMap->extent->maxy );
107         $this->moMapSession->oMap->setMetadata( "original_extents", "");
108         $this->moMapSession->oMap->setMetadata( "original_projection", "" );
109         $this->moMapSession->oMap->web->set( "minscale", $oMap->web->minscale );
110         $this->moMapSession->oMap->web->set( "maxscale", $oMap->web->maxscale );
111
112         // Get mapserver error stack and display them as HTML comments.
113         $oError = ms_GetErrorObj();
114         while($oError && $oError->code > 0)
115         {
116             if (isset($this->moErrorManager))
117             {
118                 $szMsg = getMapServerErrorMessage( $oError );
119                 $this->moErrorManager->setError(ERR_WARNING, $szMsg);
120             }
121             echo "<!-- ".$szMsg." -->";
122
123             $oError = $oError->next();
124         }
125         $this->mszCurrentState = $this->moMapSession->saveState();
126         $_SESSION["gszCurrentState"] = $this->mszCurrentState;
127         $this->moMapSession->restoreState($this->mszCurrentState,
128         $this->mszMapFile,
129         dirname($this->mszMapFile));
130
131     }
132 }
133 ?>
Note: See TracBrowser for help on using the browser.