实验二 地图数据访问与渲染

一、实验目的

  1. 熟悉不同数据类型对应的工作空间及其使用;
  2. 掌握矢量文件、栅格文件、个人数据库以及文件夹数据库的加载;
  3. 掌握ArcGIS engine 中图层的渲染类型及所用接口;
  4. 掌握对点、线、面数据的简单渲染。

二、实验仪器与设备

计算机、visual studio 软件、ArcGIS engine 开发包

三、实验内容与步骤

  1. 矢量数据的加载
    ShapeFile文件是目前主流的一种空间数据的文件存储方式,也是不同GIS软件进行数据格式转换常用的中间格式。加载ShapeFile数据的方式有两种:通过工作空间加载;通过IMapControl接口的AddShapeFile方法加载。
    通过工作空间加载ShapeFile文件的实现思路。
(1)创建ShapefileWorkspaceFactory实例pWorkspaceFactory,使用IWorkspaceFactory接口的OpenFromFile方法打开pFeatureWorkspace中存储的基于ShapeFile的工作区;(2)创建FeatureLayer的实例pFeatureLayer,并定义数据集;(3)使用IMap接口的AddLayer方法加载pFeatureLayer。

核心代码如下:
Form1.cs:

    ///     /// 打开shape文件    ///     ///     ///     private void buttonItem3_Click(object sender, EventArgs e)    {        gis3._1.myengineproject readfile = new gis3._1.myengineproject();        OpenFileDialog opendlg = new OpenFileDialog();        opendlg.Filter = "shape flie|*shp";        if (opendlg.ShowDialog() == DialogResult.OK)        {            IFeatureLayer pfraturelayer = readfile.readshapefilefromfile(opendlg.FileName);            axMapControl1.AddLayer(pfraturelayer as ILayer);            axMapControl1.Refresh();        }    }

MyEngineProject.cs:

    ///     /// shape文件    ///     ///     ///     public IFeatureLayer readshapefilefromfile(string fullfilename)    {        string pathname = System.IO.Path.GetDirectoryName(fullfilename);        string filename = System.IO.Path.GetFileName(fullfilename);        try        {            IWorkspaceFactory pwsf = new ShapefileWorkspaceFactory();            IFeatureWorkspace pws = pwsf.OpenFromFile(pathname, 0) as IFeatureWorkspace;            IFeatureClass pfeatureclass = pws.OpenFeatureClass(filename);            IFeatureLayer pfeaturelayer = new FeatureLayer();            pfeaturelayer.FeatureClass = pfeatureclass;            pfeaturelayer.Name = pfeatureclass.AliasName;            return pfeaturelayer;        }        catch (Exception ex)        {            System.Windows.Forms.MessageBox.Show(ex.ToString());            return null;        }    }

2.栅格数据的加载
栅格数据是GIS的数据源之一,主要有卫星影像、航空影像、扫描的地图、照片等。常见的栅格数据格式有*.img、.tif和.jpg等,这几种栅格数据的工作空间是其所在的文件夹。添加栅格数据主要用到IRasterPyramid3、IRasterLayer等接口。
加载栅格数据的实现思路

(1)用IWorkspaceFactory接口的OpenFromFile方法获得栅格文件的工作区;(2)用IRasterWorkspace接口的OpenRasterDataset方法获得栅格文件的数据集,实例化栅格文件对象,并对栅格数据集是否具有金字塔进行判断(这里栅格文件如果不具有金字塔则创建金字塔,可以提高大数据量图像的显示效率);(3)创建RasterLayer类的实例pRasterLayer,并定义其数据集;(4)使用MapControl控件的AddLayer方法将pRasterLayer添加.。

核心代码如下:
Form1.cs:

    ///     /// 打开栅格文件    ///     ///     ///     private void buttonItem4_Click(object sender, EventArgs e)    {        gis3._1.myengineproject readfile = new gis3._1.myengineproject();        OpenFileDialog opendlg = new OpenFileDialog();        opendlg.Filter = "raster flie|*img|tiff file|*tif";        if (opendlg.ShowDialog() == DialogResult.OK)        {            IRasterLayer prasterlayer = readfile.readrasterlayerfromfile(opendlg.FileName);            axMapControl1.AddLayer(prasterlayer as ILayer);            axMapControl1.Refresh();        }    }

