[mapserver-users] Re: MapScript WMS request

Mr. Puneet Kishor punk.kish at gmail.com
Thu Sep 8 12:27:40 EDT 2011


On Sep 7, 2011, at 10:29 PM, Frank Warmerdam wrote:

> On 11-09-07 08:18 PM, Mr. Puneet Kishor wrote:
>> I would really appreciate insight from someone well-versed in the ins and outs of WMS via MapScript. Looking at the discussion at [http://trac.osgeo.org/mapserver/ticket/670], the `img = wms_map.draw()` step seems to be a crucial part of drawing the image.
>> 
>> So, I added `my $img = $map->draw();` to my code below, and indeed, the image was drawn and sent back to the browser.
>> 
>> Is this something that should be added to the examples in the documentation? Is this the right way to do things? In any case, I am not write to STDOUT, and sending the buffer directly. Instead, my code is very much like a regular request (and it works) --
>> 
>>     my $new_extent = $map->{extent};
>> 
>>     my $req = new mapscript::OWSRequest();
>>     $req->setParameter( "SERVICE", "WMS" );
>>     $req->setParameter( "VERSION", "1.1.0" );
>>     $req->setParameter( "REQUEST", "GetMap" );
>>     $req->setParameter( "BBOX", "-180,-90,180,90" );
>>     $req->setParameter( "LAYERS", "layer_name");
>>     $req->setParameter( "SRS", "-1");
>>     $req->setParameter( "WIDTH", "1200");
>>     $req->setParameter( "HEIGHT", "600");
>> 
>>     my $dispatch_out = $map->OWSDispatch( $req );
>>     my $img = $map->draw();
>>     my $map_img = 'test_' . sprintf("%0.10d", rand(1000000000)) . '.png';
>>     $img->save("/path/to/$map_img");
>>     return send_file("/path/to/$map_img");
>> 
>> 
>> Comments? Corrections?
> 
> Puneet,
> 
> I rarely use PHP and it is possible something is broken now with PHP
> OWS dispatching.  But it should work like:
> 
>  http://mapserver.org/ogc/mapscript.html#php-example
> 
> Alternatively if you don't install the stdout redirection the OWSDispatch
> call should produce the image to stdout.
> 
> Note that if you want things handled properly as a WMS request you should
> not be calling draw().  The ticket you reference long predates the WxS
> MapScript services effort.
> 
> I suspect you are not really setting all the environment variables needed
> to make the WMS handler kick in during OWSDispatch though I'd have to
> play around with it to be sure.
> 

Frank,

The above code was for (ahem) Perl MapScript. I am sure you are correct that I am not setting all the environment variables. I am hoping for a tutorial or code fragment that shows how to do it correctly. The one in the documentation that I am following doesn't seem to have any more than what I am setting. The following is the entirety of my non-working code (simplified bit for brevity, and also available with syntax highlighting at [http://pastebin.com/fz6MCXpm]) --

  use DBI;
  use mapscript;
  
  my $map = new mapscript::mapObj() or warn "error: $mapscript::ms_error->{message}\n";
  
  $map->{name} = 'mymap';
  $map->{status} = $mapscript::MS_ON;
  $map->{units} = $mapscript::MS_DD;
  $map->setImageType('png');
  
  my $imageColor = new mapscript::colorObj;
  $imageColor->setHex('#FFFFFF');
  $map->{imagecolor} = $imageColor;
  
  $map->setSize(1200, 600);
  $map->setExtent(-180, -90, 180, 90);
  $map->selectOutputFormat('png');
  $map->setProjection("proj=latlong");
  
  my $outputFormatObj = new mapscript::outputFormatObj('AGG/PNG');
  $outputFormatObj->setExtension('png');
  $outputFormatObj->setMimetype('image/png');
  $outputFormatObj->{imagemode} = $mapscript::MS_IMAGEMODE_RGB;
  $map->setOutputFormat( $outputFormatObj );
  
  my $layerOne = new mapscript::layerObj($map);
  $layerOne->{name} = "layer one";
  $layerOne->{status} = $mapscript::MS_ON;
  $layerOne->{type} = $mapscript::MS_LAYER_POLYGON;
  $layerOne->{opacity} = 100;
  $layerOne->{connection} = 'host=localhost port=5432 dbname=mydb user=dbuser password=dbpasswd';
  $layerOne->setConnectionType($mapscript::MS_POSTGIS, '');
  $layerOne->{data} = 'the_geom FROM (SELECT class_name, the_geom FROM t1) a_view USING UNIQUE gid USING srid=-1';
  $layerOne->{classitem} = 'class_name';
  $layerOne->setProjection("proj=latlong");
  
  my $classOutlineColor = new mapscript::colorObj;
  $classOutlineColor->setHex('#444444');
  
  my $sth = database->prepare('SELECT class_name, class_color FROM t2');
  $sth->execute;
  while (my ($class_name, $class_color) = $sth->fetchrow_array) {
    my $classObj = new mapscript::classObj($layerOne);
    $classObj->{name} = $class_name;
    $classObj->setExpression("$class_name");
  
    my $classFillColor = new mapscript::colorObj;
    $classFillColor->setHex($class_color);
    
    my $styleObj = new mapscript::styleObj($classObj);
    $styleObj->{color} = $classFillColor;
    $styleObj->{outlinecolor} = $classOutlineColor; 
  }
  $sth->finish;
  
  my $req = new mapscript::OWSRequest();
  $req->setParameter( "SERVICE", "WMS" );
  $req->setParameter( "VERSION", "1.1.0" );
  $req->setParameter( "REQUEST", "GetMap" );
  $req->setParameter( "BBOX", "-173.537,35.8775,-11.9603,83.8009" );
  $req->setParameter( "LAYERS", "layer one");
  $req->setParameter( "SRS", "-1");
  $req->setParameter( "WIDTH", "1200");
  $req->setParameter( "HEIGHT", "600");
  
  my $io = mapscript::msIO_installStdoutToBuffer();
  my $dispatch_out = $map->OWSDispatch( $req );
  printf "%s\n", mapscript::msIO_getStdoutBufferString();

Anything stand out that I am doing wrong above? Or, stuff that I am missing?

Many thanks in advance,

Puneet.


More information about the mapserver-users mailing list