本文是利用ShapMap实现GIS的简单应用的小例子,以供学习分享使用。关于SharpMap的说明,网上大多是以ShapeFile为例进行简单的说明,就连官网上的例子也不多。本文是自己参考了源代码进行整理的,主要是WinForm的例子。原理方面本文也不过多论述,主要是实例演示,需要的朋友还是以SharpMap源码进行深入研究。

什么是SharpMap ?

SharpMap是一个基于.net 2.0使用C#开发的Map渲染类库,可以渲染各类GIS数据(目前支持ESRI Shape和PostGIS格式),可应用于桌面和Web程序。代码行数近10000行,可以算是一个实现了最基本功能的GIS系统,有利于研究学习使用。

涉及知识点:

  • SharpMap的基本概念:Layer(图层,常用图层:VectorLayer,LabelLayer) , IProvider(数据提供者,常用数据源:Ogr(对应MapInfo),ShapFile,DataTablePoint(对应DataSet))
  • 坐标转换:主要用于经纬度和地图坐标的转换。

SharpMap知识结构图:

效果图如下:

(一)车辆轨迹图:数据源:Excel数据

(二)定点数据(数据源:Excel)将河南省十七个城市,全部插上小红旗

(三)使用MapInfo做背景文件(此处通过程序调整了比例尺)

(四)使用ShapFile做背景图

