Back to Devexpress

How to: Automatically Load Cartesian Data to a Geo Map

windowsforms-18176-controls-and-libraries-map-control-examples-vector-data-customize-data-appearance-how-to-automatically-load-cartesian-data-to-a-geo-map.md

latest8.0 KB
Original Source

How to: Automatically Load Cartesian Data to a Geo Map

  • Nov 13, 2018
  • 4 minutes to read

To automatically load Cartesian map data from a shapefile to a Geo map, do the following.

csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LoadGeoShapeData {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
csharp
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraMap;

namespace LoadGeoShapeData {
    public partial class Form1 : Form {
        List<MapData> data = new List<MapData>();
        ShapefileDataAdapter adapter = new ShapefileDataAdapter();

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            Uri baseUri = new Uri(System.Reflection.Assembly.GetEntryAssembly().Location);
            #region #AutomaticallyLoaded
            data.Add(new MapData() {
                Name = "Automatically loaded coordinate system",
                FileUri = new Uri(baseUri, "../../Shapefiles/Albers/switzerland.shp")
            });
            #endregion #AutomaticallyLoaded
            #region #LoadPrjFile
            data.Add(new MapData() {
                Name = "LoadPrjFile( ) calling loaded coordinate system",
                FileUri = new Uri(baseUri, "../../Shapefiles/Lambert/Belize.shp"),
                CoordinateSystem = ShapefileDataAdapter.LoadPrjFile(new Uri(
                    baseUri,
                    "../../Shapefiles/Lambert/Projection.prj"))
            });
            #endregion #LoadPrjFile
            #region #ManuallyCreated
            data.Add(new MapData() {
                Name = "Manually created coordinate system",
                FileUri = new Uri(baseUri, "../../Shapefiles/TransverseMercator/israel.shp"),
                CoordinateSystem = new CartesianSourceCoordinateSystem() {
                    CoordinateConverter = new UTMCartesianToGeoConverter() {
                        Hemisphere = Hemisphere.Northern, 
                        UtmZone = 36
                    }
                }
            });
            #endregion #ManuallyCreated

            cbCoordinateSystem.DataSource = data;
            cbCoordinateSystem.DisplayMember = "Name";

            VectorItemsLayer layer = new VectorItemsLayer() { Data = adapter };
            layer.ItemStyle.Fill = Color.FromArgb(60, 255, 128, 0);
            layer.DataLoaded += layer_DataLoaded;

            mapControl1.Layers.Add(layer);
        }

        void layer_DataLoaded(object sender, DataLoadedEventArgs e) {
            mapControl1.ZoomToFitLayerItems(0.4);
        }

        private void cbCoordinateSystem_SelectedIndexChanged(object sender, EventArgs e)
        {
            MapData mapData = cbCoordinateSystem.SelectedValue as MapData;
            if (mapData == null) return;

            adapter.FileUri = mapData.FileUri;
            if (mapData.CoordinateSystem != null)
                adapter.SourceCoordinateSystem = mapData.CoordinateSystem;
        }
    }

    public class MapData {
        public String Name { get; set; }
        public Uri FileUri { get; set; }
        public SourceCoordinateSystem CoordinateSystem { get; set; }
    }
}
vb
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Threading.Tasks
Imports System.Windows.Forms

Namespace LoadGeoShapeData
    Friend NotInheritable Class Program

        Private Sub New()
        End Sub

        ''' <summary>
        ''' The main entry point for the application.
        ''' </summary>
        <STAThread> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1())
        End Sub
    End Class
End Namespace
vb
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraMap

Namespace LoadGeoShapeData
    Partial Public Class Form1
        Inherits Form

        Private data As New List(Of MapData)()
        Private adapter As New ShapefileDataAdapter()

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            Dim baseUri As New Uri(System.Reflection.Assembly.GetEntryAssembly().Location)
' #Region "#AutomaticallyLoaded"
            data.Add(New MapData() With {.Name = "Automatically loaded coordinate system", .FileUri = New Uri(baseUri, "../../Shapefiles/Albers/switzerland.shp")})
' #End Region ' #AutomaticallyLoaded
' #Region "#LoadPrjFile"
            data.Add(New MapData() With {.Name = "LoadPrjFile( ) calling loaded coordinate system", .FileUri = New Uri(baseUri, "../../Shapefiles/Lambert/Belize.shp"), .CoordinateSystem = ShapefileDataAdapter.LoadPrjFile(New Uri(baseUri, "../../Shapefiles/Lambert/Projection.prj"))})
' #End Region ' #LoadPrjFile
' #Region "#ManuallyCreated"
            data.Add(New MapData() With { _
                .Name = "Manually created coordinate system", .FileUri = New Uri(baseUri, "../../Shapefiles/TransverseMercator/israel.shp"), .CoordinateSystem = New CartesianSourceCoordinateSystem() With { _
                    .CoordinateConverter = New UTMCartesianToGeoConverter() With {.Hemisphere = Hemisphere.Northern, .UtmZone = 36} _
                } _
            })
' #End Region ' #ManuallyCreated

            cbCoordinateSystem.DataSource = data
            cbCoordinateSystem.DisplayMember = "Name"

            Dim layer As New VectorItemsLayer() With {.Data = adapter}
            layer.ItemStyle.Fill = Color.FromArgb(60, 255, 128, 0)
            AddHandler layer.DataLoaded, AddressOf layer_DataLoaded

            mapControl1.Layers.Add(layer)
        End Sub

        Private Sub layer_DataLoaded(ByVal sender As Object, ByVal e As DataLoadedEventArgs)
            mapControl1.ZoomToFitLayerItems(0.4)
        End Sub

        Private Sub cbCoordinateSystem_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbCoordinateSystem.SelectedIndexChanged
            Dim mapData As MapData = TryCast(cbCoordinateSystem.SelectedValue, MapData)
            If mapData Is Nothing Then
                Return
            End If

            adapter.FileUri = mapData.FileUri
            If mapData.CoordinateSystem IsNot Nothing Then
                adapter.SourceCoordinateSystem = mapData.CoordinateSystem
            End If
        End Sub
    End Class

    Public Class MapData
        Public Property Name() As String
        Public Property FileUri() As Uri
        Public Property CoordinateSystem() As SourceCoordinateSystem
    End Class
End Namespace