WPF绘制深圳地铁路线图

简介: WPF绘制深圳地铁路线图

经常坐地铁,却不知道地铁多少条线路?哪个站下车?今天就带领大家熟悉并绘制深圳地铁路线图。

WPF在绘制矢量图方面有非常强大的优势,利用WPF可以绘制出各种矢量图形,如线,圆,多边形,矩形,及组合图形。今天以绘制深圳地铁路线图为例,简述WPF在图形绘制方面的一些知识,仅供学习分享使用,如有不足之处,还请指正。

WPF图形概述

与传统的.NET开发使用GDI+进行绘图不同,WPF拥有自己的一套图形API,绘图为矢量图。绘图可以在任何一种布局控件中完成,wpf会根据容器计算相应坐标。最常用的是Canvas和Grid。基本图形包括以下几个,都是Shaper类的派生类。

  1. Line,直线段,可以设置Stroke
  2. Rectangle,有Stroke也有Fill
  3. Ellipse,椭圆,同上
  4. Polygon,多边形。由多条直线线段围成的闭合区域,同上。
  5. Polyline,折线,不闭合,由多条首尾相接的直线段组成
  6. Path,路径,闭合。可以由若干直线、圆弧、贝塞尔曲线(由线段与节点组成,节点是可拖动的支点,线段像可伸缩的皮筋)组成。很强大。

地铁官网效果

首先打开深圳地铁官网【https://www.szmc.net/map/】,可查看深圳地铁的路线图,如下所示:

获取地铁路线数据

通过对地铁官网的网络接口接收数据分析,可以获取地铁数据的原始JSON文件,将原始JSON文件保存到本地,在程序中进行引用,如下所示:

构建地铁数据模型

在得到shentie.json文件后,通过分析,构建模型类,如下所示:

namespace DemoSubway.Models
{
    /// <summary>
    /// 深圳地铁模型
    /// </summary>
    public class ShenTie
    {
        [JsonProperty("s")]
        public string? Name { get; set; }
        [JsonProperty("i")]
        public string? Index { get; set; }
        [JsonProperty("l")]
        public SubwayLine[]? SubwayLines { get; set; }
        [JsonProperty("o")]
        public string? Obj { get;set; }
    }
    public class SubwayLine
    {
        [JsonProperty("st")]
        public St[]? Sites { get; set; }
        [JsonProperty("ln")]
        public string? LineNumber { get; set; }
        [JsonProperty("su")]
        public string? Su { get; set; }
        [JsonProperty("kn")]
        public string? KName { get; set; }
        [JsonProperty("c")]
        public string[]? Circles { get;set; }
        [JsonProperty("lo")]
        public string? Lo { get; set; }
        [JsonProperty("lp")]
        public string[]? LinePosition { get; set; }
        [JsonProperty("ls")]
        public string? Ls { get; set; }
        [JsonProperty("cl")]
        public string? Color { get; set; }
        [JsonProperty("la")]
        public string? La { get; set; }
        [JsonProperty("x")]
        public string? X { get; set; }
        [JsonProperty("li")]
        public string? Li { get; set; }
    }
    public class St
    {
        [JsonProperty("rs")]
        public string? Rs { get; set; }
        [JsonProperty("udpx")]
        public string? Udpx { get; set; }
        [JsonProperty("su")]
        public string? Su { get; set; }
        [JsonProperty("udsu")]
        public string? Udsu { get; set;}
        [JsonProperty("n")]
        public string? Name { get; set;}
        [JsonProperty("en")]
        public string? En { get; set; }
        [JsonProperty("sid")]
        public string? Sid { get; set; }
        [JsonProperty("p")]
        public string? Position { get; set; }
        [JsonProperty("r")]
        public string? R { get; set; }
        [JsonProperty("udsi")]
        public string? Udsi { get; set; }
        [JsonProperty("t")]
        public string? T { get; set;}
        [JsonProperty("si")]
        public string? Si { get; set; }
        [JsonProperty("sl")]
        public string? Sl { get; set;}
        [JsonProperty("udli")]
        public string? Udli { get; set; }
        [JsonProperty("poiid")]
        public string? Poiid { get; set; }
        [JsonProperty("lg")]
        public string? Lg { get; set; }
        [JsonProperty("sp")]
        public string? Sp { get; set; }
    }
}

解析数据源

通过反序列化,将shentie.json文件内容,加载到内存并实例化为ShenTie对象,在此需要用到第三方库【Newtonsoft.Json】,如下所示:

