Back to Devexpress

ProjectionBase Class

windowsforms-devexpress-dot-xtramap-45ae1278.md

latest14.0 KB
Original Source

ProjectionBase Class

The base class for all projections used in the MapControl.

Namespace : DevExpress.XtraMap

Assembly : DevExpress.XtraMap.v25.2.dll

NuGet Package : DevExpress.Win.Map

Declaration

csharp
public abstract class ProjectionBase :
    IProjectionCore
vb
Public MustInherit Class ProjectionBase
    Implements IProjectionCore

The following members return ProjectionBase objects:

Remarks

For more information, see Geographical Projections.

Example

This example shows how to get a Hammer-Aitoff map projection for the shapes loaded from the Shapefiles ( Countries.shp , Countries.dbf ).

To create a custom map projection, do the following:

csharp
using System;
using System.Windows.Forms;
using DevExpress.XtraMap;

namespace CustomProjection {
    public partial class Form1 : Form {
        const string filepath = "../../Data/Countries.shp";

        GeoMapCoordinateSystem CoordinateSystem {
            get { return (GeoMapCoordinateSystem)mapControl1.CoordinateSystem; }
        }

        VectorItemsLayer MapLayer {
            get { return (VectorItemsLayer)mapControl1.Layers["MapLayer"]; }
        }

        ShapefileDataAdapter Adapter {
            get { return (ShapefileDataAdapter)MapLayer.Data; }
        }

        public Form1() {
            InitializeComponent();
            CoordinateSystem.Projection = new HammerAitoffProjection();

            Uri baseUri = new Uri(System.Reflection.Assembly.GetEntryAssembly().Location);
            Adapter.FileUri = new Uri(baseUri, filepath);
        }
    }
}
csharp
using System;
using DevExpress.XtraMap;

namespace CustomProjection {
    public class HammerAitoffProjection : ProjectionBase {
        const double minLatitude = -90.0;
        const double maxLatitude = 90.0;
        const double minLongitude = -180.0;
        const double maxLongitude = 180.0;

        public override double OffsetX { get { return 0.5; } }
        public override double OffsetY { get { return 0.5; } }
        public override double ScaleX { get { return 0.5; } }
        public override double ScaleY { get { return -0.25; } }

        bool IsValidPoint(double x, double y) {
            if (Math.Pow(x, 2) + Math.Pow(y, 2) > 1)
                return false;
            else
                return true;
        }

        static double RadianToDegree(double value) {
            return value * 180.0 / Math.PI;
        }

        static double DegreeToRadian(double value) {
            return value * Math.PI / 180.0;
        }

        public override MapUnit GeoPointToMapUnit(GeoPoint geoPoint) {
            double lonInRadian = DegreeToRadian(
                Math.Min(
                    maxLongitude,
                    Math.Max(minLongitude, geoPoint.Longitude)
                )
            );
            double latInRadian = DegreeToRadian(
                Math.Min(
                    maxLatitude,
                    Math.Max(minLatitude, geoPoint.Latitude)
                )
            );
            double z = Math.Sqrt(1 + Math.Cos(latInRadian) * Math.Cos(lonInRadian / 2));
            double x = Math.Cos(latInRadian) * Math.Sin(lonInRadian / 2) / z;
            double y = Math.Sin(latInRadian) / z;

            return new MapUnit(x * ScaleX + OffsetX, y * ScaleY + OffsetY);

        }

        public override GeoPoint MapUnitToGeoPoint(MapUnit mapPoint) {
            double x = (mapPoint.X - OffsetX) / ScaleX;
            double y = Math.Min(1, Math.Max(-1, (mapPoint.Y - OffsetY) / ScaleY));

            if (IsValidPoint(x, y)) {
                double z = Math.Sqrt(1 - 0.5 * Math.Pow(x, 2) - 0.5 * Math.Pow(y, 2));
                double c = Math.Sqrt(2) * z * x / (2 * Math.Pow(z, 2) - 1);
                double lon = 2 * Math.Atan(c);
                double lat = Math.Asin(Math.Min(Math.Max(Math.Sqrt(2) * z * y, -1), 1));
                double latInDegree = lat * 180.0 / Math.PI;
                double lonInDegree = lon * 180.0 / Math.PI;

                return new GeoPoint(
                    Math.Min(
                        maxLatitude,
                        Math.Max(minLatitude, RadianToDegree(lat))
                    ),
                    Math.Min(
                        maxLongitude,
                        Math.Max(minLongitude, RadianToDegree(lon))
                    )
                );
            }
            else {
                int signX = (x < 0) ? -1 : 1;
                int signY = (y < 0) ? -1 : 1;
                return new GeoPoint(maxLatitude * signY, maxLongitude * signX);
            }

        }

        public override MapSize GeoToKilometersSize(GeoPoint anchorPoint, MapSize size) {
            return new MapSize(              
                size.Width * LonToKilometersRatio * Math.Cos(DegreeToRadian(anchorPoint.Latitude)),
                size.Height * LatToKilometersRatio
            );
        }

        public override MapSize KilometersToGeoSize(GeoPoint anchorPoint, MapSize size) {
            return new MapSize(
                size.Width / LonToKilometersRatio / Math.Cos(DegreeToRadian(anchorPoint.Latitude)),
                size.Height / LatToKilometersRatio
            );
        }
    }
}
vb
Imports System
Imports System.Windows.Forms
Imports DevExpress.XtraMap

