Back to Devexpress

How to: Specify the Width-to-Height Ratio of Map Projection

windowsforms-113974-controls-and-libraries-map-control-examples-vector-data-customize-data-appearance-how-to-specify-the-width-to-height-ratio-of-map-projection.md

latest4.6 KB
Original Source

How to: Specify the Width-to-Height Ratio of Map Projection

  • Nov 13, 2018
  • 3 minutes to read

This example demonstrates how to customize the width/height ratio of the map projection. To specify the ratio, use the MapControl.InitialMapSize property.

In this example, the following ratios for the EqualAreaProjection are used. The ratio values can be found using the following link: Cylindrical Equal-area projection.

csharp
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraMap;

namespace CustomRatios {
    public partial class Form1 : Form {
        const string filepath = @"..\..\Data\Countries.shp";
        const int defaultSideSize = 512;

        class WidthHeightRatio {
            public string Name { get; set; }
            public double Value { get; set; }

            public override string ToString() {
                return Name;
            }
        }

        List<WidthHeightRatio> ratios = new List<WidthHeightRatio>() {
            new WidthHeightRatio() { Name = "Default", Value = 1 },
            new WidthHeightRatio() { Name = "Lambert", Value = 3.14 },
            new WidthHeightRatio() { Name = "Behrmann", Value = 2.36 },
            new WidthHeightRatio() { Name = "Trystan Edwards", Value = 2 },
            new WidthHeightRatio() { Name = "Gall-Peters", Value = 1.57 },
            new WidthHeightRatio() { Name = "Balthasart", Value = 1.3 }
        };

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            cbRatio.Properties.Items.AddRange(ratios);
            cbRatio.SelectedIndex = 0;

            Uri baseUri = new Uri(System.Reflection.Assembly.GetEntryAssembly().Location);
            Uri uri = new Uri(baseUri, filepath);
            mapControl1.Layers.Add(new VectorItemsLayer() {
                Data = new ShapefileDataAdapter() { FileUri = uri }
            });
        }

        private void OnRatioSelectedIndexChanged(object sender, EventArgs e) {
            mapControl1.InitialMapSize = new Size() {
                Width = defaultSideSize,
                Height = (int)(defaultSideSize / ((WidthHeightRatio)cbRatio.SelectedItem).Value)
            };
        }
    }
}
vb
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraMap

Namespace CustomRatios
    Partial Public Class Form1
        Inherits Form

        Private Const filepath As String = "..\..\Data\Countries.shp"
        Private Const defaultSideSize As Integer = 512

        Private Class WidthHeightRatio
            Public Property Name() As String
            Public Property Value() As Double

            Public Overrides Function ToString() As String
                Return Name
            End Function
        End Class

        Private ratios As New List(Of WidthHeightRatio)() From { _
            New WidthHeightRatio() With {.Name = "Default", .Value = 1}, _
            New WidthHeightRatio() With {.Name = "Lambert", .Value = 3.14}, _
            New WidthHeightRatio() With {.Name = "Behrmann", .Value = 2.36}, _
            New WidthHeightRatio() With {.Name = "Trystan Edwards", .Value = 2}, _
            New WidthHeightRatio() With {.Name = "Gall-Peters", .Value = 1.57}, _
            New WidthHeightRatio() With {.Name = "Balthasart", .Value = 1.3} _
        }

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            cbRatio.Properties.Items.AddRange(ratios)
            cbRatio.SelectedIndex = 0

            Dim baseUri As New Uri(System.Reflection.Assembly.GetEntryAssembly().Location)
            Dim uri As New Uri(baseUri, filepath)
            mapControl1.Layers.Add(New VectorItemsLayer() With { _
                .Data = New ShapefileDataAdapter() With {.FileUri = uri} _
            })
        End Sub

        Private Sub OnRatioSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbRatio.SelectedIndexChanged
            mapControl1.InitialMapSize = New Size() With {.Width = defaultSideSize, .Height = CInt((defaultSideSize / CType(cbRatio.SelectedItem, WidthHeightRatio).Value))}
        End Sub
    End Class
End Namespace