MapServer TEMPLATE: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
mNo edit summary
Brian Wilson (talk | contribs)
Line 67: Line 67:
  ogrinfo -so shapefilename.shp        will return a list of layers in the file
  ogrinfo -so shapefilename.shp        will return a list of layers in the file
  ogrinfo -so shapefilename.shp layer  will list info on a layer called layer
  ogrinfo -so shapefilename.shp layer  will list info on a layer called layer
Alas, TO BE CONTINUED. [mailto:[email protected]?subject=Mapserver%20Templates Contact me] if you want me to write more here!

Revision as of 23:10, 12 September 2005

"Templates" is the simplest way to create an interactive Mapserver site. (You can create non-interactive ones without templates but what's the fun of that?)

Would it not be great if the examples actually worked? I have moved my mapserver around 2 or 3 times now. Not to mention the one running on my destkop machine behind the firewall and only accessible as 'localhost'... that's life.

The non-interactive example

Without using a template, I (the masterful Web GIS programmer) can create a map file and put the appropriate URL onto a Web page, for example,

http://mapserver.wildsong.biz/cgi-bin/mapserv?map=../mapserver/html/demo/hello.map&layer=credits&mode=map

Reading the URL from left to right. this goes to my mapserver(http://mapserver.wildsong.biz/), invokes the mapserv executable (/cgi-bin/mapserv), telling it read a map file (map=../mapserver/html/demo/hello.map), look for the 'credits' layer("layer=credits"), and finally to display the results by sending the image back to your browser ("mode=map").

I am keeping my files for this project in the html directory tree so that you can download them if you want to look at them. For security normally I would keep them OUTSIDE the tree.

So for example, you can grab the map file with this URL: http://mapserver.wildsong.biz/demo/hello.map

The first template

I walked through the Mapserver template reference and created a template file that has many of the template tags in it. You can look at it; click here and do view source: http://mapserver.wildsong.biz/demo/hello.template.html

To tell Mapserver to use my template file, I can change mode to 'browse' and add a template specifier:

http://mapserver.wildsong.biz/cgi-bin/mapserv?map=../mapserver/html/demo/hello.map&mode=browse&map_web_template=../mapserver/html/demo/hello.template.html

I have to specify a layer; that example just shows you an empty blue box;

http://mapserver.wildsong.biz/cgi-bin/mapserv?map=../mapserver/html/demo/hello.map&mode=browse&map_web_template=../mapserver/html/demo/hello.template.html&layer=credits

An aside: If you click reload on the above page several times you will notice the name of the img file changing. Each click causes a new PNG file to be generated. I have to set up a separate program to delete the darned things so that my server does not fill up and keel over.

The template has lots of mapserver template tags in it, which are a word enclosed by square brackets. They look like wiki tags, come to think of it.

The clever idea is that the template can have an HTML form in it, so you can put buttons and whatnot into the form, and voila! interactivity! Each time you click submit, the request is processed by mapserv, pushed through the template, and you get the updated results.

Query templates

But wait, there's more! I started down this path of exploration to debug a problem with queries. I have a Mapserver site that has a broken search feature. So next I need to learn how MapServer handles non-spatial queries.

For this demo I will use shapefiles with real data in them, not just a bit of text. These shapefiles some spatial (vector) data, and also some attribute data that I can query.

Here is the map file for your perusal: http://mapserver.wildsong.biz/demo/shape.map and here is a simple link that will display the map: http://mapserver.wildsong.biz/cgi-bin/mapserv?map=../mapserver/html/demo/shape.map&mode=map&layer=Streets&layer=Parks&layer=credits

These URLs are starting to get unwieldy so from now on I will embed them as links.

Next, here is a link that will push the map through a template. (remember to use "view source" to look at the template.) and here is the map pushed through the template: Parks map

Examining the data in your shapefiles

Before designing a query to put in a map file you need to know more about your data. The Web Mapping book suggests using ogrinfo; I think it's modestly useful. I'd say use a GIS program; ArcCatalog comes to mind.

With ogrinfo you can get listings of the names of datafields pretty easily; try this:

ogrinfo -so shapefilename.shp         will return a list of layers in the file
ogrinfo -so shapefilename.shp layer   will list info on a layer called layer


Alas, TO BE CONTINUED. Contact me if you want me to write more here!