This project is read-only.

DotSpatialWeb - WebMap with points

Sep 20, 2013 at 5:12 PM
Brand new at this - about 8 hours in - having problems with adding a series of points to a WebMap control.

Map displays - points display - but the points are confused about how much room they are supposed to cover. The cluster of points is displaying around the 0,0 coordinate.

Code below.

Any insight on what I am missing would be helpful.
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CreateMap();

                WebToolBar1.WebMapID = "WebMap1";
                WebToolBar1.CreateStandardButtons();

                WebLegend1.WebMapID = "WebMap1";

                CreatePoints();

                IMapLayer newLayer = WebMap1.AddLayer("C:\\Temp\\newestpoints.shp");                
            }
        }

        private void CreatePoints()
        {
            Coordinate[] c = new Coordinate[50];
            Random rnd = new Random();
            Feature f = new Feature();
            
            FeatureSet fs = new FeatureSet(FeatureType.Point);
            fs.FillAttributes();
            fs.Projection = KnownCoordinateSystems.Projected.World.WebMercator;
            fs.CoordinateType = CoordinateType.Regular;


            for (int i = 0; i < 50; i++)
            {
                c[i] = new Coordinate(rnd.Next(-180,180), rnd.Next(-90, 90));
                fs.Features.Add(c[i]);
            }
            fs.SaveAs("C:\\Temp\\newestpoints.shp", true);
        }


        private void CreateMap()
        {
            String BasePath = Server.MapPath(@"~\Shape");
            WebMap1.Projection = KnownCoordinateSystems.Projected.World.WebMercator;
            WebMap1.MapViewExtents = new Extent(-20037508.342789, -20037508.342789, 20037508.342789, 20037508.342789);
            WebMapClient client = new WebMapClient();
            WMTClient WMT1 = new WMTClient();
            WMT1.Create(WebServiceType.GoogleMap);

            client.AddService(WMT1);
            WebMap1.Back = client;
        }
Sep 20, 2013 at 5:34 PM
SOLVED - found the solution in a post from last night actually.
Problem - points were not translated properly.
Changes below.

-D
        private void CreatePoints()
        {
            Coordinate[] c = new Coordinate[50];
            Random rnd = new Random();
            Feature f = new Feature();
            
            FeatureSet fs = new FeatureSet(FeatureType.Point);
            fs.FillAttributes();
            fs.Projection = KnownCoordinateSystems.Projected.World.WebMercator;
            
            fs.CoordinateType = CoordinateType.Regular;

            for (int i = 0; i < 50; i++)
            {
                c[i] = GeographicToWebMercator(rnd.Next(-90, 90), rnd.Next(-180, 180));
                fs.Features.Add(c[i]);
            }
            fs.SaveAs("C:\\Temp\\newestpoints.shp", true);
        }

        public static Coordinate GeographicToWebMercator(double lat, double lon) {
            Coordinate coord = new Coordinate(0, 0);
            if ((Math.Abs(lon) > 180) || (Math.Abs(lat) > 90))
            {
                return coord;
            }
            double num = (lon * 0.017453292519943295);
            double x = (6378137.0 * num);
            double a = (lat * 0.017453292519943295);
            coord = new Coordinate(x, (3189068.5 * Math.Log(((1 + Math.Sin(a)) / (1 - Math.Sin(a))))));
            return coord;
        }