MyEngineProject.cs:

 ///     /// 栅格文件    ///     ///     ///     public IRasterLayer readrasterlayerfromfile(string fullfilename)    {        string pathname = System.IO.Path.GetDirectoryName(fullfilename);        string filename = System.IO.Path.GetFileName(fullfilename);        try        {            IWorkspaceFactory pwsf = new RasterWorkspaceFactory();            IRasterWorkspace pws = pwsf.OpenFromFile(pathname, 0) as IRasterWorkspace;            IRasterDataset prasterds = pws.OpenRasterDataset(filename);            IRasterPyramid3 prasterpryd = prasterds as IRasterPyramid3;            if (prasterpryd != null)            {                if (prasterpryd.Present == false)                {                    prasterpryd.Create();                }            }            IRasterLayer prasterlayer = new RasterLayer();            prasterlayer.CreateFromDataset(prasterds);            return prasterlayer;        }        catch (Exception ex)        {            System.Windows.Forms.MessageBox.Show(ex.ToString());            return null;        }    }

3.个人数据库及文件数据库的加载
加载Personal Geodatabase数据程序实现思路如下:

(1)创建AccessWorkspaceFactory类的实例;(2)用IWorkspaceFactory接口的OpenFromFile方法打开*.mdb数据集的工作空间,对工作空间里面的数据进行加载。

核心代码如下:
Form1.cs:

    ///     /// 打开个人地理数据库    ///     ///     ///     private void buttonItem5_Click(object sender, EventArgs e)    {        OpenFileDialog opendlg = new OpenFileDialog();        opendlg.Filter = "personal geodatabase|*mdb";        if (opendlg.ShowDialog() == DialogResult.OK)        {            string fullfilename = opendlg.FileName;            gis3._1.myengineproject readfile = new gis3._1.myengineproject();            IMapControl3 m_map = axMapControl1.Object as IMapControl3;            readfile.readdatafrompersonaldatabase(fullfilename, m_map);            axMapControl1.Refresh();        }    }

MyEngineProject.cs:

    ///     /// 个人地理数据库    ///     ///     ///     public void readdatafrompersonaldatabase(string fullfilename, IMapControl3 m_map)    {        try        {            string pathname = System.IO.Path.GetDirectoryName(fullfilename);            string filename = System.IO.Path.GetFileName(fullfilename);            IWorkspaceFactory pwf = new AccessWorkspaceFactory();            IWorkspace pws = pwf.OpenFromFile(fullfilename, 0);            addl1layers(pws, m_map);        }        catch (Exception ex)        {            System.Windows.Forms.MessageBox.Show(ex.ToString());            return;        }    }    private void addl1layers(IWorkspace pws, IMapControl3 m_map)    {        IEnumDataset peds = pws.Datasets[esriDatasetType.esriDTAny];        peds.Reset();        IDataset pds = peds.Next();        while (pds != null)        {            if (pds is IFeatureClass)//矢量数据            {                IFeatureWorkspace pfeaturews = pws as IFeatureWorkspace;                IFeatureLayer pfeaturelayer = new FeatureLayer();                pfeaturelayer.FeatureClass = pfeaturews.OpenFeatureClass(pds.Name);                pfeaturelayer.Name = pfeaturelayer.FeatureClass.AliasName;                m_map.AddLayer(pfeaturelayer as ILayer);            }            else if (pds is IRasterDataset)//栅格数据            {                IRasterWorkspace prasterws = pws as IRasterWorkspace;                IRasterDataset prasterds = prasterws.OpenRasterDataset(pds.Name);                IRasterPyramid3 prasterpryd = prasterds as IRasterPyramid3;                if (prasterpryd != null)                {                    if (prasterpryd.Present == false)                    {                        prasterpryd.Create();                    }                }                IRasterLayer prasterlayer = new RasterLayer();                prasterlayer.CreateFromDataset(prasterds);                m_map.AddLayer(prasterlayer as ILayer);            }            else if (pds is IFeatureDataset)//矢量数据集            {                IFeatureWorkspace pfeaturews = pws as IFeatureWorkspace;                IFeatureDataset pfeatureds = pfeaturews.OpenFeatureDataset(pds.Name);                IEnumDataset peumds = pfeatureds.Subsets;                peumds.Reset();                IGroupLayer pgrouplayer = new GroupLayer();                pgrouplayer.Name = pds.Name;                IDataset pds2 = peumds.Next();                while (pds2 != null)                {                    IFeatureClass pfeatureclass2 = pfeaturews.OpenFeatureClass(pds2.Name);                    IFeatureLayer pfeaturelayer2 = new FeatureLayer();                    pfeaturelayer2.FeatureClass = pfeatureclass2;                    pfeaturelayer2.Name = pfeaturelayer2.FeatureClass.AliasName;                    pgrouplayer.Add(pfeaturelayer2 as ILayer);                    pds2 = peumds.Next();                }                m_map.AddLayer(pgrouplayer as ILayer);            }            pds = peds.Next();        }    }

文件地理数据库是以文件夹形式存储各种类型的GIS数据集,可以存储、查询和管理空间数据和非空间数据,支持的地理数据库的大小最大为1TB,在不使用DBMS的情况下能够扩展并存储大量数据,是继个人数据库之后Esri推出的新的数据管理系统。文件地理数据库支持单用户编辑,不支持版本管理。
实现思路如下:

由于文件地理数据库是以文件夹的形式存在的,可以使用FolderBrowserDialog选择文件夹进行加载。 首先创建FileGDBWorkspaceFactoryClass类的实例;其次用IWorkspaceFactory接口的OpenFromFile方法打开文件地理数据库的工作空间,对工作空间里面的数据进行加载。

核心代码如下:
Form1.cs:

    ///     /// 打开文件数据库    ///     ///     ///     private void buttonItem6_Click(object sender, EventArgs e)    {        FolderBrowserDialog foiderdlg = new FolderBrowserDialog();        if (foiderdlg.ShowDialog() == DialogResult.OK)        {            gis3._1.myengineproject readfile = new gis3._1.myengineproject();            IMapControl3 m_map = axMapControl1.Object as IMapControl3;            readfile.readdatafromfiledatabase(foiderdlg.SelectedPath, m_map);            axMapControl1.Refresh();        }    }

MyEngineProject.cs:

    ///     /// 文件数据库    ///     ///     ///     public void readdatafromfiledatabase(string selecpath, IMapControl3 m_map)    {        try        {            //string pathname = System.IO.Path.GetDirectoryName(selecpath);            //string filename = System.IO.Path.GetFileName(selecpath);            IWorkspaceFactory pwf = new FileGDBWorkspaceFactory();            IWorkspace pws = pwf.OpenFromFile(selecpath, 0);            addl1layers(pws, m_map);        }        catch (Exception ex)        {            System.Windows.Forms.MessageBox.Show(ex.ToString());            return;        }    }

4.点线面三种类型矢量数据的渲染

```csharp      ///         /// 点图层渲染        ///         ///         ///         public bool randerlayerusesimplemark(ILayer player)        {            try            {                //第一步:把ilayer为IGeoFeatureLayer                IGeoFeatureLayer pgeofeaturelayer = player as IGeoFeatureLayer;                //创建要使用的符号,并设置符号格式                ISimpleMarkerSymbol pmarksymbol = new SimpleMarkerSymbol();                pmarksymbol.Style = esriSimpleMarkerStyle.esriSMSCross;                pmarksymbol.Color = getrgb(255,0,0);                //第三步创建渲染器                ISimpleRenderer prender = new SimpleRenderer();                //第四步用创建的符号进行渲染                prender.Symbol = pmarksymbol as ISymbol;                //第五步,把渲染器赋值给igeolayer的render属性                pgeofeaturelayer.Renderer=prender as IFeatureRenderer;                return true;            }            catch(Exception ex)            {                 System.Windows.Forms.MessageBox.Show(ex.ToString());                return false;            }        }        ///         /// 线渲染(未调用)        ///         ///         ///         public bool randerlayerusesimpleline(ILayer player)        {            try            {                IGeoFeatureLayer pgeofeaturelayer = player as IGeoFeatureLayer;                ISimpleLineSymbol plinesymbol = new SimpleLineSymbol();                plinesymbol.Style = esriSimpleLineStyle.esriSLSDashDot;                plinesymbol.Color = getrgb(255, 0, 0);                plinesymbol.Width = 1.5;                ISimpleRenderer prender = new SimpleRenderer();                prender.Symbol = plinesymbol as ISymbol;                pgeofeaturelayer.Renderer = prender as IFeatureRenderer;                return true;            }            catch (Exception ex)            {                System.Windows.Forms.MessageBox.Show(ex.ToString());                return false;            }        }        ///         /// 面渲染(未调用)        ///         ///         ///         public bool randerlayerusesimplestyle(ILayer player)        {            try            {                IGeoFeatureLayer pgeofeaturelayer = player as IGeoFeatureLayer;                ISimpleFillSymbol pfilesymbol = new SimpleFillSymbol();                pfilesymbol.Style = esriSimpleFillStyle.esriSFSSolid;                pfilesymbol.Color = getrgb(255, 0, 0);                pfilesymbol.Outline.Width = 1.5;                ISimpleRenderer prender = new SimpleRenderer();                prender.Symbol = pfilesymbol as ISymbol;                pgeofeaturelayer.Renderer = prender as IFeatureRenderer;                return true;            }            catch (Exception ex)            {                System.Windows.Forms.MessageBox.Show(ex.ToString());                return false;            }        }        private IRgbColor getrgb(int r,int g ,int b)       {            IRgbColor prgbc=new RgbColor();            prgbc.Red = r;            prgbc.Blue = b;            prgbc.Green = g;            return prgbc;                }

5.DEM数据渲染

     ///         /// dem数据渲染        ///         ///         ///         public bool renderdemlayer(ILayer player)        {            try            {                IRasterLayer prasterlayer=player as IRasterLayer;                IRasterStretchColorRampRenderer prscrr=new RasterStretchColorRampRenderer();                IRasterRenderer prasterrender=prscrr as IRasterRenderer;                prasterrender.Raster=prasterlayer.Raster;                prasterrender.Update();                IAlgorithmicColorRamp pacr=new AlgorithmicColorRamp();                pacr.FromColor=getrgb(0,0,255) as IColor;                pacr.ToColor=getrgb(255,0,0) as IColor;                pacr.Size=255;                bool iscreate=false;                pacr.CreateRamp(out iscreate);                if (iscreate)                {                    prscrr.BandIndex = 0;                    prscrr.ColorRamp = pacr;                    prasterrender.Update();                    prasterlayer.Renderer = prasterrender as IRasterRenderer;                }                else                {                    return false;                }                return true;            }            catch(Exception ex)            {                System.Windows.Forms.MessageBox.Show(ex.ToString());                return false;            }        }        ///         /// 唯一值渲染        ///         ///         ///         public bool renderuinquevalue(ILayer player)        {            try            {                //第一步:把ilayer为IGeoFeatureLayer                IGeoFeatureLayer geolayer=player as IGeoFeatureLayer;                               //创建要使用的符号,并设置符号格式                ISimpleFillSymbol pfillsymbol1 = new SimpleFillSymbol();                pfillsymbol1.Color = getrgb(60 ,179, 113);                pfillsymbol1.Outline.Width = 1.5;                ISimpleFillSymbol pfillsymbol2 = new SimpleFillSymbol();                pfillsymbol2.Color = getrgb(0, 100, 0);                pfillsymbol2.Outline.Width = 1.5;                ISimpleFillSymbol pfillsymbol3 = new SimpleFillSymbol();                pfillsymbol3.Color = getrgb(0, 255, 0);                pfillsymbol3.Outline.Width = 1.5;                ISimpleFillSymbol pfillsymbol4 = new SimpleFillSymbol();                pfillsymbol4.Color = getrgb(255,255,0);                pfillsymbol4.Outline.Width = 1.5;                //第三步创建渲染器                IUniqueValueRenderer puvrenderer = new UniqueValueRendererClass();                puvrenderer.FieldCount=1;                puvrenderer.set_Field(0,"DDMM");                //第四步用创建的符号进行渲染                puvrenderer.AddValue("21","21",pfillsymbol1 as ISymbol);                puvrenderer.AddValue("22","22", pfillsymbol2 as ISymbol);                puvrenderer.AddValue("23","23", pfillsymbol3 as ISymbol);                puvrenderer.AddValue("24","24", pfillsymbol4 as ISymbol);                geolayer.Renderer = puvrenderer as IFeatureRenderer;                return true;            }            catch(Exception ex)            {                 System.Windows.Forms.MessageBox.Show(ex.ToString());                return false;            }        }

四、实验结果

1.点图层渲染

2.dem渲染

3.唯一值渲染

4.文件打开

五、实验心得与体会

1.对矢量文件、栅格文件、个人地理数据库、文件地理数据库有了更深的认识。
2.对不同的数据会用不同的渲染方法进行渲染。