User Tools

Site Tools


tecnica:gps_cartografia_gis:php_mapscript_recipes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tecnica:gps_cartografia_gis:php_mapscript_recipes [2009/09/09 12:21] – created niccolotecnica:gps_cartografia_gis:php_mapscript_recipes [2010/02/26 23:46] (current) niccolo
Line 1: Line 1:
 ====== PHP MapScript Recipes ====== ====== PHP MapScript Recipes ======
 +
 +**[[http://mapserver.org/mapscript/index.html|MapScript documentation]]**.
 +
 +===== Return a map image from a map file =====
 +
 +We can build an image from a working map file. Here we just turn on one layer, setting its status to ''DEFAULT'':
 +
 +<code php>
 +<?php
 +// PHP MapScript example.
 +// Display a map as an inline image or embedded into an HTML page.
 +$inline = true;
 +$map_path = '/var/www/mapfiles/';
 +$mapfile = 'file.map';
 +$map = ms_newMapObj($map_path . $mapfile);
 +$layer = $map->getLayerByName('layer_name');
 +$layer->set('status', MS_DEFAULT);
 +$map_image = $map->draw();
 +if ($inline) {
 +    header('Content-Type: image/png');
 +    $map_image->saveImage('');
 +    exit;
 +}
 +$image_url = $map_image->saveWebImage();
 +?>
 +<HTML>
 +<HEAD>
 +<TITLE>PHP MapScript example: Display the map</TITLE>
 +</HEAD>
 +<BODY>
 +<IMG SRC=<?php echo $image_url; ?> >
 +</BODY>
 +</HTML>
 +</code>
 +
 +===== Build a map from scratch =====
 +
 +See [[http://www.rigacci.org/wiki/doku.php/tecnica/gps_cartografia_gis/mapserver#php-mapscript|this example]].
  
 ===== Generate the legend image ===== ===== Generate the legend image =====
Line 36: Line 74:
  
 For inline images you can omit web paths, because no temporary images will be created. For inline images you can omit web paths, because no temporary images will be created.
 +
 +===== Make a query =====
 +
 +Open an existing map file and perform a **query by rectangle** on a layer, print results with a **template**.
 +
 +<code php>
 +<?php
 +$map = ms_newMapObj("/var/www/demo.map");
 +$selrect = ms_newrectObj();
 +$selrect->setextent(1681023, 4850413, 1681906, 4851167);
 +$map->queryByRect($selrect);
 +$buffer = $map->processquerytemplate(array(), false);
 +
 +print "<table border=\"1\">\n";
 +print $buffer;
 +print "</table>\n";
 +?>
 +</code>
 +
 +The **''$buffer''** variable contains one template instance for each record.
 +
 +The querable layer is definend in the ''**MAP.LAYER**'' section, and it requires a query template:
 +
 +<file>
 +MAP
 +  ...
 +  LAYER
 +    ...
 +    TEMPLATE "query_template.html"
 +    TOLERANCE 6
 +    METADATA
 +      DESCRIPTION    "Impianti Pubblicitari"
 +      RESULT_FIELDS  "objectid,field1,field2"
 +      RESULT_HEADERS "ID,Heading1,Heading2"
 +    END
 +  END
 +END
 +</file>
 +
 +This is an example for **''query_template.html''**:
 +
 +<code html>
 +<!-- MapServer Template -->
 +<tr><td>[item name=objectid]</td><td>[item name=field1]</td><td>[item name=field2]</td></tr>
 +</code>
 +
 +This example does not use the template, it **iterates through the query results**:
 +
 +<code php>
 +<?php
 +$map = ms_newMapObj("/var/www/demo.map");
 +$selrect = ms_newrectObj();
 +$selrect->setextent(1681023.81515592, 4850413.68207817, 1681906.91185973, 4851167.76185515);
 +
 +$map->queryByRect($selrect);
 +$layer = $map->getLayerByName('layer_name');
 +$count = $layer->getNumResults();
 +
 +print "Query result count: " . $count . "<br>\n";
 +print "<pre>\n";
 +for ($i = 0; $i < $count; $i++) {
 +    $qRes = $layer->getResult($i);
 +    print_r($qRes);
 +}
 +print "</pre>\n";
 +?>
 +</code>
tecnica/gps_cartografia_gis/php_mapscript_recipes.1252491692.txt.gz · Last modified: 2009/09/09 12:21 by niccolo