核心代码

  1. using BruTile.Predefined;
  2. using GeoAPI.CoordinateSystems.Transformations;
  3. using ProjNet.CoordinateSystems;
  4. using ProjNet.CoordinateSystems.Transformations;
  5. using SharpMap;
  6. using SharpMap.Data.Providers;
  7. using SharpMap.Layers;
  8. using SharpMap.Rendering;
  9. using SharpMap.Rendering.Thematics;
  10. using SharpMap.Styles;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Data;
  14. using System.Data.OleDb;
  15. using System.Drawing;
  16. using System.Drawing.Drawing2D;
  17. using System.Drawing.Text;
  18. using System.Linq;
  19. using System.Text;
  20. using Point = GeoAPI.Geometries.Coordinate;
  21. namespace DemoSharpMap
  22. {
  23. public class SharpMapHelper
  24. {
  25.  
  26. private const string XlsConnectionString = "Provider={2};Data Source={0}\\{1};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
  27.  
  28. public static Map InitializeMap(MapType tt,float angle)
  29. {
  30. Map map = null;
  31. switch (tt)
  32. {
  33. case MapType.RunLine:
  34. map = InitializeMapOsmWithXls(angle);
  35. break;
  36. case MapType.MapInfo:
  37. map = InitializeMapinfo(angle);
  38. break;
  39. case MapType.ShapeFile:
  40. map = InitializeMapOrig(angle);
  41. break;
  42. case MapType.Static:
  43. map = InitializeMapOsmWithXls2(angle);
  44. break;
  45. default:
  46. map = InitializeMapOsmWithXls(angle);
  47. break;
  48. }
  49. return map;
  50. }
  51.  
  52. /// <summary>
  53. /// MapInfo格式的地图文件
  54. /// </summary>
  55. /// <param name="angle"></param>
  56. /// <returns></returns>
  57. private static Map InitializeMapinfo(float angle)
  58. {
  59. //Initialize a new map of size 'imagesize'
  60. Map map = new Map();
  61.  
  62. //Set up the countries layer
  63. VectorLayer layCountries = new VectorLayer("Countries");
  64. //Set the datasource to a shapefile in the App_data folder
  65. try
  66. {
  67. layCountries.DataSource = new Ogr("GeoData/MapInfo/countriesMapInfo.tab");
  68. }
  69. catch (TypeInitializationException ex)
  70. {
  71. if (ex.Message == "The type initializer for 'OSGeo.OGR.Ogr' threw an exception.")
  72. {
  73. throw new Exception(
  74. String.Format(
  75. "The application threw a PINVOKE exception. You probably need to copy the unmanaged dll's to your bin directory. They are a part of fwtools {0}. You can download it from: http://home.gdal.org/fwtools/",
  76. GdalRasterLayer.FWToolsVersion));
  77. }
  78. throw;
  79. }
  80.  
  81. //Set fill-style to green
  82. layCountries.Style.Fill = new SolidBrush(Color.Green);
  83. //Set the polygons to have a black outline
  84. layCountries.Style.Outline = Pens.Black;
  85. layCountries.Style.EnableOutline = true;
  86. layCountries.SRID = ;
  87.  
  88. //Set up a river layer
  89. VectorLayer layRivers = new VectorLayer("Rivers");
  90. //Set the datasource to a shapefile in the App_data folder
  91. layRivers.DataSource = new Ogr("GeoData/MapInfo/riversMapInfo.tab");
  92. //Define a blue 1px wide pen
  93. layRivers.Style.Line = new Pen(Color.Blue, );
  94. layRivers.SRID = ;
  95.  
  96. //Set up a river layer
  97. VectorLayer layCities = new VectorLayer("Cities");
  98. //Set the datasource to a shapefile in the App_data folder
  99. layCities.DataSource = new Ogr("GeoData/MapInfo/citiesMapInfo.tab");
  100. layCities.Style.SymbolScale = 0.8f;
  101. layCities.MaxVisible = ;
  102. layCities.SRID = ;
  103.  
  104. //Set up a country label layer
  105. LabelLayer layLabel = new LabelLayer("Country labels");
  106. layLabel.DataSource = layCountries.DataSource;
  107. layLabel.Enabled = true;
  108. layLabel.LabelColumn = "Name";
  109. layLabel.Style = new LabelStyle();
  110. layLabel.Style.ForeColor = Color.White;
  111. layLabel.Style.Font = new Font(FontFamily.GenericSerif, );
  112. layLabel.Style.BackColor = new SolidBrush(Color.FromArgb(, , , ));
  113. layLabel.MaxVisible = ;
  114. layLabel.MinVisible = ;
  115. layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center;
  116. layLabel.SRID = ;
  117. layLabel.MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.Largest;
  118.  
  119. //Set up a city label layer
  120. LabelLayer layCityLabel = new LabelLayer("City labels");
  121. layCityLabel.DataSource = layCities.DataSource;
  122. layCityLabel.Enabled = true;
  123. layCityLabel.LabelColumn = "Name";
  124. layCityLabel.Style = new LabelStyle();
  125. layCityLabel.Style.ForeColor = Color.Black;
  126. layCityLabel.Style.Font = new Font(FontFamily.GenericSerif, );
  127. layCityLabel.MaxVisible = layLabel.MinVisible;
  128. layCityLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left;
  129. layCityLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom;
  130. layCityLabel.Style.Offset = new PointF(, );
  131. layCityLabel.Style.Halo = new Pen(Color.Yellow, );
  132. layCityLabel.TextRenderingHint = TextRenderingHint.AntiAlias;
  133. layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
  134. layCityLabel.SRID = ;
  135. layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;
  136. layCityLabel.Style.CollisionDetection = true;
  137.  
  138. //Add the layers to the map object.
  139. //The order we add them in are the order they are drawn, so we add the rivers last to put them on top
  140. map.Layers.Add(layCountries);
  141. map.Layers.Add(layRivers);
  142. map.Layers.Add(layCities);
  143. map.Layers.Add(layLabel);
  144. map.Layers.Add(layCityLabel);
  145. //增加Layers
  146. var xlsPath = string.Format(XlsConnectionString, System.IO.Directory.GetCurrentDirectory(), "GeoData\\Henan.xls", Properties.Settings.Default.OleDbProvider);
  147. var ds = GetDataFromExcel(xlsPath, "Cities");
  148. //var ct = GetCoordinateTransformation();
  149. //TransCoordinate(ds, ct);
  150. string columeName = "Rotation";
  151. //Add Rotation Column
  152. AddColumeToDataSet(ds, columeName, -angle);
  153.  
  154. var xlsLayer = GetLayerFromDataSet2(ds, Color.GreenYellow);//Set up provider
  155.  
  156. map.Layers.Add(xlsLayer); //Add layer to map
  157. map.Center = xlsLayer.Envelope.Centre;// new Point(0, 0);
  158. map.MapScale = ;
  159. //map.Center = new Point(0, 0);
  160.  
  161. //_ogrSampleDataset = "MapInfo";
  162.  
  163. //Matrix mat = new Matrix();
  164. //mat.RotateAt(angle, map.WorldToImage(map.Center));
  165. //map.MapTransform = mat;
  166.  
  167. //map.ZoomToBox(xlsLayer.Envelope);
  168. return map;
  169. }
  170.  
  171. /// <summary>
  172. /// ShapeFile
  173. /// </summary>
  174. /// <param name="angle"></param>
  175. /// <returns></returns>
  176. private static Map InitializeMapOrig(float angle)
  177. {
  178. //Initialize a new map of size 'imagesize'
  179. Map map = new Map();
  180.  
  181. //Set up the countries layer
  182. VectorLayer layCountries = new VectorLayer("Countries");
  183. //Set the datasource to a shapefile in the App_data folder
  184. layCountries.DataSource = new ShapeFile("GeoData/World/countries.shp", true);
  185. //Set fill-style to green
  186. layCountries.Style.Fill = new SolidBrush(Color.FromArgb(, Color.Green));
  187. //Set the polygons to have a black outline
  188. layCountries.Style.Outline = Pens.Black;
  189. layCountries.Style.EnableOutline = true;
  190. layCountries.SRID = ;
  191.  
  192. //Set up a river layer
  193. VectorLayer layRivers = new VectorLayer("Rivers");
  194. //Set the datasource to a shapefile in the App_data folder
  195. layRivers.DataSource = new ShapeFile("GeoData/World/rivers.shp", true);
  196. //Define a blue 1px wide pen
  197. layRivers.Style.Line = new Pen(Color.Blue, );
  198. layRivers.SRID = ;
  199.  
  200. //Set up a cities layer
  201. VectorLayer layCities = new VectorLayer("Cities");
  202. //Set the datasource to a shapefile in the App_data folder
  203. layCities.DataSource = new ShapeFile("GeoData/World/cities.shp", true);
  204. layCities.Style.SymbolScale = 0.8f;
  205. layCities.MaxVisible = ;
  206. layCities.SRID = ;
  207.  
  208. //Set up a country label layer
  209. LabelLayer layLabel = new LabelLayer("Country labels");
  210. layLabel.DataSource = layCountries.DataSource;
  211. layLabel.Enabled = true;
  212. layLabel.LabelColumn = "Name";
  213. layLabel.Style = new LabelStyle();
  214. layLabel.Style.ForeColor = Color.White;
  215. layLabel.Style.Font = new Font(FontFamily.GenericSerif, );
  216. layLabel.Style.BackColor = new SolidBrush(Color.FromArgb(, , , ));
  217. layLabel.MaxVisible = ;
  218. layLabel.MinVisible = ;
  219. layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center;
  220. layLabel.SRID = ;
  221. layLabel.MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.Largest;
  222. layLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;
  223. layLabel.Style.CollisionDetection = true;
  224. layLabel.LabelPositionDelegate = fdr => fdr.Geometry.InteriorPoint.Coordinate;
  225. layLabel.PriorityColumn = "POPDENS";
  226.  
  227. //Set up a city label layer
  228. LabelLayer layCityLabel = new LabelLayer("City labels");
  229. layCityLabel.DataSource = layCities.DataSource;
  230. layCityLabel.Enabled = true;
  231. layCityLabel.LabelColumn = "Name";
  232. layCityLabel.Style = new LabelStyle();
  233. layCityLabel.Style.ForeColor = Color.Black;
  234. layCityLabel.Style.Font = new Font(FontFamily.GenericSerif, );
  235. layCityLabel.MaxVisible = layLabel.MinVisible;
  236. layCityLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left;
  237. layCityLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom;
  238. layCityLabel.Style.Offset = new PointF(, );
  239. layCityLabel.Style.Halo = new Pen(Color.Yellow, );
  240. layCityLabel.TextRenderingHint = TextRenderingHint.AntiAlias;
  241. layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
  242. layCityLabel.SRID = ;
  243. layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;
  244. layCityLabel.Style.CollisionDetection = true;
  245. layCityLabel.PriorityColumn = "POPULATION";
  246. layCityLabel.Theme = new GradientTheme(layCityLabel.PriorityColumn, , ,
  247. new LabelStyle
  248. {
  249. MaxVisible = ,
  250. CollisionBuffer = new Size(, ),
  251. CollisionDetection = true,
  252. Enabled = true,
  253. ForeColor = Color.LightSlateGray,
  254. Halo = new Pen(Color.Silver, ),
  255. HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center,
  256. VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Middle,
  257. Font = new Font(GenericFontFamilies.SansSerif.ToString(), 8f, FontStyle.Regular)
  258. },
  259. new LabelStyle
  260. {
  261. MaxVisible = layLabel.MinVisible,
  262. CollisionBuffer = new Size(, ),
  263. CollisionDetection = true,
  264. Enabled = true,
  265. ForeColor = Color.LightSlateGray,
  266. Halo = new Pen(Color.Silver, ),
  267. HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center,
  268. VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Middle,
  269. Font = new Font(GenericFontFamilies.SansSerif.ToString(), 16f, FontStyle.Bold)
  270. });
  271.  
  272. bool ignoreLength = false;
  273.  
  274. var layRiverLabel = new LabelLayer("River labels")
  275. {
  276. DataSource = layRivers.DataSource,
  277. Enabled = true,
  278. LabelColumn = "Name",
  279. TextRenderingHint = TextRenderingHint.AntiAlias,
  280. SmoothingMode = SmoothingMode.AntiAlias,
  281. SRID = ,
  282. LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection,
  283. MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.CommonCenter,
  284. Style =
  285. new LabelStyle
  286. {
  287. ForeColor = Color.DarkBlue,
  288. Font = new Font(FontFamily.GenericSansSerif, ),
  289. HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center,
  290. VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Middle,
  291. //CollisionDetection = true,
  292. Halo = new Pen(Color.Azure, ),
  293. IgnoreLength = ignoreLength,
  294. Offset = new PointF(, -)
  295.  
  296. },
  297. };
  298.  
  299. //Add the layers to the map object.
  300. //The order we add them in are the order they are drawn, so we add the rivers last to put them on top
  301. //map.BackgroundLayer.Add(AsyncLayerProxyLayer.Create(layCountries));
  302. map.Layers.Add(layCountries);
  303. map.Layers.Add(layRivers);
  304. map.Layers.Add(layCities);
  305. map.Layers.Add(layLabel);
  306. map.Layers.Add(layCityLabel);
  307. map.Layers.Add(layRiverLabel);
  308.  
  309. //增加Layers
  310. var xlsPath = string.Format(XlsConnectionString, System.IO.Directory.GetCurrentDirectory(), "GeoData\\Henan.xls", Properties.Settings.Default.OleDbProvider);
  311. var ds = GetDataFromExcel(xlsPath, "Cities");
  312. //var ct = GetCoordinateTransformation();
  313. //TransCoordinate(ds, ct);
  314. string columeName = "Rotation";
  315. //Add Rotation Column
  316. AddColumeToDataSet(ds, columeName, -angle);
  317.  
  318. var xlsLayer = GetLayerFromDataSet2(ds, Color.GreenYellow);//Set up provider
  319.  
  320. map.Layers.Add(xlsLayer); //Add layer to map
  321. //limit the zoom to 360 degrees width
  322. //map.MaximumZoom = 360;
  323. //map.BackColor = Color.LightBlue;
  324.  
  325. //map.Zoom = 360;
  326. map.Center = xlsLayer.Envelope.Centre;// new Point(0, 0);
  327. map.MapScale = ;
  328. //Matrix mat = new Matrix();
  329. //mat.RotateAt(angle, map.WorldToImage(map.Center));
  330. //map.MapTransform = mat;
  331. //map.ZoomToBox(xlsLayer.Envelope);
  332. return map;
  333. }
  334.  
  335. /// <summary>
  336. /// 在线显示,圆点显示轨迹
  337. /// </summary>
  338. /// <param name="angle"></param>
  339. /// <returns></returns>
  340. private static Map InitializeMapOsmWithXls(float angle)
  341. {
  342. var map = new Map();
  343.  
  344. var tileLayer = new TileAsyncLayer(
  345. KnownTileSources.Create(KnownTileSource.OpenStreetMap), "TileLayer - OSM with XLS");
  346. tileLayer.SRID = ;
  347. map.BackgroundLayer.Add(tileLayer);
  348.  
  349. //Get data from excel
  350. var xlsPath = string.Format(XlsConnectionString, System.IO.Directory.GetCurrentDirectory(), "GeoData\\Cities.xls", Properties.Settings.Default.OleDbProvider);
  351. var ds = GetDataFromExcel(xlsPath, "Cities");
  352. var ds1 = GetDataFromExcel(xlsPath, "Cities2");
  353. var ct = GetCoordinateTransformation();
  354. TransCoordinate(ds, ct);
  355. TransCoordinate(ds1, ct);
  356. string columeName = "Rotation";
  357. //Add Rotation Column
  358. AddColumeToDataSet(ds, columeName, -angle);
  359. AddColumeToDataSet(ds1, columeName, -angle);
  360.  
  361. var xlsLayer = GetLayerFromDataSet(ds, Color.GreenYellow);//Set up provider
  362. map.Layers.Add(xlsLayer); //Add layer to map
  363.  
  364. var xlsLayer1 = GetLayerFromDataSet(ds1, Color.Red);
  365. map.Layers.Add(xlsLayer1);
  366.  
  367. var xlsLabelLayer = GetLabelLayerByVectorLayer(xlsLayer, "XLSLabel");
  368.  
  369. xlsLabelLayer.Theme = new SharpMap.Rendering.Thematics.FontSizeTheme(xlsLabelLayer, map) { FontSizeScale = 1000f };
  370. map.Layers.Add(xlsLabelLayer);
  371. map.ZoomToBox(xlsLayer.Envelope.ExpandedBy(xlsLayer1.Envelope));
  372. return map;
  373. }
  374.  
  375. /// <summary>
  376. /// 在线显示,图标显示轨迹
  377. /// </summary>
  378. /// <param name="angle"></param>
  379. /// <returns></returns>
  380. private static Map InitializeMapOsmWithXls2(float angle)
  381. {
  382. var map = new Map();
  383.  
  384. var tileLayer = new TileAsyncLayer(
  385. KnownTileSources.Create(KnownTileSource.OpenStreetMap), "TileLayer - OSM with XLS");
  386. tileLayer.SRID = ;
  387. map.BackgroundLayer.Add(tileLayer);
  388.  
  389. //Get data from excel
  390. var xlsPath = string.Format(XlsConnectionString, System.IO.Directory.GetCurrentDirectory(), "GeoData\\Henan.xls", Properties.Settings.Default.OleDbProvider);
  391. var ds = GetDataFromExcel(xlsPath, "Cities");
  392. var ct = GetCoordinateTransformation();
  393. TransCoordinate(ds, ct);
  394. string columeName = "Rotation";
  395. //Add Rotation Column
  396. AddColumeToDataSet(ds, columeName, -angle);
  397.  
  398. var xlsLayer = GetLayerFromDataSet2(ds, Color.GreenYellow);//Set up provider
  399. map.Layers.Add(xlsLayer); //Add layer to map
  400.  
  401. var xlsLabelLayer = GetLabelLayerByVectorLayer(xlsLayer, "XLSLabel");
  402.  
  403. xlsLabelLayer.Theme = new FontSizeTheme(xlsLabelLayer, map) { FontSizeScale = 1000f };
  404. map.Layers.Add(xlsLabelLayer);
  405. map.ZoomToBox(xlsLayer.Envelope);
  406. return map;
  407. }
  408.  
  409. /// <summary>
  410. /// 从Excel中读取数据
  411. /// </summary>
  412. private static DataSet GetDataFromExcel(string xlsPath, string sheetName)
  413. {
  414. DataSet ds = new DataSet("XLS");
  415. string sql = string.Format("SELECT * FROM [{0}$];", sheetName);
  416. using (var cn = new OleDbConnection(xlsPath))
  417. {
  418. cn.Open();
  419. using (var da = new OleDbDataAdapter(new OleDbCommand(sql, cn)))
  420. {
  421. da.Fill(ds);
  422. }
  423. }
  424. return ds;
  425. }
  426.  
  427. /// <summary>
  428. /// 获取坐标转换对象
  429. /// </summary>
  430. /// <returns></returns>
  431. private static ICoordinateTransformation GetCoordinateTransformation()
  432. {
  433. //The SRS for this datasource is EPSG:4326, therefore we need to transfrom it to OSM projection
  434. var ctf = new CoordinateTransformationFactory();
  435. var cf = new CoordinateSystemFactory();
  436. var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
  437. var epsg3857 = cf.CreateFromWkt("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\",\"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\",\"6055\"]],PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\",\"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\",\"3857\"]]");
  438. var ct = ctf.CreateFromCoordinateSystems(epsg4326, epsg3857);
  439. return ct;
  440. }
  441.  
  442. /// <summary>
  443. /// 转换地球经纬度到坐标
  444. /// </summary>
  445. /// <param name="ds"></param>
  446. /// <param name="ct"></param>
  447. private static void TransCoordinate(DataSet ds, ICoordinateTransformation ct)
  448. {
  449. foreach (System.Data.DataRow row in ds.Tables[].Rows)
  450. {
  451. if (row["X"] == DBNull.Value || row["Y"] == DBNull.Value) continue;
  452. var coords = new[] { Convert.ToDouble(row["X"]), Convert.ToDouble(row["Y"]) };
  453. coords = ct.MathTransform.Transform(coords);
  454. row["X"] = coords[];
  455. row["Y"] = coords[];
  456. }
  457. }
  458.  
  459. /// <summary>
  460. /// 增加列
  461. /// </summary>
  462. /// <param name="ds"></param>
  463. /// <param name="columeName"></param>
  464. /// <param name="columeValue"></param>
  465. private static void AddColumeToDataSet(DataSet ds, string columeName, float columeValue)
  466. {
  467. ds.Tables[].Columns.Add(columeName, typeof(float));
  468. foreach (System.Data.DataRow row in ds.Tables[].Rows)
  469. {
  470. row["Rotation"] = -columeValue;
  471. }
  472. }
  473.  
  474. /// <summary>
  475. /// 轨迹用点表示
  476. /// </summary>
  477. /// <param name="ds"></param>
  478. /// <param name="c"></param>
  479. /// <returns></returns>
  480. private static VectorLayer GetLayerFromDataSet(DataSet ds, Color c)
  481. {
  482. var xlsProvider = new DataTablePoint(ds.Tables[], "OID", "X", "Y");
  483. var xlsLayer = new VectorLayer("XLS", xlsProvider)
  484. { Style = new VectorStyle() { PointColor = new SolidBrush(c) } };
  485. return xlsLayer;
  486. }
  487.  
  488. /// <summary>
  489. /// 获取带图标的图层
  490. /// </summary>
  491. /// <param name="ds"></param>
  492. /// <param name="c"></param>
  493. /// <returns></returns>
  494. private static VectorLayer GetLayerFromDataSet2(DataSet ds, Color c)
  495. {
  496. var xlsProvider = new DataTablePoint(ds.Tables[], "OID", "X", "Y");
  497. var xlsLayer = new VectorLayer("XLS", xlsProvider)
  498. { Style = { Symbol=Properties.Resources.redflag} };
  499. return xlsLayer;
  500. }
  501.  
  502. private static LabelLayer GetLabelLayerByVectorLayer(VectorLayer xlsLayer, string layerName)
  503. {
  504. var xlsLabelLayer = new LabelLayer(layerName)
  505. {
  506. DataSource = xlsLayer.DataSource,
  507. LabelColumn = "NAME",
  508. //PriorityColumn = "Population",
  509. Style =
  510. {
  511. CollisionBuffer = new System.Drawing.SizeF(2f, 2f),
  512. CollisionDetection = true
  513. },
  514. LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection
  515. };
  516. return xlsLabelLayer;
  517. }
  518. }
  519.  
  520. public enum MapType {
  521. ShapeFile = ,
  522. MapInfo = ,
  523. RunLine = ,//运行轨迹
  524. Static = //定点数据
  525.  
  526. }
  527. }

