.netcore + postgis 保存地图围栏数据

一、数据库字段

字段类型选择(Type)

设置对象类型为:geometry

二、前端传递的Json格式转换

前端传递围栏的各个坐标点数据如下:

csharp 复制代码
{
    "AreaRange": [
        {
            "lat": 30.123456,
            "lng": 120.123456
        },
        {
            "lat": 30.123456,
            "lng": 120.654321
        },
        {
            "lat": 30.654321,
            "lng": 120.654321
        },
        {
            "lat": 30.654321,
            "lng": 120.123456
        }
    ]
}

后端使用Geometry类型,ORM本项目使用了SqlSugar

csharp 复制代码
/// <summary>
/// 区域围栏
/// </summary>
[SugarColumn(ColumnName= "arearange")]
public Geometry? AreaRange { get; set; }
csharp 复制代码
public class MapProperty
{
    /// <summary>
    /// Latitude的简写,表示纬度
    /// </summary>
    public double lat { get; set; }
    /// <summary>
    /// Longtitude的简写,表示经度
    /// </summary>
    public double lng { get; set; }
}

后端Json转Geometry ( Polygon)多边形或者线

csharp 复制代码
public static Polygon ConvertToPolygon(List<MapProperty> data)
{
    if (data == null || data.Count < 3)
    {
        throw new ArgumentException("至少需要三个点才能创建多边形");
    }
    // 创建几何工厂
    GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4326);
    // 将 List<MapProperty> 转换为 List<Coordinate>
    List<Coordinate> coordinates = data.Select(mp => new Coordinate(mp.lng, mp.lat)).ToList();
    // 添加第一个点以闭合多边形
    coordinates.Add(coordinates[0]);
    // 创建线性环
    LinearRing linearRing = factory.CreateLinearRing(coordinates.ToArray());
    // 创建多边形
    Polygon polygon = factory.CreatePolygon(linearRing);
    return polygon;
}
public static LineString ConvertToLineString(List<MapProperty> data)
    {
        if (data == null || data.Count < 2)
        {
            throw new ArgumentException("至少需要两个点才能创建多段线");
        }

        // 创建几何工厂
        GeometryFactory factory = new GeometryFactory();

        // 将 List<MapProperty> 转换为 List<Coordinate>
        List<Coordinate> coordinates = data.Select(mp => new Coordinate(mp.lng, mp.lat)).ToList();

        // 创建多段线
        LineString lineString = factory.CreateLineString(coordinates.ToArray());

        return lineString;
    }

保存入库

csharp 复制代码
var area = GeoJsonHelper.ConvertToPolygon(request.AreaRange);
CommunityExtEntity ext = new CommunityExtEntity()
{
    AreaRange = area,
};
await _app.Insertable(ext).ExecuteCommandAsync();
相关推荐
csdn_aspnet18 小时前
使用 MongoDB.Driver 在 C# .NETCore 中实现 Mongo DB 过滤器
mongodb·c#·.netcore
tiancao2221 天前
安装3DS MAX 2026后,无法运行,提示缺少.net core的解决方案
.net·.netcore·3dsmax
csdn_aspnet1 天前
使用 C# .NETCore 实现MongoDB
mongodb·c#·.netcore
MoFe14 天前
【.net core】【NPOI】读取表格信息(处理合并行表格数据)
.netcore
csdn_aspnet4 天前
在 .NET Core 中实现基于策略和基于角色的授权
.netcore·role·policy
The Sheep 20235 天前
.NetCore MVC
mvc·.netcore
一包烟电脑面前做一天5 天前
.NetCore下Ocelot + Nacos 实现负载均衡
nacos·负载均衡·.netcore·ocelot·ocelot集成nacos
一个帅气昵称啊5 天前
NetCoreKevin-DDD-微服务-WebApi-AI智能体、AISK集成、MCP协议服务、SignalR、Quartz 框架-16-部署与基础设施
微服务·云原生·架构·系统架构·.netcore
忧郁的蛋~6 天前
在.NET标准库中进行数据验证的方法
后端·c#·asp.net·.net·.netcore
willhuo6 天前
学生请假就餐系统
运维·服务器·.netcore