This project is read-only.

ProjectionSelectDialog

Feb 26, 2011 at 7:48 PM

I want to create a dialog to change the projection of the current map. In DotSpatial.Projections.Forms there already exists a dialog "ProjectionSelectDialog". If I programatically set the SelectedCoordinateSystem of the dialog to Map.Projection, I would like the Projection Type, major Category and minor category to be set to the correct values. Is there any built-in method in DotSpatial to do this?

(Given an instance of ProjectionInfo, find the matching KnownCoordinateSystem)?

Could we create a lookup table in DotSpatial.Projections, which will match the EsriString or Proj4String with the KnownCoordinateSystem?

Thanks,

Jiri

Mar 9, 2011 at 9:24 AM

Dear all,

I agree that this would be very usefull.

Another think. Why do all KnownCoordinateSystem have the EPGS set to zero? If the EPGS wouldn't be zero, it would be quiet easy to select the correct coordinate system.

Frank

Mar 9, 2011 at 2:12 PM

Related comment.  I noticed while profiling the other day that an application that references a KnownCoordinateSystem pays a time penalty up-front because ALL of the coordinate systems in the category get created whether needed or not.  I was thinking someday I might change the KnownCoordinateSystems to do lazy initialization of Known Coordiante Systems.  I think it would mean creating 'get' accessors for every known coordinate system and creating it inside the 'getter'.  It would be simple, but obviously a lot of work due to the sheer number.  Just something to keep in mind.  Thanks.  Kyle

Mar 11, 2011 at 3:32 PM

Dear all,

I changed the ProjectionSelectDialog in the following way:

  1. If the coordinate system is set, pior to showing the dialog, when the dialog pop's up, the correct coordinate system is selected. In order to do so, in the beginning a static list of all existing coordinate systems is created (not very efficient, but working for now)
  2. I added two new coordinate types: (i) none and (ii) custom. The first one (None) may not be interesting for you, but I need it for my system. The second one is usefull in order to provide a costume Proj4 String.

Since I have problems downloading the Code with Mercurial, here my complete code (I actually do not use ProjectionSelectDialog but my own control. Anyhow, names of the variables are all the same, so if somebody wants to cut and past this code back into the repository....

 

Frank

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotSpatial.Projections;

namespace ActionSoft.GIS.UI
{
    public partial class UCProjection : UserControl
    {

        private ProjectionInfo _selectedCoordinateSystem;

        /// <summary>
        /// Gets or sets the currently chosen coordinate system
        /// </summary>
        public ProjectionInfo SelectedCoordinateSystem
        {
            get 
            {
                if (!DesignMode)
                {
                    if (radProjected.Checked)
                    {
                        CoordinateSystemCategory c = KnownCoordinateSystems.Projected.GetCategory((string)cmbMajorCategory.SelectedItem);
                        _selectedCoordinateSystem = c.GetProjection((string)cmbMinorCategory.SelectedItem);
                    }
                    else if (radGeographic.Checked)
                    {
                        CoordinateSystemCategory c = KnownCoordinateSystems.Geographic.GetCategory((string)cmbMajorCategory.SelectedItem);
                        _selectedCoordinateSystem = c.GetProjection((string)cmbMinorCategory.SelectedItem);
                    }
                    else if (this.radCostum.Checked)
                    {
                        try
                        {
                            _selectedCoordinateSystem = new ProjectionInfo(this.proj4Text.Text);
                        }
                        catch (Exception)
                        {
                            _selectedCoordinateSystem = null;
                        }
                    }
                    else
                    {
                        _selectedCoordinateSystem = null;
                    }
                }
                return _selectedCoordinateSystem; 
            }
            set 
            {
                _selectedCoordinateSystem = value;

                this.proj4Text.Text = "";
                if (this._selectedCoordinateSystem != null)
                {
                    MajorAndMinorCategory.CoordinateTypes coordinateType;
                    if (this._selectedCoordinateSystem.IsLatLon)
                    {
                        this.radProjected.Checked = false;
                        this.radGeographic.Checked = true;
                        LoadMajorCategories();
                        coordinateType = MajorAndMinorCategory.CoordinateTypes.Geographic;
                    }
                    else
                    {
                        this.radProjected.Checked = true;
                        this.radGeographic.Checked = false;
                        LoadMajorCategories();
                        coordinateType = MajorAndMinorCategory.CoordinateTypes.Projected;
                    }

                    //Gets Category 
                    MajorAndMinorCategory selectedCategory = (from mmc in AllCoordinateSystems
                                                              where mmc.CoordinateType == coordinateType &&
                                                              mmc.ProjectionInfo.Equals(this._selectedCoordinateSystem)
                                                              select mmc).SingleOrDefault<MajorAndMinorCategory>();
                    if (selectedCategory != null)
                    {
                        this.cmbMajorCategory.SelectedIndex = this.cmbMajorCategory.Items.IndexOf(selectedCategory.MajorCategory);
                        LoadMinorCategories();
                        this.cmbMinorCategory.SelectedIndex = this.cmbMinorCategory.Items.IndexOf(selectedCategory.MinorCategory);
                    }
                    else
                    {
                        this.radCostum.Checked = true;
                        this.proj4Text.Text = this._selectedCoordinateSystem.ToProj4String();
                    }


                }
                else
                {
                    this.radNone.Checked = true;
                }
            }
        }

        

        public UCProjection()
        {
            InitializeComponent();
        }

        private void UCProjection_Load(object sender, EventArgs e)
        {
            if (this.DesignMode == false)
            {
                LoadMajorCategories();
                LoadMinorCategories();
            }
        }

        private void LoadMajorCategories()
        {

            this.cmbMajorCategory.Enabled = (this.radNone.Checked || this.radCostum.Checked) ? false : true;
            this.cmbMinorCategory.Enabled = (this.radNone.Checked || this.radCostum.Checked) ? false : true;
            this.proj4Text.Enabled = this.radCostum.Checked ? true : false;

            if (this.radProjected.Checked)
            {
                cmbMajorCategory.SuspendLayout();
                cmbMajorCategory.Items.Clear();
                string[] names = KnownCoordinateSystems.Projected.Names;
                foreach (string name in names)
                {
                    cmbMajorCategory.Items.Add(name);
                }
                cmbMajorCategory.SelectedIndex = 0;
                cmbMajorCategory.ResumeLayout();
            }
            else if (this.radGeographic.Checked)
            {
                cmbMajorCategory.SuspendLayout();
                cmbMajorCategory.Items.Clear();
                string[] names = KnownCoordinateSystems.Geographic.Names;
                foreach (string name in names)
                {
                    cmbMajorCategory.Items.Add(name);
                }
                cmbMajorCategory.SelectedIndex = 0;
                cmbMajorCategory.ResumeLayout();
            }
            else
            {
                this.cmbMajorCategory.Items.Clear();
                this.cmbMinorCategory.Items.Clear();
            }
        }

        private void LoadMinorCategories()
        {
            if (radProjected.Checked)
            {
                CoordinateSystemCategory c = KnownCoordinateSystems.Projected.GetCategory((string)cmbMajorCategory.SelectedItem);
                if (c == null) return;
                cmbMinorCategory.SuspendLayout();
                cmbMinorCategory.Items.Clear();
                string[] names = c.Names;
                foreach (string name in names)
                {
                    cmbMinorCategory.Items.Add(name);
                }
                cmbMinorCategory.SelectedIndex = 0;
                cmbMinorCategory.ResumeLayout();
            }
            else if (radGeographic.Checked)
            {
                CoordinateSystemCategory c = KnownCoordinateSystems.Geographic.GetCategory((string)cmbMajorCategory.SelectedItem);
                cmbMinorCategory.SuspendLayout();
                cmbMinorCategory.Items.Clear();
                string[] names = c.Names;
                foreach (string name in names)
                {
                    cmbMinorCategory.Items.Add(name);
                }
                cmbMinorCategory.SelectedIndex = 0;
                cmbMinorCategory.ResumeLayout();
            }
        }

        private void radProjected_CheckedChanged(object sender, EventArgs e)
        {
            LoadMajorCategories();
        }

        private void radNone_CheckedChanged(object sender, EventArgs e)
        {
            LoadMajorCategories();
        }

        private void radCostum_CheckedChanged(object sender, EventArgs e)
        {
            LoadMajorCategories();
        }

        private void cmbMajorCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadMinorCategories();
        }

        private void cmbMinorCategory_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        private static IList<MajorAndMinorCategory> allCoordinateSystems = null;
        private static IList<MajorAndMinorCategory> AllCoordinateSystems
        {
            get
            {
                if (allCoordinateSystems == null)
                {

                    allCoordinateSystems = new List<MajorAndMinorCategory>();

                    //Geographic Coordinate Systems
                    IList<String> geographics = DotSpatial.Projections.KnownCoordinateSystems.Geographic.Names.ToList();
                    foreach (string name in geographics)
                    {
                        DotSpatial.Projections.CoordinateSystemCategory c = DotSpatial.Projections.KnownCoordinateSystems.Geographic.GetCategory(name);

                        foreach (string innerName in c.Names)
                        {
                            allCoordinateSystems.Add(new MajorAndMinorCategory(name, innerName, c.GetProjection(innerName), MajorAndMinorCategory.CoordinateTypes.Geographic));
                        }
                    }

                    //Adds Projected Systems
                    IList<String> projected = DotSpatial.Projections.KnownCoordinateSystems.Projected.Names.ToList();
                    foreach (string name in projected)
                    {
                        DotSpatial.Projections.CoordinateSystemCategory c = DotSpatial.Projections.KnownCoordinateSystems.Projected.GetCategory(name);
                        if (c != null)
                        {
                            foreach (string innerName in c.Names)
                            {
                                allCoordinateSystems.Add(new MajorAndMinorCategory(name, innerName, c.GetProjection(innerName), MajorAndMinorCategory.CoordinateTypes.Projected));
                            }
                        }
                    }
                }
                return allCoordinateSystems;
            }

        }
    }

    public class MajorAndMinorCategory
    {
        public enum CoordinateTypes
        {
            Projected,
            Geographic,
            Local,
        }

        public String MajorCategory { get; set; }
        public String MinorCategory { get; set; }
        public ProjectionInfo ProjectionInfo { get; set; }
        public CoordinateTypes CoordinateType { get; set; }

        public MajorAndMinorCategory(string majorCategory, string minorCategory, ProjectionInfo pInfo, CoordinateTypes coordinateType)
        {
            this.MajorCategory = majorCategory;
            this.MinorCategory = minorCategory;
            this.ProjectionInfo = pInfo;
            this.CoordinateType = coordinateType;
        }

    }
}

Mar 15, 2011 at 6:43 PM

Hi Frank,

Thank you for your contribution. This is a good improvement to the ProjectionSelectDialog. I will test your code, and add it back to the repository.

I also like your suggestion of using EPSG code to identify KnownCoordinateSystem. That could be added in the next revision of the code.

---

Jiri