Back to Devexpress

Simplify Map Vector Items

wpf-401538-controls-and-libraries-map-control-vector-data-simplify-map-vector-items.md

latest6.1 KB
Original Source

Simplify Map Vector Items

  • May 12, 2020
  • 2 minutes to read

The Map Control provides Map Editor API and Simplifiers to simplify detailed vector items, reduce memory consumption, and enhance a map’s responsiveness. To simplify an item, the Map Control decreases the number of item vertices. The resulting shape’s resolution depends on the tolerance parameter that defines the percentage of vertices that should remain after simplification.

Use one of the following approaches for item simplification:

Use Map Editor API

This approach allows you to use the Undo/Redo command to cancel/restore item simplification actions. These commands are available only when MapEditor.AllowSaveActions is enabled.

Call the MapEditor.SimplifyItems method to simplify a collection of items with the tolerance value you specified. This method uses the VisvalingamShapeSimplifier algorithm internally.

The following example shows how to simplify items and use TrackBarEdit to change the tolerance:

csharp
using System;
using System.Windows;
namespace SvgDataAdapterSample {
    public partial class MainWindow : Window {        
        public MainWindow() {
            InitializeComponent();            
        }
        private void OnTrackbarEditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e) {
            double tolerance = Convert.ToDouble(toleranceTrackbar.EditValue);
            mapControl.MapEditor.SimplifyItems(adapter.DisplayItems, tolerance);
        }
    }
}
vb
Imports System
Imports System.Windows
Namespace SvgDataAdapterSample   
    Public Class MainWindow
        Inherits Window

        Public Sub New()
            MyBase.New
            InitializeComponent
        End Sub

        Private Sub OnTrackbarEditValueChanged(ByVal sender As Object, ByVal e As DevExpress.Xpf.Editors.EditValueChangedEventArgs)
            Dim tolerance As Double = Convert.ToDouble(toleranceTrackbar.EditValue)
            mapControl.MapEditor.SimplifyItems(adapter.DisplayItems, tolerance)
        End Sub
    End Class
End Namespace
xaml
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxm="http://schemas.devexpress.com/winfx/2008/xaml/map" 
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
        x:Class="SvgDataAdapterSample.MainWindow"
        xmlns:local="clr-namespace:SvgDataAdapterSample"
        Title="MainWindow" Height="360" Width="640">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="10*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <dxm:MapControl x:Name="mapControl" Grid.Column="0" Margin="10">
            <dxm:VectorLayer x:Name="vectorLayer">
                <dxm:SvgFileDataAdapter x:Name="adapter" FileUri="Data/countries.svg" />
            </dxm:VectorLayer>
            <dxm:MapControl.MapEditor>
                <dxm:MapEditor x:Name="mapEditor">
                    <dxm:MapEditor.EditorPanelOptions>
                        <dxm:MapEditorPanelOptions Visible="False"/>
                    </dxm:MapEditor.EditorPanelOptions>
                </dxm:MapEditor>
            </dxm:MapControl.MapEditor>
        </dxm:MapControl>
        <dxe:TrackBarEdit x:Name="toleranceTrackbar" Grid.Column="1" 
                        VerticalAlignment="Center" Margin="10" 
                        Maximum="100" Minimum="0" EditValue="100"
                        Steps="0 20 40 60 80 100"
                        EditValueChanged="OnTrackbarEditValueChanged"/>
    </Grid>
</Window>

Use Simplifiers

You can use the ShapeSimplifierBase.Simplify method to simplify items before they are displayed on a layer.

xaml
<dxm:MapControl x:Name="mapControl">
    <dxm:VectorLayer x:Name="vectorLayer">
        <dxm:SvgFileDataAdapter x:Name="adapter" FileUri="Data/countries.svg" ShapesLoaded="OnShapesLoaded"/>
    </dxm:VectorLayer>
</dxm:MapControl>
csharp
private void OnShapesLoaded(object sender, ShapesLoadedEventArgs e) {
    VisvalingamShapeSimplifier simplifier = new VisvalingamShapeSimplifier();
    simplifier.Simplify(items: e.Shapes, tolerance: 20);
}
vb
Private Sub OnShapesLoaded(ByVal sender As Object, ByVal e As ShapesLoadedEventArgs)
    Dim simplifier As VisvalingamShapeSimplifier = New VisvalingamShapeSimplifier
    simplifier.Simplify(items:=e.Shapes, tolerance:=20)
End Sub

See Also

Providing Data