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

效果图

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

相关推荐
浅陌sss9 小时前
Xlua中C#引用Lua变量,导致Lua侧的GC无法回收的原因及解决方法
c#·lua
棉晗榜9 小时前
c#模拟鼠标点击左键
c#
爱吃香蕉的阿豪11 小时前
在c#中虚方法和抽象类的区别
深度学习·c#·.netcore
晚秋大魔王13 小时前
C# 添加图标
c#·visual studio code
shepherd枸杞泡茶13 小时前
第3章 .NETCore核心基础组件:3.1 .NET Core依赖注入
开发语言·c#·.net·.netcore
yuanpan14 小时前
C#的async异步方法里如果使用了await,那么它跟同步方法有什么区别?
开发语言·c#
CodeCraft Studio16 小时前
.NET版PDF处理控件Aspose.PDF教程:在 C# 中将 TIFF 文件转换为 PDF
pdf·c#·.net
斯内科16 小时前
C#使用文件读写操作实现仙剑五前传称号存档修改
c#·二进制·修改器
虫洞没有虫1 天前
《Python与C#:虚拟机与元宇宙的次元战争》
开发语言·c#
yuanpan1 天前
C#中的动态类型用法总结带演示代码
windows·microsoft·c#