Namespace CustomProjection
    Partial Public Class Form1
        Inherits Form

        Private Const filepath As String = "../../Data/Countries.shp"

        Private ReadOnly Property CoordinateSystem() As GeoMapCoordinateSystem
            Get
                Return CType(mapControl1.CoordinateSystem, GeoMapCoordinateSystem)
            End Get
        End Property

        Private ReadOnly Property MapLayer() As VectorItemsLayer
            Get
                Return CType(mapControl1.Layers("MapLayer"), VectorItemsLayer)
            End Get
        End Property

        Private ReadOnly Property Adapter() As ShapefileDataAdapter
            Get
                Return CType(MapLayer.Data, ShapefileDataAdapter)
            End Get
        End Property

        Public Sub New()
            InitializeComponent()
            CoordinateSystem.Projection = New HammerAitoffProjection()

            Dim baseUri As New Uri(System.Reflection.Assembly.GetEntryAssembly().Location)
            Adapter.FileUri = New Uri(baseUri, filepath)
        End Sub
    End Class
End Namespace
vb
Imports System
Imports DevExpress.XtraMap

Namespace CustomProjection
    Public Class HammerAitoffProjection
        Inherits ProjectionBase

        Private Const minLatitude As Double = -90.0
        Private Const maxLatitude As Double = 90.0
        Private Const minLongitude As Double = -180.0
        Private Const maxLongitude As Double = 180.0

        Public Overrides Property OffsetX() As Double
            Set(value As Double)
            End Set
            Get
                Return 0.5
            End Get
        End Property
        Public Overrides Property OffsetY() As Double
            Set(value As Double)
            End Set
            Get
                Return 0.5
            End Get
        End Property
        Public Overrides Property ScaleX() As Double
            Set(value As Double)
            End Set
            Get
                Return 0.5
            End Get
        End Property
        Public Overrides Property ScaleY() As Double
            Set(value As Double)
            End Set
            Get
                Return -0.25
            End Get
        End Property

        Private Function IsValidPoint(ByVal x As Double, ByVal y As Double) As Boolean
            If Math.Pow(x, 2) + Math.Pow(y, 2) > 1 Then
                Return False
            Else
                Return True
            End If
        End Function

        Private Shared Function RadianToDegree(ByVal value As Double) As Double
            Return value * 180.0 / Math.PI
        End Function

        Private Shared Function DegreeToRadian(ByVal value As Double) As Double
            Return value * Math.PI / 180.0
        End Function

        Public Overrides Function GeoPointToMapUnit(ByVal geoPoint As GeoPoint) As MapUnit
            Dim lonInRadian As Double = DegreeToRadian(Math.Min(maxLongitude, Math.Max(minLongitude, geoPoint.Longitude)))
            Dim latInRadian As Double = DegreeToRadian(Math.Min(maxLatitude, Math.Max(minLatitude, geoPoint.Latitude)))
            Dim z As Double = Math.Sqrt(1 + Math.Cos(latInRadian) * Math.Cos(lonInRadian / 2))
            Dim x As Double = Math.Cos(latInRadian) * Math.Sin(lonInRadian / 2) / z
            Dim y As Double = Math.Sin(latInRadian) / z

            Return New MapUnit(x * ScaleX + OffsetX, y * ScaleY + OffsetY)

        End Function

        Public Overrides Function MapUnitToGeoPoint(ByVal mapPoint As MapUnit) As GeoPoint
            Dim x As Double = (mapPoint.X - OffsetX) / ScaleX
            Dim y As Double = Math.Min(1, Math.Max(-1, (mapPoint.Y - OffsetY) / ScaleY))

            If IsValidPoint(x, y) Then
                Dim z As Double = Math.Sqrt(1 - 0.5 * Math.Pow(x, 2) - 0.5 * Math.Pow(y, 2))
                Dim c As Double = Math.Sqrt(2) * z * x / (2 * Math.Pow(z, 2) - 1)
                Dim lon As Double = 2 * Math.Atan(c)
                Dim lat As Double = Math.Asin(Math.Min(Math.Max(Math.Sqrt(2) * z * y, -1), 1))
                Dim latInDegree As Double = lat * 180.0 / Math.PI
                Dim lonInDegree As Double = lon * 180.0 / Math.PI

                Return New GeoPoint(Math.Min(maxLatitude, Math.Max(minLatitude, RadianToDegree(lat))), Math.Min(maxLongitude, Math.Max(minLongitude, RadianToDegree(lon))))
            Else
                Dim signX As Integer = If(x < 0, -1, 1)
                Dim signY As Integer = If(y < 0, -1, 1)
                Return New GeoPoint(maxLatitude * signY, maxLongitude * signX)
            End If

        End Function

        Public Overrides Function GeoToKilometersSize(ByVal anchorPoint As GeoPoint, ByVal size As MapSize) As MapSize
            Return New MapSize(size.Width * LonToKilometersRatio * Math.Cos(DegreeToRadian(anchorPoint.Latitude)), size.Height * LatToKilometersRatio)
        End Function

        Public Overrides Function KilometersToGeoSize(ByVal anchorPoint As GeoPoint, ByVal size As MapSize) As MapSize
            Return New MapSize(size.Width / LonToKilometersRatio / Math.Cos(DegreeToRadian(anchorPoint.Latitude)), size.Height / LatToKilometersRatio)
        End Function
    End Class
End Namespace

Inheritance

Show 15 items

Object ProjectionBase BraunStereographicProjection

EqualAreaProjection

EquidistantProjection

EquirectangularProjection

Etrs89LambertAzimuthalEqualAreaProjection

SphericalMercatorProjection

MillerProjection

EllipticalMercatorProjection

EPSG4326Projection

KavrayskiyProjection

LambertAzimuthalEqualAreaProjectionBase

LambertCylindricalEqualAreaProjection

SinusoidalProjection

See Also

ProjectionBase Members

DevExpress.XtraMap Namespace