Passing Variable to a Map File

David Bitner osgis.lists at GMAIL.COM
Mon Dec 5 14:24:04 PST 2005


This is the script that I use for PHP Mapscript to do variable
substitution.  It cycles through the layers, looks at the data
definitition to see if there is anything defined in %<var>% notation. 
If there is and there is a corresponding request variable, it
substitutes it.  If there is not a corresponding request variable, it
turns the layer off so you don't get an error.

David

for ($i = 0; $i < $map->numlayers; $i++) {
	$activeLayer = $map->getlayer($i);
	$datastring=$activeLayer->data;
	if (preg_match_all("/%([\w].+?)%/",$datastring,$matches,PREG_SET_ORDER)>0){
		foreach ($matches as $match){
			$search=$match[0];
			$replace=$match[1];
			if (isset($_REQUEST[$replace])){
				$replaceval=$_REQUEST[$replace];
				$datastring=str_replace($search,$replaceval,$datastring);
				$activeLayer->set("data",$datastring);
			} else {
			$activeLayer->set("status", 0);
			}
		}
	
	}		
}

On 12/5/05, Ken Lord <kenlord at gmail.com> wrote:
> Hi Damon,
>
> I can't speak for MapScript,  but for MapServer CGI,  this can be
> found in the documentation referred to as variable substitution.
>
> Within the mapfile, you could replace part or all of a parameter in
> your mapfile with a variable,  MapServer is given the value of the
> variable by the form submission or the GET parameters in the web
> page's URL,  I've used javascript and ASP to modify hidden form
> values, or to alter the URL parameters for this effect with other
> parts of my map files ... I don't think I've tried it specificaly with
> the DATA parameter of a layer.
>
> Try searching the email archives on this, I'm not sure if the DATA
> parameter of a layer is accessible this way.
>
> Hopefully this will give you some clues for the MapScript side of things.
>
> Cheers,
> Ken Lord
> Vancouver BC
>
>
>
>
> On 12/5/05, Damon Broglie <brog at mail.utexas.edu> wrote:
> > I'm having a devil of a time working on this one.
> >
> > My setup:
> > * Linux server ( Fedore Core 3, Apache, etc. )
> > * MapServer ( 4.2.5 )
> > * MapLab w/ gMap Factory ( 2.2.1 )
> > * PHP/MapScript ( 1.194.2.6 )
> > * PostGIS-enabled postgreSQL database
> >
> > Things are running smoothly for simple map creation.  I can edit maps, add
> > PostGIS layers, etc. easily.  But I'm just a bit stumped when it comes to
> > making the map file's DATA attribute a variable and passing a SQL query
> > string to it.
> >
> > My ultimate goal is to take user input (in the form of dropdown lists - but
> > for right now I'm using a simple text box), create a query string from this
> > input, and then use it as the DATA attribute.
> >
> > So far, I have made a form element in the file automatically created by
> > gMap Factory ( <project_name>_contents.php ) which adds the string :
> >
> > sqlstring="the_geom from postgis_table"
> >
> > to the URL when a submit button is clicked.
> >
> > How can I get this string to be the DATA attribute in my <project_name>.map
> > file?
> >
> > The "MapServer MapFile Reference" mentions the ability to use variable
> > substitution in map files, which seems like the best route to
> > take.  However, it also states that "if you are using MapScript, you will
> > have to code the substitution logic in your application yourself (By
> > writing your own pre-processor)."
> >
> > I've found a few references here and there that touch on using percent
> > signs to create a variable like %DATA% in the map file, but no concrete
> > examples on how to actually pass the data to such a variable using
> > something like MapScript.
> >
> > Does anybody have some insight?
> >
> > Thanks a ton.
> >
> > --Damon
> > brog at mail dot utexas dot edu
> >
>



More information about the MapServer-users mailing list