Personal tools
You are here: Home Documentation How-tos Imagemap Creation with MapServer

Imagemap Creation with MapServer

Document Actions
Examples of how to utilize shpxy and queries to output imagemaps from both point and polygon layers.

1   Introduction

The shpxy method of creating imagemaps uses MapServer query functionality to build a html imagemap. Just like a regular MapServer query, you send a query request and MapServer uses the templates to build a block of html that it sends back to the browser. The first example shows you how to build an imagemap based on a point layer. An example template for a polygon layer is also included.

Components

  • MapServer mapfile
  • query template file
  • query header template
  • query footer template

2   Mapfile Layer Definition

Here is a simple mapfile for our example

MAP
NAME "myMapFile     
STATUS ON
SIZE 200 200
EXTENT 178784 4804000 772653 5483346

UNITS METERS
STATUS ON
SHAPEPATH "/web/maps/data"  
IMAGECOLOR 255 255 255

WEB
  IMAGEPATH "/web/maps/tmp/"                          
  IMAGEURL "/maps/tmp/"
END   

LAYER
  NAME "sites"
  STATUS DEFAULT
  TYPE point
  DATA 'aqiAreas'
      TEMPLATE "bodytemplate.html"
      HEADER "imapheader.html"
      FOOTER "imapfooter.html"
END  
END  

You can see that we have a mapfile with one point layer, and that it contains references to three query templates.

3   Templates

In MapServer, the query header and footers get processed only once. The main query template, 'bodytemplate.html' in this example, gets processed once for each record in the record set returned by the query.

3.1   Point Layers

Here is the query header, 'imapheader.html'. It creates the opening tag for your html imagemap.

<map id="mymap" name="mymap">

Here is the query template, 'bodytemplate.html'. It creates the body of the html imagemap.

<area shape="circle" coords="[shpxy precision=0 proj=image yf=",7" xf=","]" href="http://my.url/mypage.cfm?region=[NAME]" title="[NAME]" alt="[NAME]">

This template is used to create circular imagemap elements for a point layer.  NAME is a fieldname in the data source, the value for NAME for each individual record gets substituted as the template is processed.  The href specifies the URL link if the element is clicked.  Title and alt will display the value when an element is moused over.  

The resulting html element looks like

<area shape="circle" coords="80,103,7" href="http://my.url/mypage.cfm?region=Northern" >

The key part here is

coords="[shpxy precision=0 proj=image xf="," yf=",7"]"

This is where MapServer will substitute the image coordinates for that query record. With Precision=0, the coordinates will be integers.

You also see shpxy template formatting options 'xf' and 'yf'. 'xf="," tells MapServer to place a comma after the x coordinate. 'yf=",7" after the y coordinate. This is done to specify a radius of 7 pixels for the circle. More options can be found in the MapServer Template Reference .

The query footer template simply adds the closing tag for the html imagemap

</map>

3.2   Polygon Layers

Here is a query template for a polygon layer

<area shape="poly" coords="[shpxy precision=0 proj=image]" href="http://my.url/mypage.cfm?ID=[SITE_ID]" title="[NAME]" alt="[NAME]">

4   Request URL

To get the imagemap, you need to send a GET or POST request to MapServer with several URL variables defined. The below URL tells MapServer where the mapfile is located, what layer we are querying, and that we are using nquery mode to return multiple results.

http://myurl/cgi-bin/mapserv?map=/web/maps/demoimap.map&qlayer=sites&mode=nquery&searchmap=true

5   Additional Notes

If you use separate map files to generate your imagemap and your map image, make sure that the EXTENT and SIZE specified in both mapfiles are identical. If they are not, your features will not align properly.

This How-to applies to: MapServer 4.6, MapServer 4.8

by David Fawcett last modified 2006-08-04 10:34
Contributors: David Fawcett

May Need Query Map Definition to Work

Posted by Robert W. Burgholzer at 2008-04-01 06:38
In trying out this example, I needed to have a "QUERY MAP" block defined, a al:

QUERYMAP
STATUS ON
STYLE NORMAL
END

Otherwise, the image was never rendered. I am using Mapserver 5 on windows.


Powered by Plone