您的位置:首页 > 其它

基于ArcGIS API for WPF路径分析源码实例

2015-04-14 16:16 155 查看
说明:

本实例主要演示网络分析数据集制作,服务发布,最后基于ArcGIS API for WPF做路径分析。

本实例参考ArcGIS官方文档,想了解GIS网络分析可查阅官方文档。

本实例数据为西藏道路数据,若无数据可新建简单的线要素。

本实例使用软件及版本:ArcGIS10.2,ArcGIS APIfor WPF24,VS2010。

最后为了增加效果叠加局部离线卫星地图,需要安装水经注万能地图下载器(相当方便的卫星地图下载器),如果没有安装本软件,可以百度“水经注软件”到官方网站下载。

一、 准备网络分析数据

新建一个“Test”数据库,用于储存永久的服务数据,一般我们将地理要素及要素集存放在地里数据库中。这里我们新建了一个名叫“NetworkFeatureDataSet”的要素集,这是因为存放于地理数据库中的线要素如果用于构建网络数据集,则必须放在要素集中。



新建各种要素(表示设施的点要素,以及表示道路的线要素),并添加相关的属性。

新建线要素属性如下图所示



新建点要素(其实可以省略)。

编辑要素,构建地图。此处直接导入西藏道路数据,如下图所示



注意:道路数据是联通的,需要把相交的线要素进行打断处理。这里使用高级编辑工具(在10.2之前的版本应该是拓扑工具条里),如下图



完成以上内容之后,就开始构建网络数据集(构建网络数据集的线要素必须位于要素数据集中)

右键要素集,选择“新建”“网络数据集”



点击将如下图所示



设置网络数据集的名称,点击下一步。



选择参与网络数据集的数据类,点击下一步



构建拐弯模型,选是,下一步



设置连通性,这里有节点和端点两种,默认情况下为端点,这里我们选择默认即可,然后点击下一步。



设置高程字段,当涉及到高程时,在此设置,本案例没有涉及,所以选择无。然后点击下一步。



设置网络数据集的属性,我们指定了如上所示的属性。具体的添加过程:

点击添加,然后设置名称,用法和单位,然后点击赋值器,可以指定该属性是字段,函数,常量以及VB脚本。

这里我们设置“长度”属性为字段,对应线要素的“Shape_Length”属性



以上属性设置好以后,点击下一步,设置网络方向属性。



这里我们设置长度属性为Distance,如上图所示,在街道名称中设置名称为NAME字段。

点击完成,如果过程没有错误,就完成了自己的网络数据集。这是要素集的内容如下



接下来验证生产的网络数据集。

点击自定义调出网络分析工具条:



选择停靠点,并求解,如下图所示



生成的方向指南



至此,数据准备完毕,接下来发布网络分析服务。

二、 发布网络分析服务

新建MXD文档。文档必须包含用于最短路径分析图层、网络数据集“Network_ND”。地图文档可以增加其它要素,来增加显示效果。

创建最短路径分析图层,可以通过ArcToolBox新建,如下图



此实例只有两个图层(不需要的两个图层),如下图



所需图层准备好之后,保存MXD文档,然后发布。

我们可以直接在ArcMap中发布也可在ArcCatalog中发布。右键MXD地图文档——发布到ArcGIS Server。











发布成功之后,可以在本地的浏览器中打开该网络分析服务的地址.即可查看该网络分析服务的参数和相关功能。



括号为NAServer就表示该服务为网络分析服务。打开可以查看该服务的具体信息。



到此网络分析服务的发布工作已经完成。

三、 准备卫星地图数据及发布影像服务

启动水经注万能地图下载器,在查询定位中输入“拉萨”,再双击搜索出的行政边框内的区域,弹出对话框,设置相应属性。





在级别列表中,你可以选择任何级别,但一般来讲城区选择19级,郊区选择16级比理想,但选择级别之前,最好在在线地图浏览中先查看所想要下载的级别是否有数据,这里我们以18级为例。选择好级别后,点击确定开始下载。

下载完成后会弹出对话框询问是否要立即导出,选择“是”,然后显示“导出图片数据”对话框,如下图所示。



在该对话框中,不需要作任何设置,点击“输出”按钮以默认参数导出数据即可。默认导出路径为“D:\SGDownload\未命名_拼接[默认]\L18”。