绘制地铁路线图

地铁路线图在WPF主页面显示,用Grid作为容器【subwayBox】,如下所示:

<Window x:Class="DemoSubway.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DemoSubway"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center">
            <TextBlock x:Name="tbTitle" FontSize="30" HorizontalAlignment="Center"></TextBlock>
        </StackPanel>
        <Viewbox Stretch="Fill" Grid.Row="1">
            <Grid x:Name="subwayBox">
            </Grid>
        </Viewbox>
    </Grid>
</Window>

ShenTie对象创建成功后,就可以获取路线数据,然后创建地铁路线元素,如下所示:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    string jsonFile = "shentie.json";
    JsonHelper jsonHelper = new JsonHelper();
    var shentie = jsonHelper.Deserialize<ShenTie>(jsonFile);
    this.tbTitle.Text = shentie.Name;
    List<string> lstSites = new List<string>();
    for(int i = 0; i < shentie.SubwayLines?.Length; i++)
    {
        var subwayLine= shentie.SubwayLines[i];
        if(subwayLine != null)
        {
            //地铁线路
            var color = ColorTranslator.FromHtml($"#{subwayLine.Color}");//线路颜色
            var circles = subwayLine.Circles;//线路节点
            Path line = new Path();
            PathFigureCollection lineFigures = new PathFigureCollection();
            PathFigure lineFigure = new PathFigure();
            lineFigure.IsClosed= false;
            var start = circles?[0].Split(" ");//线路起始位置
            lineFigure.StartPoint = new System.Windows.Point(int.Parse(start[0]), int.Parse(start[1]));
            for (int j= 0;j< circles?.Length;j++)
            {
                var circle= circles[j].Split(" ");
                LineSegment lineSegment = new LineSegment(new System.Windows.Point(int.Parse(circle[0]), int.Parse(circle[1])),true);
                lineFigure.Segments.Add(lineSegment);
            }
            lineFigures.Add(lineFigure);
            line.Data = new PathGeometry(lineFigures, FillRule.Nonzero, null);
            line.Stroke = new SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B));
            line.StrokeThickness = 4;
            this.subwayBox.Children.Add(line);
            //地铁站点
            for (int j = 0; j < subwayLine.Sites?.Length; j++)
            {
                var site = subwayLine.Sites[j];
                if (site != null)
                {
                    //站点标识,圆圈
                    Path siteCirclePath = new Path();
                    var sitePosition = site?.Position?.Split(" ");
                    EllipseGeometry ellipse = new EllipseGeometry();
                    ellipse.Center = new System.Windows.Point(int.Parse(sitePosition[0]), int.Parse(sitePosition[1]));
                    ellipse.RadiusX = 4;
                    ellipse.RadiusY=4;
                    siteCirclePath.Data=ellipse;
                    siteCirclePath.Fill = Brushes.White;
                    siteCirclePath.Stroke = new SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B));
                    siteCirclePath.Cursor= Cursors.Hand;
                    siteCirclePath.Focusable = true;
                    siteCirclePath.Tag = site?.Name;
                    siteCirclePath.MouseDown += SiteCirclePath_MouseDown;
                    this.subwayBox.Children.Add(siteCirclePath);
                    //站点名字
                    if (lstSites.Contains(site?.Name))
                    {
                        continue;//对于交汇站点,只绘制一次
                    }
                    //站点名称
                    Path siteTextPath = new Path();
                    FormattedText siteContent = new FormattedText(site?.Name,CultureInfo.CurrentCulture,FlowDirection.LeftToRight,new Typeface("Arial"),14,Brushes.Black, 1.25);
                    var x = int.Parse(sitePosition[0]);
                    var y = int.Parse(sitePosition[1]);
                    if (j + 1 < subwayLine.Sites?.Length)
                    {
                        //站点位置适当偏移
                        var next = subwayLine.Sites[j + 1]?.Position?.Split(" ");
                        var nextx = int.Parse(next[0]);
                        var nexty = int.Parse(next[1]);
                        if (x == nextx)
                        {
                            x = x + 6;
                        }
                        else if (y == nexty)
                        {
                            y = y + 6;
                        }
                        else
                        {
                            x = x + 1;
                            y = y + 1;
                        }
                    }
                    Geometry geometry = siteContent.BuildGeometry(new System.Windows.Point(x, y));
                    siteTextPath.Data = geometry;
                    siteTextPath.Stroke = Brushes.Black;
                    siteTextPath.Focusable = true;
                    siteTextPath.Cursor = Cursors.Hand;
                    siteTextPath.MouseDown += SiteTextPath_MouseDown;
                    siteTextPath.Tag = site?.Name;
                    this.subwayBox.Children.Add(siteTextPath);
                    lstSites.Add(site?.Name);
                }
            }
            var kName = subwayLine.KName;//线路名称
            var linePosition= subwayLine.LinePosition?[0].Split(" ");
            if(kName != null)
            {
                Path lineNamePath = new Path();
                FormattedText lineNameText = new FormattedText(kName, CultureInfo.CurrentCulture,FlowDirection.LeftToRight,new Typeface("Arial"),16,Brushes.Black,1.25);
                var lineX = int.Parse(linePosition[0]);
                var lineY = int.Parse(linePosition[1]);
                if (subwayLine.LineNumber == "1")
                {
                    lineX = lineX - 10;
                    lineY = lineY + 20;
                }
                Geometry geometry = lineNameText.BuildGeometry(new System.Windows.Point(lineX, lineY));
                lineNamePath.Data=geometry;
                lineNamePath.Stroke = new SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B));
                this.subwayBox.Children.Add(lineNamePath);
            }
        }
    }
}

