ViewShed
此示例展示的是如何运用一个GP服务获取所需的结果,下面我们来看一下如何使用一个GP服务,代码如下:
public void start(Point mappoint) { // 第一个参数设定一个监测点 GPFeatureRecordSetLayer gpf = new GPFeatureRecordSetLayer("Input_Observation_Point"); gpf.setSpatialReference(map.getSpatialReference()); gpf.setGeometryType(Geometry.Type.Point); // Add the point selected by the user Graphic f = new Graphic(mappoint,new SimpleMarkerSymbol(Color.RED,25,STYLE.DIAMOND)); gpf.addGraphic(f);
// 第二个参数,设置可视区域 GPLinearUnit gpl = new GPLinearUnit("Viewshed_Distance"); gpl.setUnits("esriMeters"); gpl.setDistance(8046.72);
// Add params params = new ArrayList<GPParameter>(); params.add(gpf); params.add(gpl);
try { dialog = ProgressDialog.show(Viewshed.this, "", "Loading. Please wait...", true, true); new ViewShedQuery().execute(params); cancelViewShed = new Timer(); cancelViewShed.schedule(new TimerTask() {
@Override public void run() { uiHandler.sendEmptyMessage(CANCEL_LOADING_WINDOW); } }, 60000); } catch (Exception e) { e.printStackTrace(); } } |
上面代码中我们可以看出在start()中我们主要是构造GP服务所需要的参数,将参数传给Geoprocessor对象进行操作,执行Geoprocessor对象的execute()方法,返回所需的结果对象GPResultResource并且调用它的getOutputParameters()返回GPParameter数组对象,在GPParameter装有GP服务处理后所需的要素集,此时我们可以遍历GPGarameter数组提取要素进行渲染,具体代码如下:
class ViewShedQuery extends AsyncTask<ArrayList<GPParameter>, Void, GPParameter[]> {
GPParameter[] outParams = null;
@Override protected void onPostExecute(GPParameter[] result) { if (outParams == null) return; for (int i = 0; i < outParams.length; i++) { if (outParams[i] instanceof GPFeatureRecordSetLayer) {
GPFeatureRecordSetLayer fsl = (GPFeatureRecordSetLayer) outParams[i]; for (Graphic feature : fsl.getGraphics()) { Graphic g = new Graphic(feature.getGeometry(),new SimpleFillSymbol(Color.CYAN)); gLayer.addGraphic(g); } } } uiHandler.sendEmptyMessage(CLOSE_LOADING_WINDOW); }
@Override protected GPParameter[] doInBackground(ArrayList<GPParameter>... params1) {
gp = new Geoprocessor("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/GPServer/Viewshed"); gp.setOutSR(map.getSpatialReference().getID()); try { GPResultResource rr = gp.execute(params1[0]); outParams = rr.getOutputParameters(); } catch (Exception e) { e.printStackTrace(); } return outParams; } } |
通过代码我们可以看出,编写一个GP服务示例不是很麻烦,麻烦的地方是如何构建一个所需的服务,精华之处都在于GP服务的搭建中,在此不过多介绍怎样发布GP服务,有兴趣的可以查看相关帮助文档。