在ArcMap中导入影像数据显示如图



注意:投影坐标系需要保持一致,才能进行叠加显示。

查看刚刚下载影像的坐标系为“WGS_1984_Web_Mercator”,与之前发布的网络服务坐标系“WGS_1984_World_Mercator”不一致,因此我们需要先转换投影坐标系,再发布影像服务。



投影坐标一致后,我们可以在ArcMap中看到影像和网络数据叠加在一起了。



接下来就是发布影像服务。



此处与网络数据发布步骤一致,就不在此累述了。发布成功后可以在manager中看到刚刚发布的影像服务。



影像发布完成。

四、 基于ArcGISAPI for WPF最短路径分析

最终效果图如下



路径分析服务可以为WPFWEBGIS提供最佳路径的选择功能,用户指定两个点便可以查询出两点之间的最佳路径,同时用户还可以考虑不同的因素来找到最佳的路径,例如设置障碍点,阻抗等。使用路径分析功能时需要使用ArcGIS Api for Wpf中的TouteTask类。同时还需提供网络分析服务中路径分析图层的地址(即上一篇博文中我们发布的网络分析服务中路径服务的地址。其地址的一般格式是:
http://<GIS服务器地址>/ArcGIS/rest/services/<网络分析服务名称>/NAServer/Route
实现过程(参考官方教程):

/******************C#*************************/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using ESRI.ArcGIS.Client;

using ESRI.ArcGIS.Client.Tasks;

namespace demo_RoutingBarriers

{

public partial class RoutingBarriers : UserControl

{

RouteTask_routeTask;

List<Graphic> _stops = newList<Graphic>();

List<Graphic> _barriers = newList<Graphic>();

RouteParameters_routeParams = new RouteParameters();

GraphicsLayerstopsLayer = null;

GraphicsLayerbarriersLayer = null;

publicRoutingBarriers()

{

InitializeComponent();

_routeTask = new RouteTask("http://localhost:6080/arcgis/rest/services/XZNetworkMap/NAServer/RouteLayer1");

_routeTask.SolveCompleted +=routeTask_SolveCompleted;

_routeTask.Failed +=routeTask_Failed;

_routeParams.Stops = _stops;

_routeParams.Barriers = _barriers;

_routeParams.UseTimeWindows = false;

_routeParams.OutSpatialReference =MyMap.SpatialReference;

_routeParams.UseHierarchy = false;

barriersLayer = MyMap.Layers["MyBarriersGraphicsLayer"] as GraphicsLayer;

stopsLayer = MyMap.Layers["MyStopsGraphicsLayer"] as GraphicsLayer;

}

privatevoid MyMap_MouseClick(objectsender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)

{

if(StopsRadioButton.IsChecked.Value)

{

Graphicstop = new Graphic(){ Geometry = e.MapPoint, Symbol = LayoutRoot.Resources["StopSymbol"]as ESRI.ArcGIS.Client.Symbols.Symbol };

//stop.Attributes.Add("StopNumber",stopsLayer.Graphics.Count + 1);

stopsLayer.Graphics.Add(stop);

_stops.Add(stop);

}

elseif (BarriersRadioButton.IsChecked.Value)

{

Graphicbarrier = new Graphic(){ Geometry = e.MapPoint, Symbol = LayoutRoot.Resources["BarrierSymbol"]as ESRI.ArcGIS.Client.Symbols.Symbol };

barriersLayer.Graphics.Add(barrier);

_barriers.Add(barrier);

}

if(_stops.Count > 1)

{

if(_routeTask.IsBusy)

_routeTask.CancelAsync();

_routeTask.SolveAsync(_routeParams);//*********************错ä¨a误¨®

}

}

privatevoid routeTask_Failed(objectsender, TaskFailedEventArgs e)

{

string errorMessage = "Routing error: ";

errorMessage += e.Error.Message;

foreach(string detail in(e.Error as ServiceException).Details)

errorMessage += "," + detail;

MessageBox.Show(errorMessage);

if((_stops.Count) > 10)

{

stopsLayer.Graphics.RemoveAt(stopsLayer.Graphics.Count - 1);

_stops.RemoveAt(_stops.Count -1);

}

}

privatevoid routeTask_SolveCompleted(object sender, RouteEventArgse)

{

GraphicsLayerrouteLayer = MyMap.Layers["MyRouteGraphicsLayer"]as GraphicsLayer;

RouteResultrouteResult = e.RouteResults[0];

routeResult.Route.Symbol =LayoutRoot.Resources["RouteSymbol"]as ESRI.ArcGIS.Client.Symbols.Symbol;

routeLayer.Graphics.Clear();

GraphiclastRoute = routeResult.Route;

routeLayer.Graphics.Add(lastRoute);

}

privatevoid Button_Click(objectsender, RoutedEventArgs e)

{

_stops.Clear();

_barriers.Clear();

foreach(Layer layer inMyMap.Layers)

if(layer is GraphicsLayer)

(layer as GraphicsLayer).ClearGraphics();

}

}

}

//***********xaml****************//

<UserControl x:Class="demo_RoutingBarriers.RoutingBarriers"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

mc:Ignorable="d"

d

esignHeight="300" d

esignWidth="300"

xmlns:esri="http://schemas.esri.com/arcgis/client/2009">

<Grid x:Name="LayoutRoot" Background="White">

<Grid.Resources>

<ControlTemplate x:Key="CompositeSymbol">

<Grid>

<Ellipse Fill="{Binding Symbol.Color}" Width="{Binding Symbol.Size}" Height="{Binding Symbol.Size}" Stroke="Black"StrokeThickness="1" />

<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"

Text="{Binding Path=Attributes[StopNumber]}"

FontSize="9" Margin="0" FontWeight="Bold" Foreground="Black"/>

</Grid>

</ControlTemplate>

<esri:SimpleMarkerSymbol x:Key="StopSymbol" Size="20" Style="Circle" Color="Salmon" ControlTemplate="{StaticResourceCompositeSymbol}" />

<esri:SimpleMarkerSymbol x:Key="BarrierSymbol" Size="15" Style="Square" Color="Red" />

<esri:SimpleLineSymbol x:Key="RouteSymbol" Color="#990000FF" Width="5"/>

<LinearGradientBrush x:Key="

anelGradient" EndPoint="0.5,1" StartPoint="0.5,0">

<LinearGradientBrush.RelativeTransform>

<TransformGroup>

<ScaleTransform CenterY="0.5" CenterX="0.5"/>

<SkewTransform CenterY="0.5" CenterX="0.5"/>

<RotateTransform Angle="176" CenterY="0.5" CenterX="0.5"/>

<TranslateTransform/>

</TransformGroup>

</LinearGradientBrush.RelativeTransform>

<GradientStop Color="#FF145787" Offset="0.16"/>

<GradientStop Color="#FF3D7FAC" Offset="0.502"/>

<GradientStop Color="#FF88C5EF" Offset="0.984"/>

</LinearGradientBrush>

</Grid.Resources>

<esri:Map x:Name="MyMap" Background="White" Extent=""

MouseClick="MyMap_MouseClick">

<esri:Map.Layers>

<esri:ArcGISImageServiceLayer Url="
http://localhost:6080/arcgis/rest/services/LSimage/ImageServer"/>
<esri:ArcGISDynamicMapServiceLayer

Url="http://localhost:6080/arcgis/rest/services/XZNetworkMap/MapServer"/>

<esri:GraphicsLayer ID="MyRouteGraphicsLayer"/>

<esri:GraphicsLayer ID="MyStopsGraphicsLayer"/>

<esri:GraphicsLayer ID="MyBarriersGraphicsLayer"/>

</esri:Map.Layers>

</esri:Map>

<Grid HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10">

<Rectangle Fill="{StaticResourcePanelGradient}" Stroke="Gray" RadiusX="10" RadiusY="10" Margin="0" >

<Rectangle.Effect>

<DropShadowEffect/>

</Rectangle.Effect>

</Rectangle>

<StackPanel Orientation="Horizontal" Margin="5">

<RadioButton Content="AddStops" x:Name="StopsRadioButton" IsChecked="true"

Foreground="White" GroupName="add" VerticalAlignment="Center"/>

<RadioButton Content="AddBarriers" x:Name="BarriersRadioButton"

Foreground="White" GroupName="add" VerticalAlignment="Center"/>

<Button Content="Clear" Click="Button_Click" Margin="5,0,0,0"/>

</StackPanel>

</Grid>

</Grid>

</UserControl>

至此,本案例完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: