Table of Contents for
Web Mapping Illustrated

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Web Mapping Illustrated by Tyler Mitchell Published by O'Reilly Media, Inc., 2005
  1. Web Mapping Illustrated
  2. Cover
  3. Web Mapping Illustrated
  4. A Note Regarding Supplemental Files
  5. Foreword
  6. Preface
  7. Youthful Exploration
  8. The Tools in This Book
  9. What This Book Covers
  10. Organization of This Book
  11. Conventions Used in This Book
  12. Safari Enabled
  13. Comments and Questions
  14. Acknowledgments
  15. 1. Introduction to Digital Mapping
  16. 1.1. The Power of Digital Maps
  17. 1.2. The Difficulties of Making Maps
  18. 1.3. Different Kinds of Web Mapping
  19. 2. Digital Mapping Tasks and Tools
  20. 2.1. Common Mapping Tasks
  21. 2.2. Common Pitfalls, Deadends, and Irritations
  22. 2.3. Identifying the Types of Tasks for a Project
  23. 3. Converting and Viewing Maps
  24. 3.1. Raster and Vector
  25. 3.2. OpenEV
  26. 3.3. MapServer
  27. 3.4. Geospatial Data Abstraction Library (GDAL)
  28. 3.5. OGR Simple Features Library
  29. 3.6. PostGIS
  30. 3.7. Summary of Applications
  31. 4. Installing MapServer
  32. 4.1. How MapServer Applications Operate
  33. 4.2. Walkthrough of the Main Components
  34. 4.3. Installing MapServer
  35. 4.4. Getting Help
  36. 5. Acquiring Map Data
  37. 5.1. Appraising Your Data Needs
  38. 5.2. Acquiring the Data You Need
  39. 6. Analyzing Map Data
  40. 6.1. Downloading the Demonstration Data
  41. 6.2. Installing Data Management Tools: GDAL and FWTools
  42. 6.3. Examining Data Content
  43. 6.4. Summarizing Information Using Other Tools
  44. 7. Converting Map Data
  45. 7.1. Converting Map Data
  46. 7.2. Converting Vector Data
  47. 7.3. Converting Raster Data to Other Formats
  48. 8. Visualizing Mapping Data in a Desktop Program
  49. 8.1. Visualization and Mapping Programs
  50. 8.2. Using OpenEV
  51. 8.3. OpenEV Basics
  52. 9. Create and Edit Personal Map Data
  53. 9.1. Planning Your Map
  54. 9.2. Preprocessing Data Examples
  55. 10. Creating Static Maps
  56. 10.1. MapServer Utilities
  57. 10.2. Sample Uses of the Command-Line Utilities
  58. 10.3. Setting Output Image Formats
  59. 11. Publishing Interactive Maps on the Web
  60. 11.1. Preparing and Testing MapServer
  61. 11.2. Create a Custom Application for a Particular Area
  62. 11.3. Continuing Education
  63. 12. Accessing Maps Through Web Services
  64. 12.1. Web Services for Mapping
  65. 12.2. What Do Web Services for Mapping Do?
  66. 12.3. Using MapServer with Web Services
  67. 12.4. Reference Map Files
  68. 13. Managing a Spatial Database
  69. 13.1. Introducing PostGIS
  70. 13.2. What Is a Spatial Database?
  71. 13.3. Downloading PostGIS Install Packages and Binaries
  72. 13.4. Compiling from Source Code
  73. 13.5. Steps for Setting Up PostGIS
  74. 13.6. Creating a Spatial Database
  75. 13.7. Load Data into the Database
  76. 13.8. Spatial Data Queries
  77. 13.9. Accessing Spatial Data from PostGIS in Other Applications
  78. 14. Custom Programming with MapServer’s MapScript
  79. 14.1. Introducing MapScript
  80. 14.2. Getting MapScript
  81. 14.3. MapScript Objects
  82. 14.4. MapScript Examples
  83. 14.5. Other Resources
  84. 14.6. Parallel MapScript Translations
  85. A. A Brief Introduction to Map Projections
  86. A.1. The Third Spheroid from the Sun
  87. A.2. Using Map Projections with MapServer
  88. A.3. Map Projection Examples
  89. A.4. Using Projections with Other Applications
  90. A.5. References
  91. B. MapServer Reference Guide for Vector Data Access
  92. B.1. Vector Data
  93. B.2. Data Format Guide
  94.  
  95. ESRI Shapefiles (SHP)
  96.  
  97. PostGIS/PostgreSQL Database
  98.  
  99. MapInfo Files (TAB/MID/MIF)
  100.  
  101. Oracle Spatial Database
  102.  
  103. Web Feature Service (WFS)
  104.  
  105. Geography Markup Language Files (GML)
  106.  
  107. VirtualSpatialData (ODBC/OVF)
  108.  
  109. TIGER/Line Files
  110.  
  111. ESRI ArcInfo Coverage Files
  112.  
  113. ESRI ArcSDE Database (SDE)
  114.  
  115. Microstation Design Files (DGN)
  116.  
  117. IHO S-57 Files
  118.  
  119. Spatial Data Transfer Standard Files (SDTS)
  120.  
  121. Inline MapServer Features
  122.  
  123. National Transfer Format Files (NTF)
  124. About the Author
  125. Colophon
  126. Copyright

Preparing and Testing MapServer

A few things need to be set up before using MapServer. There are some differences between operating systems such as the name of your MapServer executable, where files are stored, etc.

MapServer for Windows (MS4W)

Chapter 4 described how to access and install the MS4W package. You might want to look at the MS4W portion of that chapter and review how to start the web server. A custom application will be built later in this chapter, but first you need to understand how MapServer works in the MS4W environment.

MS4W comes with a version of the Apache HTTP web server. The MapServer program is stored in Apache’s cgi-bin folder, the main location for many web programs. If you were compiling your own mapserv.exe file, you install it by copying it into the cgi-bin folder. By default, this folder is located in the \ms4w\Apache\cgi-bin folder on the drive MS4W is installed on (e.g., C: orD:). There may be several versions of MapServer located there. For example, mapserv_36.exe is MapServer Version 3.6 (very out of date) and mapserv_44.exe is the most recent Version 4.4. Usually the more recent version will just be called mapserv.exe.

MapServer on Linux with Apache

Preparing MapServer on Linux is also described in Chapter 4. Installing the MapServer CGI program is similar to how you do so for MS4W. The mapserv executable is simply copied (or linked) to Apache’s cgi-bin folder. For example, on SuSE Linux, the web server’s cgi-bin folder is located at /srv/www/cgi-bin. The mapserv application must be copied into this folder so it is accessible to the web server. These instructions also apply to non-MS4W installations of Apache on other platforms.

Configuring Apache

Configuring Apache to use MapServer doesn’t have to be complicated. If you put the mapserv/mapserv.exe program into the web server’s default cgi-bin location, there may not be much more to do to get the program running. The next section gives a couple of example tests to see if it is running properly.

There are other settings you will need in order to access maps produced by MapServer. When MapServer runs, it needs a temporary area to create the images for maps, legends, scale bars, etc. This temporary folder must also be accessible to the web so that users can view the images created by MapServer.

Windows temporary image folder location

MS4W comes with a temporary folder already set up. This folder is c:\ms4w\tmp\ms_tmp. The Apache configuration file at c:\ms4w\Apache\conf\httpd.conf sets this folder up with an alias called /ms_tmp. This means that when a web application requests a file in /ms_tmp, Apache looks for the file in c:\ms4w\tmp\ms_tmp. For example, if MapServer created a map image at c:\ms4w\tmp\ms_tmp\mymap.png, the web application would request to see the image by using a URL like http://localhost/ms_tmp/mymap.png.

The key thing to remember is that MS4W creates all the temporary map image files in this folder.

Linux temporary image folder location

If you are using a fresh install of Apache, you need to set up your temporary location and alias in the configuration files manually. There are several ways to configure settings. One method that uses Apache 2 on SuSE Linux is discussed here. You can set up your MapServer temporary folder anywhere on your filesystem. To keep it simple, you can put it directly under the htdocs root folder. This means that you don’t need to create an alias for Apache because the folder is a child of the web server’s root folder.

For example, the root web folder may be set in the /etc/apache2/httpd.conf file and specified as DocumentRoot /srv/www/htdocs.

You can create your temporary image folder in /srv/www/htdocs/ms_tmp so that MapServer will be able to find it when it is set to look in http://localhost/ms_tmp. As a quick test, if your earlier image mymap.png is placed in /srv/www/htdocs/ms_tmp, you should be able to see it at http://localhost/ms_tmp/mymap.png.

Permissions for the ms_tmp folder must be set properly for MapServer to be able to save the map images. The trick is to have the web server user and group assigned as owner of the folder. You can then run the command:

> chown wwwrun.www /srv/www/htdocs/ms_tmp

This should make it accessible to the web server and, in turn, to MapServer. This process will vary on other platforms, depending on how your web server is being used, but the concepts are the same.

The distinction between the filesystem path to the ms_tmp folder (it doesn’t have to be called ms_tmp by the way; call it whatever you like) and the online URL is very important to understand when setting up your MapServer applications. MapServer needs to know where on the filesystem it can write the map image files to. It will also need to know what URL to send back to the web browser for loading the map images into the web page.

Tip

It may help to think of these two different ways of locating the folder by thinking of the filesystem path as the internal or private folder in which only the web server can create files and the URL as the public folder where anyone can view the files but not change them.

Testing and Troubleshooting

You can run a few tests to check that the MapServer program is properly set up. The set up of MS4W and Linux running Apache is very similar, but there are some differences worth mentioning.

Testing mapserv.exe on Windows

The first step for testing MS4W assumes that the web server is running as described in Chapter 4. To test that the MapServer executable (mapserv.exe) is available, you make a direct request to it without any parameters. For example, point your web browser to http://localhost/cgi-bin/mapserv.exe. You should get a message like this:

No query information to decode. QUERY_STRING is set, but empty.

This is a good thing. It is a message coming directly from MapServer. You didn’t pass any of the required details to MapServer for processing, but the message confirms that you were able to make a request. It is also a reminder that you won’t be running MapServer this way. If you get any other errors, there is either a permissions problem and Apache can’t access the requested file, or the web server isn’t up and running properly.

Testing mapserv with Apache on Linux

The MapServer program on Linux can be tested the same way you do MS4W. The only difference is that the name of the program is slightly different.

With Linux, there are no standard filename suffixes such as .exe to tell you which files are executable and which aren’t. Instead, there are file permissions. On Linux the main MapServer program is simply called mapserv. This is the same file that was discussed earlier in this chapter and copied into the cgi-bin folder of the web server.

Test the mapserv executable on a Linux and Apache system by going to the URL http://localhost/cgi-bin/mapserv. Notice there is no .exe suffix. You should get the same notice from MapServer as with the MS4W examples earlier:

No query information to decode. QUERY_STRING is set, but empty.

Testing the MapServer Setup Using a Demo Application

To test that MapServer can actually create maps, you will build on the demonstration map files used with the command-line tools in Chapter 10.

The countries_simpl shapefile dataset can be downloaded as a compressed archive file from http://ftp.intevation.de/freegis/worlddata/freegis_worlddata-0.1_simpl.tar.gz.

The map file example from Chapter 10 will be customized further and made into a web-based application. To do so, there are three files involved:

Map file

Stores all the settings required for drawing the map

Start page

Creates initial settings and launches the first map

HTML template page

A template MapServer fills in to show the current map, legend, etc.

Figure 11-1 illustrates how the basic application begins, with a start page sending settings to the CGI to produce a map.

The start page initializes MapServer by passing it a map file and an HTML template document
Figure 11-1. The start page initializes MapServer by passing it a map file and an HTML template document

Example 11-1 takes the demonstration map file global.map, used in the previous chapter, and adds the settings required to make it a web map. The additional settings are highlighted.

Example 11-1. Sample of global map application’s map file
MAP
  SIZE 600 300
  EXTENT -180 -90 180 90
  IMAGECOLOR 180 180 250

  UNITS DD

  SCALEBAR
    STATUS EMBED
    UNITS KILOMETERS
    INTERVALS 3
    TRANSPARENT TRUE
    OUTLINECOLOR 0 0 0
  END

  LEGEND
    STATUS EMBED
    POSITION LR
    TRANSPARENT TRUE
  END

  WEB
    TEMPLATE global.html
    IMAGEPATH "/srv/www/htdocs/tmp/"
    IMAGEURL "/tmp/"
 END

  LAYER
    NAME countries
    TYPE POLYGON
    STATUS DEFAULT
    DATA countries_simpl
    LABELITEM 'NAME'
   LABELMAXSCALE 50000000
    CLASSITEM 'NAME'
    CLASS
      NAME 'Bulgaria'
      EXPRESSION 'Bulgaria'
      OUTLINECOLOR 100 100 100
      COLOR 255 255 150
      LABEL
        SIZE LARGE
        OUTLINECOLOR 255 255 255
        MINFEATURESIZE 40
      END
    END
    CLASS
      NAME 'All Countries'
      EXPRESSION ('[NAME]' ne 'Bulgaria')
      OUTLINECOLOR 100 100 100
      COLOR 200 200 200
    END
  END
END

Only a few lines were added to make the map file work with the web server instead of command-line tools.

Tip

If using MS4W, the WEB object looks slightly different:

WEB
TEMPLATE global.html
IMAGEPATH "/ms4w/tmp/ms_tmp/"
IMAGEURL "/ms_tmp/"
END

A whole new object called WEB was added. There are three parts to the web object:

TEMPLATE global.html

The TEMPLATE file is specified by an HTML/web page document. It has some placeholders set to take the data created by MapServer and embed it into an output web page. The HTML in this page should be customized for your particular application. The examples shown in this chapter are some of the simplest possible. The options are endless, and this page can look as good or as bad as you want.

IMAGEPATH "/srv/www/htdocs/tmp/"

IMAGEPATH is the location on the local filesystem where MapServer can store its temporary images. The web server must be able to write new files into that folder.

IMAGEURL "/tmp/"

IMAGEURL tells the user’s web browser where the output map image is to be located. This isn’t the filesystem location, but a pointer to the same location as IMAGEPATH through an Internet URL. In this case /tmp/ comes off of the Apache DocumentRoot which is /srv/www/htdocs/. No special alias is set up, just the relative path to the location of the map file.

LABELMAXSCALE 50000000 was also added to the countries layer. This sets the map to show labels only when zoomed in to a certain map scale. The scale of the initial map is so small that Bulgaria is barely visible. Having a label on top of the country makes it harder to see; instead, as you zoom in to a more readable scale, the labels will appear.

With these map file settings in place, you can set up an initialization page. This page will be called index.html and its contents are listed in Example 11-2. The page can be called whatever you want.

Tip

To keep the examples simple, you can put all the files introduced here into the main web server document folder. You will probably want to move these to another location in the future, but for now put them in:

  • On Linux: /srv/www/htdocs/

  • On Windows with MS4W: \ms4w\apache\htdocs\

Example 11-2. Listing of the index.html MapServer initialization page
<HTML>
<HEAD><TITLE>MapServer Test</TITLE></HEAD>
<CENTER><H2>MapServer Test</H2>

<FORM method=GET action="/cgi-bin/mapserv">
  <INPUT type="hidden" name="map" value="/srv/www/htdocs/global.map">
  <INPUT type="hidden" name="layer" value="countries">

  <INPUT type="hidden" name="zoomdir" value=1>
  <INPUT type="hidden" name="zoomsize" value=2>

  <INPUT type="hidden" name="program" value="/cgi-bin/mapserv">

  <INPUT type="submit" value="Start MapServer">
</FORM></CENTER></BODY></HTML>

