Mapsui绘制WKT的示例

步骤

  1. 创建.NET Framework4.8的WPF应用
  2. 在NuGet中安装Mapsui.Wpf 4.1.7
  3. 添加命名空间和组件
csharp 复制代码
<Window x:Class="TestMapsui.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:TestMapsui"
        mc:Ignorable="d"
        xmlns:wpf="clr-namespace:Mapsui.UI.Wpf;assembly=Mapsui.UI.Wpf"
        Title="MainWindow" Height="450" Width="800"
        >
    <Grid>
        <wpf:MapControl x:Name="MapCtrl"/>
    </Grid>
</Window>
  1. 添加WKT相关内容
csharp 复制代码
using Mapsui.Layers;
using Mapsui.Nts;
using Mapsui.Styles;
using Mapsui.Styles.Thematics;
using NetTopologySuite.IO;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Brush = Mapsui.Styles.Brush;
using Color = Mapsui.Styles.Color;

namespace TestMapsui
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var map = MapCtrl.Map;
            var layer = new MemoryLayer
            {
                IsMapInfoLayer = true,
                Features = GetFeatures(),
                Style = CreateDefaultVectorStyle()
            };
            map.Layers.Add(layer);
            //map.Refresh();           
        }   
     }
}

随意制作几个点线面的WKT:

csharp 复制代码
        public IEnumerable<GeometryFeature> GetFeatures()
        {
            var wkts = new List<string>()
            {
                "polygon((0 0,0 2,1 1,1 0,0 0),(0.3 0.3,0.3 0.8,0.8 0.8, 0.3 0.3))",
                "polygon((2 2,3 2,3 1,2 2))",
                "polygon((1 0,1 1,2 1,2 0,1 0))",
                "linestring(0 3,2 3,1 2)",
                "point(0 0)",
                "point(3 3)"
            };
            var r = new WKTReader();
            var geos = wkts.Select(x => r.Read(x));
            var fs = geos.Select(x => new GeometryFeature(x));
            return fs;
        }

随意设置默认的矢量样式:

csharp 复制代码
        private IThemeStyle CreateDefaultVectorStyle()
        {
            return new ThemeStyle(pFunction =>
            {
                var brush = new Brush(Color.LightBlue);
                brush.FillStyle = FillStyle.Solid;
                brush.Background = Color.Red;

                VectorStyle style = new VectorStyle
                {
                    Fill = brush,
                    Outline = new Mapsui.Styles.Pen(Color.Gray, 3),
                    Line = new Mapsui.Styles.Pen(Color.Red, 2)
                };
                return style;       
            });
        } 

效果图

一条线、两个点、三个面:

相关推荐
PfCoder1 小时前
WinForm真入门(13)——ListBox控件详解
windows·c#·visual studio·winform
佟格湾5 小时前
聊透多线程编程-线程池-6.C# APM(异步编程模型)
开发语言·后端·c#·多线程
我不是程序猿儿6 小时前
【C#】一种优雅的基于winform的串口通信管理
stm32·单片机·c#
向宇it11 小时前
【unity游戏开发入门到精通——动画篇】Animator反向动力学(IK)
开发语言·unity·c#·编辑器·游戏引擎
Liu_某12 小时前
c# 运用策略模式与模板方法模式实例
c#·策略模式
du fei13 小时前
C# 组件的使用方法
java·开发语言·c#
xiaowu08017 小时前
C# task任务异步编程提高UI的响应性
开发语言·c#
花之亡灵1 天前
.net6 中实现邮件发送
笔记·c#·.net·代码规范
rrtt_23231 天前
UE5 尝试接入 C# 脚本方案
ue5·c#·csharp·unrealcsharp
leslie_xin1 天前
(原创)[开源][.Net Framework 4.5] SimpleMVVM(极简MVVM框架)更新 v1.1,增加NuGet包
c#·wpf