ArcGIS Runtime SDK for Android开发之调用GP服务(异步调用)

一、背景说明

通过调用GP服务,Android客户端也能实现专业的、复杂的GIS分析处理功能,从而增加应用的实用价值。

ArcGIS Server发布的GP服务,分为同步和异步两种类型,一般执行步骤较多,耗时较长的GP服务都采用异步方式,执行速度较快的服务则采用同步方式。如果不确定GP服务是同步还是异步方式,则可以通过服务目录查看,如下图:


本文将介绍如何通过异步的方式来调用GP服务(同步方式在SDK的示例程序中有,较为简单),本例调用的GP服务来自ArcGIS Online,服务地址为:http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/Incident_Data_Extraction/GPServer/Extract%20Data%20Task。该服务相关的业务数据服务为:http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/Incident_Data_Extraction/MapServer。该GP服务提供数据提取的功能,就是根据用户选择的待提取图层、提取范围、输出格式,将数据提取,并打包返回客户端,由客户下载zip压缩包。先来看一下GP服务的参数要求:


可见该服务提供了三个输入参数,分别是:待提取的图层、提取范围和输出格式,此外还有一个输出参数,用于获取提取结果。


二、示例效果

本例的执行效果如下:

1.底图与待提取的业务数据:


2.绘制用于提取的多边形范围:


3.提交提取任务,开始执行提取操作:


4.提取任务执行完成,获得了提取结果的URL(图片上方),点击可下载:


5.完成下载提取结果:


6.到下载目录中查看下载结果,output-1.zip:



三、关键代码

1.设置参数

 

	public void startAsynchronous(Polygon aoiGeometry){
		//被裁切的目标图层
		GPString pointLayer = new GPString();
		pointLayer.setValue("Incident Points");		
		GPString lineLayer = new GPString();
		pointLayer.setValue("Incident Lines");		
		GPString polygonLayer = new GPString();
		pointLayer.setValue("Incident Areas");
		
		GPMultiValue<GPString> clipLayers = new GPMultiValue<GPString>("Layers_to_Clip");
		clipLayers.addValue(pointLayer);
		clipLayers.addValue(lineLayer);
		clipLayers.addValue(polygonLayer);
		
		//用于裁切的范围
		Graphic g = new Graphic(aoiGeometry, fillSymbol);
		GPFeatureRecordSetLayer aoi = new GPFeatureRecordSetLayer("Area_of_Interest");
		aoi.setGeometryType(Geometry.Type.POLYGON);
		aoi.addGraphic(g);
		
		//返回的数据格式
		GPString format = new GPString("Feature_Format");
		format.setValue("Shapefile - SHP - .shp");
		
		params2 = new ArrayList<GPParameter>();
		params2.add(clipLayers);
		params2.add(aoi);
		params2.add(format);
		
		try {
			dialog = ProgressDialog.show(Viewshed.this, "", "Extacting. Please wait...", true, true);
			
			gpExtract = new Geoprocessor("http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/Incident_Data_Extraction/GPServer/Extract%20Data%20Task");
			gpExtract.setOutSR(map.getSpatialReference());
			gpExtract.setProcessSR(map.getSpatialReference());
				
			ExtractRunable queryRunable = new ExtractRunable();
			Thread thread = new Thread(queryRunable);
			thread.start();			
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}


2.提交任务,开始异步执行

 

 

    class ExtractRunable implements Runnable {
		@Override
		public void run() {
			GPJobResource jobResource = null;
			try {
				jobResource = gpExtract.submitJob(params2);
			} 
			catch (Exception e1) {				
				e1.printStackTrace();
			}
			jobID = jobResource.getJobID();
			
		}
    }


3.通过Timer定时查询任务状态并处理结果

 

 

    class ExtractRunable implements Runnable {
		@Override
		public void run() {
			GPJobResource jobResource = null;
			try {
				jobResource = gpExtract.submitJob(params2);
			} 
			catch (Exception e1) {				
				e1.printStackTrace();
			}
			jobID = jobResource.getJobID();
			
			//通过定时器每隔2秒查询Job状态
			checkJobStatusTimer = new Timer();
			checkJobStatusTimer.schedule(new TimerTask() {
				@Override
				public void run() {
					GPJobResource jobRes = null;
					try {
						jobRes = gpExtract.checkJobStatus(jobID);
					} 
					catch (Exception e) {						
						e.printStackTrace();
					}
					if(jobRes == null) return;
					JobStatus status = jobRes.getJobStatus();
					
					switch(status){
					case CANCELLED:
						Toast.makeText(Viewshed.this, "CANCELLED", Toast.LENGTH_LONG).show();
						break;
					case DELETED:
						Toast.makeText(Viewshed.this, "DELETED", Toast.LENGTH_LONG).show();
						break;
					case FAILED:
						GPMessage[] message = jobRes.getMessages();
						Toast.makeText(Viewshed.this, "FAILED:"+message[0].toString(), Toast.LENGTH_LONG).show();
						break;
					case SUCCEEDED:
						try {
							GPDataFile result = (GPDataFile)gpExtract.getResultData(jobID, "Output_Zip_File");
							url = result.getUrl();
							uiHandler.sendEmptyMessage(CLOSE_LOADING_WINDOW);							
						} 
						catch (Exception e) {
							e.printStackTrace();
						}
						
						break;
					case TIMED_OUT:
						Toast.makeText(Viewshed.this, "TIMED_OUT", Toast.LENGTH_LONG).show();
						break;
					default:						
						break;
					}
				}
			}, 1000, 2000);
		}
    }


以上是本例的关键代码,如需完整代码,请在评论中留下邮箱。

 


 

posted @ 2013-07-03 20:46  爱生活,爱编程  阅读(707)  评论(0编辑  收藏  举报