概述:
本文解说怎样通过Arcgis Server REST 的导出地图(Export)接口。实如今OL2中直接以WMS的方式调用Arcgis Server REST服务。
实现思路:
1、rest的export接口介绍
服务支持导出地图
导出參数
Parameter | Details |
---|---|
f | Description: The response format. The default response format is html. If the format is image, the image bytes are directly streamed to the client. Values: html | json | image | kmz |
bbox | Description: (Required) The extent (bounding box) of the exported image. Unless the bboxSR parameter has been specified, the bbox is assumed to be in the spatial reference of the map. Syntax: <xmin>, <ymin>, <xmax>, <ymax> Example: bbox=-104,35.6,-94.32,41 The bbox coordinates should always use a period as the decimal separator even in countries where traditionally a comma is used. |
size | Description: The size width *height) of the exported image in pixels. If the size is not specified, an image with a default size of 400 * 400 will be exported. Syntax: <width>, <height> Example: size=600,550 |
dpi | Description: The device resolution of the exported image (dots per inch). If the dpi is not specified, an image with a default DPI of 96 will be exported. Example: dpi=200 |
imageSR | Description: The spatial reference of the exported image. The spatial reference can be specified as either a well-known ID or as a spatial reference json object. If the imageSR is not specified, the image will be exported in the spatial reference of the map. |
bboxSR | Description: The spatial reference of the bbox. The spatial reference can be specified as either a well-known ID or as a spatial reference json object. If the bboxSR is not specified, the bbox is assumed to be in the spatial reference of the map. |
format | Description: The format of the exported image. The default format is .png. Values: png | png8 | png24 | jpg | pdf | bmp | gif | svg | svgz | emf | ps | png32 Note: Support for the png32 format was added at 9.3.1. This format is only available formap services whose supportedImageFormatTypes property includes PNG32. |
layerDefs | Description: Allows you to filter the features of individual layers in the exported map by specifying definition expressions for those layers. Definition expression for a layer that is published with the service will be always honored. Simple Syntax: Syntax: layerId1:layerDef1;layerId2:layerDef2 Where layerId1, layerId2 are the layer ids returned by the map service resource. Example: 0:POP2000 > 1000000;5:AREA > 100000 JSON Syntax (new in 10.0): You can also use a JSON representation for Layer Definitions. Syntax: { "<layerId1>" : "<layerDef1>" , "<layerId2>" : "<layerDef2>" } Where layerId1, layerId2 are the layer ids returned by the map service resource. Example: {"0":"POP2000 > 1000000","5":"AREA > 100000"} |
layers | Description: Determines which layers appear on the exported map. There are four ways to specify which layers are shown:
Syntax: [show | hide | include | exclude]:layerId1,layerId2 Where layerId1, layerId2 are the layer ids returned by the map service resource. Example: layers=show:2,4,7 |
transparent | Description: If true, the image will be exported with the background color of the map set as its transparent color. The default is false. Only the .png and .gif formats support transparency. Internet Explorer 6 does not display transparency correctly for png24 image formats. Values: true | false |
time | Description: The time instant or time extent of the exported map image. Time instant: Syntax: time=<timeInstant> Example: time=1199145600000 (1 Jan 2008 00:00:00 GMT) Time extent: Syntax: time=<startTime>, <endTime> Example: time=1199145600000, 1230768000000 (1 Jan 2008 00:00:00 GMT to 1 Jan 2009 00:00:00 GMT) A null value specified for start time or end time will represent infinity for start or end time respectively. |
layerTimeOptions | Description: The time options per layer. Users can indicate whether or not the layer should use the time extent specified by the time parameter or not, whether to draw the layer features cumulatively or not and the time offsets for the layer. Syntax: Example: |
dynamicLayers | //This option was added at 10.1. Description: Use dynamicLayers parameter to modify the layer drawing order, change layer drawing info, and change layer data source version for this request. New layers (dataLayer) can also be added to the dynamicLayers based on the map service registered workspaces. The order of dynamicLayers array defines the layer drawing order. The first element of the dynamicLayers array draws on top of all other layers. Note:
Syntax: Example: |
gdbVersion | //This option was added at 10.1. Description: Use this parameter to specify the geodatabase version. Syntax: gdbVersion=<geodatabase version> Example: gdbVersion=sde.USER1 |
mapScale | //This option was added at 10.1. Description: Use this parameter to export a map image at a specific scale, with the map centered around the center of the specified bounding box (bbox). Syntax: mapScale=<scale> Examples: mapScale=5000000, mapScale=5E6 |
请求实例:
http://localhost:6080/arcgis/rest/services/china/MapServer/export?bbox=66.02425609744357%2C34.94598754534843%2C141.47696459232174%2C57.860197031494025&bboxSR=&layers=show%3A1&layerDefs=&size=&imageSR=&format=png&transparent=true&dpi=&time=&layerTimeOptions=&dynamicLayers=&gdbVersion=&mapScale=&f=image
返回结果:
必须參数:
a、layers——展示的图层
b、bbox——四至范围
c、width——宽度
d、height——高度
2、返回图片
本文通过简单的servlet实现获取网络图片并返回前台的方式。
代码:
1、servlet代码
package com.lzugis.web; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; @WebServlet(description = "rest to wms", urlPatterns = {"/wms"}) public class WmsServices extends HttpServlet { private static final long serialVersionUID = 1L; private static String baseUrl = "http://localhost:6080/arcgis/rest/services/china/MapServer/export?"; private static String basePara = "&format=png&transparent=true&f=image"; public WmsServices() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String encoding = getInitParameter("encoding"); if ((encoding != null) && (!"".equals(encoding))) { request.setCharacterEncoding(encoding); } String time = request.getParameter("t"); baseUrl = baseUrl+"t="+time; String layers = request.getParameter("LAYERS"); String bbox= request.getParameter("BBOX"); String width= request.getParameter("WIDTH"); String height= request.getParameter("HEIGHT"); String wmsUrl = baseUrl+"&bbox="+bbox+"&layers=show:"+layers+"&size="+width+","+height+basePara; try { showImage(response,wmsUrl); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void showImage(HttpServletResponse response,String src) throws Exception { response.setContentType("text/html; charset=UTF-8"); response.setContentType("image/png"); //new一个URL对象 URL url = new URL(src); //打开链接 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //设置请求方式为"GET" conn.setRequestMethod("GET"); //超时响应时间为5秒 conn.setConnectTimeout(5 * 1000); //通过输入流获取图片数据 InputStream inStream = conn.getInputStream(); OutputStream os = response.getOutputStream(); try { int count = 0; byte[] buffer = new byte[1024 * 1024]; while ((count = inStream.read(buffer)) != -1){ os.write(buffer, 0, count); } os.flush(); } catch (IOException e) { e.printStackTrace(); } finally { os.close(); inStream.close(); } } }
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>openlayers map</title> <link rel="stylesheet" href="../../../plugin/OpenLayers-2.13.1/theme/default/style.css" type="text/css"> <style> html, body, #map{ padding:0; margin:0; height:100%; 100%; overflow: hidden; } </style> <script src="../../../plugin/OpenLayers-2.13.1/OpenLayers.js"></script> <script src="../../../plugin/jquery/jquery-1.8.3.js"></script> <script> var map; var tiled; OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5; OpenLayers.DOTS_PER_INCH = 25.4 / 0.28; $(window).load(function() { var format = 'image/png'; var bounds = new OpenLayers.Bounds( 73.45100463562233, 18.16324718764174, 134.97679764650596, 53.531943152223576 ); var options = { controls: [], maxExtent: bounds, maxResolution: 0.2403351289487642, projection: "EPSG:4326", units: 'degrees' }; map = new OpenLayers.Map('map', options); var time = new Date(); time = time.getTime(); var url = "http://localhost:8081/lzugis/wms?t="+time; tiled = new OpenLayers.Layer.WMS( "Geoserver layers - Tiled", url, { "LAYERS": '1,6', "STYLES": '', format: format }, { buffer: 0, displayOutsideMaxExtent: true, isBaseLayer: true, yx : {'EPSG:4326' : true} } ); map.addLayers([tiled]); OpenLayers.INCHES_PER_UNIT["千米"] = OpenLayers.INCHES_PER_UNIT["km"]; OpenLayers.INCHES_PER_UNIT["米"] = OpenLayers.INCHES_PER_UNIT["m"]; OpenLayers.INCHES_PER_UNIT["英里"] = OpenLayers.INCHES_PER_UNIT["mi"]; OpenLayers.INCHES_PER_UNIT["英寸"] = OpenLayers.INCHES_PER_UNIT["ft"]; //比例尺 map.addControl(new OpenLayers.Control.ScaleLine({topOutUnits:"千米",topInUnits:"米",bottomOutUnits:"英里", bottomInUnits:"英寸" })); map.addControl(new OpenLayers.Control.Zoom()); map.addControl(new OpenLayers.Control.Navigation()); map.addControl(new OpenLayers.Control.OverviewMap()); map.zoomToExtent(bounds); }); </script> </head> <body> <div id="map"></div> </body> </html>
实现后效果: