gpt4 book ai didi

c# - 将 List 转换为包含 GeographyCollection 的 SqlGeography

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:51 25 4
gpt4 key购买 nike

我的应用程序生成了一个 GeoJson 要素集合(仅包含多边形),我正尝试使用 Entity Framework 将其存储在我的 SqlServer 数据库中/

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

这有两个问题:

  1. 保存的多边形在它们重叠的地方合并。不是世界末日,但也不理想。
  2. 当我将形状转换回 GeoJson 并将其添加到我的 map 进行编辑时,我没有得到 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;
}

最佳答案

我不是 C# 程序员,但您使用的是几何体而不是特征,这就是为什么您最终得到的是多面体而不是特征集合。

所以伪代码是这样的:

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

FeatureCollection fc = new FeatureCollection(list)
return fc

关于c# - 将 List<SqlGeography> 转换为包含 GeographyCollection 的 SqlGeography,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36578303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com