上一遍文章介绍了shp文件的的加载,读取等的基本操作,现在介绍一下关于对于shp文件的操作。
1.获取Shp中的某一字段的值
假设shp文件中有一个字段名字叫做name,首先对shp文件里的要素进行遍历,shp文件要素数量为shp.NumShapes,先来看看函数原型get_CellValue(int FieldIndex,int ShapeIndex),第一个参数是shp中某一个字段名的索引,第二个为shp文件中要素的索引。如果已经知道了name字段的索引则可以直接调用该函数,如果不知道name字段的索引,那么我们需要先计算出name字段的索引,通过get_FieldIndexByName(string FiledName)传入字段的名字可以计算出字段的索引。如果需要遍历name字段下的所有值,代码如下
for (int i = 0; i < shp.NumShapes; i++)
{
string Name = shp.get_CellValue(shp.get_FieldIndexByName("name"), i).ToString();
shp.set_ShapeSelected(i, true);
}
上面的代码就是遍历了shp中所有字段为name的值,并且选中该要素。
2.选中与定位shp要素
获取了shp中某一字段的值后,就可以通过这个值找到shp中代表改要素的索引进行选择。void Shapefile.set_ShapeSelected(int ShapeIndex,bool pVal),选择指定索引的要素,可以通过上面的查找shp的字段,通过外部比较找到需要的要素,比如说:
string SearchName=“XXX";
for (int i = 0; i < shp.NumShapes; i++)
{
string Name = shp.get_CellValue(shp.get_FieldIndexByName("name"), i).ToString();
if(SearchName==Name)
{
shp.set_ShapeSelected(i, true);
break;
}
}
查找某一字段为XXX的要素,可以通过循环实现。
3.清空选择的shp要素
清空选择的要素很简单:
shp.SelectNone();
这个是清空shp中选中的要素,因此使用的是shp,不是axMap控件,
4.设置shp要素的显示属性
有时shp要素的属性需要修改一下,比如更改宽度,颜色等等属性5.选择选中要素的状态
比如设置线宽与线的颜色
shp.SelectionAppearance = tkSelectionAppearance.saDrawingOptions; //一定需要有这句话,后面的代码才能生效
shp.SelectionDrawingOptions.LineWidth= 4.0f;
shp.SelectionDrawingOptions.LineColor = (uint)ColorTranslator.ToOle(Color.FromArgb(0, 255, 255));
应为传入的颜色是一个值,而颜色是由
RGB来进行表现的可以通过方法来进行转换,或者在加载的时候就设置shp的状态
sfProvince.Open(SHPPath);
hndProvince = axMap1.AddLayer(sfProvince, true);
axMap1.set_ShapeLayerDrawFill(hndProvince, true);
uint linecolor = (uint)ColorTranslator.ToOle(Color.Black);
float linewidth = 3.0F;
axMap1.set_ShapeLayerLineColor(hndProvince, linecolor);
axMap1.set_ShapeLayerLineWidth(hndProvince, linewidth);
tkLineStipple line = tkLineStipple.lsDashDotDash;
axMap1.set_ShapeLayerLineStipple(hndProvince, line);