Marker Cluster Symbolizer
Marker Cluster Symbolizer
The Marker Cluster Symbolizer is one of advanced symbolizers. It allows the clustering of markers in the specified distance.
In this C# example, we have drawn cluster centers as red circles. Also, we have drawn all nested cluster points by using different colors. To do this, we used the FeaturesBasedConfiguration function, which setups own style for each cluster.
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (var map = new Map(500, 300)) | |
{ | |
// take only part of the word | |
map.Extent = new Extent(-100, -60, 100, 60){SpatialReferenceSystem = SpatialReferenceSystem.Wgs84}; | |
// use difference colors for nested cluster points. | |
Color[] colors = new Color[] { Color.Green, Color.Aquamarine, Color.Gold, Color.Fuchsia, Color.Red, Color.Blue, Color.Brown, Color.Thistle, Color.Cyan, }; | |
var count = 0; | |
// create a cluster symbolizer and setup a cluster size | |
var symbolizer = new MarkerCluster(Measurement.Pixels(15)) | |
{ | |
// use own a style for each a cluster | |
FeaturesBasedConfiguration = (features, cluster) => | |
{ | |
// use more size for more points | |
var itemsInCluster = features.Count(); | |
cluster.Marker = new SimpleMarker() {FillColor = Color.Red, Size = 3 + itemsInCluster % 10}; | |
// allow draw inner cluster points (by default is none) | |
cluster.NestedMarker = new SimpleMarker() {FillColor = colors[count % colors.Length], Size = 3, StrokeStyle = StrokeStyle.None }; | |
count++; | |
}, | |
}; | |
map.Add(VectorLayer.Open(dataDir + "land.shp", Drivers.Shapefile)); | |
map.Add(VectorLayer.Open(dataDir + "places.shp", Drivers.Shapefile), symbolizer); | |
map.Padding = 20; | |
map.Render(dataDir + "out_cluster_countries.svg", Renderers.Svg); | |
} |
Here’s the result:
Draw Total Of Items
This sample displays total of items in the cluster.
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET | |
using (var map = new Map(500, 300)) | |
{ | |
// take only part of the word | |
map.Extent = new Extent(-100, -60, 100, 60) { SpatialReferenceSystem = SpatialReferenceSystem.Wgs84 }; | |
// create a cluster symbolizer and setup a cluster size | |
var clusterSymbolizer = new MarkerCluster(Measurement.Pixels(25)) | |
{ | |
// Use own a style for each a cluster | |
FeaturesBasedConfiguration = (features, cluster) => | |
{ | |
var itemsInCluster = features.Count(); | |
// First, we create an image with a drawn number for the cluster. This code not optimized of memory using. | |
// It is better to load prepare pictures from your file system. | |
var digitText = itemsInCluster.ToString(); | |
Stream memoryStream; | |
using (Bitmap digitBitmap = new Bitmap(40, 40)) | |
{ | |
using (Graphics graphics = Graphics.FromImage(digitBitmap)) | |
{ | |
// increase text quality | |
graphics.SmoothingMode = SmoothingMode.AntiAlias; | |
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; | |
if (digitText.Length == 1) | |
{ | |
graphics.DrawString(digitText, new Font("Arial", 30, GraphicsUnit.Pixel), | |
new SolidBrush(Color.Black), 5, 3); | |
} | |
else | |
{ | |
graphics.DrawString(digitText, new Font("Arial", 30, GraphicsUnit.Pixel), | |
new SolidBrush(Color.Black), -4, 3); | |
} | |
graphics.Flush(); | |
} | |
// store the bitmap to a stream | |
memoryStream = new MemoryStream(); | |
digitBitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); | |
} | |
var pathToDigit = AbstractPath.FromStream(memoryStream); | |
// Secondly, we use RasterImageMarker which renders our image (bitmap). | |
var symbolizer = new RasterImageMarker(pathToDigit) | |
{ | |
Width = Measurement.Pixels(12), | |
Height = Measurement.Pixels(12), | |
VerticalAnchorPoint = VerticalAnchor.Center, | |
HorizontalAnchorPoint = HorizontalAnchor.Center | |
}; | |
// As result, we create a complex symbolizer to join a background circle and the drawn digit. | |
var complexMarker = new LayeredSymbolizer(); | |
complexMarker.Add(new SimpleMarker() {Size = Measurement.Pixels(16), FillColor = Color.Azure}); | |
complexMarker.Add(symbolizer); | |
cluster.Marker = complexMarker; | |
}, | |
}; | |
// add layers with symbolizers | |
map.Padding = 20; | |
map.Add(VectorLayer.Open(dataDir + "land.shp", Drivers.Shapefile), new SimpleLine(){Width = 0.5 }); | |
map.Add(VectorLayer.Open(dataDir + "places.shp", Drivers.Shapefile), clusterSymbolizer); | |
// complete our map creation | |
map.Render(dataDir + "out_cluster_texts.svg", Renderers.Svg); | |
} |
Here’s the result: