|
|
|
|

ArcGIS Server 开发系列(三)--漫游 Graphics data sources

和ArcGIS Server Local、ArcGIS Server Internet一样,GraphicsLayer是ArcGIS Server MapResource的一种,提供functionality给web controls使用。本文将在《ArcGIS Server 开发系列(二)--Web ADF 编程》示例基础上,增加查询结果高亮显示的功能,因为...

作者:flyingis来源:http://www.cnblogs.com/flyingis/|2009年01月13日
   和ARCGIS Server Local、ARCGIS Server Internet一样,GraphicsLayer是ARCGIS Server MapResource的一种,提供functionality给web controls使用。本文将在《ARCGIS Server 开发系列(二)--Web ADF 编程》示例基础上,增加查询结果高亮显示的功能,因为高亮显示的结果并不是图层本身所具备的,因此只需将高亮显示的图片存为graphics即可。

    目标:
    查询结果的高亮显示

    准备工作:
    1.以《ARCGIS Server 开发系列(二)--Web ADF 编程》示例配置和代码为基础。
    2.MapResourceManager属性中增加一个名为Selection的MapResource,并将它移动到编号为0的位置,即显示在所有MapResource最上面。


    可以看到GraphicsLayer的datasource是在内存中的,也就是说是为了临时显示或存储使用的,这样速度比较快。Selection一定要放在World上面,否则就被World图层覆盖掉了。    代码实现:
    在UI界面上,增加一个command,用来清除graphics。


    双击“Select”生成事件响应方法:

protected
void cmdSelect_Click(object sender, EventArgs e)
{
    SelectFeatures();
}


    代码的核心就在SelectFeature()里,它分为两个步骤,第一步对图层进行属性查询,第二步对查询结果进行高亮显示。首先是图层的属性查询:

int resource_index =
1;
string targetlayername =
"countries";
System.Data.DataTable datatable
=
null;
//直接获取MapResourceName为world的MapFunctionality,它的编号为1
ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)Map1.GetFunctionality(resource_index);
//先得到functionality,再获取resource
ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = mf.Resource;
bool supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));
if (supported)
{
    ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc;
    qfunc
= (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisresource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);

   
string[] lids;
   
string[] lnames;
    qfunc.GetQueryableLayers(
null, out lids, out lnames);

    ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter
=
new ESRI.ArcGIS.ADF.Web.SpatialFilter();
    spatialfilter.ReturnADFGeometries
=
false;
    spatialfilter.MaxRecords
=
1000;
    spatialfilter.WhereClause
= txtQuery.Text;
           
    datatable
= qfunc.Query(null, lids[0], spatialfilter);
}

    这段代码和《ArcGIS Server 开发系列(二)--Web ADF 编程》示例中的代码相比,没有太多改动的地方,用到了ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality,它继承于ESRI.ArcGIS.ADF.Web.DataSources.IGISFunctionality 接口。每一个web control可能拥有多个functionality,而funtionality是各种resource展现出来的,因此可以通过web controls--funcionalities--resources这条路线来获得当前的资源,那么如何让查询结果高亮显示呢?

//重新获得Map1控件所有的functionality
IEnumerable gfc = Map1.GetFunctionalities();
ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource gResource
= null;
foreach (IGISFunctionality gfunc in gfc)
{
   
//找到名为"Selection"的MapResource

if (gfunc.Resource.Name =="Selection")
   
{
        
//down cast到ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource
        gResource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;
    }

}


if (gResource ==null)
   
return;

ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer glayer
= null;

foreach (System.Data.DataTable dt in gResource.Graphics.Tables)
{
   
if (dt is ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)
   
{
        glayer
= (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dt;
        
break;
    }

}


if (glayer == null)
{
    glayer
= new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();

    gResource.Graphics.Tables.Add(glayer);
}


//清除已有数据
glayer.Clear();

DataRowCollection drs
= datatable.Rows;

int shpind = -1;
for (int i = 0; i < datatable.Columns.Count; i++)
{
   
if (datatable.Columns.DataType == typeof(ESRI.ArcGIS.ADF.Web.Geometry.Geometry))
   
{
        
//找到Geometry字段的序号
        shpind = i;
        
break;
    }

}


try
{
   
foreach (DataRow dr in drs)
   
{
        ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom
= (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)dr[shpind];
      
        
//创建一个GraphicElement
        ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge =null;
        ge
= new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geom, System.Drawing.Color.Yellow);
        ge.Symbol.Transparency
= 50.0;
      
        
//将GraphicElement添加到ElementGraphicsLayer中
        glayer.Add(ge);
    }

}

catch (InvalidCastException ice)
{
   
throw
new Exception("No geometry available in datatable");
}


if (Map1.ImageBlendingMode == ImageBlendingMode.WebTier)
{ Map1.Refresh(); }
else
if (Map1.ImageBlendingMode == ImageBlendingMode.Browser)
{
   
//只刷新Graphics Resource
    Map1.RefreshResource(gResource.Name);
}

    这次我们没有将搜索到的结果绑定到控件上,只要得到高亮显示的结果,测试一下程序,看看能得到什么样的效果。


    搜索出国家名称以"C"开头的国家,最典型的“中国”、“加拿大”已经找到了,这样我们就实现了高亮显示的功能。同样,我们进行开发后的小结,能想到些什么呢?还是按照CH风格来进行总结:

    第一,GraphicsLayer有两个子类,ElementGraphicLayer和FeatureGraphicLayer,因为程序中只需要暂时显示查询的结果,因此将查询要素存为ElementGraphicLayer就可以,想想在什么情况下使用FeatureGraphicLayer。

    第二,ElementGraphicLayer继承于System.Data.DataTable,gResource.graphics属于 System.Data.DataSet类型,这样使得我们在开发过程中,可以将GraphicElement添加到 ElementGraphicLayer,然后将ElementGraphicLayer添加到gResource.graphics,通过这种途径来向 GraphicsLayer的mapresource中添加数据,这种机制方便了我们能够像操纵datatable和dataset一样来控制 mapresource中的数据,既和.Net无缝整合,也在一定程度上降低了Server开发难度,例如代码中glayer.Clear()调用了datatable的clear()方法,还有后面GraphicElement的创建。

    第三,Map1.ImageBlendingMode决定了地图的刷新是刷新整个页面,还是仅刷新当前mapresource,这样的设计在web开发中尽可能的较少了网络数据传输量。

上一篇:ArcGIS Server 开发系列(二)--Web ADF …

下一篇:ArcGIS Server 开发系列(四)--ArcGIS S…