【问题标题】:Combine (union?) and simplify/reduce DbGeometry records for GeoJson合并(联合?)并简化/减少 GeoJson 的 DbGeometry 记录
【发布时间】:2018-08-08 20:58:18
【问题描述】:

我在一个表中有许多空间实体,有一个名为 Boundariesgeometry 字段。我想生成一个具有简化形状/几何形状的 GeoJson 文件。

这是我的第一次尝试:

var entities = await db.Entities.ToListAsync();

dynamic geoJson = new ExpandoObject();
geoJson.type = "FeatureCollection";

var features = new List<dynamic>();

foreach (var entity in entities)
{
    // simplify (uses SqlGeometry.Reduce)
    var simplified = Utilities.Geo.Simplify(entity.Boundaries, tolerance);

    // convert to GeoJSON4EntityFramework.Feature with the appropriate Id
    var feature = Utilities.Geo.GetFeature(simplified, entity.Id);

    features.Add(feature);
}

geoJson.features = features;

return geoJson;

结果的问题在于,由于几何是单独简化的,边界并不常见,如下所示:

第二个尝试是先合并实体,然后简化,然后输出为 GeoJson:

var entities = await db.Entities.ToListAsync();

// bit of a hack to union all the boundaries
DbGeometry allBoundaries = null;
for (var i = 0; i < entities.Count; i++)
{
    if (i == 0) allBoundaries = entities[i].Boundaries;
    else allBoundaries = allBoundaries.Union(entities[i].Boundaries);
}

// simplify (uses SqlGeometry.Reduce)
var simplified = Utilities.Geo.Simplify(allBoundaries, tolerance);

dynamic geoJson = new ExpandoObject();
geoJson.type = "FeatureCollection";

var features = new List<dynamic>();

// convert to GeoJSON4EntityFramework.Feature with the (in)appropriate Id
var feature = Utilities.Geo.GetFeature(simplified, "ALL");

features.Add(feature);

geoJson.features = features;

return geoJson;

但是,.Union 将实体组合成一个实体,尽管 said here 不会发生这种情况。 (我也没有机会在每个功能上放置一个 Id,所以现在只使用“ALL”)。结果是完全合并的形状:

所以问题是:我如何跨行组合边界,然后简化,然后生成一个特征集合,每个特征都有 正确的 ID,可以在 MapShaper 中完成(如下所示)?

【问题讨论】:

    标签: c# .net sql-server geospatial


    【解决方案1】:

    看起来这在 SQL Server 中是不可能的。

    您需要将几何图形转换为拓扑,然后简化,然后匹配回原始几何图形以保留属性/属性/id/等。

    见:https://trac.osgeo.org/postgis/wiki/UsersWikiSimplifyWithTopologyExt

    SQL Server 不支持拓扑。


    编辑

    我正在编写下面的代码,它将多边形(不是多面体)转换为线串,合并线串以有效地获得拓扑层,然后对其进行简化。它工作得非常好,但困难不在于将多线串转换为多面,这可能需要tool like this

    select
        geometry::STGeomFromText(replace(replace(e1.boundaries.STAsText(), 'POLYGON (', 'LINESTRING '), '))', ')'), 4326)
        .STUnion(geometry::STGeomFromText(replace(replace(e2.boundaries.STAsText(), 'POLYGON (', 'LINESTRING '), '))', ')'), 4326))
        .STUnion(geometry::STGeomFromText(replace(replace(e3.boundaries.STAsText(), 'POLYGON (', 'LINESTRING '), '))', ')'), 4326))
        .Reduce(0.1)
    from entities e1
    cross join entities e2 
    cross join entities e3
    where e1.code  = 'dc7'
    and e2.code = 'dc6'
    and e3.code = 'dc8'
    


    编辑

    使用NetTopologySuite,可以做到。我有written it up here。使用Polygonizer,您可以将线串转换回多边形。然后,您必须使用面积相交比率将多边形与原始多边形匹配,然后(如果匹配)您可以重新关联属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 2010-09-08
      • 2012-09-24
      • 1970-01-01
      • 2022-01-09
      相关资源
      最近更新 更多