|   | Home | Docs | Issue Tracker | FAQ | Download | 
|               | 
Table of Contents
Since version 6.0, MapServer has the ability to combine multiple features from a point layer into single (aggregated) features based on their relative positions. Only POINT layers are supported. This feature was added through MS RFC 69: Support for clustering of features in point layers.
POINT
The following processing options can be used with the cluster layers:
LAYER
  NAME "my-cluster"
  TYPE POINT
  ...
  CLUSTER
     MAXDISTANCE 20  # in pixels
     REGION "ellipse"  # can be rectangle or ellipse
     GROUP (expression)  # an expression to create separate groups for each value
     FILTER (expression) # a logical expression to specify the grouping condition
  END
  LABELITEM "Cluster:FeatureCount"
  CLASS
    ...
    LABEL
     ...
    END
  END
  ...
END
The clustered layer itself provides the following aggregated attributes:
These attributes (in addition to the attributes provided by the original data source) can be used to configure the labels of the features and can also be used in expressions. The ITEMS processing option can be used to specify a subset of the attributes from the original layer in the query operations according to the user’s preference.
We can use simple aggregate functions (Min, Max, Sum, Count) to specify how the clustered attribute should be calculated from the original attributes. The aggregate function should be specified as a prefix separated by ‘:’ in the attibute definition, like: [Max:itemname]. If we don’t specify aggregate functions for the source layer attributes, then the actual value of the cluster attribute will be non-deterministic if the cluster contains multiple shapes with different values. The Count aggregate function in fact provides the same value as Cluster:FeatureCount.
The CLUSTER object is exposed through PHP MapScript. An example follows:
$map = ms_newMapobj("/var/www/vhosts/mysite/httpdocs/test.map");
$layer1=$map->getLayerByName("test1");
$layer1->cluster;
The following example uses a point datasource, in this case in KML format, to display clusters of railway stations. Two classes are used: one to style and label the cluster, and one to style and label the single railway station.
Note
Since we can’t declare 2 labelitems, for the single railway class we use the TEXT parameter to label the station.
####################
# Lightrail Stations
####################
SYMBOL
  NAME "lightrail"
  TYPE PIXMAP
  IMAGE "../etc/lightrail.png"
END
LAYER
    NAME "lightrail"
    GROUP "default"
    STATUS DEFAULT
    TYPE POINT
    CONNECTIONTYPE OGR
    CONNECTION "lightrail-stations.kml"
    DATA "lightrail-stations"
    LABELITEM "Cluster:FeatureCount"
    CLASSITEM "Cluster:FeatureCount"
    ###########################
    # Define the cluster object
    ###########################
    CLUSTER
      MAXDISTANCE 50
      REGION "ellipse"
    END
    ################################
    # Class1: For the cluster symbol
    ################################
    CLASS
      NAME "Clustered Lightrail Stations"
      EXPRESSION ("[Cluster:FeatureCount]" != "1")
      STYLE
        SIZE 30
        SYMBOL "citycircle"
        COLOR 255 0 0
      END
      LABEL
        FONT scb
        TYPE TRUETYPE
        SIZE 8
        COLOR 255 255 255
        ALIGN CENTER
        PRIORITY 10
        BUFFER 1
        PARTIALS TRUE
        POSITION cc
      END
    END
    ################################
    # Class2: For the single station
    ################################
    CLASS
      NAME "Lightrail Stations"
      EXPRESSION "1"
      STYLE
        SIZE 30
        SYMBOL "lightrail"
      END
      TEXT "[Name]"
      LABEL
        FONT scb
        TYPE TRUETYPE
        SIZE 8
        COLOR 0 0 0
        OUTLINECOLOR 255 255 255
        ALIGN CENTER
        PRIORITY 9
        BUFFER 1
        PARTIALS FALSE
        POSITION ur
      END
    END
    # the following is used for a query
    TOLERANCE 50
    UNITS PIXELS
    HEADER "../htdocs/templates/cluster_header.html"
    FOOTER "../htdocs/templates/cluster_footer.html"
    TEMPLATE "../htdocs/templates/cluster_query.html"
END
