MapServer introduction

MapServer is a map publishing system based on fat server/thin client mode. The core part of MapServer is an open-source geospatial data rendering engine written in C language, as well as some map operation modules. Many of its functions depend on some open-source or free libraries. With the support of GEOS and OGR/GDAL for various vector and grid data, projection transformation is carried out in real time through proj shared library. MapServer follows OGC series specifications, can integrate open-source database PostgreSQL and PostGIS based on it, store and query geospatial data, support the transmission and expression of geospatial data based on a series of client JavaScript APIs such as leafet and openlayers, and comply with WMS, WFS, WCS, WMC, SLD, GML and A series of specifications such as filter encoding.

Support for different data formats is important. When implementing a project, or all format conversion, unified, or try to keep the original format unchanged, both are feasible strategies. The ability to instantly access multiple data formats without conversion makes MapServer one of the options for those who can’t (or won’t) convert to a specific format in bulk.

Whether it’s preparing still map images for web pages or publishing interactive Web sites, MapServer is up to the task. Interactive maps allow users to zoom in on specific areas and turn map layers on or off. Applications that let users select features on a map and link them to other data. The possibilities are endless, but the final product is usually the process of pointing, clicking, and viewing. The user identifies the area to view or the layer to view, and then updates the page with the requested content. If you need a static map, you can save the map for later use.

MapServer can be used as a common gateway interface (CGI) application, or scripted using a common Web programming language such as PHP, Perl, Python, and Java, or it can be run from the command line. Whether you’re using the CGI version or writing your own scripts, MapServer’s runtime profile controls which layers are displayed and how they are drawn. Editing profiles makes it easy to add mapping data to existing applications.

History of MapServer

MapServer originated from a cooperative project of the University of Minnesota (UMN), NASA, and the Minnesota Department of Natural Resources (MNDNR)-ForNet in the mid-1990s, and the TerrSIP project. Government support played a large role in the early development of MapServer. MapServer is released as open source software based on the MIT-style License. It is used to publish spatial data and interactive map applications on the Web. It can support all major operating systems, including Windows, Linux, Mac OS X. Wait. Its positioning is not to implement a full-featured GIS, but to provide core GIS functions that satisfy most Web applications.

MapServer has grown not as an isolated, but supported by many open source communities and open source enthusiasts. In November 2005, it was proposed to establish the MapServer Foundation, which is “promoting a professional open source network mapping development environment and community”. Even though the initial focus on network mapping projects, it is hoped that other open source geographic information projects will be able to fund projects “to promote the professional development of MapServer and to promote the development of the entire open source network mapping technology.” With the further development of open source GIS software and the further optimization of the open source network mapping environment, in February 2006 the MapServer Foundation plans to abandon it and instead formally emerge as an open source geospatial foundation (OSGeo), and Autodesk will be using MapGuide Joining the foundation as an open source further contributed to the development of MapServer.

Features of MapServer

There are many reasons why people decide to use MapServer. One is the ability to give others, especially over the Internet, extensive access to their mapping information. Many GIS and map analysts need to create custom map products for the products they support or work on. MapServer enables users to create maps without installing specific tools or getting help from a map analyst.This, in turn, relieves the pressure on professionals.

Others chose MapServer because it is one of the few solutions that can be used for solutions with multiple data formats. By using libraries such as GDAL/OGR, MapServer can access a variety of data formats without the need for data conversion.

In its most basic form, MapServer is a CGI program on a Web server. When a request is sent to MapServer, it creates an image of the requested map using the Mapfile and the corresponding parameters passed in the requested URL, and can return the legend, scale bar, reference map, and variable values passed by CGI. It has the following features:

(1)Supports displaying and querying hundreds of raster, vector and database formats, and supports tiled vector and raster data. Supported vector formats:ESRI shapefiles, PostGIS, ESRI's ArcSDE, etc. (implemented through OGR library);Supported raster formats:TIFF/GeoTIFF,EPPL7, etc.( realized by GDAL library). For Shapefile files, a quaternary tree spatial index can be established.

