Load map in Mercator Transformation

Mar 13, 2007 at 7:14 PM
I want to load a VectorLayer with the Mercator transformation by default.

I have seen the Transformation example page in the demo web site, I have made the necessary changes to my demo application, but I get to map at all.

This is the code used:

    public partial class frmMain : Form
    {
        private Map mapControl;
        ICoordinateSystem coordinateSystem;
 
        public frmMain()
        {
            InitializeComponent();
            InitializeMap(imageMap.Size);
        }
 
        private void InitializeMap(Size size){
            mapControl = new Map(size);
 
            VectorLayer layCountries = new SharpMap.Layers.VectorLayer("Countries");
 
            //Set the datasource to a shapefile in the App_data folder
            SharpMap.Data.Providers.ShapeFile mapDataSource = new ShapeFile(@"data\countries.shp", true);
            layCountries.DataSource = mapDataSource;
            coordinateSystem = mapDataSource.CoordinateSystem;
 
            // Transform coordinate system to Mercator
            layCountries.CoordinateTransformation = Transform2Mercator(coordinateSystem);
 
            //Set fill-style to green
            layCountries.Style.Fill = new SolidBrush(Color.BurlyWood);
 
            //Set the polygons to have a black outline
            layCountries.Style.Outline = System.Drawing.Pens.Black;
            layCountries.Style.EnableOutline = true;
 
            layCountries.SRID = 4326;
            layCountries.SmoothingMode = SmoothingMode.None;
            
            mapControl.Layers.Add(layCountries);
            mapControl.MaximumZoom = 1024;
            mapControl.MinimumZoom = 1/64;
            mapControl.BackColor = Color.LightSeaGreen;    // Αυτό μπορεί να είναι η θάλασσα
 
            mapControl.Zoom = 16;
            mapControl.Center = new SharpMap.Geometries.Point(24.5, 38);
            imageMap.Map = mapControl;
            imageMap.ActiveTool = MapImage.Tools.Pan;
 
        }
 
        public static ICoordinateTransformation Transform2Mercator(ICoordinateSystem source)
        {
            CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
 
            System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
            parameters.Add(new ProjectionParameter("latitude_of_origin", 0));
            parameters.Add(new ProjectionParameter("central_meridian", 0));
            parameters.Add(new ProjectionParameter("false_easting", 0));
            parameters.Add(new ProjectionParameter("false_northing", 0));
            IProjection projection = cFac.CreateProjection("Mercator", "Mercator_2SP", parameters);
 
            IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Mercator", source as IGeographicCoordinateSystem, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
 
            return new CoordinateTransformationFactory().CreateFromCoordinateSystems(source, coordsys);
        }
 
 
    }
    

Any ideas, where is the mistake?


Thanks,
George J.
Coordinator
Mar 14, 2007 at 12:28 AM
I don't see a Map.ZoomToExtents() call or an explicit set of the Map.Zoom property anywhere... are you doing this somewhere I can't see?
Mar 14, 2007 at 5:25 AM
This is your problem:
mapControl.Zoom = 16;
mapControl.Center = new SharpMap.Geometries.Point(24.5, 38);

It looks like you are specifying longitude/latitude based values, but you are now working in a Mercator projection that uses completely different values (kinda Meter-based, but with a scale-distortion increasing away from Equator).
With those values, you will be centering on 24.5 meters left of Greenwich and 38 meters north of Equator, using a map width of 16 meters. Basically you are rendering a very small map in the Atlantic ocean.

Try to use a Map.ZoomToExtents() instead as codekaizen suggests.
Mar 14, 2007 at 5:26 AM
uuuh... you might want to double-check your min/max zoom values for the same reason.
Mar 14, 2007 at 6:39 AM
Ok, I had to do all these!

I removed the lines

            mapControl.MaximumZoom = 1024;
            mapControl.MinimumZoom = 1/64;

And I replaced the lines

            mapControl.Zoom = 16;
            mapControl.Center = new SharpMap.Geometries.Point(24.5, 38);

with the line

            mapControl.ZoomToExtents();

Thanks!
George J.
Mar 19, 2007 at 4:11 AM
I'm wondering when does it actually do the coordinate transformation? does it do on the fly when the layer is viewed?
Mar 19, 2007 at 9:48 AM
I am not quite sure about that, but I suppose each time the map is rendered - pan movement, zoom in, zoom out - then it has to recalculate all the coordinates in order to render the map.


George J.