首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将List<SqlGeography>转换为包含GeographyCollection的SqlGeography

将List<SqlGeography>转换为包含GeographyCollection的SqlGeography
EN

Stack Overflow用户
提问于 2016-04-12 15:52:08
回答 1查看 931关注 0票数 4

我的应用程序生成一个GeoJson特性集合(只包含多边形),我试图使用实体框架/将其存储在SqlServer数据库中。

目前,我循环遍历所有多边形,并将它们转换为SqlGeometry对象,并通过调用multiPolygon = multiPolygon.STUnion(nextGeometry);创建多个多边形来合并这些对象,然后可以将其转换为DbGeography并保存。

这方面有两个问题:

  1. 保存的多边形在它们重叠的地方合并。不是世界末日,但不是理想。
  2. 当我将形状转换回GeoJson并将其添加到地图中进行编辑时,我不会得到一个FeatureCollection,而是得到一个多多边形。它显示良好,但当我单击保存所有转换逻辑,以保存中断,因为它期待一个功能集合。

理想情况下,我希望循环遍历功能集合中的所有多边形,并将它们保存为几何集合中的不同多边形。

我看过如何使用SqlGeometryBuilder类,但我并不想遍历每个几何学的所有点来将它们添加到构建器中。这似乎有点麻烦。

我正在使用NetTopologySuite进行转换。我现在的密码是这样的..。

代码语言:javascript
复制
// 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;
}
EN

回答 1

Stack Overflow用户

发布于 2016-04-14 16:36:45

我不是C#程序员,但您使用的是几何学,而不是功能,这就是为什么您最终使用的是多多边形而不是FeatureCollection。

所以伪码是这样的:

代码语言:javascript
复制
List<Features> list
for poly in polygons
  Feature f = new Feature(poly)
  list.add(f)

FeatureCollection fc = new FeatureCollection(list)
return fc
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36578303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档