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

ArcGIS Engine常用开发代码整理

 
阅读更多

ArcGIS Engine常用开发代码整理(1)

1. 创建工作空间工厂——EDN

View Code
复制代码
public void IWorkspaceFactory_Create_Example_Access()    {
// create a new Access workspace factory
IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass();
// Create a workspacename with the workspace factory
IWorkspaceName workspaceName = workspaceFactory.Create("C:\\temp\\", "MyNewpGDB.mdb", null, 0);       
// Cast for IName
ESRI.ArcGIS.esriSystem.IName name = (ESRI.ArcGIS.esriSystem.IName)workspaceName;
//Open a reference to the access workspace through the name object
IWorkspace pGDB_workspace = (IWorkspace)name.Open();
Console.WriteLine("Current path of the {0} is {1}", pGDB_workspace.Type, pGDB_workspace.PathName);
}
复制代码

2. 遍历所有图层

注意帮助文档中“Loop Through Layers of Specific UID Snippet”主题中详细列出了不同的UIDClass过滤选项,替换get_layers()中的null:

View Code
复制代码
 1 /// The different layer GUID's and Interface's are:
2 /// "{AD88322D-533D-4E36-A5C9-1B109AF7A346}" = IACFeatureLayer
3 /// "{74E45211-DFE6-11D3-9FF7-00C04F6BC6A5}" = IACLayer
4 /// "{495C0E2C-D51D-4ED4-9FC1-FA04AB93568D}" = IACImageLayer
5 /// "{65BD02AC-1CAD-462A-A524-3F17E9D85432}" = IACAcetateLayer
6 /// "{4AEDC069-B599-424B-A374-49602ABAD308}" = IAnnotationLayer
7 /// "{DBCA59AC-6771-4408-8F48-C7D53389440C}" = IAnnotationSublayer
8 /// "{E299ADBC-A5C3-11D2-9B10-00C04FA33299}" = ICadLayer
9 /// "{7F1AB670-5CA9-44D1-B42D-12AA868FC757}" = ICadastralFabricLayer
10 /// "{BA119BC4-939A-11D2-A2F4-080009B6F22B}" = ICompositeLayer
11 /// "{9646BB82-9512-11D2-A2F6-080009B6F22B}" = ICompositeGraphicsLayer
12 /// "{0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E}" = ICoverageAnnotationLayer
13 /// "{6CA416B1-E160-11D2-9F4E-00C04F6BC78E}" = IDataLayer
14 /// "{0737082E-958E-11D4-80ED-00C04F601565}" = IDimensionLayer
15 /// "{48E56B3F-EC3A-11D2-9F5C-00C04F6BC6A5}" = IFDOGraphicsLayer
16 /// "{40A9E885-5533-11D0-98BE-00805F7CED21}" = IFeatureLayer
17 /// "{605BC37A-15E9-40A0-90FB-DE4CC376838C}" = IGdbRasterCatalogLayer
18 /// "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}" = IGeoFeatureLayer
19 /// "{34B2EF81-F4AC-11D1-A245-080009B6F22B}" = IGraphicsLayer
20 /// "{EDAD6644-1810-11D1-86AE-0000F8751720}" = IGroupLayer
21 /// "{D090AA89-C2F1-11D3-9FEF-00C04F6BC6A5}" = IIMSSubLayer
22 /// "{DC8505FF-D521-11D3-9FF4-00C04F6BC6A5}" = IIMAMapLayer
23 /// "{34C20002-4D3C-11D0-92D8-00805F7C28B0}" = ILayer
24 /// "{E9B56157-7EB7-4DB3-9958-AFBF3B5E1470}" = IMapServerLayer
25 /// "{B059B902-5C7A-4287-982E-EF0BC77C6AAB}" = IMapServerSublayer
26 /// "{82870538-E09E-42C0-9228-CBCB244B91BA}" = INetworkLayer
27 /// "{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF}" = IRasterLayer
28 /// "{AF9930F0-F61E-11D3-8D6C-00C04F5B87B2}" = IRasterCatalogLayer
29 /// "{FCEFF094-8E6A-4972-9BB4-429C71B07289}" = ITemporaryLayer
30 /// "{5A0F220D-614F-4C72-AFF2-7EA0BE2C8513}" = ITerrainLayer
31 /// "{FE308F36-BDCA-11D1-A523-0000F8774F0F}" = ITinLayer
32 /// "{FB6337E3-610A-4BC2-9142-760D954C22EB}" = ITopologyLayer
33 /// "{005F592A-327B-44A4-AEEB-409D2F866F47}" = IWMSLayer
34 /// "{D43D9A73-FF6C-4A19-B36A-D7ECBE61962A}" = IWMSGroupLayer
35 /// "{8C19B114-1168-41A3-9E14-FC30CA5A4E9D}" = IWMSMapLayer
复制代码

