How to: Implement a Custom Map Projection
- 6 minutes to read
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:
- Inherit a ProjectionBase class;
- Override the ProjectionBase.GeoPointToMapUnit and ProjectionBase.MapUnitToGeoPoint methods, which specify formulas to calculate custom projection coordinates and map geographical points (longitude and latitude);
- Override the ProjectionBase.GeoToKilometersSize and ProjectionBase.KilometersToGeoSize methods to convert the specified size in geographical points to the corresponding size in kilometers for the specified anchor point and vice versa.
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);
}
}
}