当前位置:   article > 正文

林业图斑范围点提取工具_图斑监测范围选取工具

图斑监测范围选取工具

注意:初次使用时请测试结果是否满足要求

此工具基于arcgis软件使用,较适用于计算地块数量较多的四至坐标,批量获取每个地块最北、最东、最西、最南四个方位的坐标点,适用于林地用地范围描叙,获取坐标点后,导出属性表直接获得四至点的坐标。
下载地址 链接: https://pan.baidu.com/s/19_tya6hQ_YTzaTD2CfFMPQ 提取码: 5na9 

使用方式 双击安装后,工具条中自定义>加载项管理器>自定义

将工具拖动到工具条中即可使用

工具界面如下:

插件代码:

form窗体代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using GISCommonHelper;
  10. using ESRI.ArcGIS.Carto;
  11. using ESRI.ArcGIS.Geodatabase;
  12. using ESRI.ArcGIS.Geometry;
  13. namespace SiZhiCoordinate
  14. {
  15. public partial class frmSet : Form
  16. {
  17. public frmSet()
  18. {
  19. InitializeComponent();
  20. }
  21. private IMap pMap = null;
  22. private IFeatureLayer pftlyr;
  23. private string sx;
  24. public string Sx
  25. {
  26. get { return sx; }
  27. set { sx = value; }
  28. }
  29. private string sy;
  30. public string Sy
  31. {
  32. get { return sy; }
  33. set { sy = value; }
  34. }
  35. private string ex;
  36. public string Ex
  37. {
  38. get { return ex; }
  39. set { ex = value; }
  40. }
  41. private string ey;
  42. public string Ey
  43. {
  44. get { return ey; }
  45. set { ey = value; }
  46. }
  47. private string nx;
  48. public string Nx
  49. {
  50. get { return nx; }
  51. set { nx = value; }
  52. }
  53. private string ny;
  54. public string Ny
  55. {
  56. get { return ny; }
  57. set { ny = value; }
  58. }
  59. private string wx;
  60. public string Wx
  61. {
  62. get { return wx; }
  63. set { wx = value; }
  64. }
  65. private string wy;
  66. public string Wy
  67. {
  68. get { return wy; }
  69. set { wy = value; }
  70. }
  71. public IFeatureLayer Pftlyr
  72. {
  73. get { return pftlyr; }
  74. set { pftlyr = value; }
  75. }
  76. private void frmSet_Load(object sender, EventArgs e)
  77. {
  78. pMap = ArcMap.Document.FocusMap;
  79. GISCommonHelper.CartoLyrHelper.setFeatureLyrCombox(ref cmbLayer, pMap, esriGeometryType.esriGeometryPolygon);
  80. }
  81. private void cmbLayer_SelectedIndexChanged(object sender, EventArgs e)
  82. {
  83. if (cmbLayer.SelectedIndex == -1)
  84. return;
  85. pftlyr = cmbLayer.SelectedValue as IFeatureLayer;
  86. IFields pFields = pftlyr.FeatureClass.Fields;
  87. CartoFieldHelper.setFieldCombox(ref cmbNX, pFields);
  88. CartoFieldHelper.setFieldCombox(ref cmbNY, pFields);
  89. CartoFieldHelper.setFieldCombox(ref cmbSX, pFields);
  90. CartoFieldHelper.setFieldCombox(ref cmbSY, pFields);
  91. CartoFieldHelper.setFieldCombox(ref cmbEX, pFields);
  92. CartoFieldHelper.setFieldCombox(ref cmbEY, pFields);
  93. CartoFieldHelper.setFieldCombox(ref cmbWX, pFields);
  94. CartoFieldHelper.setFieldCombox(ref cmbWY, pFields);
  95. }
  96. private void btnOK_Click(object sender, EventArgs e)
  97. {
  98. if (cmbNX.SelectedIndex == -1 || cmbNY.SelectedIndex==-1 || cmbSX.SelectedIndex==-1 || cmbSY.SelectedIndex==-1||
  99. cmbEX.SelectedIndex==-1 || cmbEY.SelectedIndex==-1 || cmbWX.SelectedIndex==01 || cmbWY.SelectedIndex==-1)
  100. return;
  101. nx = cmbNX.SelectedValue.ToString();
  102. ny = cmbNY.SelectedValue.ToString();
  103. sx = cmbSX.SelectedValue.ToString();
  104. sy = cmbSY.SelectedValue.ToString();
  105. wx = cmbWX.SelectedValue.ToString();
  106. wy = cmbWY.SelectedValue.ToString();
  107. ex = cmbEX.SelectedValue.ToString();
  108. ey = cmbEY.SelectedValue.ToString();
  109. this.DialogResult = DialogResult.OK;
  110. }
  111. private void btnCancel_Click(object sender, EventArgs e)
  112. {
  113. this.DialogResult = DialogResult.Cancel;
  114. }
  115. }
  116. }

