CoordinateConvertCommon.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using DevExpress.XtraEditors;
  5. using DevExpress.XtraGrid;
  6. using DevExpress.XtraGrid.Columns;
  7. using DevExpress.XtraGrid.Views.Grid;
  8. using DevExpress.XtraTreeList;
  9. using DevExpress.XtraTreeList.Columns;
  10. using Tofly.Core.Context.Support;
  11. using Tofly.GIS.CoordinateConvert;
  12. using Tofly.GIS.Geometry;
  13. using Tofly.GIS.SpatialReference;
  14. using Tofly.GIS.Util;
  15. namespace CoordinateConvert.Win
  16. {
  17. class CoordinateConvertCommon
  18. {
  19. /// <summary>
  20. /// 添加列到控件中(如果为GridControl ,则指定其MainView)
  21. /// </summary>
  22. /// <typeparam name="T">控件类型</typeparam>
  23. /// <param name="pControl">控件对象</param>
  24. /// <param name="sColumnNames">字段名集合</param>
  25. /// <param name="sColumnCaption">字段标题集合</param>
  26. public static void AddColumn<T>(T pControl, string[] sColumnNames, string[] sColumnCaption, bool[] bAllowEidt)
  27. {
  28. if (pControl is GridView)
  29. {
  30. AddColumnMethod(pControl, sColumnNames, sColumnCaption, bAllowEidt);
  31. }
  32. else if (pControl is GridControl)
  33. {
  34. AddColumnMethod((pControl as GridControl).MainView, sColumnNames, sColumnCaption, bAllowEidt);
  35. }
  36. }
  37. /// <summary>
  38. /// 添加列到控件中
  39. /// </summary>
  40. /// <typeparam name="T">控件类型</typeparam>
  41. /// <param name="pControl">控件对象</param>
  42. /// <param name="sColumnNames">字段名集合</param>
  43. /// <param name="sColumnCaption">字段标题集合</param>
  44. internal static void AddColumnMethod<T>(T pControl, string[] sColumnNames, string[] sColumnCaption, bool[] bAllowEidt)
  45. {
  46. if (pControl == null)
  47. throw new Exception("控件对象为空!");
  48. if (sColumnNames == null || sColumnNames.Length == 0)
  49. throw new Exception("字段名称为空!");
  50. if (sColumnCaption == null)
  51. sColumnCaption = sColumnNames;
  52. if (sColumnCaption.Length != sColumnNames.Length)
  53. throw new Exception("字段标题与字段名个数不一致!");
  54. if (bAllowEidt != null && bAllowEidt.Length != sColumnNames.Length)
  55. throw new Exception("字段是否可编辑设置与字段名个数不一致!");
  56. if (typeof(T) == typeof(GridView))
  57. {
  58. GridView pGridView = pControl as GridView;
  59. GridColumn gridColumn = null;
  60. for (int i = 0; i < sColumnNames.Length; i++)
  61. {
  62. gridColumn = pGridView.Columns.Add();
  63. gridColumn.Visible = true;
  64. gridColumn.Name = sColumnNames[i];
  65. gridColumn.Caption = sColumnCaption[i];
  66. gridColumn.FieldName = sColumnNames[i];
  67. if (bAllowEidt != null)
  68. {
  69. gridColumn.OptionsColumn.AllowEdit = bAllowEidt[i];
  70. }
  71. }
  72. }
  73. else if (typeof(T) == typeof(TreeList))
  74. {
  75. TreeList pTreeList = pControl as TreeList;
  76. TreeListColumn pTreeListColumn = null;
  77. for (int i = 0; i < sColumnNames.Length; i++)
  78. {
  79. pTreeListColumn = pTreeList.Columns.Add();
  80. pTreeListColumn.Visible = true;
  81. pTreeListColumn.Name = sColumnNames[i];
  82. pTreeListColumn.FieldName = sColumnNames[i];
  83. pTreeListColumn.Caption = sColumnCaption[i];
  84. if (bAllowEidt != null)
  85. {
  86. pTreeListColumn.OptionsColumn.AllowEdit = bAllowEidt[i];
  87. }
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// 影藏控件列(如果为GridControl ,则指定其MainView)
  93. /// </summary>
  94. /// <typeparam name="T">控件类型</typeparam>
  95. /// <param name="pControl">控件名称</param>
  96. /// <param name="sColumnNames">影藏字段名称</param>
  97. public static void HiddenColumn<T>(T pControl, string[] sColumnNames)
  98. {
  99. if (pControl is GridView)
  100. {
  101. HiddenColumnMethod(pControl, sColumnNames);
  102. }
  103. else if (pControl is GridControl)
  104. {
  105. HiddenColumnMethod((pControl as GridControl).MainView, sColumnNames);
  106. }
  107. }
  108. /// <summary>
  109. /// 影藏控件列
  110. /// </summary>
  111. /// <typeparam name="T">控件类型</typeparam>
  112. /// <param name="pControl">控件名称</param>
  113. /// <param name="sColumnNames">影藏字段名称</param>
  114. internal static void HiddenColumnMethod<T>(T pControl, string[] sColumnNames)
  115. {
  116. if (pControl == null)
  117. throw new Exception("控件对象为空!");
  118. if (sColumnNames == null || sColumnNames.Length == 0)
  119. throw new Exception("字段名称为空!");
  120. if (typeof(T) == typeof(GridView))
  121. {
  122. GridView pGridView = pControl as GridView;
  123. GridColumn pGridColumn = null;
  124. for (int i = 0; i < sColumnNames.Length; i++)
  125. {
  126. pGridColumn = pGridView.Columns.ColumnByFieldName(sColumnNames[i]);
  127. if (pGridColumn != null)
  128. {
  129. pGridColumn.Visible = false;
  130. }
  131. }
  132. }
  133. else if (typeof(T) == typeof(TreeList))
  134. {
  135. TreeList pTreeList = pControl as TreeList;
  136. TreeListColumn pTreeListColumn = null;
  137. for (int i = 0; i < sColumnNames.Length; i++)
  138. {
  139. pTreeListColumn = pTreeList.Columns.ColumnByFieldName(sColumnNames[i]);
  140. if (pTreeListColumn != null)
  141. {
  142. pTreeListColumn.Visible = false;
  143. }
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// 创建DataTable
  149. /// </summary>
  150. /// <param name="sColumnNames">字段名</param>
  151. /// <param name="sColumnsAilas">字段别名</param>
  152. /// <param name="tColumnTypes">字段类型</param>
  153. /// <returns></returns>
  154. public static DataTable CreateDataTable(string[] sColumnNames, string[] sColumnsAilas, Type[] tColumnTypes)
  155. {
  156. if (sColumnNames == null || sColumnNames.Length == 0) return null;
  157. if (sColumnsAilas == null || sColumnsAilas.Length != sColumnNames.Length) return null;
  158. if (tColumnTypes == null || tColumnTypes.Length != sColumnNames.Length) return null;
  159. DataTable dtResult = new DataTable();
  160. DataColumn dcNewColumn = null;
  161. if (sColumnsAilas == null) sColumnsAilas = sColumnNames;
  162. for (int i = 0; i < sColumnNames.Length; i++)
  163. {
  164. dcNewColumn = dtResult.Columns.Add();
  165. dcNewColumn.ColumnName = sColumnNames[i];
  166. dcNewColumn.Caption = sColumnsAilas[i];
  167. dcNewColumn.DataType = tColumnTypes[i];
  168. }
  169. return dtResult;
  170. }
  171. /// <summary>
  172. /// 添加下拉选项到控件中
  173. /// </summary>
  174. /// <param name="lstItems"></param>
  175. /// <param name="pComboBoxEdit"></param>
  176. public static void AddItem(List<object> lstItems, ComboBoxEdit pComboBoxEdit)
  177. {
  178. if (lstItems == null || lstItems.Count == 0)
  179. throw new Exception("添加数据项为空!");
  180. if (pComboBoxEdit == null)
  181. throw new Exception("控件对象为空!");
  182. for (int i = 0; i < lstItems.Count; i++)
  183. {
  184. pComboBoxEdit.Properties.Items.Add(lstItems[i]);
  185. }
  186. }
  187. /// <summary>
  188. /// 根据图符号坐下点坐标 和分带标准获取西南角点所在空间参考
  189. /// </summary>
  190. /// <param name="iX"></param>
  191. /// <param name="iZone"></param>
  192. /// <returns></returns>
  193. public static ICoordinateSystem GetMapFrameSpatailReference(double dX, int iZone, EnumConvertSpatialType pEnumOffsetType)
  194. {
  195. try
  196. {
  197. int iResulteZone = GetPositionZone(dX, iZone);
  198. switch (pEnumOffsetType)
  199. {
  200. case EnumConvertSpatialType.Bj54ToXa80:
  201. case EnumConvertSpatialType.Bj54ToWgs84:
  202. return MapSpatialReferenceUtils.CreateProjectSR_GaussKruger
  203. (String.Format("Beijing54 {0} Zone", iZone), iResulteZone, true, true,
  204. MapSpatialReferenceUtils.CreateGSR_Beijing54());
  205. case EnumConvertSpatialType.Xa80ToBj54:
  206. case EnumConvertSpatialType.Xa80ToWgs84:
  207. return MapSpatialReferenceUtils.CreateProjectSR_GaussKruger
  208. (String.Format("Xian 80 {0} Zone", iZone), iResulteZone, true, true,
  209. MapSpatialReferenceUtils.CreateGSR_XiAn80());
  210. case EnumConvertSpatialType.Wgs84ToXa80:
  211. case EnumConvertSpatialType.Wgs84ToBj54:
  212. return MapSpatialReferenceUtils.CreateProjectSR_GaussKruger
  213. (String.Format("WGS 84 {0} Zone", iZone), iResulteZone, true, true,
  214. MapSpatialReferenceUtils.CreateGSR_WGS84());
  215. default:
  216. return null;
  217. }
  218. }
  219. catch (Exception ex)
  220. {
  221. return null;
  222. }
  223. }
  224. /// <summary>
  225. /// 根据图符号坐下点坐标 和分带标准获取西南角点所在空间参考
  226. /// </summary>
  227. /// <param name="iX"></param>
  228. /// <param name="iZone"></param>
  229. /// <returns></returns>
  230. public static ICoordinateSystem GetMapFrameSpatailReference2(double dX, int iZone, EnumConvertSpatialType pEnumOffsetType)
  231. {
  232. try
  233. {
  234. int iResulteZone = GetPositionZone(dX, iZone);
  235. switch (pEnumOffsetType)
  236. {
  237. case EnumConvertSpatialType.Bj54ToXa80:
  238. case EnumConvertSpatialType.Wgs84ToXa80:
  239. return MapSpatialReferenceUtils.CreateProjectSR_GaussKruger
  240. (String.Format("Xian 80{0} Zone", iZone), iResulteZone, true, true,
  241. MapSpatialReferenceUtils.CreateGSR_XiAn80());
  242. case EnumConvertSpatialType.Xa80ToBj54:
  243. case EnumConvertSpatialType.Wgs84ToBj54:
  244. return MapSpatialReferenceUtils.CreateProjectSR_GaussKruger
  245. (String.Format("Beijing 54 {0} Zone", iZone), iResulteZone, true, true,
  246. MapSpatialReferenceUtils.CreateGSR_Beijing54());
  247. case EnumConvertSpatialType.Bj54ToWgs84:
  248. case EnumConvertSpatialType.Xa80ToWgs84:
  249. return MapSpatialReferenceUtils.CreateProjectSR_GaussKruger
  250. (String.Format("WGS 84 {0} Zone", iZone), iResulteZone, true, true,
  251. MapSpatialReferenceUtils.CreateGSR_WGS84());
  252. default:
  253. return null;
  254. }
  255. }
  256. catch (Exception ex)
  257. {
  258. return null;
  259. }
  260. }
  261. /// <summary>
  262. /// 获取地理坐标点所在代号
  263. /// </summary>
  264. /// <param name="iX">坐标点 x坐标 (地理)</param>
  265. /// <param name="iZone"></param>
  266. /// <returns></returns>
  267. public static int GetPositionZone(double dX, int iZone)
  268. {
  269. int iResulteZone = 0;
  270. if ((dX % iZone) != 0)
  271. {
  272. int iTempMaxValue = Convert.ToInt32(Convert.ToDouble((dX / iZone)).ToString().Split('.')[0]);
  273. if (iTempMaxValue * iZone + (iZone / 2) < dX)
  274. {
  275. iResulteZone = iTempMaxValue + 1;
  276. }
  277. else
  278. {
  279. iResulteZone = iTempMaxValue;
  280. }
  281. }
  282. else
  283. {
  284. iResulteZone = Convert.ToInt32(dX / iZone);
  285. }
  286. return iResulteZone;
  287. }
  288. /// <summary>
  289. /// 通过图幅号获取图幅号矩形(新图幅号)
  290. /// </summary>
  291. /// <param name="sNewNumber">图幅号</param>
  292. /// <returns></returns>
  293. public static IEnvelope GetEnvelope(string sNewNumber)
  294. {
  295. IEnvelope pEnvelope = ContextRegistry.GetContext().GetObject("GIS_Envelope") as IEnvelope;
  296. IPoint pLeftDown = ContextRegistry.GetContext().GetObject("GIS_Point") as IPoint;
  297. double delx = 0;
  298. double dely = 0;
  299. GetScaleDelxy(sNewNumber[3], out delx, out dely);
  300. pLeftDown = GetPointFromMapFrame(sNewNumber);
  301. IPoint pLeftUp = ContextRegistry.GetContext().GetObject("GIS_Point") as IPoint;
  302. pLeftUp.X = pLeftDown.X;
  303. pLeftUp.Y = pLeftDown.Y + dely;
  304. pLeftUp.Z = 1;
  305. IPoint pRightUp = ContextRegistry.GetContext().GetObject("GIS_Point") as IPoint;
  306. pRightUp.X = pLeftDown.X + delx;
  307. pRightUp.Y = pLeftDown.Y + dely;
  308. pRightUp.Z = 1;
  309. IPoint pRightDown = ContextRegistry.GetContext().GetObject("GIS_Point") as IPoint;
  310. pRightDown.X = pLeftDown.X + delx;
  311. pRightDown.Y = pLeftDown.Y;
  312. pRightDown.Z = 1;
  313. pEnvelope.XMin = pLeftDown.X;
  314. pEnvelope.YMin = pLeftDown.Y;
  315. pEnvelope.XMax = pRightUp.X;
  316. pEnvelope.YMax = pRightUp.Y;
  317. return pEnvelope;
  318. }
  319. /// <summary>
  320. /// 获取精度误差
  321. /// </summary>
  322. /// <param name="iScale"></param>
  323. /// <returns></returns>
  324. static void GetScaleDelxy(char sValue, out double delX, out double delY)
  325. {
  326. delX = 0;
  327. delY = 0;
  328. switch (sValue)
  329. {
  330. case 'B': //50w
  331. delX = 3;
  332. delY = 2;
  333. break;
  334. case 'C': //25w
  335. delX = 1.5;
  336. delY = 1;
  337. break;
  338. case 'D': //10w
  339. delX = 0.5;
  340. delY = 1.0 / 3;
  341. break;
  342. case 'E': //5w
  343. delX = 0.25;
  344. delY = 1.0 / 6;
  345. break;
  346. case 'F': //2.5w
  347. delX = 0.125;
  348. delY = 5 / 60;
  349. break;
  350. case 'G': //1w
  351. delX = 0.0625; //3.75'
  352. delY = 0.125 / 3;
  353. break;
  354. case 'H': //5000
  355. delX = 0.03125; //1.875'
  356. delY = 0.125 / 6;
  357. break;
  358. default:
  359. break;
  360. }
  361. }
  362. /// <summary> 从新图幅号计算角点坐标 </summary>
  363. /// <param name="newNumber">输入的新图幅号(请先验证图幅号格式合法)</param>
  364. /// <returns>角点列表</returns>
  365. public static IPoint GetPointFromMapFrame(string sNewNumber)
  366. {
  367. //该比例尺下的图幅经度差和纬度差
  368. double delX = 0;
  369. double delY = 0;
  370. //iH为1:100万图幅纬度带字符对应的数字(A对应1,以此类推),iL为1:100万图幅经度带的数字码,例如J50中的50
  371. int iH = 0, iL = 0;
  372. //ih为所求比例尺地形图所在的行号,例如I50B002001中的002 ,il为所求比例尺地形图所在的列号,例如I50B002001中的001
  373. int ih = 0, il = 0;
  374. //100w 比例尺的情况下,没有后边六位的行列号,直接认为ih和il是0即可
  375. if (sNewNumber.Length == 3)
  376. {
  377. delX = 6;
  378. delY = 4;
  379. }
  380. else
  381. {
  382. //其它比例尺下,提取行列号
  383. if (!int.TryParse(sNewNumber.Substring(4, 3), out ih)
  384. || !int.TryParse(sNewNumber.Substring(7, 3), out il))
  385. return null;
  386. }
  387. if (!int.TryParse(sNewNumber.Substring(1, 2), out iL))
  388. return null;
  389. iH = (int)sNewNumber[0] - (int)'A' + 1;
  390. GetScaleDelxy(sNewNumber[3], out delX, out delY);
  391. //左下角点
  392. double x = (iL - 31) * 6 + (il - 1) * delX;
  393. double y = (iH - 1) * 4 + (4 / delY - ih) * delY;
  394. IPoint pResultPoint = ContextRegistry.GetContext().GetObject("GIS_Point") as IPoint;
  395. pResultPoint.PutCoords(x, y);
  396. //这里赋Z值并没有实际作用,只是为了防止出现“the geometry has null z value”错误
  397. pResultPoint.Z = 1;
  398. return pResultPoint;
  399. }
  400. }
  401. }