有的时候,特别是在做分析时,需要经常添加新的图层到当前的地图中。在这个时候,我们一般采用非池化的Server Object,以保证当前用户的修改不会影响到其他用户。添加图层的代码如下:
首选,得到Resource和mapFunctionality和MapServerInfo,以备后面使用:
- AGSLocalMapResource gisrec=(AGSLocalMapResource)webContext.getResources().get( "ags1" );
- AGSMapFunctionalitymapfunction=(AGSMapFunctionality)gisrec.getFunctionalities().get( "map" );
- MapServerPortmapServer=gisrec.getMapServer();
- MapServerInfoserverInfo=mapServer.getServerInfo(mapServer.getDefaultMapName());
AGSLocalMapResource gisrec=(AGSLocalMapResource)webContext.getResources().get("ags1"); AGSMapFunctionalitymapfunction=(AGSMapFunctionality)gisrec.getFunctionalities().get("map"); MapServerPortmapServer=gisrec.getMapServer(); MapServerInfoserverInfo=mapServer.getServerInfo(mapServer.getDefaultMapName());
得到MapServer的IMap接口
- IMaptargetMap=gisrec.getLocalMapServer().getMap(gisrec.getLocalMapServer().getDefaultMapName());
IMaptargetMap=gisrec.getLocalMapServer().getMap(gisrec.getLocalMapServer().getDefaultMapName());
IMap接口可以用来添加数据到ServerObject。下面的代码可以用来从文件数据库中读取数据并把它添加到IMap中:
- IWorkspacewk= this .connectToFileDB( "D:\\data\\sapdata.gdb" ,gisrec.getServerContext());
- IFeatureClassfc= this .getFeatureclass(wk, "MSA_Intersect" );
- FeatureLayerflayer=(FeatureLayer)gisrec.getServerContext().createObject(FeatureLayer.getClsid());
- flayer.setFeatureClassByRef(fc);
- targetMap.addLayer(flayer);
IWorkspacewk=this.connectToFileDB("D:\\data\\sapdata.gdb",gisrec.getServerContext()); IFeatureClassfc=this.getFeatureclass(wk,"MSA_Intersect"); FeatureLayerflayer=(FeatureLayer)gisrec.getServerContext().createObject(FeatureLayer.getClsid()); flayer.setFeatureClassByRef(fc); targetMap.addLayer(flayer);
里面用到的两个function的代码如下:
- public IWorkspace connectToFileDB(StringfileGDB,IServerContext sc) throws java.net.UnknownHostException,java.io.IOException{
- com.esri.arcgis.datasourcesGDB.FileGDBWorkspaceFactoryfactory = (FileGDBWorkspaceFactory)sc.createObject(FileGDBWorkspaceFactory.getClsid());
- com.esri.arcgis.geodatabase.IWorkspaceworkspace = factory.openFromFile(fileGDB, 0 );
- return workspace;
- }
- publicIFeatureClass getFeatureclass(IWorkspace workspace,String fName) throwsjava.net.UnknownHostException, java.io.IOException{
- com.esri.arcgis.geodatabase.IEnumDatasetenumDataset =workspace.getDatasets(com.esri.arcgis.geodatabase.esriDatasetType.esriDTFeatureClass);
- com.esri.arcgis.geodatabase.IDatasetds = null ;
- ds= enumDataset.next();
- while (ds!= null ){
- if (ds.getName().endsWith(fName)){
- return
- new IFeatureClassProxy(ds);
- }
- ds= enumDataset.next();
- }
- retur nnull;
- }
public IWorkspace connectToFileDB(StringfileGDB,IServerContext sc) throws java.net.UnknownHostException,java.io.IOException{ com.esri.arcgis.datasourcesGDB.FileGDBWorkspaceFactoryfactory = (FileGDBWorkspaceFactory)sc.createObject(FileGDBWorkspaceFactory.getClsid()); com.esri.arcgis.geodatabase.IWorkspaceworkspace = factory.openFromFile(fileGDB,0); return workspace; } publicIFeatureClass getFeatureclass(IWorkspace workspace,String fName) throwsjava.net.UnknownHostException, java.io.IOException{ com.esri.arcgis.geodatabase.IEnumDatasetenumDataset =workspace.getDatasets(com.esri.arcgis.geodatabase.esriDatasetType.esriDTFeatureClass); com.esri.arcgis.geodatabase.IDatasetds = null; ds= enumDataset.next(); while(ds!= null){ if(ds.getName().endsWith(fName)){ return new IFeatureClassProxy(ds); } ds= enumDataset.next(); } retur nnull; }
添加图层到ServerObject后,调用下面的代码,让修改生效。调用下面的代码后,将会使ServerObject的状态发生改变。
gisrec.getLocalMapServer().refreshServerObjects();
Server Object状态改变后,需要重新得到serverInfo,并且设置MapFunctionality里面的LayerDescription,把新的layer加入进去。这里我们就不手工添加,而是直接把整个MapDescription设置一遍,如下面的代码所示:
- serverInfo=mapServer.getServerInfo(mapServer.getDefaultMapName());
- mapfunction.getMapServerInfo().setDefaultMapDescription(serverInfo.getDefaultMapDescription());
- mapfunction.setMapDescription(serverInfo.getDefaultMapDescription());
serverInfo=mapServer.getServerInfo(mapServer.getDefaultMapName()); mapfunction.getMapServerInfo().setDefaultMapDescription(serverInfo.getDefaultMapDescription()); mapfunction.setMapDescription(serverInfo.getDefaultMapDescription());
最后,当然要加上一句refresh context的代码:
- webContext.refresh();
webContext.refresh();
把此代码放在一个tool或者command中运行即可。这应该是最简洁明了的添加一个图层的代码了。