tool执行的过程需要与地图交互,而后再执行命令,从下面调用语句可以看出。
<a:tool id="countFeature" defaultImage="images/selection.gif"
hoverImage="images/ selection.gif"
selectedImage="images/ selectionD.gif"
clientAction="EsriMapRectangle"
serverAction="com.demo.MyCountFeatureTool"
clientPostBack="true" />
其中,clientAction可参考com.esri.adf.web.faces.event中ClientActions接口,客户端支持的操作类型包括MAP_CIRCLE,MAP_CONTINUOUS_PAN,MAP_LINE,MAP_OVAL,MAP_PAN,MAP_POINT,MAP_POLYGON,MAP_POLYLINE,MAP_RECTANGLE
指定clientAction后,我们需要编写serverAction对应的操作类。此处实现获取选择区域选中元素个数功能。
MyCountFeatureTool
import java.rmi.RemoteException;
import com.esri.adf.web.ags.ADFAGSException;
import com.esri.adf.web.ags.data.AGSMapResource;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.faces.event.MapEvent;
import com.esri.adf.web.faces.event.MapToolAction;
import com.esri.arcgisws.EnvelopeN;
import com.esri.arcgisws.EsriSearchOrder;
import com.esri.arcgisws.EsriSpatialRelEnum;
import com.esri.arcgisws.MapServerPort;
import com.esri.arcgisws.SpatialFilter;
/**
* 计算选择区内选中元素个数
* 通过axis调用远程webservice的方式去调用服务器端的方法实现
*/
public class MyCountFeatureTool implements MapToolAction {
WebContext context = null;
int countedFeatures;
private void countFeatures(WebExtent extent) {
AGSMapResource agsMap = ((AGSMapResource) context.getResources().get("agsws1"));
MapServerPort mapServer = agsMap.getMapServer();
// Make a new envelope from the web extent
EnvelopeN env = new EnvelopeN(extent.getMinX(), extent.getMinY(),
extent.getMaxX(), extent.getMaxY(), null, null, null, null,null);
// Setup a spatial filter for an Intersection relationship
SpatialFilter spatialFilter = new SpatialFilter();
spatialFilter.setSpatialRel(EsriSpatialRelEnum.esriSpatialRelIntersects);
spatialFilter.setWhereClause("");
spatialFilter.setSearchOrder(EsriSearchOrder.esriSearchOrderSpatial);
spatialFilter.setSpatialRelDescription("");
spatialFilter.setGeometryFieldName("");
// Set the envelope as the geometry
spatialFilter.setFilterGeometry(env);
// MapServer::queryFeatureCount() executes on the server and can throw a
// RemoteException
try {
// Count features in 4th layer which intersect with the envelope
int layerId = 1;
this.countedFeatures = mapServer.queryFeatureCount(mapServer
.getDefaultMapName(), layerId, spatialFilter);
System.out.println("你选择了 " + countedFeatures + " 要素");
} catch (RemoteException rme) {
throw new ADFAGSException(
"Could not execute MapServer::queryFeatureCount()", rme);
}
}
public void execute(MapEvent arg0) throws Exception {
try {
this.context = arg0.getWebContext();
WebExtent ex = (WebExtent) arg0.getWebGeometry();
ex = (WebExtent) ex.toMapGeometry(arg0.getWebContext().getWebMap());
this.countFeatures(ex);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
MyCountFeatureTool2
import java.util.List;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.geometry.WebGeometry;
import com.esri.adf.web.data.query.IdentifyCriteria;
import com.esri.adf.web.data.query.QueryResult;
import com.esri.adf.web.data.query.WebQuery;
import com.esri.adf.web.faces.event.MapEvent;
import com.esri.adf.web.faces.event.MapToolAction;
/**
* 使用WebQuery进行查询
*/
public class MyCountFeatureTool2 implements MapToolAction{
private static final long serialVersionUID = 1L;
public void execute(MapEvent event) {
//得到客户端绘制图形
WebContext ctx = event.getWebContext();
WebGeometry screenGeo = event.getWebGeometry();
WebGeometry mapGeo = screenGeo.toMapGeometry(ctx.getWebMap());
IdentifyCriteria ic = new IdentifyCriteria(mapGeo);
WebQuery query = (WebQuery)ctx.getAttribute("query");
List<QueryResult> results=query.query(ic,query.getQueryLayers());
System.out.println(results.size());
}
}
WebQuery is a WebContextAttribute class used in a WebContext for querying attribute data and displaying related geometry objects spatially.
It is generally defined in the faces-config.xml of web applications. The following shows how it can be added as an attribute of the WebContext.
<managed-bean>
<managed-bean-name>mapContext</managed-bean-name>
<managed-bean-class>com.esri.adf.web.data.WebContext</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>attributes</property-name>
<map-entries>
<map-entry>
<key>query</key>
<value>#{webappQuery}</value>
</map-entry>
...
Below is the declaration of a WebQuery managed bean. Note the LayerDefinition managed beans being referenced below.
<managed-bean>
<managed-bean-name>webappQuery</managed-bean-name>
<managed-bean-class>com.esri.adf.web.data.query.WebQuery</managed-bean-class>
<managed-bean-scope>none</managed-bean-scope>
<managed-property>
<property-name>layerDefinitions</property-name>
<list-entries>
<value>#{arcgis_localmapgrid2_usa_2d_v_layer1}</value>
....
</list-entries>
</managed-property>
</managed-bean>
参考:Server Java讲座--自定义Tools开发