2
我的應用程序生成GeoJSON的要素集合(containig僅多邊形),我正在嘗試使用實體框架/轉換列表<SqlGeography>到含有GeographyCollection
此刻我的SQL Server數據庫來存儲SqlGeography我循環遍歷所有多邊形並將它們轉換爲SqlGeometry
對象,並通過調用multiPolygon = multiPolygon.STUnion(nextGeometry);
來創建多邊形,然後將它們轉換爲DbGeography
並保存。
有兩個問題與:在重疊
- 保存的多邊形合併。不是世界的盡頭,但並不理想。
- 當我將形狀轉換回GeoJson並將其添加到我的地圖進行編輯時,我沒有得到
FeatureCollection
,我得到了一個多邊形。它顯示好,但當我點擊保存所有我的轉換邏輯保存休息,因爲它期待一個功能集合。
理想情況下,我想遍歷要素集合中的所有多邊形,並將它們作爲不同的多邊形保存在幾何集合中。
我已經看過使用SqlGeometryBuilder類,但我並不想循環遍歷每個幾何體的所有點以將它們添加到生成器。這似乎有點繁重。
我正在使用NetTopologySuite進行轉換。我當前的代碼看起來是這樣的......
// FeatureCollection is a NTS object
private static DbGeography GetGeographyFromFeatureCollection(FeatureCollection featureCollection)
{
DbGeography toStore;
// Get all polygons and ignore any other features, there shouldn't be any due to the way the shape is created anyway
var polygons = featureCollection.Features.Where(x => x.Geometry is IPolygon).Select(x => x.Geometry as IPolygon);
var writer = new MsSql2008GeometryWriter();
SqlGeography geometryCollection = null;
foreach (var shape in polygons)
{
// convert to sql geometry rather than geography because if you convert directly to geography then datum information is missing.
var sqlGeometry = writer.WriteGeometry(shape)
.MakeValid();
// convert geometry to geography
var sqlGeography = SqlGeography.STGeomFromWKB(sqlGeometry.STAsBinary(), WGS84Datum);
// some code removed to re-orient the geometry
if (geometryCollection == null)
{
// you can't union SqlGeography.Null
geometryCollection = sqlGeography;
}
else
{
geometryCollection = geometryCollection.STUnion(sqlGeography);
}
}
// convert SqlGeography to DbGeography for Entity Framework
toStore = DbSpatialServices.Default.GeographyFromProviderValue(geometryCollection);
return toStore;
}
我不認爲SQL服務器地理庫都有一個要素類。 – BenCr
https://nettopologysuite.github.io/html/class_net_topology_suite_1_1_features_1_1_feature.html –
這就是網絡拓撲套件。我已經傳入了NTS FeatureCollection。我試圖從NTS轉換到SqlServer DbGeography – BenCr