ArcGIS 发布服务并在API4.x中调用实现最短路径
环境:Win10
软件版本:ArcGIS Desktop10.7 Enterprise10.7
目的:自己尝试发布一个服务,用来实现最短路径分析操作。
数据:某市路网图
参考:ArcGIS API for JavaScript 4.12 Demo——RouteTask
操作步骤
1.在ArcMap中添加矢量格式的路网数据。
2.为此数据建立网络数据集 注意:我的连通性选的是Any Vertex(任意节点),老师在讲网络分析的时候讲过,不细说了。
3.打开Network模块和Network工具条。
4.在工具条中点击New Route,此处可建立stop(停靠点)测试一下最短路径的效果。
5.File-->Share as-->Service,点击Publish a Service,之后确定服务的名称和存储文件夹。
6.进入Service Editor,在Capabilities选项卡中打开Network Analyst
7.点击分析,一般会有十几个警告,只要不报错就没事,之后发布。发布过程中会跳出一个确定复制数据的窗口,点击ok后继续发布。
8.发布成功会弹出窗口提示“服务已成功发布” 我们就可以在ArcGIS server Manager中查看。
9.在ArcGIS中找到GIS Servers在“发布者”中找到你发布的服务,右键服务属性,进入Service Editor
10.在Service Editor中打到Network Analyst选项卡,将REST URL复制下来。在浏览器中打开即可查看发布信息的属性内容。
我复制下来的REST URL是:
https://localhost:6443/arcgis/rest/services/NT/Net720/MapServer/NAServer
在API中调用
拿API4.12中的RouteTask为例(3版本API估计调用方式大差不离,因为4版本是未来的趋势,所以3版本自己看看吧)。
我们要调用的RouteTask url需要将上文中的REST URL做一定的调整方可调用成功。
需要调整的地方
- 将localhost改正为你的计算机机器名或IP,计算机的机器名可以在Server中站点-->计算机中找到
- 将上文中的REST URL中的MapServer删除
- 将上文中的REST URL中NAServer后加上你建立的最短路径图层的名字,若不做改动在10.7英文版中为“Route”,大小写没有影响。
所以,经过调整之后的URL为:
https://我的机器名:6443/arcgis/rest/services/NT/Net720/NAServer/Route
在此附上API4.12版本的RouteTask
(别想直接用,改改你的JS和CSS引用路径先!)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /> <title>RouteTask - 4.12</title> <style> html, body, #viewDiv { padding: 0; margin: 0; height: 100%; width: 100%; } #paneDiv { position: absolute; top: 10px; left: 62px; padding: 0 12px 0 12px; background-color: rgba(0, 0, 0, 0.5); color: white; } </style> <link rel="stylesheet" href="http://localhost/arcgis_js_api/library/4.12/esri/css/main.css" /> <script src="http://localhost/arcgis_js_api/library/4.12/init.js"></script> <script> //官方demo require([ "esri/Map", "esri/views/MapView", "esri/Graphic", "esri/layers/GraphicsLayer", "esri/tasks/RouteTask", "esri/tasks/support/RouteParameters", "esri/tasks/support/FeatureSet", // "esri/config" ], function( Map, MapView, Graphic, GraphicsLayer, RouteTask, RouteParameters, FeatureSet //esriConfig ) { // esriConfig.portalUrl = "https://trail.arcgisonline.cn/portal"; // Point the URL to a valid route service that uses an // ArcGIS Online hosted service proxy var routeTask = new RouteTask({ url: "https://desktop-7gg1hoi:6443/arcgis/rest/services/NT/Net720/NAServer/Route" // "https://desktop-7gg1hoi:6443/arcgis/rest/services/NT/lcnet/NAServer/Route" //GIS is not a simply tool but a way to exploring the unknown. }); // The stops and route result will be stored in this layer var routeLayer = new GraphicsLayer(); // Setup the route parameters var routeParams = new RouteParameters({ stops: new FeatureSet(), outSpatialReference: { wkid: 3857 } }); // Define the symbology used to display the stops var stopSymbol = { type: "simple-marker", // autocasts as new SimpleMarkerSymbol() style: "cross", size: 15, outline: { // autocasts as new SimpleLineSymbol() width: 4 } }; // Define the symbology used to display the route var routeSymbol = { type: "simple-line", // autocasts as SimpleLineSymbol() color: [0, 0, 255, 0.5], width: 5 }; var map = new Map({ basemap: "streets", layers: [routeLayer] // Add the route layer to the map }); var view = new MapView({ container: "viewDiv", // Reference to the scene div created in step 5 map: map, // Reference to the map object created before the scene center: [115.976491,36.46233], zoom: 14 }); // Adds a graphic when the user clicks the map. If 2 or more points exist, route is solved. view.on("click", addStop); function addStop(event) { // Add a point at the location of the map click var stop = new Graphic({ geometry: event.mapPoint, symbol: stopSymbol }); routeLayer.add(stop); // Execute the route task if 2 or more stops are input routeParams.stops.features.push(stop); if (routeParams.stops.features.length >= 2) { routeTask.solve(routeParams).then(showRoute); } } // Adds the solved route to the map as a graphic function showRoute(data) { var routeResult = data.routeResults[0].route; routeResult.symbol = routeSymbol; routeLayer.add(routeResult); } }); </script> </head> <body> <div id="viewDiv"></div> <div id="paneDiv" class="esri-widget"> <div> <p> Click on the map to add stops to the route. The route from the last stop to the newly added stop is calculated. If a stop is not reachable, it is removed and the last valid point is set as the starting point. </p> </div> </div> </body> </html>
本文完。
看完如果对您有帮助麻烦您点个赞谢谢。