This example is almost as stripped down as it can be. There is a title and heading on the page. Other than that, the rest of the HTML code is for starting MapServer. This page can be completely customized as long as the form and its variables are maintained.

All this page shows is a single button: Start MapServer. Behind the page are a bunch of hidden settings that are passed to MapServer when the form is submitted. This starts the first map. After the application is started, the index.html page isn’t used.

Tip

CGI applications operate like stock brokers. Stock brokers receive certain requests, information, and a commission. In exchange, they provide some stocks for the client.

MapServer CGI applications receive requests for maps, including information like geographic extent, layers to show, etc. They then return a web page showing the requested map content. Fortunately, CGI applications don’t require a commission.

The index.html page isn’t essential to the application, but does make it a lot easier to get started. If you know how to form the proper request manually, you can put together a URL that requests the map from the MapServer CGI. After the initial request, all the settings and request information are stored in the URL and sent back to the CGI upon further requests.

The MapServer CGI application sends the viewer a new web page. The URL to that web page contains several variables (like the ones defined in index.html). When a user requests a change to the map, it then sends those variables back to the CGI for reprocessing. The purpose of the index.html page is to get the whole process started without forcing you to have a complex-looking initial URL.

As you saw in Example 11-1, a template file called global.html is specified in the map file. This template file is also a web page written in HTML. MapServer takes this page and populates portions of it before sending it back to the user’s web browser. Example 11-3 shows my global.html code.

Example 11-3. Listing of the global.html MapServer template file
<HTML>
<HEAD><TITLE>MapServer Test</TITLE></HEAD>
<CENTER><H2>MapServer Test</H2>
<HR>

<FORM method=GET action="/cgi-bin/mapserv">
  <INPUT NAME="img" TYPE="image" SRC="[img]" width=600 height=300 border=0
               ALT="Map Image">

  <INPUT type=hidden name=zoomdir value=1 [zoomdir_1_check] >
  <INPUT type=hidden name=zoomsize size=4 value=[zoomsize]>

  <INPUT type="hidden" name="imgxy" value="[center_x] [center_y]">
  <INPUT type="hidden" name="imgext" value="[mapext]">
  <INPUT type="hidden" name="map" value="[map]">
  <INPUT type="hidden" name="savequery" value="true">
  <INPUT type="hidden" name="mapext" value="shapes">

</FORM></CENTER></BODY></HTML>

If you are familiar with HTML forms and tags, you may notice that the only objects visible on this page are the heading and title and an image. The image source (SRC) is filled in by MapServer in real time when the page is generated.

When MapServer populates global.html, it checks for any MapServer-specific variables, such as [img]. MapServer replaces this text with the URL to the map image on the web site. This is the temporary map image that MapServer created.

Tip

There are many other MapServer variables that can be used in template documents. The [legend] and [scalebar] variables are discussed later in this chapter.

Figure 11-2 shows the output web page created by the global.html.

The map will look exactly like some of the map output from the command-line tools in Chapter 10. The only difference is that this map is being created by MapServer when requested, and automatically embedded into a web page.

The basic global.html template has virtually no controls, or at least none that are apparent. Controls refers to the buttons and check boxes you see on many web maps. These were deliberately left out so that this example would be simple to implement, and the HTML code wouldn’t be more confusing than necessary.

To interact with this map, just click on the map, and it will zoom in towards that location. Click once on the map, and then wait for the map to update. Once the image is updated, click again. Notice that if you zoom in toward Bulgaria, the label starts to appear. The results of clicking twice on the map are shown in Figure 11-3.

Because the number of controls has been kept to a minimum, there are no zoom-out buttons. In a web-mapping application this simple, the only way to zoom out is for

The basic global web map example
Figure 11-2. The basic global web map example
Zooming in to Bulgaria by clicking two times on the map
Figure 11-3. Zooming in to Bulgaria by clicking two times on the map

the user to go back in the web browser. This actually works reasonably intuitively because web page users are used to going back to previous pages.

If you’ve made it this far with your application, MapServer is set up and running well. Of course there are many other options and settings you can use. Some of these are covered in the next section when you start to make further customizations to the global map example.