[mapserver-users] Dynamic mapping with CGI parameters

Roberto Bianconi roberto.bianconi at gmail.com
Wed Feb 27 05:23:09 EST 2008


Hello Colin,

I'm probably missing something, but I don't see the problem with Approach #1.
It gives you total control on your application.

Conceptually, I would put something like this (not tested) in a CGI script.

#!/usr/bin/perl

use CGI;
my $q = new CGI;

# Get the parameters from the CGI
my $someUserId = $q->param('userid');
my $lon = $q->param('lon');
my $lat = $q->param('lat');
# ...

# Write a mapfile (a temporary one using userid in the name) using
also the parameters from the CGI
my $mapfile = '/tmp_path/'.$someUserId.'.map';
open (MAP,">$mapfile);
print MAP "MAP\n";
print MAP"    STATUS ON\n";
# ....
# other mapfile stuff, including printing your DATA block substituting
the user id
print MAP "
    DATA \"line FROM (
        SELECT num, id, line
        FROM jsview_journeyroutes
        WHERE us_id = $someUserId
      ) AS foo USING UNIQUE id USING SRID=4326\"
\n"';
# ....
print MAP "END\n";
close (MAP);

# Reload the mapfile
my $mapObj = new mapscript::mapObj($mapFile);

my $img = $map->prepareImage();

# Do whatever you want (load and draw layers from the mapfile
# and/or create/draw new ones), for example

my $layer = $map->getLayerByName('one_of_my_layers_in_mapfile');
$layer->draw($map, $img); #

# And then prepare and save the image

my $image_file = '/path/whatever.png';
$img->save($image_file);

# Here we're ready to print the HTML response to the browser (image generated
above and input forms + submit button to get new parameters and
generate a new image)

print $q->header();
print $q->start_html();
#
# Whatever you want to put in your page here, including the generated image
print $q->img($imagefile);

print $q->end_html();


The only sure thing is that I'd use CGI.pm module for writing CGI applications.
It is much, much easier and SAFER than anything you can write from
scratch, guaranteed :))

Hope this helps
Roberto

http ://www.enviroware.com


On Tue, Feb 26, 2008 at 9:16 PM, Colin Wetherbee <cww at denterprises.org> wrote:
> Greetings.
>
>  I have a fledgling MapServer application that uses PostgreSQL as its
>  back-end.  Each set of data I would like to retrieve from PostgreSQL is
>  specific to a certain user.  Currently, since the application is only in
>  an alpha phase, I have MapServer retrieve the aggregate sum of the data
>  in the database and display it on my map.  This isn't going to work for
>  the real application, since different users need to see different sets
>  of data.
>
>  I would like to do one of two things, but I've been poring over the
>  documentation today and can't find any way to achieve either.  I would
>  very much appreciate some assistance.  My two approaches are as follows,
>  but if there are others, I would certainly be open to implementing
>  something different.
>
>  Approach #1:
>
>  Since my application is written in Perl, I could use MapScript in Perl
>  to generate bits of maps and return them to the browser as needed.  Perl
>  would interpret cookies and perform session authentication before
>  generating a customized map file to pass along to MapServer.  The Perl
>  code [0] would look something like:
>
>  sub GetMapPiece()
>  {
>    $req = new mapscript::OWSRequest();
>    $req->setParameter("SERVICE", "WMS");
>    $req->setParameter("VERSION", "1.1.0");
>    $req->setParameter("REQUEST", $something);
>
>    # The next two lines are the important ones...
>    my $mapFile = GenerateMapFile({ userid => $someUserId });
>    my $mapObj = new mapscript::mapObj($mapFile);
>
>    mapscript::msIO_installStdoutToBuffer();
>    my $dispatch_out = $map->OWSDispatch($req);
>    return mapscript::msIO_getStdoutBufferString();
>  }
>
>  The problem with this is that, as far as I can tell, I can't create a
>  mapObj from anything other than a file on disk.  I would like $mapFile
>  to contain the full text of a dynamically created map file, and I would
>  then like to be able to create a new mapObj from it.  My imaginary
>  GenerateMapFile() function would generate the text for the map file, but
>  it would never be saved to disk.
>
>  Approach #2:
>
>  This would be my preferred method, but from what I've read, I think
>  approach #1 is more likely to happen.
>
>  This approach entails passing a CGI parameter to mapserv that could be
>  substituted somewhere in the map file.  For example, the URL could be:
>
>  http://example.com/cgi-bin/mapserv?userid=1234
>
>  Then, the map file could contain something like:
>
>      DATA "line FROM (
>          SELECT num, id, line
>          FROM jsview_journeyroutes
>          WHERE us_id = {userid}
>        ) AS foo USING UNIQUE id USING SRID=4326"
>
>  And, MapServer would see "WHERE us_id = {userid}" and, for {userid},
>  substitute the 1234 that was passed into mapserv as a CGI parameter.
>
>  Of course, doing this directly would cause nasty SQL injection attack
>  problems, but I could properly sanitize the input with Perl.  No big
>  deal there.
>
>  End Approaches.
>
>  Any thoughts on this?
>
>  Thanks.
>
>  Colin
>
>  [0] Adapted from
>  <http://mapserver.gis.umn.edu/docs/howto/wxs_mapscript/#perl-example>.
>  _______________________________________________
>  mapserver-users mailing list
>  mapserver-users at lists.osgeo.org
>  http://lists.osgeo.org/mailman/listinfo/mapserver-users
>


More information about the mapserver-users mailing list