Polygon Styling
Overview
This exercise displays building from OpenStreetMap.
Polygons
The buildings are a polygon dataset, so we set our LAYER TYPE
to POLYGON
:
When styling polygons we can set the colour of the polygon, and also its outline color:
Expressions
In this Mapfile we have two different classes for the dataset.
The first class has an EXPRESSION that limits
which features will be drawn. This compares the value for the "type" field for each feature with "office".
If there is a match then the feature is drawn with the STYLE
s from the CLASS
.
Code
Example
- Direct MapServer request: http://localhost:5000/?map=/etc/mapserver/polygons.map&mode=map&layer=buildings
- Local OpenLayers example: http://localhost:5001/polygons.html
Tip
A LAYER has a CONNECTIONTYPE that is used to connect to different data sources. The connection types are "native" - when the reading of the data is handled by MapServer code. The OGR connection type uses GDAL/OGR to read data sources. For some data types, as in the flatgeobuf example used here, there is an option to use either a native connection or an OGR connection.
There is also a PLUGIN connection type to allow connections to MS SQL Server and Oracle databases.
polygons.js
import '../css/style.css';
import ImageWMS from 'ol/source/ImageWMS.js';
import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM.js';
import View from 'ol/View.js';
import { Image as ImageLayer, Tile as TileLayer } from 'ol/layer.js';
const mapserverUrl = import.meta.env.VITE_MAPSERVER_BASE_URL;
const mapfilesPath = import.meta.env.VITE_MAPFILES_PATH;
const layers = [
new TileLayer({
source: new OSM(),
}),
new ImageLayer({
extent: [2968743.65508978, 8038921.67212233, 2982981.8632402, 8053818.05714347],
source: new ImageWMS({
url: mapserverUrl + mapfilesPath + 'polygons.map&',
params: { 'LAYERS': 'buildings', 'STYLES': 'offices' },
ratio: 1
}),
}),
];
const map = new Map({
layers: layers,
target: 'map',
view: new View({
center: [2975862.75916499, 8046369.8646329],
zoom: 14,
}),
});
polygons.map
MAP
NAME "Buildings"
EXTENT 26.668678 58.339241 26.796582 58.40941
UNITS DD
SIZE 800 600
PROJECTION
"init=epsg:4326"
END
WEB
METADATA
"ows_title" "OSM Buildings"
"ows_enable_request" "*"
"ows_srs" "EPSG:4326 EPSG:3857"
END
END
LAYER
NAME "buildings"
TYPE POLYGON
STATUS OFF
CONNECTIONTYPE OGR
# CONNECTIONTYPE FLATGEOBUF
# DATA "data/osm/buildings_a.fgb"
CONNECTION "data/osm/buildings_a.fgb"
CLASSGROUP "offices" # we can switch the default set of CLASSes here
CLASS
NAME "Offices" # this value is used for Legend titles for the CLASS
GROUP "offices"
EXPRESSION ( "[type]" = "office" )
STYLE
COLOR 255 0 0
OUTLINECOLOR 0 0 0
END
END
CLASS
NAME "Other"
GROUP "other"
STYLE
COLOR 246 241 223
OUTLINECOLOR 0 0 0
END
END
END
END
Exercises
-
Switch the
CLASSGROUP
in the Mapfile to see different styles. There are two groupsoffices
andother
. -
Switch the style used in the
polygon.js
file fromoffices
toother
: -
Switch the
CONNECTIONTYPE
to use the nativeFLATGEOBUF
driver. -
Experiment with styling the polygons.
WIDTH
can be used to change the width of the polygon outline.