tool代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using GISCommonHelper;
  6. using System.Windows.Forms;
  7. using ESRI.ArcGIS.Carto;
  8. using ESRI.ArcGIS.Geometry;
  9. using ESRI.ArcGIS.Geodatabase;
  10. namespace SiZhiCoordinate
  11. {
  12. public class btnSiZhiCmd : ESRI.ArcGIS.Desktop.AddIns.Button
  13. {
  14. public btnSiZhiCmd()
  15. {
  16. }
  17. protected override void OnClick()
  18. {
  19. //
  20. // TODO: Sample code showing how to access button host
  21. //
  22. ArcMap.Application.CurrentTool = null;
  23. try
  24. {
  25. frmSet fs = new frmSet();
  26. if (fs.ShowDialog() == DialogResult.OK)
  27. {
  28. Excute(fs);
  29. MessageBox.Show("处理完成");
  30. }
  31. }
  32. catch (System.Exception ex)
  33. {
  34. MessageBox.Show("发生异常:"+ex.Message);
  35. }
  36. }
  37. protected override void OnUpdate()
  38. {
  39. Enabled = ArcMap.Application != null;
  40. }
  41. private void Excute(frmSet fs)
  42. {
  43. IFeatureClass pftcls = fs.Pftlyr.FeatureClass;
  44. IDataset pds = pftcls as IDataset;
  45. IWorkspaceEdit pwsEdit = pds.Workspace as IWorkspaceEdit;
  46. pwsEdit.StartEditing(true);
  47. pwsEdit.StartEditOperation();
  48. IFeatureCursor pftCursor = pftcls.Update(null, true);
  49. IFeature pFeature = pftCursor.NextFeature();
  50. while (pFeature != null)
  51. {
  52. IPolygon pPolygon = pFeature.ShapeCopy as IPolygon;
  53. NSEW nw = getNSEW(pPolygon);
  54. pFeature.set_Value(pFeature.Fields.FindField(fs.Nx), nw.nx);
  55. pFeature.set_Value(pFeature.Fields.FindField(fs.Ny), nw.ny);
  56. pFeature.set_Value(pFeature.Fields.FindField(fs.Sx), nw.sx);
  57. pFeature.set_Value(pFeature.Fields.FindField(fs.Sy), nw.sy);
  58. pFeature.set_Value(pFeature.Fields.FindField(fs.Wx), nw.wx);
  59. pFeature.set_Value(pFeature.Fields.FindField(fs.Wy), nw.wy);
  60. pFeature.set_Value(pFeature.Fields.FindField(fs.Ex), nw.ex);
  61. pFeature.set_Value(pFeature.Fields.FindField(fs.Ey), nw.ey);
  62. pftCursor.UpdateFeature(pFeature);
  63. pFeature = pftCursor.NextFeature();
  64. }
  65. pwsEdit.StopEditOperation();
  66. pwsEdit.StopEditing(true);
  67. }
  68. private NSEW getNSEW(IPolygon pPolygon)
  69. {
  70. NSEW nw = new NSEW();
  71. IPointCollection pntCol = pPolygon as IPointCollection;
  72. int ni=-1, si=-1, wi=-1, ei=-1;
  73. double nflag=0, sflag=0, wflag=0, eflag=0;
  74. for (int i = 0; i < pntCol.PointCount; i++)
  75. {
  76. double x = pntCol.get_Point(i).X;
  77. double y = pntCol.get_Point(i).Y;
  78. if (i == 0)
  79. {
  80. //获取初值
  81. nflag = y;
  82. sflag = y;
  83. wflag = x;
  84. eflag = x;
  85. ni = i; si = i; wi = i; ei = i;
  86. }
  87. else
  88. {
  89. if (x > eflag)
  90. {
  91. //更东
  92. eflag = x;
  93. ei = i;
  94. }
  95. if (x < wflag)
  96. {
  97. //更西
  98. wflag = x;
  99. wi = i;
  100. }
  101. if (y > nflag)
  102. {
  103. //更北
  104. nflag=y;
  105. ni = i;
  106. }
  107. if (y < sflag)
  108. {
  109. //更南
  110. sflag=y;
  111. si = i;
  112. }
  113. }
  114. }
  115. nw.ex = pntCol.get_Point(ei).X;
  116. nw.ey = pntCol.get_Point(ei).Y;
  117. nw.wx = pntCol.get_Point(wi).X;
  118. nw.wy = pntCol.get_Point(wi).Y;
  119. nw.sx = pntCol.get_Point(si).X;
  120. nw.sy = pntCol.get_Point(si).Y;
  121. nw.nx = pntCol.get_Point(ni).X;
  122. nw.ny = pntCol.get_Point(ni).Y;
  123. return nw;
  124. }
  125. }
  126. class NSEW
  127. {
  128. public double nx, ny, sx, sy, wx, wy, ex, ey;
  129. }
  130. }

程序中用到的其他类

winform字段设置辅助类

  1. public class CartoFieldHelper
  2. {
  3. /// <summary>
  4. /// 匹配模式
  5. /// </summary>
  6. public enum NAmode
  7. {
  8. NameEqual,
  9. AliasNameEqual,
  10. NAEqual,
  11. NameLike,
  12. AliasNameLike,
  13. NALike
  14. }
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. /// <param name="cmb"></param>
  19. /// <param name="pFields"></param>
  20. /// <param name="mode"></param>
  21. /// <param name="kstr"></param>
  22. /// <param name="useBlank"></param>
  23. /// <param name="ets"></param>
  24. public static void setFieldCombox(ref ComboBox cmb, IFields pFields, NAmode mode, string kstr, bool useBlank = false, params esriFieldType[] ets)
  25. {
  26. setFieldCombox(ref cmb, pFields, useBlank, ets);
  27. for (int i = 0; i < cmb.Items.Count; i++)
  28. {
  29. Name_AliasName na = cmb.SelectedItem as Name_AliasName;
  30. if (na == null)
  31. continue;
  32. bool b_get = false;
  33. switch (mode)
  34. {
  35. case NAmode.NALike:
  36. if (na.name.Contains(kstr) || na.alias_name.Contains(kstr))
  37. {
  38. cmb.SelectedIndex = i;
  39. b_get = true;
  40. }
  41. break;
  42. case NAmode.NameLike:
  43. if (na.name.Contains(kstr))
  44. {
  45. cmb.SelectedIndex = i;
  46. b_get = true;
  47. }
  48. break;
  49. case NAmode.AliasNameLike:
  50. if ( na.alias_name.Contains(kstr))
  51. {
  52. cmb.SelectedIndex = i;
  53. b_get = true;
  54. }
  55. break;
  56. case NAmode.NAEqual:
  57. if (na.name==kstr || na.alias_name==kstr)
  58. {
  59. cmb.SelectedIndex = i;
  60. b_get = true;
  61. }
  62. break;
  63. case NAmode.NameEqual:
  64. if (na.name == kstr)
  65. {
  66. cmb.SelectedIndex = i;
  67. b_get = true;
  68. }
  69. break;
  70. case NAmode.AliasNameEqual:
  71. if (na.alias_name==kstr)
  72. {
  73. cmb.SelectedIndex = i;
  74. b_get = true;
  75. }
  76. break;
  77. }
  78. if (b_get)
  79. {
  80. break;//中断循环
  81. }
  82. }
  83. }
  84. public static void setFieldCombox(ref ComboBox cmb,IFields pFields,bool useBlank=false)
  85. {
  86. cmb.DisplayMember = "alias_name";
  87. cmb.ValueMember = "name";
  88. List<Name_AliasName> list = getLyrFields(pFields);
  89. if (useBlank)
  90. {
  91. Name_AliasName na = new Name_AliasName("", "");
  92. list.Insert(0, na);
  93. }
  94. //list.Insert(0, new Name_AliasName("",""));
  95. cmb.DataSource = list;
  96. cmb.SelectedIndex = -1;
  97. cmb.DropDownStyle = ComboBoxStyle.DropDownList;
  98. }
  99. public static void setFieldCombox(ref ComboBox cmb, IFields pFields,bool useBlank=false,params esriFieldType[] ets)
  100. {
  101. cmb.DisplayMember = "alias_name";
  102. cmb.ValueMember = "name";
  103. List<Name_AliasName> list = getLyrFields(pFields,ets);
  104. if(useBlank)
  105. {
  106. Name_AliasName na = new Name_AliasName("", "");
  107. list.Insert(0, na);
  108. }
  109. cmb.DataSource = list;
  110. cmb.SelectedIndex = -1;
  111. cmb.DropDownStyle = ComboBoxStyle.DropDownList;
  112. }
  113. /// <summary>
  114. /// 获取特定字段类型的字段名与假名集合
  115. /// </summary>
  116. /// <param name="pFields"></param>
  117. /// <param name="et">字段类型</param>
  118. /// <returns></returns>
  119. public static List<Name_AliasName> getLyrFields(IFields pFields,params esriFieldType[] ets)
  120. {
  121. List<Name_AliasName> list = new List<Name_AliasName>();
  122. for (int i = 0; i < pFields.FieldCount; i++)
  123. {
  124. IField pfd = pFields.get_Field(i);
  125. if (ets.Length == 0)
  126. {
  127. list.Add(new Name_AliasName(pfd.Name, pfd.AliasName));
  128. }
  129. else
  130. {
  131. if (ets.Contains(pfd.Type))
  132. {
  133. list.Add(new Name_AliasName(pfd.Name, pfd.AliasName));
  134. }
  135. }
  136. }
  137. return list;
  138. }
  139. /// <summary>
  140. /// 获取所有字段名与假名集合
  141. /// </summary>
  142. /// <param name="pFields"></param>
  143. /// <returns></returns>
  144. public static List<Name_AliasName> getLyrFields(IFields pFields)
  145. {
  146. List<Name_AliasName> list = new List<Name_AliasName>();
  147. for (int i = 0; i < pFields.FieldCount; i++)
  148. {
  149. IField pfd = pFields.get_Field(i);
  150. list.Add(new Name_AliasName(pfd.Name, pfd.AliasName));
  151. }
  152. return list;
  153. }
  154. }

winform中图层辅助类

  1. public class CartoLyrHelper
  2. {
  3. #region 设置要素图层Combox
  4. /// <summary>
  5. /// 设置要素图层Combox
  6. /// </summary>
  7. /// <param name="cmb"></param>
  8. /// <param name="pMap"></param>
  9. /// <param name="tp"></param>
  10. /// <param name="nl">默认选中的图层</param>
  11. /// <param name="useBlank"></param>
  12. /// <returns></returns>
  13. public static List<name_Layer> setFeatureLyrCombox(ref ComboBox cmb, IMap pMap, esriGeometryType tp,name_Layer nl,bool useBlank = false)
  14. {
  15. List<name_Layer> lyrlist = setFeatureLyrCombox(ref cmb, pMap, tp, useBlank);
  16. for (int i = 0; i < cmb.Items.Count; i++)
  17. {
  18. name_Layer nltmp = cmb.Items[i] as name_Layer;
  19. if (nltmp.id == nl.id)
  20. {
  21. cmb.SelectedIndex = i;
  22. break;
  23. }
  24. }
  25. return lyrlist;
  26. }
  27. /// <summary>
  28. /// 设置图要素层Combox
  29. /// </summary>
  30. /// <param name="cmb"></param>
  31. /// <param name="pMap"></param>
  32. /// <param name="tp">几何类型</param>
  33. /// <param name="useBlank">使用默认的空白项</param>
  34. /// <returns></returns>
  35. public static List<name_Layer> setFeatureLyrCombox(ref ComboBox cmb, IMap pMap,esriGeometryType tp,bool useBlank=false)
  36. {
  37. cmb.DisplayMember = "name";
  38. cmb.ValueMember = "layer";
  39. List<name_Layer> lyrlist = new List<name_Layer>();
  40. if (useBlank)
  41. {
  42. name_Layer nl = new name_Layer("", null);
  43. lyrlist.Insert(0, nl);
  44. }
  45. getAllFtlyr(ref lyrlist, pMap,tp);
  46. cmb.DataSource = lyrlist;
  47. cmb.DropDownStyle = ComboBoxStyle.DropDownList;
  48. cmb.SelectedIndex = -1;
  49. return lyrlist;
  50. }
  51. #endregion
  52. #region 设置图层Combox
  53. public static List<name_Layer> setLyrCombox<T>(ref ComboBox cmb, IMap pMap,name_Layer nl, bool useBlank)
  54. {
  55. List<name_Layer> nllist = setLyrCombox<T>(ref cmb, pMap, useBlank);
  56. for (int i = 0; i < cmb.Items.Count; i++)
  57. {
  58. name_Layer nlTmp = cmb.Items[i] as name_Layer;
  59. if (nlTmp.id == nl.id)
  60. {
  61. cmb.SelectedIndex = i;
  62. break;
  63. }
  64. }
  65. return nllist;
  66. }
  67. /// <summary>
  68. /// 设置图层Combox
  69. /// </summary>
  70. /// <typeparam name="T"></typeparam>
  71. /// <param name="cmb"></param>
  72. /// <param name="pMap"></param>
  73. /// <returns></returns>
  74. public static List<name_Layer> setLyrCombox<T>(ref ComboBox cmb,IMap pMap,bool useBlank=false)
  75. {
  76. cmb.DisplayMember = "name";
  77. cmb.ValueMember = "layer";
  78. List<name_Layer> lyrlist = new List<name_Layer>();
  79. if (useBlank)
  80. {
  81. name_Layer nl = new name_Layer("", null);
  82. lyrlist.Insert(0, nl);
  83. }
  84. getAllLayer<T>(ref lyrlist, pMap);
  85. cmb.DataSource = lyrlist;
  86. cmb.DropDownStyle = ComboBoxStyle.DropDownList;
  87. cmb.SelectedIndex = -1;
  88. return lyrlist;
  89. }
  90. #endregion
  91. #region 获取所有图层
  92. public static List<T> getAllLayer<T>(IMap pMap)
  93. {
  94. List<name_Layer> nllist = new List<name_Layer>();
  95. getAllLayer<T>(ref nllist, pMap);
  96. List<T> lyrlist = new List<T>();
  97. for (int i = 0; i < nllist.Count; i++)
  98. {
  99. lyrlist.Add((T)nllist[i].layer);
  100. }
  101. return lyrlist;
  102. }
  103. #endregion
  104. #region 获取所有图层集合,适用于Combox
  105. /// <summary>
  106. /// 获取图层集合,适用于combox
  107. /// </summary>
  108. /// <typeparam name="T"></typeparam>
  109. /// <param name="nllist"></param>
  110. /// <param name="pMap"></param>
  111. public static void getAllLayer<T>(ref List<name_Layer> nllist, IMap pMap)
  112. {
  113. for (int i = 0; i < pMap.LayerCount; i++)
  114. {
  115. ILayer plyr = pMap.get_Layer(i);
  116. if (plyr is T)
  117. {
  118. nllist.Add(new name_Layer(plyr.Name, plyr));
  119. }
  120. else if (plyr is IGroupLayer)
  121. {
  122. getGroupLayer<T>(ref nllist, (IGroupLayer)plyr);
  123. }
  124. }
  125. }
  126. #endregion
  127. #region 获取所有要素图层
  128. /// <summary>
  129. /// 获取所有要素图层
  130. /// </summary>
  131. /// <param name="nllist"></param>
  132. /// <param name="pMap"></param>
  133. /// <param name="tp"></param>
  134. public static void getAllFtlyr(ref List<name_Layer> nllist, IMap pMap, ESRI.ArcGIS.Geometry.esriGeometryType tp)
  135. {
  136. for (int i = 0; i < pMap.LayerCount; i++)
  137. {
  138. ILayer plyr = pMap.get_Layer(i);
  139. if (plyr is IFeatureLayer)
  140. {
  141. if (((IFeatureLayer)plyr).FeatureClass != null) //避免图层感叹号时,可能存在的问题
  142. {
  143. if (tp == esriGeometryType.esriGeometryAny)
  144. {
  145. nllist.Add(new name_Layer(plyr.Name, plyr));
  146. }
  147. else
  148. {
  149. if (((IFeatureLayer)plyr).FeatureClass.ShapeType == tp)
  150. nllist.Add(new name_Layer(plyr.Name, plyr));
  151. }
  152. }
  153. }
  154. else if (plyr is IGroupLayer)
  155. {
  156. getGroupLayer(ref nllist, (IGroupLayer)plyr,tp);
  157. }
  158. }
  159. }
  160. #endregion
  161. #region 获取图层组的子图层
  162. /// <summary>
  163. /// 获取图层组的子图层
  164. /// </summary>
  165. /// <typeparam name="T"></typeparam>
  166. /// <param name="nllist"></param>
  167. /// <param name="pGroupLyr"></param>
  168. private static void getGroupLayer<T>(ref List<name_Layer> nllist, IGroupLayer pGroupLyr)
  169. {
  170. ICompositeLayer pCmsLyr = pGroupLyr as ICompositeLayer;
  171. for (int i = 0; i < pCmsLyr.Count; i++)
  172. {
  173. ILayer plyr = pCmsLyr.get_Layer(i);
  174. if (plyr is T)
  175. {
  176. nllist.Add(new name_Layer(plyr.Name, plyr));
  177. }
  178. if (plyr is IGroupLayer)
  179. {
  180. getGroupLayer<T>(ref nllist, (IGroupLayer)plyr);
  181. }
  182. }
  183. }
  184. /// <summary>
  185. /// 获取图层组的子图层,只针对于FeatureLayer
  186. /// </summary>
  187. /// <param name="nllist"></param>
  188. /// <param name="pGroupLyr"></param>
  189. /// <param name="tp"></param>
  190. private static void getGroupLayer(ref List<name_Layer> nllist, IGroupLayer pGroupLyr,esriGeometryType tp)
  191. {
  192. ICompositeLayer pCmsLyr = pGroupLyr as ICompositeLayer;
  193. for (int i = 0; i < pCmsLyr.Count; i++)
  194. {
  195. ILayer plyr = pCmsLyr.get_Layer(i);
  196. if (plyr is IFeatureLayer)
  197. {
  198. if(((IFeatureLayer)plyr).FeatureClass!=null){
  199. if (((IFeatureLayer)plyr).FeatureClass.ShapeType == tp)
  200. nllist.Add(new name_Layer(plyr.Name, plyr));
  201. }
  202. }
  203. if (plyr is IGroupLayer)
  204. {
  205. getGroupLayer(ref nllist, (IGroupLayer)plyr,tp);
  206. }
  207. }
  208. }
  209. #endregion
  210. #region 图层更改数据源
  211. /// <summary>
  212. /// 根据图层中的数据源的数据集名称设置数据源
  213. /// </summary>
  214. /// <typeparam name="T"></typeparam>
  215. /// <param name="pLyr"></param>
  216. /// <param name="pWs"></param>
  217. public static void setDataSourceByDsname<T>(ILayer pLyr, IWorkspace pWs,string pnm="")
  218. {
  219. string dsName;
  220. if (string.IsNullOrEmpty(pnm))
  221. {
  222. dsName = (((IDataLayer)pLyr).DataSourceName as IDatasetName).Name;
  223. }
  224. else
  225. {
  226. dsName = pnm;
  227. }
  228. try
  229. {
  230. if (typeof(T) == typeof(IFeatureLayer))
  231. {
  232. IFeatureWorkspace pfws = pWs as IFeatureWorkspace;
  233. IFeatureLayer pftLyr = pLyr as IFeatureLayer;
  234. pftLyr.FeatureClass = pfws.OpenFeatureClass(dsName);
  235. }
  236. else if (typeof(T) == typeof(IRasterLayer))
  237. {
  238. IRasterWorkspace2 prstWorkspace = pWs as IRasterWorkspace2;
  239. IRasterLayer prstLyr = pLyr as IRasterLayer;
  240. IRasterDataset rasterDataset = prstWorkspace.OpenRasterDataset(dsName);
  241. prstLyr.CreateFromDataset(rasterDataset);
  242. }
  243. }
  244. catch (System.Exception ex)
  245. {
  246. //可能会有未知的名称问题导致图层名称更改而无法获取名称的问题
  247. }
  248. }
  249. public static void setDataSourceByDsnameTwice<T>(ILayer pLyr, IWorkspace pWs)
  250. {
  251. IWorkspace2 pws2 = pWs as IWorkspace2;
  252. string dsName;
  253. dsName = (((IDataLayer)pLyr).DataSourceName as IDatasetName).Name;
  254. esriDatasetType edt;
  255. if (typeof(T) == typeof(IFeatureLayer))
  256. {
  257. edt = esriDatasetType.esriDTFeatureClass;
  258. }
  259. else if (typeof(T) == typeof(IRasterLayer))
  260. {
  261. edt = esriDatasetType.esriDTRasterDataset;
  262. }
  263. else
  264. edt = esriDatasetType.esriDTAny;
  265. if(pws2.get_NameExists(edt,dsName))
  266. {
  267. setDataSourceByDsname<T>(pLyr, pWs);
  268. }
  269. else
  270. {
  271. dsName = "_" + dsName;
  272. if (pws2.get_NameExists(edt, dsName))
  273. setDataSourceByDsname<T>(pLyr, pWs,dsName);
  274. else
  275. {
  276. //不存在,不处理
  277. }
  278. }
  279. }
  280. /// <summary>
  281. /// 根据图层的名称设置数据源
  282. /// </summary>
  283. /// <typeparam name="T"></typeparam>
  284. /// <param name="pLyr"></param>
  285. /// <param name="pWs"></param>
  286. public static void setDataSourceByLayerName<T>(ILayer pLyr, IWorkspace pWs)
  287. {
  288. string dsName = pLyr.Name;
  289. if (typeof(T) == typeof(IFeatureLayer))
  290. {
  291. IFeatureWorkspace pfws = pWs as IFeatureWorkspace;
  292. IFeatureLayer pftLyr = pLyr as IFeatureLayer;
  293. pftLyr.FeatureClass = pfws.OpenFeatureClass(dsName);
  294. }
  295. else if (typeof(T) == typeof(IRasterLayer))
  296. {
  297. if (dsName.Contains("."))
  298. dsName = dsName.Substring(0, dsName.LastIndexOf("."));
  299. IRasterWorkspace2 prstWorkspace = pWs as IRasterWorkspace2;
  300. IRasterLayer prstLyr = pLyr as IRasterLayer;
  301. IRasterDataset rasterDataset = prstWorkspace.OpenRasterDataset(dsName);
  302. prstLyr.CreateFromDataset(rasterDataset);
  303. }
  304. }
  305. #endregion
  306. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/951521
推荐阅读
相关标签
  

闽ICP备14008679号