城子河網(wǎng)站建設(shè)網(wǎng)拍外宣怎么推廣
一、數(shù)據(jù)庫(kù)字段
字段類(lèi)型選擇(Type)
設(shè)置對(duì)象類(lèi)型為:geometry
二、前端傳遞的Json格式轉(zhuǎn)換
前端傳遞圍欄的各個(gè)坐標(biāo)點(diǎn)數(shù)據(jù)如下:
{"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類(lèi)型,ORM本項(xiàng)目使用了SqlSugar
/// <summary>
/// 區(qū)域圍欄
/// </summary>
[SugarColumn(ColumnName= "arearange")]
public Geometry? AreaRange { get; set; }
public class MapProperty
{/// <summary>/// Latitude的簡(jiǎn)寫(xiě),表示緯度/// </summary>public double lat { get; set; }/// <summary>/// Longtitude的簡(jiǎn)寫(xiě),表示經(jīng)度/// </summary>public double lng { get; set; }
}
后端Json轉(zhuǎn)Geometry ( Polygon)多邊形或者線
public static Polygon ConvertToPolygon(List<MapProperty> data)
{if (data == null || data.Count < 3){throw new ArgumentException("至少需要三個(gè)點(diǎn)才能創(chuàng)建多邊形");}// 創(chuàng)建幾何工廠GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4326);// 將 List<MapProperty> 轉(zhuǎn)換為 List<Coordinate>List<Coordinate> coordinates = data.Select(mp => new Coordinate(mp.lng, mp.lat)).ToList();// 添加第一個(gè)點(diǎn)以閉合多邊形coordinates.Add(coordinates[0]);// 創(chuàng)建線性環(huán)LinearRing linearRing = factory.CreateLinearRing(coordinates.ToArray());// 創(chuàng)建多邊形Polygon polygon = factory.CreatePolygon(linearRing);return polygon;
}
public static LineString ConvertToLineString(List<MapProperty> data){if (data == null || data.Count < 2){throw new ArgumentException("至少需要兩個(gè)點(diǎn)才能創(chuàng)建多段線");}// 創(chuàng)建幾何工廠GeometryFactory factory = new GeometryFactory();// 將 List<MapProperty> 轉(zhuǎn)換為 List<Coordinate>List<Coordinate> coordinates = data.Select(mp => new Coordinate(mp.lng, mp.lat)).ToList();// 創(chuàng)建多段線LineString lineString = factory.CreateLineString(coordinates.ToArray());return lineString;}
保存入庫(kù)
var area = GeoJsonHelper.ConvertToPolygon(request.AreaRange);
CommunityExtEntity ext = new CommunityExtEntity()
{AreaRange = area,
};
await _app.Insertable(ext).ExecuteCommandAsync();