示例:

View Code
复制代码
    IMap pMap = axMapControl1.Map;

IEnumLayer pEnumLayer = pMap.get_Layers(null, true);

pEnumLayer.Reset();

ILayer pLayer = pEnumLayer.Next();

while (pLayer != null)

{

// Console.WriteLine(pLayer.Name);

pLayer = pEnumLayer.Next();

}
复制代码

3. 判断图层类型

http://bbs.esrichina-bj.cn/ESRI/thread-59674-1-1.html

4. 【转】根据图层名称找到当前的图层的两种方法

http://www.gisempire.com/blog/user1/1/58.html

View Code
复制代码
'功能:根据图层名称找到当前的图层
'
返回:当前图层对象
Private Function GetCurLayer() As ILayer
Dim i As Integer
Set GetCurLayer = Nothing
For i = 0 To ff_m_Map.LayerCount - 1
If UCase$(ff_m_Map.Layer(i).Name) = UCase$(ff_m_strCurLayername) Then
Set GetCurLayer = ff_m_Map.Layer(i)
Exit For
End If
Next i
End Function

'功能:找到当前的图层
'
返回:当前图层对象
'
修改时间:
Private Function GetCurLayer() As ILayer
Set ff_m_ActiveView = ff_m_Map
Dim pEnumLayer As IEnumLayer
Dim pId As IFeatureLayer
Dim pLayer As ILayer
Set ff_m_Map = ff_m_ActiveView.FocusMap
Set pEnumLayer = ff_m_Map.Layers(pId, True)
Set GetCurLayer = Nothing
pEnumLayer.Reset
Set pLayer = pEnumLayer.Next
Do While Not pLayer Is Nothing
If UCase$(pLayer.Name) = UCase$(ff_m_strCurLayername) Then
Set GetCurLayer = pLayer
End If
Set pLayer = pEnumLayer.Next
Loop
End Function
复制代码

5. 打开TIN数据集

View Code
复制代码
public ILayer openTinLayer(string fullPath)

{

ITinWorkspace pTinWorkspace;

IWorkspace pWS;

IWorkspaceFactory pWSFact = new TinWorkspaceFactoryClass();

// ITinLayer pTinLayer = new TinLayerClass();

string pathToWorkspace = System.IO.Path.GetDirectoryName(fullPath);

string tinName = System.IO.Path.GetFileName(fullPath);

pWS = pWSFact.OpenFromFile(pathToWorkspace, 0);

pTinWorkspace = pWS as ITinWorkspace;

if (pTinWorkspace.get_IsTin(tinName))

{

pTin = pTinWorkspace.OpenTin(tinName);

pTinLayer.Dataset = pTin;

pTinLayer.ClearRenderers();

return pTinLayer as ILayer;

}

else

{

MessageBox.Show("该目录不包含Tin文件");

return null;

}

}
复制代码

6. 读取选中的IElement对象

http://bbs.esrichina-bj.cn/ESRI/thread-101968-1-1.html

7. 通过点的集合IPointCollection构建线IPolyline或面要素IPolygon

View Code
复制代码
IPolyline m_ProfilePolyline =new PolylineClass();

IPointCollection m_PtCol = m_ProfilePolyline as IPointCollection;

IPoint pPoint1 = new PointClass();

pPoint1.X = x ;

pPoint1.Y = y;

m_PtCol.AddPoint(pPoint1, ref missing, ref missing);

IPoint pPoint2 = new PointClass();

pPoint2.X = x ;

pPoint2.Y = y;

m_PtCol.AddPoint(pPoint2, ref missing, ref missing);
复制代码

8. 【转】平头缓冲

http://www.cnblogs.com/zuiyirenjian/archive/2011/01/13/1934267.html(AE中应该提供了其他的接口,这里主要学习点的操作)思路就是将线向左右两边移动相同的距离,然后将一条线的方向反向,加入另外一条,构造矩形或者矩形面

View Code
复制代码
private IPolygon FlatBuffer(IPolyline myLine, double bufferDis)

{

object o = System.Type.Missing;

//分别对输入的线平移两次(正方向和负方向)

IConstructCurve mycurve = new PolylineClass();

mycurve.ConstructOffset(myLine, bufferDis, ref o, ref o);

IPointCollection pCol = mycurve as IPointCollection;

IConstructCurve mycurve2 = new PolylineClass();

mycurve2.ConstructOffset(myLine, -1 * bufferDis, ref o, ref o);

//把第二次平移的线的所有节点翻转

IPolyline addline = mycurve2 as IPolyline;

addline.ReverseOrientation();

//把第二条的所有节点放到第一条线的IPointCollection里面

IPointCollection pCol2 = addline as IPointCollection;

pCol.AddPointCollection(pCol2);

//用面去初始化一个IPointCollection

IPointCollection myPCol = new PolygonClass();

myPCol.AddPointCollection(pCol);

//把IPointCollection转换为面

IPolygon myPolygon = myPCol as IPolygon;

//简化节点次序

myPolygon.SimplifyPreserveFromTo();

return myPolygon;

}
复制代码

9. 遍历要素类中的所有字段

View Code
复制代码
IField pField = null;

IFields pFields = pFeatureClass.Fields;

for (int i = 0; i < pFields.FieldCount - 1;i++ )

{

pField = pFields.get_Field(i);

if (pField.Type!=esriFieldType.esriFieldTypeGeometry)

{

dgvCombo.Items.Add(pField.Name);

}

}
复制代码

10. 获取图层的3DProperties

View Code
复制代码
/// <summary>

/// 获取图层三维属性

/// </summary>

/// <param name="pTinLayer">pFeatLayer图层</param>

/// <returns></returns>

public I3DProperties get3DProps(IFeatureLayer pFeatLayer)

{

I3DProperties p3DProps = null;

ILayer pLayer = pFeatLayer as ILayer;

ILayerExtensions lyrExt = pLayer as ILayerExtensions;

for (int i = 0; i < lyrExt.ExtensionCount; i++)

{

if (lyrExt.get_Extension(i) is I3DProperties)

{

p3DProps = lyrExt.get_Extension(i) as I3DProperties;

}

}

return p3DProps;

}

pSceneControl.Scene.AddLayer(pLyr,false);

//必须先添加图层到Scene中

I3DProperties p3DProps = get3DProps(pFeatLyr);

p3DProps.ExtrusionType = esriExtrusionType.esriExtrusionAbsolute;

p3DProps.ExtrusionExpressionString = "["+combHeight.Text+"]";

p3DProps.Apply3DProperties(pFeatLyr);
复制代码

ArcEngine代码整理(2)

1. 获取Map中选择的元素(Element)

