Jesus Loves GRASS

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

Tuesday, February 26, 2008

$oMap->setExtent problems and general data gathering

I made some PHP programming/MapScript for rendering a GeoTiff created by an interpolation service (WPS) as a PNG. The Geotiff is stored in the server's temporary folder and all the mapserver parameters are set by PHP programing.

The fist part of the posted code gets the image info using GdalInfo and regular expressions to obtain the extent value of the image, I haven't managed to do this properly using the PHP's Exif lib....

The major problem was the SetExtent of the Map object it was always complaining that the values had some problem, the problem was that the parameters were being passed as a string, and they needed to be as double, even if they are passed as float (or int?) the SetExtent will complain

//Some code before

$FileName=/usr/local/apache2/htdocs/tmp/interpolation3454.tiff;
$CmdStr="/usr/local/bin/gdalinfo ".$FileName;
//General string for getting float numbers
$StrPattern='/[-+]?\b[0-9]*\.?[0-9]+\b/';
exec($CmdStr,$ResultArray);

//Upper left point info
$UpperLeftPoint=$ResultArray[6];
preg_match_all($StrPattern,$UpperLeftPoint,$TmpArray);
$MinX=$TmpArray[0][0];
$MaxY=$TmpArray[0][1];

//Lower right point info
$LowerRightPoint=$ResultArray[9];
preg_match_all($StrPattern,$LowerRightPoint,$TmpArray);
$MaxX=$TmpArray[0][0];
$MinY=$TmpArray[0][1];

//Passing to Map object
//double minx, double miny, double maxx, double maxy
$oMap->setExtent((double)$MinY,(double)$MinY,(double)$MaxX,(double)$MaxY);

//Same code after

Labels: , , , , , ,

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: , ,

Friday, April 20, 2007

PHP popup

This is the PHP that will make the popup, it is more or less prepared for when the WFS is installed:

<?php
/* This PHP will generate the HTML code for the final submission of the request
The Javascript calling string is :
url_str='popup.php?service='+OWS_service+'&request=getCoverage&COVERAGE='+OWS_name+'&CRS='+CRS+'&BBOX='+BBOX.join(",")+'&RESX='+RESX[0]+'&RESY='+(-1*parseFloat(RESY[1]))+'&FORMAT='+FORMAT; */

?>

<body>
<table width="350px" border="0">
<tbody>
<tr bgcolor="#CFCFCF" >
<td><?php echo(strtoupper($_GET["service"])); ?> name:</td>
<!--getting the name of layer -->
<?php if (strnatcasecmp ($_GET["service"],'wcs') == 0){
$OWS_name=$_GET["COVERAGE"];
}
?>
<td align="center" ><?php echo($OWS_name); ?></td>
</tr>
<tr>
<td>CRS (projection):</td>
<td align="center"><?php echo($_GET["CRS"]); ?></td>
</tr>
<tr bgcolor="#CFCFCF">
<!-- getting the bbox back to array -->
<?php
$BBOX=explode(",",$_GET["BBOX"]); ?>
<td>BBOX minX:</td>
<td align="center"><?php echo($BBOX[0]); echo(" º N"); ?></td>
</tr>
<tr>
<td>BBOX minY:</td>
<td align="center"><?php echo($BBOX[1]); echo(" º N"); ?></td>
</tr>
<tr bgcolor="#CFCFCF">
<td>BBOX maxX:</td>
<td align="center"><?php echo($BBOX[2]); echo(" º W"); ?></td>
</tr>
<tr>
<td>BBOX maxY:</td>
<td align="center"><?php echo($BBOX[3]); echo(" º W"); ?></td>
</tr>
<tr bgcolor="#CFCFCF">
<td>ResX:</td>
<td align="center"><?php echo($_GET["RESX"]); ?></td>
</tr>
<tr>
<td>ResY:</td>
<td align="center"><?php echo("-"); echo($_GET["RESY"]); ?></td>
</tr>
<tr bgcolor="#CFCFCF">
<td>Format:</td>
<td align="center"><?php echo($_GET["FORMAT"]); ?></td>
</tr>
</tbody>
</table>

<!--building request-->
<?php
//checking the service
if (strnatcasecmp ($_GET["service"],'wcs') == 0) {

//start of form
echo('<FORM action="ows.php" method="GET"><input type="hidden" name="service" value="WCS"><input type="hidden" name="request" value="getCoverage"><input type="hidden" name="coverage" value="');echo($OWS_name);echo('">');

//mid section
echo('<input type="hidden" name="CRS" value="');echo($_GET["CRS"]);echo('">');
echo('<input type="hidden" name="BBOX" value="');echo($_GET["BBOX"]);echo('">');
echo('<input type="hidden" name="RESX" value="');echo($_GET["RESX"]);echo('">');
echo('<input type="hidden" name="RESY" value="');echo($_GET["RESY"]);echo('">');
echo('<input type="hidden" name="FORMAT" value="');echo($_GET["FORMAT"]);echo('">');

//final secion
echo('<p align="center"><INPUT type="submit" value="Request Coverage"></p></FORM>');

}
?>

Labels: , ,

Thursday, April 05, 2007

WCS wrapper for Mapserver

WMS,WFS and WCS is how it is possible to connected to a webGIS server and to download data:
WMS == Raster as an image
WFS == Vectorial information
WCS == Raster as a layer (original raster with values)

The request of WxF is normally done by a GET to the Mapserv program (is like a normal CGI-BIN for program execution), well I don't want Apache to run any program, so the solution is a wrapper PHP script, where the script catches the requests and uses them for a valid WCS reply.

The wrapper is very simple and there is a basic example on the mapserver home page, This is a wrapper with small modifications to work with WCS:

<?php
/*ows.php?service=WCS&version=1.0.0&Request=GetCapabilities*/
$request = ms_newowsrequestobj();

//$request->loadparams();

foreach ($_GET as $k=>$v) {
$request->setParameter($k, $v);
}



// forcing to version 1.0.0
$request->setParameter("VeRsIoN","1.0.0");

ms_ioinstallstdouttobuffer();

$oMap = ms_newMapobj("/usr/local/apache2/htdocs/secure/ms/data/calter.map");
$oMap->owsdispatch($request);
$contenttype = ms_iostripstdoutbuffercontenttype();
$buffer = ms_iogetstdoutbufferstring();

header('Content-type: application/xml');
echo $buffer;

ms_ioresethandlers();

?>

For start Mapserver only suports WCS version 1.0.0 so any request for higher versions has to be redirect:
$request->setParameter("VeRsIoN","1.0.0");

There seems to be a bug with $request->loadparams(); this produces an empy file reply from the server with no errors, so the solution it to get the parameters directely from the GET
foreach ($_GET as $k=>$v) {
$request->setParameter($k, $v);
}

if replacing $_GET for $_POST is should be possible to use POST to request the WCS, mapserver only accepts GET.

Labels: , , , , ,

Everything is now locked !!!!

Well the acesses to the mapserver is now locked !!!!!!!

For locking the system with a login I used the PHP classes called adminpro that I got from the site:

http://www.phpclasses.org/

The programming was done by Giorgios from Greece, and is the most straight forward login system based on MySQL, that I know, all programming is simple to follow and the configuration is very simple.

I had some problems making the socket connection to the MySQL, I don't understand why the socket wouldn't work, the file was inside chroot but I was having error messages saying no socket.

I couln't find much about the problem on the net, so instead of socket I used a direct connection: 127.0.0.1:3306

I don't know if this is secure but it worked !!!

<?php include("adminpro_class.php");
$prot=new protect();
if ($prot->showPage) {
?>

<!-- HTML/PHP GOES HERE --!>

<!-- end of php protection-->
<?php } ?>

Cool and simple !!!

Labels: , , , ,