arcengine C#关于动态添加图层

动态加载影像图层为例

研究了两三天算是弄出来了。本例适合影像数据量特别的大程序使用,可以动态的添加删除影像数据,还有不完善的地方,不过基本满足要求。

1.首先得到关键点的图层

m_Map = axMapControl.Map;

            int l = 0;

            int f = 0;

            for (int j = 0; j < m_Map.LayerCount; j++)

            {

                if (m_Map.get_Layer(j).Name == "Points")

                {

                    l = j;//获得Points图层

                }

            }

2.取得该范围

double xmax = env.XMax;

            double ymax = env.YMax;

            double xmin = env.XMin;

            double ymin = env.YMin;

3.经纬度以及名称字段的属性信息存入arraylist

ArrayList aList = new ArrayList();

            ArrayList bList = new ArrayList();

            ArrayList nList = new ArrayList();

          

            IFeatureLayer ilayer = (IFeatureLayer)m_Map.get_Layer(l);//得到点图层

            IFeatureCursor pFeatureCursor;

            pFeatureCursor = ilayer.FeatureClass.Search(null, false);

            IFeature pFeature;

            pFeature = pFeatureCursor.NextFeature();

            while (pFeature != null)

            {

                string jdValue = Convert.ToString(pFeature.get_Value(4));

                string wdValue = Convert.ToString(pFeature.get_Value(5));

                string name = Convert.ToString(pFeature.get_Value(2));

                pFeature = pFeatureCursor.NextFeature();

                aList.Add(jdValue);//经纬度以及name存入arraylist

                bList.Add(wdValue);

                nList.Add(name);

            }

4.遍历arraylist并判断点是否在可视范围内

if (m_Map.MapScale < 400000)

            {

                for (int j = 0; j < 35; j++)

                {

                    double jd = double.Parse(aList[j].ToString());

                    double wd = double.Parse(bList[j].ToString());

                    //判断土层中是否已经存在某个影像数据,若存在则不进行坐标的判断

                    if (jd >= xmin && jd <= xmax)

                    {

                        if (wd >= ymin && wd <= ymax)

                        {  nameid = nList[j].ToString();//获得name属性

                            for (int q = 0; q < axMapControl.Map.LayerCount; q++)

                            {

                                temp = getname == nameid + ".tif";

                              

                                getname = Convert.ToString(axMapControl.get_Layer(q).Name);

                                if(temp)

                                indexa = q - 1;

                                  

                               

                            }

5.调用添加影像数据函数dynamicadd()(目前是本地)

//动态加载删格数据

        public void dynamicadd(string strFullPath)

        {

            int Index = strFullPath.LastIndexOf("\");

            string fileName = strFullPath.Substring(Index + 1);

            string filePath = strFullPath.Substring(0, Index);

            IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();

            IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(filePath, 0);

            IRasterDataset rasterDataset = (IRasterDataset)rasterWorkspace.OpenRasterDataset(fileName);

            IRasterLayer rasterLayer = new RasterLayerClass();

            rasterLayer.CreateFromDataset(rasterDataset);

            axMapControl.AddLayer(rasterLayer, 2);//有心的话你会发现此函数与axMapControl.Map.AddLayer()的区别(没有插入的图层序号),可以试试哦!

        }

6.判断是否已经动态加载

if (temp)

                            {

                               

                                //MessageBox.Show(Convert.ToString(axMapControl.get_Layer(indexa).Name));//测试

                                axMapControl.get_Layer(indexa).MinimumScale = 250000;//设定这个图层的可见范围?

                                return;

                            }

                            else

                            {

                                string strFullPath = "";

                                strFullPath = Application.StartupPath + @"\data影像" + nameid + ".tif";//这里就是影像图层的路径

                                dynamicadd(strFullPath);

                                axMapControl.get_Layer(2).MinimumScale = 250000;

                                break;

 

                            }

7将影像图层控制在五个以内超过了就删除

  clearLayer.Add(nameid);

                                axMapControl.get_Layer(2).MinimumScale = 250000;

                                if (clearLayer.Count > 5)//当图层超过5个时清除最早加载的图层(应该是不在视图范围内的)

                                {

                                    for (int e = 0; e < axMapControl.LayerCount;e++ )

                                    {

                                        if(axMapControl.get_Layer(e).Name.ToString() == clearLayer[0].ToString()+".tif")

                                        {

                                            //ILayer layer = null;

                                            //layer = axMapControl.Map.get_Layer(e);

                                            axMapControl.DeleteLayer(e);

                                        }

                                       

                                        }

8.综上所述

调用部分

//范围
IEnvelope env = axMapControl.Extent;//获取范围
fanwei(env);//可以返回string nameid再根据nameid寻找路径

主要声明的字段以及arraylist

          string nameid = "";//关键的传值字段,连接影像数据的路径

        string getname = "";//

        bool temp =false;//判断是否已经加载的bool类型变量

ArrayList clearLayer = new ArrayList();

 

主要函数部分

        public void fanwei(IEnvelope env)

        {

            m_Map = axMapControl.Map;

            int l = 0;

            int f = 0;

            for (int j = 0; j < m_Map.LayerCount; j++)

            {

                if (m_Map.get_Layer(j).Name == "Points")

                {

                    l = j;//获得Points图层为以后得到图层传递l

                }

            }

 

            double xmax = env.XMax;

            double ymax = env.YMax;

            double xmin = env.XMin;

            double ymin = env.YMin;//屏幕范围参数取值

            ArrayList aList = new ArrayList();

            ArrayList bList = new ArrayList();

            ArrayList nList = new ArrayList();//用来存储经纬度以及名称字段的arraylist

          

            IFeatureLayer ilayer = (IFeatureLayer)m_Map.get_Layer(l);//得到点图层

            IFeatureCursor pFeatureCursor;

            pFeatureCursor = ilayer.FeatureClass.Search(null, false);

            IFeature pFeature;

            pFeature = pFeatureCursor.NextFeature();//用来获取字段值的关键

            while (pFeature != null)

            {

                string jdValue = Convert.ToString(pFeature.get_Value(4));

                string wdValue = Convert.ToString(pFeature.get_Value(5));

                string name = Convert.ToString(pFeature.get_Value(2));

                pFeature = pFeatureCursor.NextFeature();

                aList.Add(jdValue);//经纬度以及name存入arraylist

                bList.Add(wdValue);

                nList.Add(name);

            }

            if (m_Map.MapScale < 400000)//比例尺大于此值将进行判断

            {

                for (int j = 0; j < 35; j++)//在这里我知道共有35个点

                {

                    double jd = double.Parse(aList[j].ToString());

                    double wd = double.Parse(bList[j].ToString());

                  //判断点是否在可视范围内,在范围内则取值nameid与影像数据路径相关联

//判断图层中是否已经存在某个影像数据,若存在则不进行坐标的判断

                 

  if (jd >= xmin && jd <= xmax)

                    {

                        if (wd >= ymin && wd <= ymax)

                        {

                            nameid = nList[j].ToString();//获得name属性

                            for (int q = 0; q < axMapControl.Map.LayerCount; q++)

                            {

                                temp = (getname == nameid + ".tif");

                              

                                getname = Convert.ToString(axMapControl.get_Layer(q).Name);

                                if(temp)

{

                                indexa = q - 1;

break;//很重要!!!!

                                }  

                            }

                            if (temp)

                            {

for (int e = 0; e < axMapControl.LayerCount; e++)
{
for (int r = 0; r < clearLayer.Count; r++)
{
if (axMapControl.get_Layer(e).Name.ToString() == clearLayer[r].ToString() + ".tif")
{
//ILayer layer = null;
//layer = axMapControl.Map.get_Layer(e);
axMapControl.get_Layer(e).MinimumScale = 250000;
}
}
}

                              return;//return还是break要注意

                            }

                             else

                            {

                                string strFullPath = "";

                                strFullPath = Application.StartupPath + @"\data影像" + nameid + ".tif";

                                dynamicadd(strFullPath);

                                clearLayer.Add(nameid);

          

                               if (clearLayer.Count > 5)//当图层超过5个时清除最早加载的图层(应该是不在视图范围内的)

                                {

                                    for (int e = 0; e < axMapControl.LayerCount;e++ )

                                    {

                                        if(axMapControl.get_Layer(e).Name.ToString() == clearLayer[0].ToString()+".tif")

                                        {

                                            //ILayer layer = null;

                                            //layer = axMapControl.Map.get_Layer(e);

                                            axMapControl.DeleteLayer(e);

                                        }

                                       

                                        }

                                }

                                break;

 

                            }                        }

 

                    }

                }

            }

        }

 

如果有问题欢迎提出建议!刚刚做出来可能有需要完善的地方

 

原文地址:https://www.cnblogs.com/duoduo0605/p/3257457.html