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;       
            });
        } 

效果图

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

相关推荐
c#上位机2 小时前
wpf之数据类型转换
c#·wpf·mvvm
宝桥南山3 小时前
.NET - .NET Aspire的Command-Line和GitHub Copilot
microsoft·微软·c#·asp.net·.net·.netcore
向宇it6 小时前
【推荐100个unity插件】unity易于使用模块化设计的天空、体积云和天气系统——Enviro 3
游戏·3d·unity·c#·游戏引擎
我是唐青枫6 小时前
C#.NET SqlKata 使用详解:优雅构建动态 SQL 查询
sql·c#·.net
唐青枫8 小时前
C#.NET NCrontab 深入解析:轻量级 Cron 表达式解析器
c#·.net
“抚琴”的人15 小时前
C# 取消机制(CancellationTokenSource/CancellationToken)
开发语言·c#·wpf·1024程序员节·取消机制
helloworddm17 小时前
Orleans Grain Directory 系统综合分析文档
c#·1024程序员节
dengzhenyue19 小时前
C# 初级编程
开发语言·c#
津津有味道1 天前
ISO18000-6C协议UHF6C超高频RFID读写C#源码
c#·uhf6c·超高频·iso18000-6c
白雪公主的后妈1 天前
Auto CAD二次开发——创建圆弧对象
c#·cad二次开发·创建圆弧对象