Hi Mark. (I send this message again because previously I use the wrong mail address and I presume the mail will not be delivered to the list). A few month ago (Jan 22) Stefan Schwarzer ask a similar
question and in that moment I send him my response outside the list. I
paste here my last mail to him in wich I put a long explanation about
all the steps I had to do to use PHP Mapscript with mscross. I dont
even remember what I wrote but I paste it here. If you have any
question ask me again. Sorry my english. Rodrigo.
<br><br>Hi. Ok, lets see if with some
examples I can show you what I was talking about previously. Sorry if
sometimes I ask basics questions or explain some basic stuff but I
allways try to explain in a way that even my mother can understand what
I&#39;m talking about.
<br><br>First of all, if you play around a little bit with the mscross
code, you will see that the way mscross obtains the image of the map is
asking directly to mapserver cgi interface, generating the necesary url
and assigning it to an &lt;img&gt; HTML element (created dinamically by
de javascript code). So, what ajax interface was expecting from the
server is an image file and not an HTML file. That is why you must
modify your PHP Mapscript file to return an image file instead of just
HTML code.
<br><br>So, lets go with a simple example... To make it simple, supose
you have a mapfile ready to use with mapserver and do not need to
create anything dinamically so the PHP Mapscript becomes quite simple.
Lets supose the mapfile have a png OUTPUTFORMAT, and three LAYERS named
&quot;countrys&quot; &quot;rivers&quot; and &quot;sea&quot;. If the mapfile is located in /mymaps/
and it name is &quot; example1.map&quot;, then the CGI url to obtain a map (the
one generated by mscross) is something like this:<br><br><a href="http://myserver/cgi-bin/mapserv.exe" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://myserver/cgi-bin/mapserv.exe</a>? map=/mymaps/example1.map&amp;mode
<div style="margin-left: 40px;">=map&amp;mapext=5649512+6099485+5711778+6144474&amp;mapsize=800+600&amp; layers=countrys%20rivers%20sea
<br><br>(note that I&#39;m using mapserver for windows but on linux the url will be almost the same)<br><br>The &quot;object&quot; returned by mapserver CGI interface will be an Image object ( i.e. the content of the image).
<br><br>So the first thing you need to use your PHPMapscript with the
mscross interface is make that your PHP Mapscript file return an image
object and not a html object with an &lt;img&gt; element on it. So,
lets see the contest of this simple PHP Mapscript file:
<br><br>&lt;?php<br>header(&quot;Content-type: image/png&quot;);<br>//Before we send any data to the browser we set the header &quot;Content-type&quot;<br>//of the response to &quot;image/png&quot; so the browser interprets the content of the response as a png image file.
<br><br>$map = ms_newMapObj(&quot;/mymaps/example1.map&quot;);<br>// We create the map object based on the example1.map file as template<br><br>$map-&gt;setExtent(5649512,6099485,5711778,6144474);<br>// We set the extent of map
<br><br>$map-&gt;setSize(800,600);<br>// and set the image size (resolution)<br><br>$map-&gt;getLayerByName(&quot;countrys&quot;)-&gt;set(status,MS_ON);<br>$map-&gt;getLayerByName(&quot;rivers&quot;)-&gt;set(status,MS_ON);
<br>$map-&gt;getLayerByName(&quot;sea&quot;)-&gt;set(status,MS_ON);<br>// Set the status of three layers to on<br><br>$image=$map-&gt;draw();<br>$imagename=$image-&gt;saveWebImage();<br>//Draw the image to a temp file and copy image name to the $imagename variable
<br><br>$image = ImageCreateFromPng(&quot;/ms4w/tmp/&quot;.$imagename);<br>// Read the image saved previously to the $image variable<br><br>imagePng($image);<br>// Return the image content to the browser<br>?&gt;<br><br>
Maybe
there is a way to avoid the save to disk and read from disk operation
but I didn&#39;t waste too much time looking for an alternative. So the
previous PHP Mapscript will create a mapobject, set the properties
properly and then return the image to the browser just like an image
object.
<br><br>If you call this php file from your browser, the browser will
get a png image file with the map drawed. Obviously the PHP Mapscript
could be more complex, create layers, class and labels dinamically, but
it must be always return an image file which is achieved with the
header function in conjunct with the imagePng call at the end of the
Mapscript.
<br><br>The
next thing to do is make this Mapscript more flexible, so it could
recieve the map extent, map size and layers (and maybe the mapfile
even) as parameters. Sinse mscross use the GET method to obtain images
we can modify our Mapscript file to get this parameters and set it
properly. So, if we mantain the variables name as the ones used to get
images from CGI interface to make it more simple we can modify our PHP
Mapscript file so it can be more flexible and modify its output
according to the url parameters:
<br><br>&lt;?php<br>header(&quot;Content-type: image/png&quot;);<br><br>$map = ms_newMapObj($_GET[&#39;map&#39;]);<br>// We create the map object based on the mapfile received as parameter<br><br>$extent = explode(&quot; &quot;,$_GET[&#39;mapext&#39;]);
<br>$map-&gt;setExtent($extent[0], $extent[1], $extent[2], $extent[3]);<br>// We get the mapext parameter... split it on its 4 parts using the space character as splitter<br><br>$size = explode(&quot; &quot;,$_GET[&#39;mapsize&#39;]);
<br>$map-&gt;setSize($size[0], $size[1]);<br>// and set the image size (resolution) based on mapsize parameter<br><br>$layerslist=$_GET[&#39;layers&#39;];<br>for ($layer = 0; $layer &lt; $map-&gt;numlayers; $layer++) {<br>

&nbsp;&nbsp;&nbsp; $lay = $map-&gt;getLayer($layer);<br>&nbsp;&nbsp;&nbsp; if ((strpos($layerslist,($map-&gt;getLayer($layer)-&gt;name)) !== false) or (($map-&gt;getLayer($layer)-&gt;group != &quot;&quot;) and (strpos($layerslist,($map-&gt;getLayer($layer)-&gt;group)) !== false))){
<br>// if the name property of actual $lay object is in $layerslist<br>// or the group property is in $layerslist then the layer was requested<br>//so we set the status ON... otherwise we set the stat to OFF<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $lay-&gt;set(status,MS_ON);
<br>&nbsp;&nbsp;&nbsp; } else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $lay-&gt;set(status,MS_OFF);<br>&nbsp;&nbsp;&nbsp; }<br>}<br><br>// The next lines are the same as previous mapscript<br>$image=$map-&gt;draw();<br>$imagename=$image-&gt;saveWebImage();<br>$image = ImageCreateFromPng(&quot;/ms4w/tmp/&quot;.$imagename);
<br>imagePng($image);<br>?&gt;<br><br>If you want to get a map drawed throw this mapscript, you must now pass various parameters in the url... something like this:<br><br><a href="http://myserver/myPHPMapscript.php?map=/mymaps/example1.map&amp;mapext=5649512+6099485+5711778+6144474&amp;" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">

http://myserver/myPHPMapscript.php?map=/mymaps/example1.map&amp;mapext=5649512+6099485+5711778+6144474&amp;</a> mapsize=800+600&amp;layers=countrys%20rivers%20sea<br><br>Again, the Mapscript could be more complex but always must return an image object.
<br><br>So the last thing we must do is get it work with the mscross
ajax... Lets work with the last version of mscross javascript available
at:<br><a href="http://datacrossing.crs4.it/download.php?l=en&amp;id=mscross-1.1.8.js" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://datacrossing.crs4.it/download.php?l=en&amp;id=mscross-1.1.8.js</a><br><br>First
take a look at the get_map_url() method of mscross class on
mscross-1.1.8.js file... It looks something like this (I remove some
lines which gives support to use wms servers):
<br><br>&nbsp; this.get_map_url = function()<br>&nbsp; {<br>&nbsp;&nbsp;&nbsp; var my_url;<br><br>&nbsp;&nbsp;&nbsp; if (_protocol == &#39;mapservercgi&#39;)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var size = &#39;mapsize=&#39; + (_map_w+_map_w_bord+_map_w_bord) + &#39;+&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + (_map_h+_map_h_bord+_map_h_bord);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var ext&nbsp; = &#39;mapext=&#39; + (_ext_Xmin-i.wPixel2real(_map_w_bord)) + &#39;+&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + (_ext_Ymin-i.hPixel2real(_map_h_bord)) + &#39;+&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + (_ext_Xmax+i.wPixel2real(_map_w_bord)) + &#39;+&#39;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + (_ext_Ymax+i.hPixel2real(_map_h_bord)) ;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my_url = _cgi + &#39;?mode=&#39; + _mode + &#39;&amp;&#39; + _map_file + &#39;&amp;&#39; +<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ext + &#39;&amp;&#39; + size + &#39;&amp;layers=&#39; + _layers;
<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Opera9 Bug Fix (onload event don&#39;t work if image is in cache)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (browser.isOP ) {my_url = my_url + &#39;&amp;&#39; + Math.random();}<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; return my_url + &#39;&amp;&#39; + _args;
<br>&nbsp; }<br><br>Basically, this function build the url of the Mapserver
CGI interface, so we must get it work with our recently created PHP
Mapscript file. If you take a look at the line in bold, there is where
the url is created:
<br>my_url = _cgi + &#39;?mode=&#39; + _mode + &#39;&amp;&#39; + _map_file + &#39;&amp;&#39; + ext + &#39;&amp;&#39; + size + &#39;&amp;layers=&#39; + _layers;<br><br>there
are some fixed (strings) parts and some variable parts in this line...
first it put the _cgi content which is set using the mscross setCgi()
method. Then it add the mode parameter that we dont need to use so it
could be removed. Then the mapfile parameter which is set using the
mscross setMapFile() method. afther mapfile parameter it puts the
extent parameter at ext variable which is created some lines before and
is based on the mscross object properties setted by the setFullExtent()
method and the size parameter (width and height of image) which are
calculated on the mscross object creation from container div width and
height. Finaly it pass the layers parameter based on _layers variable
setted by the mscross setLayers() method.
<br><br>So we want to get this url look like the one we create for the
second PHP Mapscript file, so we need to set the variables properly to
get my_url look like:<br><br><a href="http://myserver/myPHPMapscript.php" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://myserver/myPHPMapscript.php
</a> ?map=/mymaps/example1.map&amp;mapext=5649512+6099485+5711778+6144474&amp;mapsize=800+600&amp; layers=countrys%20rivers%20sea<br><br>The way to obtain this is:<br>set the _cgi variable to &quot;<a href="http://myserver/myPHPMapscript.php" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">

http://myserver/myPHPMapscript.php</a>&quot; using the setCgi() method.<br>set the mapfile to &quot;/mymaps/example1.map&quot; using the setMapFile() method.<br>set map extentention to (5649512,6099485,5711778,6144474) using the setFullExtent() method.
<br>and set the layers to &quot;countrys rivers sea&quot; using the setLayers() method<br>(the map size is setted automatically when the mscross object is created).<br>and
finally if yo want to avoid the pass of mode parameter you can modify
the get_map_url() method and remove it from the line as follows:
<br><br>my_url = _cgi + &#39;?&#39; + _map_file + &#39;&amp;&#39; + ext + &#39;&amp;&#39; + size + &#39;&amp;layers=&#39; + _layers;<br><br>So,
at this point we can make a simple html file to get our mscross and PHP
Mapscript working together... a simple html could be the next one:
<br><br>&lt;html&gt;<br>&lt;script src=&quot;mscross-1.1.8.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;<br>&lt;body&gt;<br>&lt;div
id=&quot;map_tag&quot; style=&quot;position:absolute; top:10px; left:10px; width:
800px; height: 600px; border-width:1px; border-color:#000088;
border-style:solid;&quot;&gt;&lt;/div&gt;
<br>&lt;/body&gt;<br>&lt;script type=&quot;text/javascript&quot;&gt;<br>myMap = new msMap(document.getElementById(&#39;map_tag&#39;),&#39; &#39;);<br>myMap.setCgi( &#39;<a href="http://myserver/myPHPMapscript.php" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://myserver/myPHPMapscript.php
</a> &#39; );<br>myMap.setFullExtent( 5649512,5711778,6099485);<br>myMap.setMapFile( &#39;/mymaps/example1.map&#39; );<br>myMap.setLayers(&#39;countrys rivers sea&#39;);<br>myMap.redraw();<br>&lt;/script&gt;<br>&lt;/html&gt;
<br><br>Ok, I think that this is all :P. I hope I was clear enough but
I encourage you to ask me whatever you want. Sorry my veeeeeery long
mail and I hope that it be helpful.<br><br>Greetings from Argentina.</div><br><br><div><span class="gmail_quote">On 6/11/07, <b class="gmail_sendername">Mark Brooks</b> &lt;<a href="mailto:mark_brooks@ncsu.edu">mark_brooks@ncsu.edu
</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I&#39;m trying to implement msCross (ajax) ith mapserver and mapscript
<br>(PHP).&nbsp;&nbsp;My maps are dynamic and are usually created on-the-fly based on<br>user input.&nbsp;&nbsp;Therefore, much of my code is PHP using mapscript.<br><br>Does anyone have any examples I can see that demonstrate the integration
<br>of msCross with mapscript?<br><br>Much thanks,<br>Mark<br>NC State University<br></blockquote></div><br>