要访问ArcObject API,首先必须以Local的方式连接服务。
ADF中使用Local方式连接服务产生的资源可以通过AGSLocalMapResource获取,AGSLocalMapResource.getServerContext()返回一个实现了IServerContext接口对象,IServerContext很重要,通过它可以访问所有生成的AO对象。
下面是一段通过Tool,实现缓冲区的代码:
public void queryM(MapEvent event) throws IOException{
WebContext webContext = event.getWebContext();
AGSLocalMapResource agsLocalMapResource = (AGSLocalMapResource)webContext.getResourceById("ags1");
IServerContext serverContext = agsLocalMapResource.getServerContext();
WebPoint webPoint = (WebPoint)event.getWebGeometry().toMapGeometry(webContext.getWebMap());
Point point = (Point)AGSUtil.toAGSGeometry(webPoint);
Object objPoint = AGSUtil.createArcObjectFromStub(point, serverContext);
ITopologicalOperator topologicalOperator = (ITopologicalOperator)objPoint;
IPolycurve objPolygon = (IPolycurve)topologicalOperator.buffer(0.001);
objPolygon.densify(-1, -1);
PolygonN polygonN = (PolygonN)AGSUtil.createStubFromArcObject(objPolygon, PolygonN.class, serverContext);
WebPolygon webPolygon = (WebPolygon)AGSUtil.fromAGSGeometry(polygonN);
WebSimplePolygonSymbol symbol = new WebSimplePolygonSymbol();
symbol.setAntialiasing(true);
symbol.setFillTransparency(0.7);
symbol.setColor("0,255,0");
symbol.setFillColor("255,0,0");
WebGraphics webGraphics = webContext.getWebGraphics();
GraphicElement element = new GraphicElement();
element.setGeometry(webPolygon);
element.setSymbol(symbol);
webGraphics.addGraphics(element);
webContext.refresh();
}
下面我们来一分析获取进入AO处理的执行过程,
WebContext webContext = event.getWebContext();
不用多说,必须的第一步。
AGSLocalMapResource agsLocalMapResource = (AGSLocalMapResource)webContext.getResourceById("ags1");
IServerContext serverContext = agsLocalMapResource.getServerContext();
这两段代码,agsLocalMapResource获取ags1这个Local下的地图资源,agsLocalMapResource.getServerContext()返回了负责服务环境中所有AO处理的代理,serverContext因此获得了操纵AO的权利。
WebPoint webPoint = (WebPoint)event.getWebGeometry().toMapGeometry(webContext.getWebMap());
Point point = (Point)AGSUtil.toAGSGeometry(webPoint);
当激活Tool,在地图上点击时,我们可以得到一个WebPoint的ADF对象,要让其参与AO的处理,需要两步方法,第一步要将其转化为SOAP对象,第二步再将SOAP转为最终使用的AO对象,以上两段代码完成了转换功能。
在完成AO处理后,得到的AO对象还要转换为ADF对象,如下:
PolygonN polygonN = (PolygonN)AGSUtil.createStubFromArcObject(objPolygon, PolygonN.class, serverContext);
WebPolygon webPolygon = (WebPolygon)AGSUtil.fromAGSGeometry(polygonN);
激活Tool,单击地图,获取当前点生成缓冲区。