View Code
复制代码
IGraphicsContainer m_GraphicsContainer =axMapControl1.Map as IGraphicsContainer;
m_GraphicsContainer.Reset();
IGraphicsContainerSelect pGraphicSelect = m_GraphicsContainer as IGraphicsContainerSelect;
//pGraphicSelect.SelectedElements.Reset();
int a = pGraphicSelect.ElementSelectionCount;
IEnumElement m_EnumEle=pGraphicSelect.SelectedElements;
m_EnumEle.Reset();
IElement m_Element = m_EnumEle.Next();
IElementProperties m_ElementProperties = m_Element as IElementProperties;
while (m_Element != null)
{
comboBox1.SelectedItem = m_ElementProperties.Name;
propertyGrid1.SelectedObject = (Stra)m_ElementProperties.CustomProperty;
m_Element = m_EnumEle.Next();
m_ElementProperties = m_Element as IElementProperties;
}
复制代码

2. 获取MapControl中选择的要素(Feature)

View Code
复制代码
IMap m_Map = pMapCtrl.Map;
            ESRI.ArcGIS.esriSystem.IUID uid = new ESRI.ArcGIS.esriSystem.UIDClass();

            uid.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}";

            ESRI.ArcGIS.Carto.IEnumLayer enumLayer = m_Map.get_Layers(((ESRI.ArcGIS.esriSystem.UID)(uid)), true);
            enumLayer.Reset();
            ESRI.ArcGIS.Carto.ILayer layer = enumLayer.Next();
            int j = -1;
            IFeatureLayer pCurrentLyr = null;
            while (!(layer == null))
            {
                j++;
                if (layer.Name == "Holes")
                {
                    pCurrentLyr = layer as IFeatureLayer;
                    break;
                }
                layer = enumLayer.Next();
            }
            IQueryFilter queryFilter = new QueryFilterClass();
            IFeatureSelection pFeatSelection = pCurrentLyr as IFeatureSelection;
            ISelectionSet selectionSet = pFeatSelection.SelectionSet;

            ICursor cursor = null;
                selectionSet.Search(queryFilter, true, out cursor);

            IFeatureCursor featureCursor = cursor as IFeatureCursor;
            IFeature feature;
            int i = 0;
            string holeid="";
            while ((feature = featureCursor.NextFeature()) != null)
            {
                if (i==0)
                {
                   holeid= feature.get_Value(2).ToString();
                   break;
                }
                
            }
复制代码

3. 打开AccessWorkspaceFactory中的栅格数据集

View Code
复制代码
 1                 string strFeatName = pFeatDlg.FileName;//自己定义的对话框
2 IWorkspaceFactory m_WorkspaceFactory = new AccessWorkspaceFactory();
3 IWorkspace m_Workspce = m_WorkspaceFactory.OpenFromFile(MineFrm.m_WorkspacePath, 0);
4 IRasterWorkspaceEx m_RasterWorkspace = m_Workspce as IRasterWorkspaceEx;//Access工作空间需要用这个接口跳转
5 IRasterDataset pRasterDataset = null;
6 pRasterDataset = m_RasterWorkspace.OpenRasterDataset(strFeatName);
7 IRasterLayer pRasterLayer = new RasterLayerClass();
8 pRasterLayer.CreateFromDataset(pRasterDataset);
9 ILayer player = pRasterLayer as ILayer;
10 player.Name = pRasterLayer.Name;
11 map = axMapControl1.Map;
12 map.AddLayer(player);
复制代码

4. 获取栅格类型的Surface

首先定义一个UIDClass:

ESRI.ArcGIS.esriSystem.IUID uid2 = new ESRI.ArcGIS.esriSystem.UIDClass();

uid2.Value = "{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF}" ;//= IRasterLayer

View Code
复制代码
 1             IRasterLayer m_RasterLayer=null;
