ArcEngine实现动态加载地图
目录
动态加载地图
说一下主要思想:通过判断相关点是否在视图范围,并且达到某一地图比例尺时加载相应范围的地图(需要有相应基础才能看懂本文章)。
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
ArrayListaList = 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 =newRasterWorkspaceFactory();
IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(filePath,0);
IRasterDataset rasterDataset = (IRasterDataset)rasterWorkspace.OpenRasterDataset(fileName);
IRasterLayer rasterLayer =newRasterLayerClass();
rasterLayer.CreateFromDataset(rasterDataset);
axMapControl.AddLayer(rasterLayer,2);
}
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.综上所述
//缩放至一定范围时加载地图
//可视范围还有问题
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 =newArrayList();
ArrayList bList =newArrayList();
ArrayList nList =newArrayList();//用来存储经纬度以及名称字段的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
{
stringstrFullPath = "";
strFullPath =Application.StartupPath + @"\\data\影像\" + nameid +".tif";
dynamicadd(strFullPath);
clearLayer.Add(nameid);
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.get_Layer(e).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);
}
}
}
break;
} }
}
}
}
}
9.从数据库中添加数据
字段及其主函数
string nameid = "";
string getname = "";
bool temp = false;
ArrayList clearLayer =newArrayList();
string strFullPath = "";//关?键¨¹路¡¤径?参?数ºy!ê?!ê?!ê?!ê?!ê?
public void fanwei(IEnvelope env)
{
m_Map = axMapControl.Map;
int L = 0;
for (int j = 0; j< m_Map.LayerCount; j++)
{
if (m_Map.get_Layer(j).Name =="Points")//可¨¦改?
{
L = j;//获?得Ì?Points图ª?层?
}
}
double xmax = env.XMax;
double ymax = env.YMax;
double xmin = env.XMin;
double ymin = env.YMin;
ArrayList aList = newArrayList();
ArrayList bList = newArrayList();
ArrayList nList = newArrayList();
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);//经-纬3度¨¨以°?及¡ãname存ä?入¨?arraylist
bList.Add(wdValue);
nList.Add(name);
}
if (m_Map.MapScale < 400000)
{
for (intj = 0; j < 35; j++)
{
double jd = double.Parse(aList[j].ToString());
doublewd = double.Parse(bList[j].ToString());
//
if (jd >= xmin && jd <=xmax)
{
if (wd >= ymin && wd <= ymax)
{
nameid =nList[j].ToString();//
//
strFullPath =FSelect(nameid);
for (int q = 0; q< axMapControl.Map.LayerCount; q++)
{
getname = Convert.ToString(axMapControl.get_Layer(q).Name);
temp = (getname== strFullPath);
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() ==strFullPath)
{
axMapControl.get_Layer(e).MinimumScale = 250000;
}
}
}
return;
}
else
{
dynamicadd(strFullPath);
clearLayer.Add(strFullPath);
if (clearLayer.Count > 5)
{
for (int e = 0; e< axMapControl.LayerCount; e++)
{
if (axMapControl.get_Layer(e).Name.ToString() ==clearLayer[0].ToString() +".tif"|| axMapControl.get_Layer(e).Name.ToString() == clearLayer[0].ToString())
{
axMapControl.DeleteLayer(e);
}
}
}
break;
}
}
}
}
}
}
加载数据的函数
publicvoid dynamicadd(stringstrFullPath)
{
//int Index = strFullPath.LastIndexOf("\\");
//string fileName = strFullPath.Substring(Index + 1);
//string filePath = strFullPath.Substring(0, Index);
IPropertySet pProSet =newPropertySet();
pProSet.SetProperty("Server","192.168.159.186");
pProSet.SetProperty("Instance","sde:oracle11g:192.168.159.186/orcl");
pProSet.SetProperty("Database","orcl");
pProSet.SetProperty("user","sde");
pProSet.SetProperty("password","sde");
pProSet.SetProperty("version","SDE.DEFAULT");
IWorkspace pSdeWorkspace =null;
IFeatureWorkspace pFeatureWorkspace =null;
IRasterWorkspaceEx pRasterWorkspace =null;
IWorkspaceFactory pWSF =newSdeWorkspaceFactory();
pSdeWorkspace = pWSF.Open(pProSet, 0);
pFeatureWorkspace = pSdeWorkspace as IFeatureWorkspace;
pRasterWorkspace = pSdeWorkspace as IRasterWorkspaceEx;
//
IRasterDatasetpRasterDataset = (IRasterDataset)pRasterWorkspace.OpenRasterDataset(strFullPath);
IRasterLayer pLayerBackground =newRasterLayerClass();
pLayerBackground.CreateFromDataset(pRasterDataset);
axMapControl.AddLayer(pLayerBackground,1);
}
对数据库的操作
public string FSelect(stringstrPath)
{
string sdename = "";
OracleConnection conn =newOracleConnection(@ClassCommon.GetConnect());
conn.Open();
string sql = "selectsdename from SYSTEM_HERITAGE_SDE_VIEW where hname = '"+strPath+"'";//
//OracleCommand cmd = new OracleCommand(sql, conn);
using (OracleCommandcmd =newOracleCommand(sql,conn))
{
cmd.CommandType = CommandType.Text;
try
{
OracleDataReader reader =cmd.ExecuteReader();
while (reader.Read())
{
sdename = reader["sdename"].ToString(); ;
}
}
catch (Exceptionex)
{
}
}
conn.Close();
//cmd.Dispose();
return sdename;
}
转载自:https://blog.csdn.net/nothing_is_imposible/article/details/17355679