MapServer usage process
MapServer is a WebGIS solution. It is object-oriented, and the API organization of the basic configuration files MapFile and MapScript modules are object-based. MapServer supports distribution and interoperability by supporting several standards of the OGC Association. Whether using the MapServer CGI module or the server-side scripting language + MapScript, dynamic interaction between the server and the client's browser can be achieved. The syntax for configuring MapFile files is simple and easy to learn, this is especially true if you have experience with topical maps using desktop GIS software such as ArcMap. Compared to commercial software, MapServer is open source, you can use it for free, and you can modify the source code as needed. It should be pointed out that MapServer is an open source project and is constantly evolving. The content described in this article is based on the 4.4.2 version of Mapserver.
After configuring the MapFile file, you can use the MapServer CGI module or MapScript module to develop the WebGIS program. The difference between them:
Using the MapScript module requires other server-side solutions (referred to as scripting languages), such as PHP (installed on the Web Server as a CGI module). The MapScript module is used as an extension to PHP and is placed in the extensions directory of the PHP installation path. The MapScript module preserves the hierarchical object structure of the MapFile file and provides PHP with an API for the object structure. Then, the programmer can use PHP to flexibly select the MapFile API to modify the MapFile file on the server side; it is not as rigid as the MapServer CGI module. At the same time, combined with PHP support for many databases - non-spatial databases, such as Oracle, Sybase, MySQL, etc, it is easy to integrate spatial data and non-spatial data in WebGIS.
Languages supported by MapScript: PHP, Perl, Python, Java, Tcl, C#, etc.
Mapfile Basic Requirements
As you can see, the syntax of the Mapfile is relatively simple. In general, MapServer keywords and values are not case sensitive. In this book, however, the keywords are always uppercase and the values are lowercase. This practice is for clarity and is not required. However you should be aware that this situation may be important when you interact with a spatial dataset. For example, attribute names in the underlying database may be case-sensitive, so references to expressions containing attributes will also be case-sensitive.
Note that the 4.4.1 version of MapServer has a property name that is not case sensitive. Although this statement is the opposite of the "MAPFILE Reference MapServer 4.4" file (http://mapserver.gis.umn.edu/doc44/mapfile-reference.html). Assuming they are case sensitive, it is good practice. Of course, the attribute value is still case sensitive.
Strings containing embedded spaces must be quoted. Single or double quotes are acceptable, but they must be used in pairs. You can't take a string and use two different quote characters.
MapServer map file format analysis
MapServer configures the map to be published through a map file. The mapfile is the core of MapServer, which organizes various map elements into a hierarchical object system. The mapfile defines the elements that make up the map and the relationships between them, and tells the MapServer where to load the data and how to present it. In the mapfile, a layer is a combination of data and style, and the data is a combination of spatial data and attribute data. The style is implemented by the Class and Style objects.
Map files use
# as a line comment.
Map files are all started with the keyword MAP as the map object.
The keyword END is used as the end mark defined by the map object, and the entire map file represents a map object.
The attributes of the map object are described in a keyword/value pair between the MAP and the END key.
Map objects have many properties, such as map image format (IMAGETYPE), map size (SIZE), map data path (SHAPEPATH), map range (EXTENT), map symbol set (SYMBOLSET) and font set (FONTSET).
Each map file must have at least one layer object. Users can define as many layers as they want, but MapServer has a default maximum layer limit of 100 layers, which can be modified by modifying the map.h file.
MapServer not only supports vector layer, but also supports image layer in raster form. Such as GeoTIFF, JPEG, PNG and GIF. More worthy of attention is that MapServer also supports WMS layers. That is, a map image is obtained from other WMS servers to form a layer of the map object.
Each layer also has some common properties like the layer name (NAME), data set (DATA), Type (TYPE) and whether or not (STATUS) is displayed.
Each layer has one or more Class objects that are used to classify the features in the layer.Usually there is a keyword CLASSITEM (attribute) in the layer object, which is used to specify an attribute field, so that the layer can be divided into several categories (Class) or theme (Theme) according to the value of the field.If the keyword CLASSITEM is specified in the layer object, a keyword EXPRESSION is required in each Class object to indicate the attribute value represented by the category. Class objects usually have a Name property.
Each Class object uses one or more Style objects to define the display style of the feature of the class. Such as symbol type (SYMBOL), filled color (COLOR or OUTLINECOLOR) and symbol size (SIZE).
A Mapfile consists of hierarchical objects. At the top of the hierarchy is the map object (that is, the Mapfile itself). Map objects contain both simple and structured items. Simple projects consist of key-value pairs, and structured projects contain other projects, each of which can be simple or structured. You have seen examples of the structure of these two types of mapfiles. For example, the first.map discussed in Chapter 2 contains the following lines:
NAME "First" EXTENT -125.00 20.00 -65.00 50.00
Each of these lines contains some simple mapfile objects. You'll also notice that these keywords specify a value that makes the map as a whole meaningful. For example, the keyword "name" first sets the name of the map. This keyword is used at the map level to specify a string that identifies all maps produced during the mapfile's generation of the output file MapServer. Note, that keywords can use the same name at other levels, and its functionality depends on where it is used. Similarly, the scope of the keyword sets the scope of the entire map, so it must also be defined at the map hierarchy level. Like NAME, it can also be used at lower levels.
Note that if you are a new MapServer and consumer, using the same keywords (such as name, template, or color) can be confusing at different levels in the mapfile. When you are familiar with the concept of mapfile, this will not be a problem, in fact, you will be very grateful that developers choose to define the same keywords at different levels in the face of similar concepts.
First.map contains the following lines
WEB TEMPLATE '/var/www/htdocs/first.html' IMAGEPATH '/var/www/htdocs/tmp' IMAGEURL '/tmp/' END
This is an example of a structured object. The web object determines which HTML template MapServer will use and the location of the template. Web objects are typically used to determine how MapServer responds to web requests and can contain more keywords than shown here. Since the web object defines what is used to display the entire map, it makes sense to specify the map. However, the same keyword template can be used at a lower level and its functionality is very different. There are many MapServer objects that can define maps, but for now I will introduce a layer object for everyone.
Developing WebGIS with MapServer CGI Module
CGI is a set of rules that define the communication between a web server and other software on the same machine.Other software that follows the CGI communication rules is called a CGI program or a CGI script. The web server can implement a dynamic interaction function (Server-side) using a CGI program.Usually a CGI program is a small program running on the server side, which is called by the web server to process the data obtained from the web server (such as processing the form data, querying the database, etc.), and returning the processing result to the web server:
Web server - CGI program - other software (such as database).
CGI programs can be written in any language as long as they follow CGI communication rules. Compiled languages: C, C++, etc; Scripting languages: Perl, Python, Bourne shell, Java, etc. .
The MapServer CGI module is a CGI program written in C, which is very small. The core mapserv.exe of the MapServer CGI module is only 36KB.
When the browser tells the Web Server to call the MapServer CGI module through the URL, the Web Server creates a process for the CGI module.The CGI module runs and loads the MapFile file specified by the URL, reads the TEMPLATE file (HTML file) in the MapFile file, and replaces the CGI variable substitutions (template substitutions) in the TEMPLATE file with specific values;after processing the TEMPLATE file, the CGI module returns the TEMPLATE file processing result (HTML file) to the Web Server, which outputs the HTML file to the user's browser. At this time, the TEMPLATE file (HTML file) acts as the user's interactive interface.
The basic steps for designing a WebGIS using the MapServer CGI program:
Configure MapFile The user interface used is generally indicated in the TEMPLATE property of the Web Object. The user can also indicate the user interface used by the URL.
Design initialization interface The design initialization interface takes into account that the URL to access the CGI program (mapserv.exe) is very long, and the URL format for accessing the CGI program (mapserv.exe) is not known to the average user; therefore, the mapping to the CGI program (mapserv.exe) is embedded in the initial interactive interface. link (URL).
Design user interaction and interaction interface Here you need to bring out the key concepts of the MapServer CGI module: CGI variables and Templates. The MapServer CGI variable can be thought of as the interface to the CGI module mapserv.exe. Calling CGI variables in a URL or HTML form can do most of the dynamic interactions, such as layer selection, zooming in and out.
Templates are HTML files or URLs. The Templates contain CGI variables and their substitution variables, where the template substitutions correspond to the CGI variables. Design user interaction and interaction interfaces, using CGI variables and substitution variables in URLs or HTML files. Since template substitutions correspond to CGI variables one by one, why should we introduce the concept of template substitutions? It is indeed a bit more conceptual. However, using a substitution variable can be used to indicate that the CGI variable value is empty.
An example of developing a WebGIS using the MapServer CGI module is provided in Appendix 2.
Developing WebGIS with MapScript Module
Programming with CGI modules takes advantage of CGI variables and its replacement variables, while CGI modules are compiled programs (mapserv.exe); It is difficult to implement more complex dynamic interactions using CGI modules. Complex WebGIS functionality is made easier with other server-side scripting languages and MapScript.
Here we will introduce the use of MapScript module with the popular server-side scripting language PHP as an example.
PHP is installed in the Web Server as a CGI program. The MapScript module is placed under the extensions of the PHP installation path. The php.int file is configured to support the use of the MapScript module to complete the PHP/Mapscript installation.
The MapScript module can be loaded using the function: dl ("MapScript module name") in a
*.php file or a
The API provided by the MapScript module is object-based, which organizes objects in MapFile into object interfaces. The properties and methods of the object can be called in a
*.php file or a
The source code of the program written by the author using PHP/MapScript will be provided in Appendix 3.
MapServer data processing flow
The client passes the parameters and the required cgi program to MapServer. After MapServer receives the parameters transmitted by the client, it reads the mapfile according to the mapfile path specified in the parameter. Reading relevant data according to the data path specified in the mapfile. The data to be read is transferred to the text data. This work is done by calling the FreeType Font Engine with the GD Graphics Library. After the graphics are created, save them to the directory specified in the client parameters. MapServer reads the specified template (template file) according to the information defined in the mapfile, and performs the configuration and related data of html and screen display, and the production and embedding of graphics (such as proportional size, sample drawing, reference text). Dynamically generated spatial infographics embed templates to generate files that can be used by clients. Return to the client when done.
Some tools for Mapfile, including VIM highlighting tools, Sublime highlighting tools.
The MapFile file organizes various map elements into a hierarchical object system. The data source, the data format used, user interaction and support for the OGC protocol are also defined in MapFile. - MapObject - Querymap - Symbol - Projection - WebObject - Reference Map - Outputformat - Scalebar - Lengend - Layer Object - Label Object - Label Object - Feature - Projection - Class Object - Join Object - Label Object - Style Object - Grid Object
![Objects in MapFile and their hierarchical relationship; note that some objects are omitted for drawing convenience] (mapfile_obj_rel.png)
Objects in MapFile and their hierarchical relationships; note that some objects are omitted for drawing convenience.
The syntax of MapFile is simple. The MapFile keyword includes the object name (all objects in Figure 2), the object property (keyword), and the object end tag "END".
Object name and object end tag "END" are not case sensitive; attribute names are case sensitive, ESRI's shapfile file is configured to be capitalized, and PostGIS is always lowercase.
In the MapFile file, the symbol
# is commented on a single line. The MapFile file has a
.map extension and can be written using a variety of text editors.
The following is an example of MapFile:
To configure a complete MapFile file: GIS data source, font file, symbol file. GIS data sources can be distributed on different computers; Font files are used to define the type of font used in the output map. The points, lines, and polygon symbols used in the map can be defined in the MapFile file Symbol object; However, in order to achieve symbol reuse - there is no need to define the same symbol in different MapFile files, usually a separate symbol file is defined outside the MapFile.
Most of the objects in the MapFile file correspond to map features, or the concepts commonly used in GIS; Such as Lengend, Scalebar, Layer, etc. The role of these objects is more obvious and easy to understand. The meaning of the objects and attributes in the MapFile file is explained in detail in Appendix 1. Explain the meaning of four special objects here: Web Object, Outputformat Object, Query Object, Reference Map Object.
- Web Object defines how to handle web interfaces. For example, the attribute IMAGEPATH defines the path for storing temporary files and images, and the TEMPLATE defines the interface file used .
- Outputformat Object defines the format of the output image. MapServer converts GIS data format files (such as shapefiles) into image formats supported by the MIME (Multipurpose Internet Mail Extensions) protocol, such as gif, png, jpeg, etc. .
- QueryMap Object defines the generation mechanism of the query results. For example: attribute STYLE (not object) sets how the selected feature is displayed .
- Reference Map Object defined with reference to how the map creation. MapServer supports three reference map types. The most common usage of reference maps is as an observation window for maps.
The reference map can be used to observe the query results. When a point query occurs, a dot symbol is generated in the output Reference Map to indicate the location being queried. The region query produces a box in the Reference Map to indicate the query area. The feature query displays the characteristics of the query in the Reference Map.In summary, Reference Map can be considered as an observation window for queries 
After planning the WebGIS website, configure the MapFile file steps:
- Prepare the resources needed for the MapFile file, including GIS data, font files, and symbol files.
- According to the requirements of the WebGIS website, follow the syntax of MapFile, and use the text editing software or MapLab to write MapFile.
- Use the debugging software (such as MapLab) to test and check whether the MapFile configuration is correct and appropriate.