開源地圖 SharpMap

來源:互聯網
上載者:User

標籤:des   cWeb   style   blog   http   io   color   os   ar   

Step1 建立一個地圖控制項

1、啟動Visual Studio 2012 並建立一個新的Windows應用程式

2、調整項目到.net Framework 4.0全架構

3、開啟Form1的設計檢視

4、在工具箱底部,常規右擊點擊“選擇項” 

4、瀏覽SharpMap.UI.dll並添加

 

 SharpMap的dll和地圖檔案網盤共用地址:http://pan.baidu.com/s/1hqzG0de (內含Demo)

5、點擊確定

6、拖動MapBox控制項插入Form1表單中

7、將mapBox1控制項背景色設定為白色,Dock屬性設定為Fill

    

  

 

Step2 添加一個圖層到地圖控制項

 

1、添加SharpMap.dll到項目

  

 

2、添加地圖檔案到項目

  

 

3、修改表單建構函式Fomr1()

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;//設定平移        }

 

4、運行地圖可以看到地圖,並操作放大、縮小、平移

 

 Step3 給圖層添加樣式

1、修改表單建構函式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;        }

 

2、運行程式,

 

 

   Step4 添加WMS-層到地圖

1、修改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);        }

 

 

 

 2、運行程式,顯示城鎮。

 

  Step5 添加一個平鋪層作為背景

 在這個步驟中,可以結合網上瓦片伺服器資料連同本機資料顯示。

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

 2、添加輔助方法來建立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;        }

 

 3、修改建構函式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;        }

 

 

 4、運行程式,

 

開源地圖 SharpMap

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.