2016-04-12 17 views
2

我的應用程序生成GeoJSON的要素集合(containig僅多邊形),我正在嘗試使用實體框架/轉換列表<SqlGeography>到含有GeographyCollection

此刻我的SQL Server數據庫來存儲SqlGeography我循環遍歷所有多邊形並將它們轉換爲SqlGeometry對象,並通過調用multiPolygon = multiPolygon.STUnion(nextGeometry);來創建多邊形,然後將它們轉換爲DbGeography並保存。

有兩個問題與:在重疊

  1. 保存的多邊形合併。不是世界的盡頭,但並不理想。
  2. 當我將形狀轉換回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; 
} 

回答

0

我不是一個C#程序員,但你用幾何圖形代替特點的工作這就是爲什麼你結束了一個多面的,而不是一個的FeatureCollection。

所以僞代碼是一樣的東西:

List<Features> list 
for poly in polygons 
    Feature f = new Feature(poly) 
    list.add(f) 

FeatureCollection fc = new FeatureCollection(list) 
return fc 
+0

我不認爲SQL服務器地理庫都有一個要素類。 – BenCr

+0

https://nettopologysuite.github.io/html/class_net_topology_suite_1_1_features_1_1_feature.html –

+0

這就是網絡拓撲套件。我已經傳入了NTS FeatureCollection。我試圖從NTS轉換到SqlServer DbGeography – BenCr

相關問題