(2)It can run on a variety of different systems (Windows, Linux, Mac OS X, etc.), supports many open source application environments, and has some application suites developed based on MapServer.Provides two working methods, CGI method (applicable to CGI, Ajax,Flex developers) and MapScript methods (applicable to Php, Java, C# developers), the highest efficiency is the native CGI method;Provide support for popular scripting languages and development environments (PHP, Python, Perl, Ruby, Java, .NET).

(3)High-quality drawing model. It supports TrueType, a font standard jointly developed by Microsoft and Apple. Map elements (such as scale, legend, reference map, etc.) are automatically controlled. The image ratio is automatically adjusted. Feature labeling (including labeling conflict resolution). Support dynamic projection transformation (on-the-fly).

(4)Mapfile is the core of MapServer, which organizes various map elements into a hierarchical object system; And define the data source, the data format used, user interaction and support for the OGC protocol. Use logic or regular expressions to generate thematic maps. Fully supports customized template output. MapServer can be configured dynamically through URLs.

(5)Support for several OGC network service specifications, including WMS (client/server),Non-transactional WFS (client/server),WCS ( server only ),WMC,SLD,GML and Filter Encoding.

MapServer and its support tools are available for many hardware and operating systems. In addition, MapServer functionality can be accessed through a variety of programming language interfaces, allowing the MapServer functionality to be integrated into custom programs. MapServer can be used in custom environments that other Web mapping servers may not be able to run.

Because MapServer is open source, developers can improve, fix, and customize the actual code behind MapServer and port it to a new operating system or platform.In fact, if you need a new feature, you can hire a developer to add it, and everyone in the community can benefit from the work.

MapServer is primarily a viewing and mapping application that allows users to access maps through a Web browser or other Internet data sharing protocol. This allows map information to be visually shared using the OGC specification and data to be shared in real time with other applications. MapServer can perform pseudo-data conversions by reading various formats and using a common protocol to provide access to another server or application. MapServer is not an analysis tool, but it can display map information using different mapping techniques to visualize the results.

Data formats supported by MapServer

MapServer supports multiple formats. Some are internally supported by the MapServer program, while others are accessed through the GDAL/OGR library. Access through the library adds extra communication between MapServer and the data source itself, which can cause performance degradation in some cases.

MapServer creates map images from spatial information stored in a digital format. It can handle vector and raster data. MapServer natively reads two raster formats: GeoTIFF and EPPL7, but the supported formats (including Windows bitmaps, GIF and JPEG) can be read through the GDAL package. What is more interesting is that MapServer also supports network data resources (such as WMS, WFS) as layers, that is, to obtain map images from other WMS servers to form a layer of map objects.

Generally, formats supported by MapServer should run faster than formats using GDAL/OGR. For example, the most basic format used by MapServer is an ESRI shapefile or a GeoTiff image. OGR supports the US Census TIGER file format. Performance differences between loading TIGER or Shapefile can be significant. However, using GDAL/OGR may not be a problem. Further research has shown that data formats are often the bottleneck. If the data in the file is constructed in an inaccessible way or requires multiple levels of interpretation, it will affect the speed of the map drawing.

A general rule of thumb for best performance is to use the ESRI shapefile format or the GeoTiff image format. Since gdal_translate and ogr2ogr can write to these formats, you can use these tools to convert most source data. If the data is accessed via the network, storing the data in a PostGIS database may be a better choice. Because PostGIS processes the query of the data directly on the server, it can only send the desired results back over the network. For file-based data, more data must be passed before MapServer decides which data is needed. Server-side processing in the PostGIS database can significantly improve the performance of MapServer applications.

In this book’s introduction, two vector data formats: Shapefile and SpatiLite; and GeoTIFF raster data format will be used. There are some differences in the specific usage of using other data formats, and further reading of related technical documents is required.

Application architecture of MapServer

A complete WebGIS Application includes at least three levels of Architecture:

  • Application layer: client browser;

  • Middle layer: MapServer CGI module / MapScript scripting environment, Web server;

  • Storage layer: GIS spatial database.

A simple MapServer contains the following parts, as shown in Fig. 2:

MapServer Architecture

Fig. 2 MapServer architecture

Mapfile is a structured text configuration file of MapServer application. All data resources and font resources should be declared in mapfile. It defines the scope of the map, which is used to tell MapServer where the data is and where it is to be exported; it also defines map layers, including data sources, projections, and symbols. The extension of Mapfile must be .map, otherwise MapServer will not recognize it. Mapfile file organizes all kinds of map elements into a hierarchical object system. User interface and support for OGC protocol are also defined in mapfile.

HTML pages are optional and are generally used as templates in MapServer to wrap data for display. The HTML page is the interface between the user and MapServer. They are usually located at the web root. In the simplest form, MapServer can be called as a static map image on an HTML page; in order to enable the map to interact, the image is placed on an HTML form on the page.

MapServer CGI programs are binary executable files. Can receive requests and return images, data, etc. It is located in the ``cgi-bin ‘’ or `` scripts ``directory of the web server. The user of the web server must have execute permissions on these directories. For security reasons, they cannot be located in the root directory of the web. Under Unix / Linux operating system, this program is generally ``mapserv``, and under Windows operating system, it has ``.exe`` extension.

The role of the WEB / HTTP server is to serve HTML pages when a user’s browser initiates a request. For example, Apache or Microsoft’s IIS, MapServer needs to use them to provide services, is tightly coupled, and runs on the same machine as MapServer. This approach can be viewed as a traditional three-tier structure.

MapServer usually runs behind a web server application. The web server receives the request for the map and passes it to MapServer for creation. MapServer generates the requested map image and hands it to the web server, which then sends it back to the user. Shows how the user interacts with the Web server, and then the Web server makes a request to the MapServer program.

This drawing process (also called rendering) occurs whenever a user makes a new map request to MapServer, for example, when the user zooms in on the map for closer inspection. This process also occurs when the user manually requests a redraw (for example, when the content of a data layer changes and the user wants to see the changes).

Two modes of MapServer

MapServer can run in two different modes: CGI and MapScript. In CGI mode, MapServer runs as a CGI script in a web server environment. This is easy to set up and produces fast, straightforward applications. In MapScript mode, you can access the MapServer API from PHP, Perl, Python, Java, Tcl, or C#. The MapScript interface allows flexible, feature-rich applications that can still take advantage of MapServer’s template tools. You can use any module on the server side to write WebGIS programs. However, they use the same configuration file Mapfile.

CGI mode

Programming with CGI modules is to use CGI variables and its replacement variables, and CGI modules are compiled programs (mapserv.exe or mapserv); using CGI modules is difficult to achieve more complex dynamic interactions, but This situation is changing with the emergence of modern WebGIS services. Using other server-side scripting languages and MapScript can more easily implement complex WebGIS functions.

CGI is the most basic method for MapServer application development. In the CGI mode, MapServer uses the AGG graphics library to transform spatial data into geographic graphics and then send them back to the user’s browser. Users do not need to install any specific browser to operate it through the browser.

The user can directly send a request to the MapServer server through a URL similar to the following. MapServer creates a suitable map image according to the requested parameters and returns the result to the user’s browser:

>>> [print(i,p) for i,p in enumerate(re.split('[?&]',url))]
0 http://webgis.cn/cgi-bin/mapserv
1 map=/owg/mfa1.map
2 layer=world-country
3 mode=map

Here you just need to open the link to view, you don’t need to delve into the meaning of the URL, the related description will be expanded later.

MapScript mode

MapScript is an API (Application Programming Interface) that allows applications to call MapServer functions in multiple languages and then use the results in the desired way. MapScript releases developers from the limitations of the web-based user interface and provides access to other tools, such as database management systems, so that very powerful applications can be constructed. Such applications can provide mapping and spatial query capabilities as part of a tool suite.

In the official MapServer website, the languages that now support MapScript include PHP and Python. PHP has better support for MapScript. Regarding Python’s MapScrpit, the MapServer integrated only supports Python 2.7; In addition, there is independent PyPI-maintained code that can support Python 3, but currently only supports Visual Studio under Windows Compilation, support for Linux is planned.

Sean Gillies was a MapScript maintainer (until 2006), and he has several blog posts about MapScript that make a strong case for using Mapfiles instead of Mapscript. In particular, some design concepts and methods that have recently appeared in WebGIS can use more intuitive and flexible ways to implement some functions. Therefore, MapScript is not covered in this book.

MapScript bindings come in several different languages, thanks to SWIG’s creation of wrapper code for the C language. SWIG was developed by David Beazley, who later established PLY on which Mapfile was originally based. PLY is an implementation of Lex and Yacc parsing tools for Python-MapServer itself is a tool for parsing Mapfiles in C.

Comparison of MapServer and GeoServer

MapServer and GeoServer are both widely used open source WebGIS tools. Both of these are used to publish maps through the network, and they are often compared by others. Through comparison, you can further understand the characteristics of MapServer. MapServer complies with both OGC WMS and non-transactional WFS; GeoServer is written in Java, is servlet-based, and uses the struts framework.

It’s hard to tell which is better between MapServer and GeoServer, both of which are excellent, and both have excellent documentation and user base. GeoServer enables online editing of spatial data to generate thematic maps. Map publishing is an XML file. MapServer is better at generating thematic maps than ever before, and map publishing is through text profiles. MapServer is less functional than GeoServer, which is not a fully functional GIS, does not provide integrated DBMS (database management system) tools, has limited analytical capabilities, and does not have geo-registration tools.

If you can use MapServer without allowing changes to simply publish the map, MapServer’s support for WMS (Web Map service) is more efficient and easier to maintain. GeoServer was chosen because it wanted better functionality, and Geoserver is better at property queries combined with WFS (Web Feature service) specifications, such as online editing and database support such as PostgreSQL or Oracle Space Database; Another advantage of rver is that there is a free client software UDIG.

In addition to functional comparisons, especially when choosing technology in development, MapServer focuses on map service features that can be used as components of a project (even core components), but most of the other features need to be implemented by developers; After the installation is deployed, it can basically be used as a product.