2 ESRI.ArcGIS.Carto.IEnumLayer enumLayer = m_Map.get_Layers(((ESRI.ArcGIS.esriSystem.UID)(uid2)), true); // Explicit Cast
3 enumLayer.Reset();
4 ESRI.ArcGIS.Carto.ILayer layer = enumLayer.Next();
5 int i = -1;
6
7 while (!(layer == null))
8 {
9 i++;
10 if (i == listSruface.SelectedIndex)//这里是为了获取ListBox中选择项对应的图层,不在通过图层名称获取图层,通过遍历添加,再遍历选择
11 {
12 m_RasterLayer = layer as IRasterLayer;
13 }
14
15 layer = enumLayer.Next();
16 }
17 IRasterSurface m_RasterSurface = new RasterSurfaceClass();
18 MessageBox.Show(m_RasterLayer.BandCount.ToString());
19 //m_RasterSurface.PutRaster(m_RasterLayer.Raster, 0);
20 IRasterBandCollection pBands;
21 pBands = m_RasterLayer.Raster as IRasterBandCollection;
22 m_RasterSurface.RasterBand = pBands.Item(0);
23
24 ISurface m_Surface = m_RasterSurface as ISurface;
25 IGeometry pGeoPolyline;
26 m_Surface.InterpolateShape(m_CutLine as IGeometry, out pGeoPolyline, ref mis);
27 IPolyline pPolyline = pGeoPolyline as IPolyline;
28 IGraphicsContainer pGraphicContainer = m_PreViewMapCtrl.ActiveView as IGraphicsContainer;
29 ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbolClass();
30 pSimpleLineSymbol.Width = 1;
31 pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
32 IColor pColor = ColorAndIcolor.ConvertColorToIColor(Color.Red);
33 pSimpleLineSymbol.Color = pColor;
34 IElement pEleAxisX = null;//X轴线
35 ILineElement pLineEleX = new LineElementClass();
36 pLineEleX.Symbol = pSimpleLineSymbol;
37 pEleAxisX = pLineEleX as IElement;
38 pEleAxisX.Geometry = pPolyline;
39 pGraphicContainer.AddElement(pEleAxisX, 0);
40 m_PreViewMapCtrl.Extent = pGeoPolyline.Envelope;
41 m_PreViewMapCtrl.ActiveView.Refresh();
复制代码

5.从FeatureClass中读取要素IFeature

View Code
复制代码
IList<IPolyline> m_Polylines = new List<IPolyline>();//存放线要素
            IQueryFilter pQueryFilter = new QueryFilterClass();
            //pQueryFilter.SubFields = "LineId,Shape,StartPointId,EndPointId";      //过滤字段
            //  .WhereClause = "SUBTYPE = 'INDSTL'"
            IFeatureCursor pCur = m_FeatSelect.Search(null, false);//查找字段
            int iLineTypeIndex = m_FeatSelect.FindField("Type");
            IFeature pFeat = pCur.NextFeature();
            int show = 0;

            while (pFeat != null)
            {
                show++;
                if (double.Parse(pFeat.get_Value(iLineTypeIndex).ToString())>0)
                {
                    IPolyline pPoline = pFeat.Shape as IPolyline;
                     m_Polylines.Add(pPoline);
                }
                
                pFeat = pCur.NextFeature();
            }
复制代码

6.读取ITable中的IRow

View Code
复制代码
 1 IQueryFilter pQueryFilter = new QueryFilterClass();
 2                 pQueryFilter.WhereClause = sFinallySql;
 3                 //int i = m_Table.RowCount(pQueryFilter);
 4                 //MessageBox.Show(i.ToString());
 5                 pList = new List<Rooms>();
 6                 ICursor pcur = m_Table.Search(pQueryFilter, true);
 7                 IRow pRow = pcur.NextRow();
 8                 
 9                 while (pRow != null)
10                 {
11                     int k = pRow.Fields.FieldCount;
12                     Rooms m_Room = new Rooms();
13                     m_Room.RoomId = pRow.get_Value(roomidIndex).ToString();
14                     m_Room.RoomName = pRow.get_Value(roomNameIndex).ToString();
15                     m_Room.RoonEnglish = pRow.get_Value(engIndex).ToString();
16                     m_Room.Deprt = pRow.get_Value(deaprtIndex).ToString();
17                     m_Room.RoomType = pRow.get_Value(typeIndex).ToString();
18                     m_Room.Useage = pRow.get_Value(usageIndex).ToString();
19                     m_Room.LocationSchool = pRow.get_Value(localIndex).ToString();
20                     if (pRow.get_Value(areaIndex).ToString() != null && pRow.get_Value(areaIndex).ToString() != "")
21                     {
22                         m_Room.Area_1 = double.Parse(pRow.get_Value(areaIndex).ToString());
23                     }
24                    
25                     m_Room.User_1 = pRow.get_Value(userIndex).ToString();
26                     pList.Add(m_Room);    
27                     pRow = pcur.NextRow();
28                 }
29                 dataGridView1.DataSource = pList;
30                 for (int i = 0; i < dataGridView1.Columns.Count; i++)
31                     dataGridView1.Columns[i].HeaderText = plistheader[i];
复制代码

作者:yhexie
文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。 欢迎大家留言交流。转载请注明出处。.

原创文章,转载请注明出处!

这里学习了一下IStream和IPersistStream接口。

FileStream、ObjectStream和XmlStream类都继承了IStream接口。IPersist-->IPersistStream和IStream都是COM的接口,微软的接口。

串行化(serialization)是指将一个对象的当前状态转换成字节流(a stream of bytes)的过程,而反串行化(deserialization)则指串行化过程的逆过程,将字节流转换成一个对象。.Net目前通过Iserializeable接口实现序列化。这也就是我用C#开发的时候想序列化包含COM成员的类是产生的问题。难道必须转换?

情况描述为:1. 自己定义的一个类,类的成员有.net的值类型和对象,还包括COM对象,这时候应该如何保持我的这个类?

2.在保存COM对象的时候,比如下面对Scene的序列化,怎样同时保存同一窗体中其他的成员变量(.net变量或对象)?

View Code
复制代码
 1 private void 保存场景ToolStripMenuItem_Click(object sender, EventArgs e)
2 {
3 SaveFileDialog pSaveDlg = new SaveFileDialog();
4 pSaveDlg.Filter = "3D场景(*.sce)|*.sce";
5
6 pSaveDlg.Title = "生成3D场景";
7 if (pSaveDlg.ShowDialog() == DialogResult.OK)
8 {
9 string exportname = pSaveDlg.FileName;
10 IMemoryBlobStream pMemoryBlobStream = new MemoryBlobStreamClass();
11 IObjectStream pObjectStream = new ObjectStreamClass();
12 pObjectStream.Stream = pMemoryBlobStream;
13 IPersistStream pPersistStream = (IPersistStream)m_scene;
14 pPersistStream.Save((IStream)pObjectStream, 0);
15 pMemoryBlobStream.SaveToFile(exportname);
16 MessageBox.Show("场景保存成功!");
17 }
18 }
19
20 private void 打开场景ToolStripMenuItem_Click(object sender, EventArgs e)
21 {
22 OpenFileDialog pDlg = new OpenFileDialog();
23 pDlg.Filter = "3D场景(*.sce)|*.sce";
24 pDlg.Multiselect = false;
25 pDlg.Title = "3D场景";
26 if (pDlg.ShowDialog() == DialogResult.OK)
27 {
28 string fileName = pDlg.FileName;
29 string path = System.IO.Path.GetDirectoryName(fileName);
30 string name = System.IO.Path.GetFileNameWithoutExtension(fileName);
31 IMemoryBlobStream pMemoryBlobStream = new MemoryBlobStreamClass();
32 pMemoryBlobStream.LoadFromFile(fileName);
33 IObjectStream pObjectStream = new ObjectStreamClass();
34 pObjectStream.Stream = pMemoryBlobStream;
35 IPersistStream pPersistStream = (IPersistStream)m_scene;
36 pPersistStream.Load((IStream)pObjectStream);
37 axSceneControl1.SceneGraph.Scene = m_scene;
38 axSceneControl1.SceneGraph.RefreshViewers();
39 }
40 }
复制代码

参考文献:

http://www.cnblogs.com/myparamita/archive/2009/01/21/1379325.html

http://blog.csdn.net/puttytree/article/details/5376423


以上转自:http://www.cnblogs.com/yhlx125/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics