.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();
相关推荐
gerrywhu3 天前
PostGIS强大栅格分析工具-栅格代数运算(回调函数版)【ST_MapAlgebra 】
postgis·回调函数·栅格数据分析·栅格代数·st_mapalgebra·栅格回调函数·栅格加权
鸠摩智首席音效师3 天前
如何在 macOS 上安装 .NET Core ?
macos·.netcore
宝桥南山7 天前
Microsoft Agent Framework(MAF) - 如何将workflow或者A2A client转换成一个AI Agent
microsoft·ai·微软·aigc·.net·.netcore
滴滴答答哒17 天前
.NET Core 基于 AOP + Polly 实现数据库死锁自动重试
数据库·.netcore·sqlsugar
gerrywhu20 天前
PostGIS实现栅格数据合并—影像拼接、镶嵌【ST_Union】
postgis·栅格数据分析·影像镶嵌·栅格合并·st_union·影像拼接
.NET修仙日记20 天前
.NET EFCore批量插入性能优化实战:30秒 → 0.5秒
性能优化·c#·.net·.netcore·微软技术·efcore·踩坑实录
Kimhill张23 天前
.net core8 WPF 依赖注入(DI)
wpf·.netcore
gerrywhu24 天前
PostGIS实现栅格分块与瓦片生成【ST_Tile】
postgis·栅格数据分析·栅格分块·遥感影像分块·栅格瓦片生成·st_tile·瓦片生成
wangl_9224 天前
C# / .NET 在工业环境中的优势
开发语言·c#·.net·.netcore·.net core·visual studio
gerrywhu1 个月前
PostGIS实现栅格数据可视化-单波段映射为多波段彩色栅格【ST_ColorMap】
postgis·栅格分析·栅格数据可视化·st_colormap·多波段彩色栅格·彩色栅格