開源地圖 SharpMap

Step1 建立一個地圖控件

一、啓動Visual Studio 2012 並建立一個新的Windows應用程序服務器

二、調整項目到.net Framework 4.0全框架框架

三、打開Form1的設計視圖ide

四、在工具箱底部,常規右擊點擊「選擇項」 

四、瀏覽SharpMap.UI.dll並添加函數

 

 SharpMap的dll和地圖文件網盤共享地址:http://pan.baidu.com/s/1hqzG0de (內含Demo)工具

五、點擊肯定如圖:google

六、拖動MapBox控件插入Form1窗體中spa

七、將mapBox1控件背景色設置爲白色,Dock屬性設置爲Fill

    

  

 

Step2 添加一個圖層到地圖控件

 

一、添加SharpMap.dll到項目.net

  

 

二、添加地圖文件到項目設計

  

 

三、修改窗體構造函數Fomr1()code

複製代碼
public Form1()
        {
            InitializeComponent();
            VectorLayer vlay = new VectorLayer("States")
            {
                DataSource = new ShapeFile(@"path_to_data\states_ugl.shp", true)
            };
            mapBox1.Map.Layers.Add(vlay);
            mapBox1.Map.ZoomToExtents();
            mapBox1.Refresh();
            mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;//設置平移
        }
複製代碼

 

四、運行地圖能夠看到地圖,並操做放大、縮小、平移

 

 Step3 給圖層添加樣式

一、修改窗體構造函數Fomr2() 參見Dome

 

複製代碼
public Form2()
        {
            InitializeComponent();

            SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
            vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"path_to_data\states_ugl.shp", true);

            //構造土地樣式
            VectorStyle landStyle = new VectorStyle();
            landStyle.Fill = new SolidBrush(Color.FromArgb(232, 232, 232));

            //構造水樣式
            VectorStyle waterStyle = new VectorStyle();
            waterStyle.Fill = new SolidBrush(Color.FromArgb(198, 198, 255));

            //建立地圖
            Dictionary<string, SharpMap.Styles.IStyle> styles = new Dictionary<string, IStyle>();
            styles.Add("land", landStyle);
            styles.Add("water", waterStyle);

            //分配主題
            vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme<string>("class", styles, landStyle);

            mapBox1.Map.Layers.Add(vlay);
            mapBox1.Map.ZoomToExtents();
            mapBox1.Refresh();
            mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
        }
複製代碼

 

二、運行程序,如圖

 

 

  Step4 添加WMS-層到地圖

一、修改From3構造函數()

   調用http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer 服務器記載數據。

 

複製代碼
public Form3()
        {
            InitializeComponent();

            SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
            vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"path_to_data\states_ugl.shp", true);

            //構造土地樣式
            VectorStyle landStyle = new VectorStyle();
            landStyle.Fill = new SolidBrush(Color.FromArgb(232, 232, 232));

            //構造水樣式
            VectorStyle waterStyle = new VectorStyle();
            waterStyle.Fill = new SolidBrush(Color.FromArgb(198, 198, 255));

            //建立地圖
            Dictionary<string, SharpMap.Styles.IStyle> styles = new Dictionary<string, IStyle>();
            styles.Add("land", landStyle);
            styles.Add("water", waterStyle);

            //分配主題
            vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme<string>("class", styles, landStyle);

            mapBox1.Map.Layers.Add(vlay);
            mapBox1.Map.ZoomToExtents();
            mapBox1.Refresh();
            mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;

            SharpMap.Layers.WmsLayer wmsL =new SharpMap.Layers.WmsLayer("US Cities","http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer");
            //轉換爲PNG
            wmsL.SetImageFormat("image/png");
            //11.0版本
            wmsL.Version = "1.1.0";
            //添加城市圖層 服務名稱2
            wmsL.AddLayer("2");
            //設置 SRID
            wmsL.SRID = 4326;
            mapBox1.Map.Layers.Add(wmsL);
        }
複製代碼

 

 

 

 二、運行程序如圖,顯示城鎮。

 

 Step5 添加一個平鋪層做爲背景

 在這個步驟中,能夠結合網上瓦片服務器數據連同本地數據顯示。

 一、添加BruTile.dll、ProjNet.dll、GeoAPI.dll 到項目中

 二、添加輔助方法來建立google座標系

複製代碼
  private  GeoAPI.CoordinateSystems.IProjectedCoordinateSystem GetEPSG900913(ProjNet.CoordinateSystems.CoordinateSystemFactory csFact)
        {
            List<GeoAPI.CoordinateSystems.ProjectionParameter> parameters = new List<GeoAPI.CoordinateSystems.ProjectionParameter>();
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("semi_major", 6378137.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("semi_minor", 6378137.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("latitude_of_origin", 0.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("central_meridian", 0.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("scale_factor", 1.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("false_easting", 0.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("false_northing", 0.0));
            GeoAPI.CoordinateSystems.IProjection projection = csFact.CreateProjection("Google Mercator", "mercator_1sp", parameters);
            GeoAPI.CoordinateSystems.IGeographicCoordinateSystem wgs84 = csFact.CreateGeographicCoordinateSystem(
                "WGS 84", ProjNet.CoordinateSystems.AngularUnit.Degrees, ProjNet.CoordinateSystems.HorizontalDatum.WGS84, ProjNet.CoordinateSystems.PrimeMeridian.Greenwich,
                new GeoAPI.CoordinateSystems.AxisInfo("north", GeoAPI.CoordinateSystems.AxisOrientationEnum.North), new GeoAPI.CoordinateSystems.AxisInfo("east", GeoAPI.CoordinateSystems.AxisOrientationEnum.East)
            );

            GeoAPI.CoordinateSystems.IProjectedCoordinateSystem epsg900913 = csFact.CreateProjectedCoordinateSystem("Google Mercator", wgs84, projection, ProjNet.CoordinateSystems.LinearUnit.Metre,
              new GeoAPI.CoordinateSystems.AxisInfo("East", GeoAPI.CoordinateSystems.AxisOrientationEnum.East), new GeoAPI.CoordinateSystems.AxisInfo("North", GeoAPI.CoordinateSystems.AxisOrientationEnum.North));
            return epsg900913;
        }
複製代碼

 

 三、修改構造函數Form4()

 

複製代碼
        public Form4()
        {
            InitializeComponent();

            SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
            vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"path_to_data\states_ugl.shp", true);

            //構造土地樣式
            VectorStyle landStyle = new VectorStyle();
            landStyle.Fill = new SolidBrush(Color.FromArgb(232, 232, 232));

            //創造水樣式
            VectorStyle waterStyle = new VectorStyle();
            waterStyle.Fill = new SolidBrush(Color.FromArgb(198, 198, 255));

            //創造地圖
            Dictionary<string, SharpMap.Styles.IStyle> styles = new Dictionary<string, IStyle>();
            styles.Add("land", landStyle);
            styles.Add("water", waterStyle);

            //分配主題
            vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme<string>("class", styles, landStyle);

            mapBox1.Map.Layers.Add(vlay);

            ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            ProjNet.CoordinateSystems.CoordinateSystemFactory csFact = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
            vlay.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, GetEPSG900913(csFact));
            vlay.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(GetEPSG900913(csFact), ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);


            mapBox1.Map.BackgroundLayer.Add(new SharpMap.Layers.TileAsyncLayer(
                new BruTile.Web.OsmTileSource(), "OSM"));

            mapBox1.Map.ZoomToExtents();
            mapBox1.Refresh();
            mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;

        }
複製代碼

 

 

 四、運行程序,如圖:

 

相關文章
相關標籤/搜索