源码下载链接

备注:

1. 因用的MapInfo和Shape源文件为源码里面的,所有为英文显示。

C# SharpMap的简单使用的更多相关文章

  1. SharpMap简析

    1.背景 因为项目需求,需要基于开源项目来对SHP进行相关操作.涉及到的主要功能就是加载SHP读取其中的属性信息和几何信息.于是选择了Sharpmap来进行,在使用中对其相关功能做了初步了解,做个总结 ...

  2. 开源地图SharpMap源码解析-(1)简介

    1.简介 SharpMap最新版基于.NET Framework 4,采用C#开发的地图渲染引擎,非常易于使用.我这次研究的是比较稳定发布的V1.1版本.可以在GitHub下载该源码,地址:https ...

  3. SharpMap在web上的应用

    最近公司用SharpMap做了一个做桌面程序,它是一个开源的Gis项目,功能还可以,最大的特点就是简单易用,这里介绍下怎么在web下使用: 这次我们根据demo先了解一下如何show一个地图.这是最基 ...

  4. SharpMap源代码解析

    1. 简介 SharpMap是基于.net2.0的GIS系统.支持多种.NET开发语言(C# C++ 等).使用属性数据作为注记.符合OpenGIS的简单要素规范(OpenGIS Simple Fea ...

  5. [转]基于C#的开源GIS项目介绍之SharpMap篇

    我是一个刚毕业的GIS本科毕业生,目前在杭州从事GIS软件应用开发.在项目开发中总感觉自己的编程水平还不够,于是想找些开源GIS小项目来研究研究,借以提高自己的编程能力和项目开发能力.在网上搜了一下“ ...

  6. 【造轮子】打造一个简单的万能Excel读写工具

    大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...

  7. Fabio 安装和简单使用

    Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...

  8. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  9. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

随机推荐

  1. shell 中的单行注释和多行注释

    1. 单行注释 众所周知,#  比如想要注释:echo “ni” # echo "ni" 2. 多行注释: 法一: : << ! 语句1 语句2 语句3 语句4 ! 例 ...

  2. 一文了解Python中的循环(for while break continue 嵌套循环...)

    循环 目标 程序的三大流程 while 循环基本使用 break 和 continue while 循环嵌套 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 —— 从上向下,顺序执行 ...

  3. PHP之ThinkPHP框架(会话)

    网页会话即是实现页面跳转及数据传递,在web开发中,Cookie和Session的使用是极其重要的,GET和POST是最常使用的页面间数据传递的方法,相对于PHP脚本基础,在ThinkPHP中对网页会 ...

  4. python基础-变量运算符(3)

    一.注释 注释就是对代码的解释和说明.目的是为了让别人和自己很容易看懂.为了让别人一看就知道这段代码是做什么用的.正确的程序注释一般包括序言性注释和功能性注释.序言性注释的主要内容包括模块的接口.数据 ...

  5. chrome强制刷新,非ctrl+f5

    开发时,经常有ctrl+f5无法做到真正的强制刷新,以下可以帮到你 Ctrl+Shift+Del 清除Google浏览器缓存的快捷键 Ctrl+Shift+R 重新加载当前网页而不使用缓存内容

  6. kibana6.2.4版本更新x-pack认证

    我在上一次介绍了如何安装时基本使用elk留下了一个问题,这次来解决这个问题,相必大家也想知道,接下来就看详细过程. 上次说到,直接看图吧. 因为x-pack是收费的,所以试用期只有一个月.长期使用就必 ...

  7. 使用IntelliJ IDEA新建Java Web后端resfulAPI模板

    初始化项目 打开IntelliJ IDEA,我的版本是Version 2018.1.4.点击Create New Project.在左侧的列表中选择Maven.然后在右侧勾选Create from a ...

  8. API防重放机制

    说说API的防重放机制 我们在设计接口的时候,最怕一个接口被用户截取用于重放攻击.重放攻击是什么呢?就是把你的请求原封不动地再发送一次,两次...n次,一般正常的请求都会通过验证进入到正常逻辑中,如果 ...

  9. Unicode 是不是只有两个字节,为什么能表示超过 65536 个字符

      Unicode 目前规划的总空间是17个平面(平面0至16),0x0000 至 0x10FFFF.每个平面有 65536 个码点.你只是大致知道平面0(「Basic Multilingual Pl ...

  10. Jenkins结合.net平台工具之ReportGenerator

    上一节我们讲解了如何使用opencover生成单元测试覆盖率报告,opencover默认生成的report为xml格式,可读性并不是特别强,如果靠阅读opencover生成的results.xml来分 ...