效果展示

本示例效果图如下所示:

在获取的JSON文件中,有些属性名都是简写,所以在编写示例代码过程中,对有些属性的理解并不准确,需要不断测试优化,绘制出的地铁路线图可能与实际存在稍微的差异,如站点名称,路线名称等内容的位置。

以上就是本篇文章的全部内容,旨在学习分享,传播知识。

相关文章
|
XML 存储 开发工具
|
编解码 Android开发 iOS开发
airserver软件激活码mac版
AirServer 可以方便的利用 AirPlay, Google Cast 以及 Miracast 等技术,将 iPhone, iPad, Mac, Android, Nexus, Pixel, Chromebook, 以及 PC 上面的内容投影到更大的屏幕上面,从而更好的促进协作。
8367 0
|
11月前
|
人工智能 监控 安全
AI智能体上线的流程
AI智能体上线如数字员工入职,需严格安全管控。通过沙箱隔离、红队测试、权限最小化等措施保障安全,经灰度发布、人机协同验证后上线,并持续监控迭代,构建闭环优化体系,确保稳定可靠运行。#AI智能体 #AI应用 #软件外包公司
|
11月前
|
自然语言处理 监控 数据挖掘
2026 年数据分析Agent与ChatBI产品推荐指南:数据智能驱动业务新增长
2026年,数据分析迈向“主动洞察”时代。瓴羊Quick BI凭借“智能小Q”实现自然语言交互、自动归因分析与报告生成,支持秒级响应、多端协同及行业定制化落地,助力企业高效决策,成为数据驱动转型的标杆选择。
|
11月前
|
人工智能 监控 数据可视化
企业常用的BI工具有哪些?2026主流BI工具对比分析
本文盘点国内10款主流BI工具,从数据源支持、核心功能、部署模式等维度对比分析,重点解读瓴羊Quick BI、Tableau、Power BI等产品优势,结合企业选型常见问题,提供实战选型建议,助力数据驱动决策。
|
人工智能 自然语言处理 测试技术
Goedel-Prover:专为自动化数学问题的形式证明生成而设计的 LLM,快速解决形式化数学问题
Goedel-Prover 是一款由普林斯顿大学和清华大学等机构联合推出的开源模型,专注于自动化数学问题的形式证明生成。它通过将自然语言数学问题翻译成形式语言(如 Lean 4),显著提升了数学问题的证明效率。
1006 4
Goedel-Prover:专为自动化数学问题的形式证明生成而设计的 LLM,快速解决形式化数学问题
|
存储 监控 物联网
蜂窝网络基础架构详解:从基站到核心网
蜂窝网络基础架构详解:从基站到核心网
4258 9
|
Ubuntu 开发工具 Android开发
Repo下载、编译AOSP源码:基于Ubuntu 21.04,android-12.1.0_r27
文章记录了作者在Ubuntu 21.04服务器上配置环境、下载并编译基于Android 12.1.0_r27版本的AOSP源码的过程,包括解决编译过程中遇到的问题和错误处理方法。
1968 0
|
机器学习/深度学习 自然语言处理 语音技术
自然语言处理(NLP)中的语言模型
自然语言处理(NLP)中的语言模型
1327 1

热门文章

最新文章