`
devgis
  • 浏览: 134126 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

ArcGIS Engine -- 常用方法

 
阅读更多

空间关系

计算两点间距离
复制代码
1         /// <summary>计算两点间距离
2 /// </summary>
3 /// <param name="point1"></param>
4 /// <param name="point2"></param>
5 /// <returns></returns>
6 public static double getDistanceOfTwoPoints(ESRI.ArcGIS.Geometry.IPoint point1, ESRI.ArcGIS.Geometry.IPoint point2)
7 {
8 return Math.Sqrt((point1.X - point2.X) * (point1.X - point2.X) + (point1.Y - point2.Y) * (point1.Y - point2.Y));
9 }
复制代码
feature平移
1 IGeometry geo=feature.Shape;
2 ((ITransform2D)geo).Move(20,20);


计算范围

得到点集合的n倍Envelope范围
复制代码
 1         /// <summary>得到点集合的n倍Envelope范围
2 /// </summary>
3 /// <param name="points"></param>
4 /// <param name="zoomInNumber"></param>
5 /// <returns></returns>
6 public static IEnvelope getBigEnvelope(IPointCollection points, double zoomInNumber)
7 {
8 IEnvelope result = new EnvelopeClass();
9
10 double xmax = 0, xmin = 999999999999, ymax = 0, ymin = 999999999999;
11
12 for (int i = 0; i < points.PointCount; i++)
13 {
14 ESRI.ArcGIS.Geometry.IPoint p = points.get_Point(i);
15 if (xmax < p.X) xmax = p.X;
16 if (ymax < p.Y) ymax = p.Y;
17 if (xmin > p.X) xmin = p.X;
18 if (ymin > p.Y) ymin = p.Y;
19 }
20 result.XMax = xmax + xmax - xmin;
21 result.XMin = xmin - xmax + xmin;
22 result.YMax = ymax + ymax - ymin;
23 result.YMin = ymin - ymax + ymin;
24
25 return result;
26 }
复制代码

查询

查询要素,返回多个要素union后的Geometry
复制代码
 1         /// <summary>查询要素
2 /// 多个要素取其union后的范围
3 /// </summary>
4 /// <param name="map">地图</param>
5 /// <param name="layer">矢量图层</param>
6 /// <param name="where">查询条件</param>
7 /// <returns></returns>
8 public static IGeometry queryUnionGeometry(IMap map, IFeatureLayer layer, string where)
9 {
10 if (layer == null) return null;
11
12 IFeatureClass featCls = layer.FeatureClass;
13
14 IQueryFilter filter = new QueryFilterClass();
15 filter.WhereClause = where;
16 IFeatureCursor cursor = featCls.Search(filter, false);
17
18 IFeature feature = cursor.NextFeature();
19 if (feature == null) return null;
20
21 Boolean isFirstFeature = true;
22 IGeometry union = null;
23 while (feature != null)
24 {
25 if (isFirstFeature)
26 {
27 isFirstFeature = false;
28 union = feature.Shape;
29 }
30 else
31 {
32 union=((ITopologicalOperator)union).Union(feature.Shape);
33 }
34 feature = cursor.NextFeature();
35 }
36
37 return union; ;
38 }
复制代码

查找图层

得到地图上图层列表
复制代码
 1         /// <summary>得到地图上图层列表
2 /// </summary>
3 /// <param name="map"></param>
4 /// <returns></returns>
5 public static IEnumLayer getFeatureLayers(IMap map)
6 {
7 UID uid = new UIDClass();
8
9 //{6CA416B1-E160-11D2-9F4E-00C04F6BC78E} IDataLayer (all)
10 //{40A9E885-5533-11d0-98BE-00805F7CED21} IFeatureLayer
11 //{E156D7E5-22AF-11D3-9F99-00C04F6BC78E} IGeoFeatureLayer
12 //{34B2EF81-F4AC-11D1-A245-080009B6F22B} IGraphicsLayer
13 //{5CEAE408-4C0A-437F-9DB3-054D83919850} IFDOGraphicsLayer
14 //{0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E} ICoverageAnnotationLayer
15 //{EDAD6644-1810-11D1-86AE-0000F8751720} IGroupLayer
16 uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";//FeatureLayer
17 IEnumLayer layers = map.get_Layers(uid, true);
18 return layers;
19 }
复制代码
根据名称在地图上查找对应矢量图层
复制代码
 1         /// <summary>根据名称在地图上查找对应矢量图层
2 /// </summary>
3 /// <param name="map"></param>
4 /// <param name="layerName"></param>
5 /// <returns></returns>
6 public static IFeatureLayer getFeatureLayer(IMap map,string layerName)
7 {
8 IEnumLayer layers = getFeatureLayers(map);
9 layers.Reset();
10 ILayer layer = null;
11 while ((layer = layers.Next()) != null)
12 {
13 if (layer.Name == layerName)
14 return layer as IFeatureLayer;
15 }
16 return null;
17 }
复制代码

选择要素集

得到指定图层上的选中的要素集
复制代码
 1         /// <summary>得到指定图层上的选中的features
2 /// </summary>
3 /// <param name="map">地图</param>
4 /// <param name="layerName">图层名</param>
5 /// <returns>IFeatureCursor</returns>
6 public static IFeatureCursor getSelectFeatures(IMap map, string layerName)
7 {
8 IFeatureLayer featureLayer = Util.getFeatureLayer(map, layerName); ;
9
10 //得到选中的feature
11 IFeatureClass inputFeatureClass = featureLayer.FeatureClass;
12 IDataset inputDataset = (IDataset)inputFeatureClass;
13 IDatasetName inputDatasetName = (IDatasetName)inputDataset.FullName;
14 IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
15 ISelectionSet selectionSet = featureSelection.SelectionSet;
16
17 ICursor cursor;
18 selectionSet.Search(null, false, out cursor);
19 IFeatureCursor featureCursor = (IFeatureCursor)cursor;
20
21 return featureCursor;
22 }
复制代码

加载数据

将GDB中数据添加至地图
复制代码
 1         /// <summary>将GDB中数据添加至地图
2 /// </summary>
3 /// <param name="map">地图IMap</param>
4 /// <param name="gdbPath">GDB路径</param>
5 /// <param name="name">数据名称</param>
6 public static void addFeatLayerToMapFromGDB(IMap map,string gdbPath,string name)
7 {
8 IWorkspaceFactory toWsf = new FileGDBWorkspaceFactoryClass();
9 IFeatureWorkspace toFeatWs = (IFeatureWorkspace)toWsf.OpenFromFile(gdbPath, 0);
10 IFeatureClass featCls = toFeatWs.OpenFeatureClass(name);
11 ILayer layer=new FeatureLayerClass();
12 layer.Name = name;
13 ((IGeoFeatureLayer)layer).FeatureClass=featCls;
14 if (featCls != null) {
15 map.AddLayer(layer);
16 }
17 }
复制代码
加载dwg中的polygon图层

新建shapefile文件/GDB

创建点shapefile
复制代码
 1         /// <summary>创建点shapefile
2 /// </summary>
3 /// <param name="filePath">target point shapefile path</param>
4 /// <param name="fileName">target point shapefile name</param>
5 public static void createPointShapefile(IMap map, string filePath, string fileName)
6 {
7 //建立shape字段
8 IFields pFields = new FieldsClass();
9 IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
10 IField pField = new FieldClass();
11 IFieldEdit pFieldEdit = pField as IFieldEdit;
12 pFieldEdit.Name_2 = "Shape";
13 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
14
15 //设置geometry definition
16 IGeometryDef pGeometryDef = new GeometryDefClass();
17 IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
18 pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;//点、线、面
19 pGeometryDefEdit.SpatialReference_2 = map.SpatialReference;
20 pFieldEdit.GeometryDef_2 = pGeometryDef;
21 pFieldsEdit.AddField(pField);
22
23 //新建字段
24 pField = new FieldClass();
25 pFieldEdit = pField as IFieldEdit;
26 pFieldEdit.Length_2 = 10;
27 pFieldEdit.Name_2 = "id";
28 pFieldEdit.AliasName_2 = "id";
29 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger;
30 pFieldsEdit.AddField(pField);
31 //继续增加其它字段
32
33 IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
34 IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace;
35
36 //IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
37 //IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace;
38
39 int i = fileName.IndexOf(".shp");
40 if (i == -1)
41 pFeatureWorkspace.CreateFeatureClass(fileName + ".shp", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
42 else
43 pFeatureWorkspace.CreateFeatureClass(fileName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
44
45 //MessageBox.Show("OK");
46
47 }
复制代码

将选中要素另存至GDB

  另存为shapefile类似,修改workspacefactory为ShapefileWorkspaceFactoryClass,修改对应路径即可。

将选中的line feature作为线文件存至gdb
复制代码
 1 /// <summary>将选中的line feature作为线文件存至gdb
2 /// </summary>
3 /// <param name="featureLayer">select layer</param>
4 /// <param name="gdbPath">gdb路径</param>
5 /// <param name="name">文件名称</param>
6 public static void saveSelectLineFeatureToGDB(IMap map,IFeatureLayer featureLayer,string gdbPath,string name)
7 {
8 IFeatureClass inputFeatureClass = featureLayer.FeatureClass;
9 IDataset inputDataset = (IDataset)inputFeatureClass;
10 IDatasetName inputDatasetName = (IDatasetName)inputDataset.FullName;
11
12 // Get the layer's selection set.
13 IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
14 ISelectionSet selectionSet = featureSelection.SelectionSet;
15
16 IPropertySet ps = new PropertySetClass();
17 ps.SetProperty("DATABASE", gdbPath);
18
19 IWorkspaceFactory wsf = new FileGDBWorkspaceFactoryClass();
20 IWorkspace ws = null;
21 try
22 {
23 ws = wsf.Open(ps, 0);
24 }
25 catch (Exception e)
26 {
27 Console.WriteLine(e.Message);
28 }
29 IDataset ds = (IDataset)ws;
30 IWorkspaceName wsName = (IWorkspaceName)ds.FullName;
31 IFeatureClassName featClsName = new FeatureClassNameClass();
32 IDatasetName dsName = (IDatasetName)featClsName;
33 dsName.WorkspaceName = wsName;
34 dsName.Name = name;
35
36 //// Use the IFieldChecker interface to make sure all of the field names are valid for a shapefile.
37 IFieldChecker fieldChecker = new FieldCheckerClass();
38 IFields shapefileFields = null;
39 IEnumFieldError enumFieldError = null;
40 fieldChecker.InputWorkspace = inputDataset.Workspace;
41 fieldChecker.ValidateWorkspace = ws;
42 fieldChecker.Validate(inputFeatureClass.Fields, out enumFieldError, out shapefileFields);
43
44 // At this point, reporting/inspecting invalid fields would be useful, but for this example it's omitted.
45
46 // We also need to retrieve the GeometryDef from the input feature class.
47 int shapeFieldPosition = inputFeatureClass.FindField(inputFeatureClass.ShapeFieldName);
48 IFields inputFields = inputFeatureClass.Fields;
49
50 IField shapeField = inputFields.get_Field(shapeFieldPosition);
51 IGeometryDef geometryDef = shapeField.GeometryDef;
52
53 IGeometryDef pGeometryDef = new GeometryDef();
54 IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
55 pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
56 pGeometryDefEdit.SpatialReference_2 = map.SpatialReference;
57
58 // Now we can create a feature data converter.
59 IFeatureDataConverter2 featureDataConverter2 = new FeatureDataConverterClass();
60 IEnumInvalidObject enumInvalidObject = featureDataConverter2.ConvertFeatureClass(inputDatasetName, null,
61 selectionSet, null, featClsName, pGeometryDef, shapefileFields, "", 1000, 0);
62
63 // Again, checking for invalid objects would be useful at this point...
64
65 inputFeatureClass = null;
66 ds = null;
67 ws = null;
68 wsf = null;
69 }
复制代码

编辑

编辑--删除
复制代码
 1             //加载目标点图层
2 //IWorkspaceFactory toWsf = new ShapefileWorkspaceFactoryClass();
3 IWorkspaceFactory toWsf = new FileGDBWorkspaceFactoryClass();
4 IFeatureWorkspace toFeatWs = (IFeatureWorkspace)toWsf.OpenFromFile(fullGdbPath, 0); ;
5 IFeatureClass toFeatCls = toFeatWs.OpenFeatureClass(pointLayerName);
6
7 //编辑目标点图层
8 IWorkspaceEdit toEdit = (IWorkspaceEdit)toFeatWs;
9 try
10 {
11 toEdit.StartEditing(true);
12 toEdit.StartEditOperation();
13
14 IQueryFilter filter=new QueryFilterClass();
15 filter.WhereClause="id=0";
16 IFeatureCursor cursor = toFeatCls.Search(filter, false);
17
18 IFeature feature = cursor.NextFeature();
19
20 while(feature != null)
21 {
22 feature.Delete();
23 feature = cursor.NextFeature();
24 }
25
26 }
27 catch (Exception e)
28 {
29 Console.WriteLine(e.Message);
30 }
31 finally
32 {
33 toEdit.StopEditOperation();
34 toEdit.StopEditing(true);
35 }
复制代码
编辑--新增
1 IFeature toFeature = toFeatCls.CreateFeature();
2 ESRI.ArcGIS.Geometry.IPoint newPoint = new PointClass();
3 newPoint.PutCoords(fromPoint.X, fromPoint.Y);
4 toFeature.Shape = newPoint;
5 toFeature.set_Value(2, nowPointId);
6 toFeature.Store();

label

显示label
复制代码
 1         /// <summary>显示label
2 /// </summary>
3 /// <param name="map"></param>
4 /// <param name="featLayer"></param>
5 /// <param name="field"></param>
6 public static void displayLabel(IMap map, IGeoFeatureLayer featLayer, string field)
7 {
8 featLayer.DisplayAnnotation = true;
9 IAnnotateLayerPropertiesCollection pAnnProCol = featLayer.AnnotationProperties;
10 ILabelEngineLayerProperties pLabelEngine = null;
11 IAnnotateLayerProperties prop;
12 IBasicOverposterLayerProperties pBasicOverposterLayerProps = new BasicOverposterLayerProperties();
13 pBasicOverposterLayerProps.NumLabelsOption = esriBasicNumLabelsOption.esriOneLabelPerShape;
14 ITextSymbol symbol = new TextSymbolClass();
15 IColor c = new RgbColorClass();
16 c.RGB = 123;
17 symbol.Color = c;
18 symbol.Size = 4;
19
20 IBasicOverposterLayerProperties bo = new BasicOverposterLayerPropertiesClass();
21 IPointPlacementPriorities ipp = new PointPlacementPrioritiesClass();
22 ipp.BelowCenter = 1;
23 bo.PointPlacementPriorities = ipp;
24 for (int i = 0; i < pAnnProCol.Count; i++)
25 {
26 IElementCollection ec = new ElementCollectionClass();
27 pAnnProCol.QueryItem(i, out prop, out ec, out ec);
28 pLabelEngine = (ILabelEngineLayerProperties)prop;
29 pLabelEngine.Expression = "[" + field + "]";
30 pLabelEngine.Symbol = symbol;
31 pLabelEngine.BasicOverposterLayerProperties = bo;
32 }
33
34 ITrackCancel pCon = new CancelTracker();
35 pCon.Continue();
36 featLayer.Draw(esriDrawPhase.esriDPAnnotation, ((IActiveView)map).ScreenDisplay, pCon);
37 }
复制代码
label 转换为 Annotation
复制代码
 1         /// <summary>label2Annotation
2 /// </summary>
3 /// <param name="fullGdbPath"></param>
4 /// <param name="featLayer"></param>
5 /// <param name="displayField"></param>
6 private void ConvertLabelsToGDBAnnotationSingleLayer(string fullGdbPath,IFeatureLayer featLayer,string displayField)
7 {
8 IMap map = m_map;
9
10 IPropertySet ps = new PropertySetClass();
11 ps.SetProperty("DATABASE", fullGdbPath);
12
13 IWorkspaceFactory wsf = new FileGDBWorkspaceFactoryClass();
14 IWorkspace ws = wsf.Open(ps, 0);
15 IFeatureWorkspace featWs = (IFeatureWorkspace)ws;
16 IGeoFeatureLayer pGeoFeatureLayer = featLayer as IGeoFeatureLayer;
17 IFeatureClass featCls = pGeoFeatureLayer.FeatureClass;
18
19 //设置指定字段为label字段并显示
20 Util.displayLabel(map, pGeoFeatureLayer, displayField);
21 ((IActiveView)map).PartialRefresh(esriViewDrawPhase.esriViewForeground, null, null);
22
23 IConvertLabelsToAnnotation pConvertLabelsToAnnotation = new
24 ConvertLabelsToAnnotationClass();
25 ITrackCancel pTrackCancel = new CancelTrackerClass();
26
27 //Change global level options for the conversion by sending in different parameters to the next line.
28 pConvertLabelsToAnnotation.Initialize(map,
29 esriAnnotationStorageType.esriDatabaseAnnotation,
30 esriLabelWhichFeatures.esriAllFeatures, true, pTrackCancel, null);
31
32 if (featCls != null)
33 {
34 IDataset pDataset = featCls as IDataset;
35
36 //Add the layer information to the converter object. Specify the parameters of the output annotation feature class here as well.
37 pConvertLabelsToAnnotation.AddFeatureLayer(pGeoFeatureLayer,
38 pGeoFeatureLayer.Name + "_anno", featWs,
39 featCls.FeatureDataset, true, false, false, true, true,
40 "");
41
42 //Do the conversion.
43 pConvertLabelsToAnnotation.ConvertLabels();
44 IEnumLayer pEnumLayer = pConvertLabelsToAnnotation.AnnoLayers;
45
46 //Turn off labeling for the layer converted.
47 pGeoFeatureLayer.DisplayAnnotation = false;
48
49 //Add the result annotation layer to the map.
50 map.AddLayers(pEnumLayer, true);
51
52 //Refresh the map to update the display.
53 IActiveView pActiveView = map as IActiveView;
54 pActiveView.Refresh();
55 }
56 }
复制代码

GP

转换cad时,从shp转换cad不成功,从gdb中转换成功,原因不知。

GP:createCadXData需要在转换cad之前执行,给cad添加扩展属性

GP:shp2cad
复制代码
 1         /// <summary>GP:shp2cad
2 /// </summary>
3 /// <param name="fromFile"></param>
4 /// <param name="toFile"></param>
5 public static void convertGdb2Cad(string fromFile, string toFile)
6 {
7 Geoprocessor GP = new Geoprocessor();
8
9 ESRI.ArcGIS.ConversionTools.ExportCAD tool = new
10 ESRI.ArcGIS.ConversionTools.ExportCAD();
11 //tool.in_features = "d:/lzx/data/road/test.gdb/line_Annod";
12 //tool.Output_File = "d:/lzx/data/kk2.dwg";
13 tool.in_features = fromFile;
14 tool.Output_File = toFile;
15 tool.Output_Type = "DWG_R2004";
16 GP.Execute(tool, null);
17
18 //MessageBox.Show("shp2cad ok");
19 }
复制代码
GP:shp export to gdb
复制代码
 1         /// <summary>GP:shp export to gdb
2 /// </summary>
3 /// <param name="fromFile"></param>
4 /// <param name="toFile"></param>
5 public static void convertShp2Gdb(string inputFeatures, string outputGdb, string outputName)
6 {
7
8 Geoprocessor GP = new Geoprocessor();
9
10 ESRI.ArcGIS.ConversionTools.FeatureClassToGeodatabase tool = new ESRI.ArcGIS.ConversionTools.FeatureClassToGeodatabase();
11 tool.Input_Features = inputFeatures;
12 tool.Output_Geodatabase = outputGdb;
13
14 GP.Execute(tool, null);
15
16 //MessageBox.Show("shp2gdb ok");
17
18 }
复制代码
GP:将线文件的节点转换为点文件
复制代码
 1         /// <summary>将线文件的节点转换为点文件
2 /// 使用gp
3 /// </summary>
4 /// <param name="fromFile"></param>
5 /// <param name="toFile"></param>
6 public static void convertFeatureVerticesToPoints(string fromFile, string toFile)
7 {
8 // Initialize the geoprocessor.
9 Geoprocessor GP = new Geoprocessor();
10
11 ESRI.ArcGIS.DataManagementTools.FeatureVerticesToPoints tool = new
12 ESRI.ArcGIS.DataManagementTools.FeatureVerticesToPoints();
13
14 tool.in_features = fromFile;
15 tool.out_feature_class = toFile;
16 tool.point_location = "BOTH_ENDS";
17 GP.Execute(tool, null);
18 }
复制代码
GP:createCadXData
复制代码
 1         /// <summary>GP:createCadXData
2 /// </summary>
3 /// <param name="fromFile"></param>
4 /// <param name="toFile"></param>
5 public static void createCadXData(string fromFile,string field)
6 {
7 Geoprocessor GP = new Geoprocessor();
8 CreateCADXData tool = new CreateCADXData(fromFile, field, "ArcGIS", "ADE");
9 GP.Execute(tool, null);
10 //MessageBox.Show(" ok");
11 }
复制代码

地图显示/刷新

清除选择集并刷新
复制代码
 1         /// <summary>清除选择集并刷新
2 /// </summary>
3 /// <param name="map"></param>
4 public static void clearSelectionInMap(IMap map)
5 {
6 IActiveView activeView = (IActiveView)map;
7 activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
8 map.ClearSelection();
9 //需要在前后刷新2次
10 activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
11 }
复制代码
删除地图中除指定图层组外的其他图层
复制代码
 1         /// <summary>删除地图中除指定图层组外的其他图层
2 /// </summary>
3 /// <param name="map"></param>
4 /// <param name="layerNames">不删除的图层列表</param>
5 public static void clearLayers(IMap map, string[] layerNames)
6 {
7 Boolean isToDel = true;
8 IEnumLayer layers = Util.getFeatureLayers(map);
9 layers.Reset();
10 ILayer layer = null;
11 while ((layer = layers.Next()) != null)
12 {
13 for (int i = 0; i < layerNames.Length; i++)
14 {
15 if (layer.Name == layerNames[i])
16 {
17 isToDel = false;
18 }
19 }
20 if (isToDel)
21 {
22 map.DeleteLayer(layer);
23 System.Runtime.InteropServices.Marshal.ReleaseComObject(layer);
24 }
25 }
26 }
复制代码
删除地图中除指定图层外的其他图层
复制代码
 1         /// <summary>删除地图中除指定图层外的其他图层
2 /// </summary>
3 /// <param name="map"></param>
4 /// <param name="layerNames">不删除的图层列表</param>
5 public static void clearLayers(IMap map, string layerName)
6 {
7 Boolean isToDel = true;
8 IEnumLayer layers = Util.getFeatureLayers(map);
9 layers.Reset();
10 ILayer layer = null;
11 while ((layer = layers.Next()) != null)
12 {
13 if (layer.Name != layerName)
14 {
15 map.DeleteLayer(layer);
16 System.Runtime.InteropServices.Marshal.ReleaseComObject(layer);
17 }
18 }
19 }
复制代码
缩放到指定要素并高亮显示
复制代码
 1         /// <summary>缩放到指定要素并高亮显示
2 /// </summary>
3 /// <param name="av"></param>
4 /// <param name="env"></param>
5 public static void zoomAndHighlight(IActiveView av, IGeometry geo)
6 {
7 av.GraphicsContainer.DeleteAllElements();
8 av.Extent = geo.Envelope;
9 IElement element = new LineElementClass();
10 element.Geometry = geo;
11
12 IRgbColor color = new RgbColorClass();
13 color.Red = 255;
14 color.Green = 0;
15 color.Blue = 0;
16 ISimpleLineSymbol symbol = new SimpleLineSymbolClass();
17 symbol.Color = (IColor)color;
18 symbol.Width = 2;
19 symbol.Style = esriSimpleLineStyle.esriSLSSolid;
20 ((ILineElement)element).Symbol = (ILineSymbol)symbol;
21
22 av.GraphicsContainer.AddElement(element, 0);
23 av.Refresh();
24 }
复制代码

Element

根据RGB值得到IColor
复制代码
1 public static IColor getColor(int R, int G, int B){
2 IRgbColor color=new RgbColorClass();
3 color.Red=R;
4 color.Green=G;
5 color.Blue=B;
6 return (IColor)color;
7 }
复制代码
根据多边形得到填充element
复制代码
 1 public static IElement getFillShapeElement(IGeometry geo){
2 IElement element=new PolygonElementClass();
3 element.Geometry=geo;
4
5 ISimpleLineSymbol symbol=new SimpleLineSymbolClass();
6 symbol.Color=getColor(255,0,0);
7 symbol.Width=2;
8 symbol.Style=esriSimpleLneStyle.esriSLSolid;
9
10 ISimpleFillSymbol fillSymbol=new SimpleFillSymbolClass();
11 fillSymbol.outline=(ILineSymbol)symbol;
12 fillSymbol.Color=getColor(255,250,200);
13 ((IFillShapeElement)element).symbol=(IFillSymbol)fillSymbol;
14
15 return element;
16 }
复制代码

ArcGis Engine删除影像文件

  当利用AE生成tiff、img等格式的影像文件时,需要检查是否存在同名文件,如果存在则替换原有文件。这里如果直接用System .IO .File .Delete (string path)方法删除原有文件会报错,这是由于该方法并没有完全清除影像文件附带的文件(如.aux,.rrd等),导致生成新文件时发生冲突。

后来发现其实AE提供了自己的文件删除方法,就是先打开数据集,再调用数据集的Delete方法即可,代码如下:

IWorkspaceFactory pWSF = new RasterWorkspaceFactoryClass();
IRasterWorkspace pRWS = pWSF.OpenFromFile(pathName,0) as IRasterWorkspace;//pathName为路径名
IDataset pDataset = pRWS.OpenRasterDataset(fileName) as IDataset;//fileName为文件名(不包含路径)
pDataset.Delete();

  对于矢量的shp文件,并没有进行尝试,以前是使用System .IO .File .Delete (string path)方法将shp格式数据包含的六个文件全部删除,现在想来应该也可以使用这种方式。

ArcGIS Engine属性查询并加亮显示(查询结果在其他窗体中显示)

2012-05-07 11:09 by beileierhao, 111 阅读,0评论,收藏,编辑

查询分为属性查询和空间查询。我现在做的是属性查询,用IQueryFilter

属性查询的基本步骤是:

1)获取要查询的图层

2)获取要素要素图层

3)获取要素集

4)用IQueryFilter进行过滤筛选

Dim pFeatureSelection As IFeatureSelection = CType(pFeatureLayer, IFeatureSelection)
Dim pQueryFilter As IQueryFilter = New QueryFilterClass()
pQueryFilter.WhereClause = (TypeComboBox.Text + TextBox1.Text)
pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)

5)要将筛选出的要素集显示在数据表中,用ICursor和IFeatureCursor对每个要素进行遍历

在这里谈一下个人对ICursor和IFeatureCursor的理解。ICursor主要是针对所有的类型的行进行的操作,而IFeatureCursor则主要是针对地图的要素进行的操作。ICursor是IFeatureCursor的父类。

复制代码
Dim pCursor As ICursor = Nothing
pSelectionSet.Search(Nothing, True, pCursor) '找所有满足查询过滤器的行,返回一个ICursor。这是Nothing,所有返回了所有行。
Dim pFeatureCursor As IFeatureCursor = CType(pCursor, IFeatureCursor)
Dim pFeature As IFeature
pFeature = pFeatureCursor.NextFeature
复制代码

6)将各元素依次赋值给datatable,以datatable为数据源传给datagridview

复制代码
If pSelectionSet IsNot Nothing Then
            Dim dt As New DataTable
            Dim dc As DataColumn = Nothing
            For i = 1 To pFeatureLayer.FeatureClass.Fields.FieldCount
                dc = New DataColumn(pFeatureLayer.FeatureClass.Fields.Field(i - 1).Name)
                dt.Columns.Add(dc)
            Next

                Dim dr As DataRow
            While (pFeature IsNot Nothing)
                Dim pGeometry As IGeometry
                pGeometry = pFeature.Shape
                Dim obj As Object = Nothing
                My.Forms.MainForm.AxMapControl1.FlashShape(pGeometry, 3, 400, obj)
                dr = dt.NewRow
                Dim j As Integer
                For j = 0 To pFeature.Fields.FieldCount - 1 Step 1
                    If pFeatureLayer.FeatureClass.FindField(pFeatureLayer.FeatureClass.ShapeFieldName) = j Then
                        dr(j) = pFeatureLayer.FeatureClass.ShapeType.ToString()
                    Else
                        dr(j) = pFeature.Value(j).ToString()
                    End If
                Next
                dt.Rows.Add(dr)
                pFeature = pFeatureCursor.NextFeature()
            End While
            DataGridView1.DataSource = dt
        End If
复制代码

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics