Jesus Loves GRASS

Technical blog about GRASS/GIS,open source geoinformatics and MAPSERVER.

Monday, February 04, 2008

Basic Image in PHP/Mapscript

This script makes a simple web page with a PNG image based on a GeoTiff that contains interpolation values in Float32. The script uses the ms_GetErrorObj() to echo any errors.

The objective of the script is to totally replace any info from the map file, nevertheless it is necessary for a map file to start the Map object.

The map file called test2.map is only:
MAP

END

The rest is defined as follows.....

<?php
/*
* Created on Feb 1, 2008
*/

// Loading Lib (just to be certain)
dl('php_mapscript.so');

//Reset error list (just to be certain)
ms_ResetErrorList();

$map_path="/usr/local/apache2/htdocs/wpstmp/";

// Creation of map object
$oMap = ms_newMapObj($map_path."test2.map");

//Setting the name
$oMap->set("name","map1");

//Setting size and backcolor
$oMap->setSize(800,400);
$oMap->imagecolor->setRGB(125,125,125);

//Setting Extent
//Data gathered using GdalInfo from the GeoTIFF
//double minx, double miny, double maxx, double maxy
$oMap->setExtent(-69000.000,-42000.000,261000.000,633000.000);

//Seting units
$oMap->set("units",MS_METERS);

//Output type
$oMap->selectOutputFormat("PNG24");
$oMap->outputformat->setOption("imagemode",MS_IMAGEMODE_RGB);


//Paths
$oMap->web->set("imagepath","/usr/local/apache2/htdocs/wpstmp/");
$oMap->web->set("imageurl","/wpstmp/");


//Layer Object
$oLayer=ms_newLayerObj($oMap);
$oLayer->set("name","interpolation1");
$oLayer->set("status",MS_ON);
$oLayer->set("type",MS_LAYER_RASTER);
$oLayer->set("data","/usr/local/apache2/htdocs/wpstmp/interpolation610385.tif");

//RASTER processing of band1 of Geottif, automatic color scaling
//http://mapserver.gis.umn.edu/docs/howto/raster_data
$oLayer->setProcessing("SCALE=AUTO");
$oLayer->setProcessing("BANDS=1");


//Dumping image
$oImage=$oMap->draw();
$image_url=$oImage->saveWebImage();

//Error dumping part, not very necessary, only for debug
$error=ms_GetErrorObj();
while($error && $error->code!=MS_NOERR)
{
printf("Error in %s: %s
\n",$error->routine,$error->message);
$error=$error->next();
}

?>
<html>
<head>
</head>
<body>

<img src="http://localhost<?php echo $image_url?>" alt="Map" />

</body>
</html>

Labels: , ,

0 Comments:

Post a Comment

<< Home