[ka-Map-users] Configuring Ka-Map! at the web page level (alternative init.php & config.php)

Steve Walker walker at mfgis.com
Thu Jan 25 22:28:56 EST 2007


I would like to share here a strategy I have been pursuing which allows me
to configure Ka-Map! (in part) from the web page level (or more precisely,
from the web pages' file system path level).

The motivation was my desire to create a separate web page for multiple
cities,  states, countries, etc., and to specify important Ka-Map!
parameters (namely the map name and map extent) associated with these
pages from the same file system path as the web page itself.

I have been able to achieve this by making changes to init.php and to
config.php (as spelled out below) as well as introducing new files named
variables.php and load_mapfile.php

As an example, I will use the following four pages:

http://usmapserver.com/Florida/Miami
http://usmapserver.com/Illinois/Springfield
http://usmapserver.com/Illinois/Joliet and
http://usmapserver.com/Illinois/Chicago

And the following three MapServer map files:
Florida.map
Illinois.map
Chicago.map


In this example, I want the Miami page to draw the Florida.map, while
Springfield and Joliet can each reference the same Illinois.map.  Chicago
however, has some specialized content and a different map projection than
the rest of Illinois, so I have created a separate Mapserver mapfile
specific to Chicago.

At the file system level then the Miami page will include the following
/var/www/us/htdocs/Florida/Miami/index.phtml  (the index page)
/var/www/us/htdocs/Florida/Miami/init.php   (my modified Ka-Map! init.php)
/var/www/us/htdocs/Florida/Miami/variables.php

and similarly for the other pages.

The key here is my new variables.php file and the modifications to
init.php which allow it to read variables.php.

Variables.php (Miami)
<?
$szMap='Florida';
$extent='-80.32,25.71,-80.14,25.86;
// also, what ever else I want index.phtml to read - $cityName='Miami' etc.
?>

Variables.php (Springfield)
<?
$szMap='Illinois';
$extent='-89.754,39.654,-89.569,39.874';
// also, what ever else I want - $cityName='Springfild' etc.
?>

Variables.php (Joliet)
<?
$szMap='Illinois';
$extent='-88.282,41.457,-87.99,41.587';
// also, what ever else I want - $cityName='Joliet' etc.
?>

Variables.php (Chicago)
<?
$szMap='Illinois';
$extent='445500,4635000,451000,4640000';
// also, what ever else I want - $cityName='Chicago' etc.
?>

You'll note one reason that Chicago needs it's own Map is that it is in a
separate co-ordinate system than the rest of the Illinois pages (though as
an aside I am reformating *everything* to move away from lat-longs)

=================================
Now that I have set up the variables.php files, I wish to modify init.php
to access them.  (I have ka-map-1.0.b1-20060817 installed)

So, right at the very top of init.php I want to include my variables.php <?
include_once(variables.php);  // should be modified to test for existence
include_once(/var/www/ka-map/include/config.php);

The first thing this has done is to have set the mapscript szMap variable
and thus chosen my map before config.php is ever read (more about
config.php modifications below).  The second thing it has done assigned
the desired map extent to a variable which I then use to over-ride the
values set in the Mapserver map file (after all, the map extent of Florida
exceeds that of Miami, and I want to show Miami)

Thus, at around line 245 of init.php where it is found
$szExtents = $oMap->extent->minx.",".$oMap->extent->miny.",".

my new code looks like
// SET THE EXTENTS FROM THE LOCAL VARIABLE IF IT EXISTS
    if (isset($extent)) {
       $szExtents = $extent;
       }
    else {
       // Default to the Map file's extent
       $szExtents = $oMap->extent->minx.",".$oMap->extent->miny.",".   }

Finally, at the end of init.php I make two changes:

if( isset( $_SERVER['REQUEST_URI'] ) ) {
    if ((substr($szURL, -1, 1) != '/') &&
        (substr($_SERVER['REQUEST_URI'],0,1) != '/')) {
        $szURL .= "/";
    }
    // MFGIS URL forced to server root - commented out below
    // $szURL .= dirname($_SERVER['REQUEST_URI'])."/";
    // MFGIS commented out above
    // NOT TOO SURE ABOUT THIS HACK!!!!!
    // IS THIS WHY MY 'LINK-TO-THIS-VIEW' IS BROKEN  ????
} else {
    if ((substr($szURL, -1, 1) != '/') &&
        (substr($_SERVER['PHP_SELF'],0,1) != '/')) {
        $szURL .= "/";
    }
    $szURL .= dirname($_SERVER['PHP_SELF'])."/";
}

$szResult .= "this.server = '".$szURL."';";
// MFGIS TILE.PHP is placed in root????
$szResult .= "this.tileURL = '/ka-map/tile.php';";

$szResult .= "this.selectMap('$szMap');";
echo $szResult;

?>  //end of init.php

Note the caveats in-line in the php code.

Now, given that I have chosen my map and set my $szMap variable here in
init.php, I need to ensure that it is not over-ridden when
include/config.php is read.   Here is how I have implemented that:

Essentially, I modify config.php to test whether szMap is already set, and
if so to use that value rather than the one specified in config.php.   In
other words, I have taken the map choice away from config.php and placed
it at the level of the web page, where (to me) it more appropriately
belongs.

In config.php, after the parameters are set and before the first map is
identified (around line 105) I include the following code:

// if the map name is set see if we can quick load it but if not we'll
want //to use the standard approach, below
if (!isset($szMap)) { $szMap='DEFAULTMAP'; }

$my_szMapPath='/var/www/ka-map/include/mapfiles/';
$my_szMapFile=$my_szMapPath.$szMap;
if (file_exists($my_szMapFile)) { include('load_mapfile.php'); }

// if the aszMapFiles variable has been set we can skip the parameters  //
below. but if not, proceed as per the stand approach below
if(!isset($aszMapFiles)) {

// old-style map configuration
$aszGMap = array (
         'title' => 'GMap 75',
         'path' => '../../gmap/htdocs/gmap75.map',
         'scales' => array( 40000000, 25000000, 12000000, 7500000,
3000000, 1000000 ),
         'format' =>'PNG'

And the only other change to config.php is the closing bracket for the 
test  - if(!isset($aszMapFile)) {

What I have done here is introduce a new folder include/mapfiles and a new
script load_mapfile.php

This does two things,  first, it finds and loads my map file, and it
allows me to create and place as many map files as I wish without having
to edit config.php for each new map, thus avoiding the possibility of
wrecking the config.php with each edit.

So, recalling my example from above, I will have files
include/mapfiles/Florida
include/mapfiles/Illinois
include/mapfiles/Chicago
and as many more as I wish.

Each entry in mapfiles is simply a text file with four lines representing
the contents of the array variables which previously had to be hard-wired
into the code of config.php, such as:

Chicago
/var/www/us/map/Chicago.map
50000,25000,12500,6000,3000,1500,750
GIF

These are read into the same structure as the original config.php through
the use of my new load_mapfile.php script, which looks like this:

<?php
//load_mapfile.php
//Read a text file containing ka-map required map parameters in lieu //of
specifying them within config.php
//
//$szMap and $my_szMapFile validated by default before calling
$lines = file($my_szMapFile);
$thisTitle=rtrim($lines[0]);
$thisPath=rtrim($lines[1]);
$thisScaleList=rtrim($lines[2]);
$thisScales=explode(",",$thisScaleList);
$thisFormat=rtrim($lines[3]);

$thisArray = array(
   'title' => $thisTitle,
   'path'   => $thisPath,
   'scales' => $thisScales,
   'format' => $thisFormat);
//
// Note that my array is really just a single map, so I have broken the
// chance to change maps from within the web page.
$aszMapFiles = array ( $szMap => $thisArray);

?>   // end load_mapfile.php

==================================================================
So that's it!

This is working fine with 50000+ web pages and soon to be ~100 Mapserver
map files.

The issues I have identified with this approach are that:

1.  I have broken the link-to-this-view functionality (perhaps by hacking
the tail end of init.php ???)  This is a big deal which I need to resolve.

2.  I have broken the ability to choose (in the web page) one from among
many map files.  This isn't too important to me at this time.

I have placed copies of the files under

http://mfgis.com/docs/ka-map/Configuring_Ka-Map_At_The_Web_Page_Level.html

and would welcome any feedback.

-- 
Steve Walker
Middle Fork Geographic Information Services
walker at mfgis dott comm















More information about the ka-Map-users mailing list