我的应用程序生成一个GeoJson特性集合(只包含多边形),我试图使用实体框架/将其存储在SqlServer数据库中。
目前,我循环遍历所有多边形,并将它们转换为SqlGeometry对象,并通过调用multiPolygon = multiPolygon.STUnion(nextGeometry);创建多个多边形来合并这些对象,然后可以将其转换为DbGeography并保存。
这方面有两个问题:
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;
}发布于 2016-04-14 16:36:45
我不是C#程序员,但您使用的是几何学,而不是功能,这就是为什么您最终使用的是多多边形而不是FeatureCollection。
所以伪码是这样的:
List<Features> list
for poly in polygons
Feature f = new Feature(poly)
list.add(f)
FeatureCollection fc = new FeatureCollection(list)
return fchttps://stackoverflow.com/questions/36578303
复制相似问题