Concepts on GeoServer and MapServer
Introduction to GeoServer and MapServer
GeoServer and MapServer are open-source servers that allow publishing geospatial data on the web. They are widely used for sharing interactive maps, geospatial web services (WMS, WFS, WMTS), and spatial data in standard formats defined by the Open Geospatial Consortium (OGC). These tools are essential for creating GIS (Geographic Information Systems) applications accessible via browsers or clients like QGIS.
Overview of GeoServer and MapServer
- GeoServer: An open-source Java server that enables the publishing of geospatial data from sources like PostGIS, shapefiles, or rasters. It provides an intuitive web interface for configuring layers, styles (via SLD), and OGC services (WMS, WFS, WCS, WMTS). GeoServer is ideal for projects requiring simplified management and advanced features like dynamic rendering.
- MapServer: An open-source mapping server written in C, designed for high performance and maximum flexibility. It uses text-based configuration files (Mapfiles) to define layers and styles. MapServer is perfect for environments needing fine customization or lightweight integration.
GeoServer
Configuration and Server Management
GeoServer can be installed on a local server or deployed via a Docker container. Download the stable version from the official site or use a Docker image.
Starting GeoServer:
- Download and unzip GeoServer or use Docker:
docker run -p 8080:8080 geoserver/geoserver
- Access the web interface at
http://localhost:8080/geoserver
. - Log in with default credentials (
admin/geoserver
) and change the password.
Adding a Data Store:
- In the web interface, go to Data Stores > Add a new data store.
- Select a source (e.g., PostGIS or shapefile).
- Configure the settings (e.g., host, database, file path).
Tip: Ensure that Java dependencies (JDK) are installed for a manual installation.
Publishing Spatial Data
Create a Layer:
- In Layers > Add a new resource, select a configured data store (e.g., a PostGIS table with a
POINT
geometry). - Publish the layer by defining a name and style (e.g., an SLD style for red points).
SLD style example for points:
<StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld">
<NamedLayer>
<Name>PointsOfInterest</Name>
<UserStyle>
<FeatureTypeStyle>
<Rule>
<PointSymbolizer>
<Graphic>
<Mark>
<WellKnownName>circle</WellKnownName>
<Fill>
<CssParameter name="fill">#FF0000</CssParameter>
</Fill>
</Mark>
<Size>10</Size>
</Graphic>
</PointSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
Accessing the Layer: Test the WMS service via a URL like:
http://localhost:8080/geoserver/wms?service=WMS&version=1.1.0&request=GetMap&layers=your_layer&bbox=-180,-90,180,90&width=800&height=600&srs=EPSG:4326&format=image/png
Geospatial Queries and Services
GeoServer supports OGC services like WMS (visualization), WFS (vector data), and WMTS (tiles).
Example WMS Query: Get a map of a layer:
http://localhost:8080/geoserver/wms?service=WMS&version=1.1.0&request=GetMap&layers=points_of_interest&bbox=2.2,48.8,2.4,48.9&width=800&height=600&srs=EPSG:4326&format=image/png
Example WFS Query: Retrieve vector data in GeoJSON:
http://localhost:8080/geoserver/wfs?service=WFS&version=1.0.0&request=GetFeature&typeName=points_of_interest&outputFormat=application/json
Spatial Query (CQL Filter): Filter points within a bounding box:
http://localhost:8080/geoserver/wms?service=WMS&version=1.1.0&request=GetMap&layers=points_of_interest&bbox=2.2,48.8,2.4,48.9&width=800&height=600&srs=EPSG:4326&format=image/png&CQL_FILTER=BBOX(geom,2.2,48.8,2.4,48.9)
Performance Optimization
- Tile Caching: Enable the GeoWebCache module (integrated with GeoServer) to generate WMTS tiles:
http://localhost:8080/geoserver/gwc/service/wmts?REQUEST=GetTile&VERSION=1.0.0&LAYER=points_of_interest&TILEMATRIX=EPSG:4326:0&TILEMATRIXSET=EPSG:4326&TILECOL=0&TILEROW=0&FORMAT=image/png
- Index Data: If using PostGIS, create spatial indexes on geometry columns.
- Optimize Styles: Simplify SLD rules to reduce rendering time.
MapServer
Configuration and Server Management
MapServer is usually installed on a Linux or Windows server. It can be deployed via packages or compiled from source.
Install MapServer (on Ubuntu):
sudo apt-get update
sudo apt-get install mapserver
Create a Mapfile:
A Mapfile is a text file that defines layers and services. Minimal example (mapfile.map
):
MAP
NAME "SimpleMap"
EXTENT -180 -90 180 90
SIZE 800 600
IMAGETYPE PNG
LAYER
NAME "Countries"
TYPE POLYGON
DATA "countries.shp"
STATUS ON
CLASS
STYLE
COLOR 200 200 200
OUTLINECOLOR 0 0 0
END
END
END
END
Test the Mapfile:
Use the mapserv
tool to verify:
mapserv -nh "QUERY_STRING=map=/path/to/mapfile.map&mode=map"
Tip: Host MapServer via a web server (Apache, Nginx) to expose OGC services.
Publishing Spatial Data
Add a Layer in a Mapfile: To publish a shapefile of points of interest:
LAYER
NAME "PointsOfInterest"
TYPE POINT
DATA "/path/to/points.shp"
STATUS ON
CLASS
STYLE
SYMBOL "circle"
SIZE 10
COLOR 255 0 0
END
END
END
Publish via WMS: Configure MapServer to respond to WMS requests by adding this to the Mapfile:
WEB
METADATA
"wms_title" "Points of Interest Map"
"wms_onlineresource" "http://localhost/cgi-bin/mapserv?map=/path/to/mapfile.map"
"wms_srs" "EPSG:4326"
END
END
Tip: Validate the Mapfile with the
shp2img
tool to generate a test image.
Geospatial Queries and Services
MapServer also supports WMS, WFS, and WMTS, configured through the Mapfile.
Example WMS Query:
http://localhost/cgi-bin/mapserv?map=/path/to/mapfile.map&service=WMS&version=1.1.0&request=GetMap&layers=PointsOfInterest&bbox=2.2,48.8,2.4,48.9&width=800&height=600&srs=EPSG:4326&format=image/png
Example WFS Query:
http://localhost/cgi-bin/mapserv?map=/path/to/mapfile.map&service=WFS&version=1.0.0&request=GetFeature&typeName=PointsOfInterest&outputFormat=GeoJSON
Spatial Query (Filter): Use a spatial filter in a WFS request:
http://localhost/cgi-bin/mapserv?map=/path/to/mapfile.map&service=WFS&version=1.0.0&request=GetFeature&typeName=PointsOfInterest&filter=<Filter><BBOX><PropertyName>geom</PropertyName><Box srsName='EPSG:4326'><coordinates>2.2,48.8 2.4,48.9</coordinates></Box></BBOX></Filter>&outputFormat=GeoJSON
Tip: Test services with a client like QGIS to validate the renders and data.
Performance Optimization
- Use Spatial Indexes: For shapefiles, generate an index with
shp2tile
or use databases like PostGIS with GIST indexes. - Enable Caching: Configure MapServer with a tiling system (e.g., TileCache) for WMTS services.
- Optimize Mapfiles: Reduce unnecessary layers and limit complex projections.
Tip: Monitor logs (GeoServer:
geoserver/data/logs
; MapServer: web server logs) to identify bottlenecks.
Best Practices
- Use OGC Standards: Ensure compatibility with GIS clients by adhering to WMS, WFS, and WMTS standards.
- Validate Data: Check geometry and reference system (e.g., EPSG:4326) before publishing.
- Test Services: Use tools like QGIS, OpenLayers, or Postman to validate service responses.
- Secure Access: Configure users and roles in GeoServer or access restrictions on MapServer via the web server.
- Document Configurations: Keep track of Mapfiles or SLD styles for easier maintenance.
Example of data validation in PostGIS before publication:
SELECT name, ST_IsValid(geom) AS is_valid
FROM points_of_interest;