将两个 C# DbGeography 多边形连接成一个多边形

Concatenate two C# DbGeography Polygons into a single multipolygon

我已经从官方来源下载了一张带有瑞典地区的地图作为 Shape 文件。然后我使用 QGIS 将数据转换为 geojson,然后将 geojson 转换为 DbGeography,如下所示:

地图来源:

https://www.lantmateriet.se/sv/Kartor-och-geografisk-information/Kartor/oppna-data/hamta-oppna-geodata/#faq:gsd-distriktsindelning

区可以有两个或更多区域,但不是 multipolygon 而是 polygon 每个区域共享相同的密钥(代码)。使用其他官方来源,我直接获得了 multipolygon 但不是来自此来源。当我将它保存到我的数据库时,我想做正确的并且只有代码和名称属性一次并将其存储为 multipolygon。我使用 Entity Framework 将信息存储到我的数据库中。

如何将两个或多个多边形连接成一个多多边形?

代码为 101019.

Stora Hammars distrikt 示例

当前代码:

型号:

/// <summary>
/// GSD means Geografiska Sverigedata and is available via Lantmäteriet in Sweden.
/// </summary>
public class GSDDistrict
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string Code { get; set; }

    public string Name { get; set; }

    public string ObjectId { get; set; }

    public string ObjectVer { get; set; }

    public DateTime ValidFrom { get; set; }

    public DbGeography Area { get; set; }
}

方法:

public void AddGsdDistricts()
{
    using (var reader = File.OpenText($"{path}\GIS\lantmateriet-gsd-distriktsindelning.geojson"))
    {
        var json = reader.ReadToEnd();
        var featureCollection = JsonConvert.DeserializeObject<GeoJSON.Net.Feature.FeatureCollection>(json);

        foreach (var feature in featureCollection.Features)
        {
            var code = feature.Properties["DISTRKOD"].ToString();

            var gsdDistrict = new GSDDistrict();
            string geoJson = JsonConvert.SerializeObject(feature.Geometry);
            var dbGeography = JsonConvert.DeserializeObject<DbGeography>(geoJson, new DbGeographyGeoJsonConverter());
            gsdDistrict.Area = dbGeography;
            gsdDistrict.Area = gsdDistrict.Area.MakePolygonValid();

            if (db.GSDDistricts.All(x => x.Code != code))
            {
                gsdDistrict.Code = feature.Properties["DISTRKOD"].ToString();
                gsdDistrict.Name = feature.Properties["DISTRNAMN"].ToString();
                gsdDistrict.ObjectId = feature.Properties["OBJEKT_ID"].ToString();
                gsdDistrict.ObjectVer = feature.Properties["OBJEKT_VER"].ToString();
                gsdDistrict.ValidFrom = DateTime.ParseExact(feature.Properties["GALLERFRAN"].ToString(), "yyyy/MM/dd", CultureInfo.InvariantCulture);
                db.GSDDistricts.Add(gsdDistrict);
                //Yes this will be slow but the method will only run once
                db.SaveChanges();
                Program.LogWithGreenConsoleColour($"Added geo data for {gsdDistrict.Name}");
            }
            else if (db.GSDDistricts.Any(x => x.Code == code && x.Area.Disjoint(gsdDistrict.Area)))
            {
                //Add the other area here
                Program.LogWithGreenConsoleColour($"Here");
            }
        }

        if (db.ChangeTracker.HasChanges())
        {
            db.SaveChanges();
            Program.LogWithGreenConsoleColour($"Saved geo data GSD District from Lantmäteriet to database");
        }
    }
}

MakePolygonValid()扩展方法是为了解决由于SQL服务器使用左手方向而瑞典几乎所有源都使用右手方向导致的环方向错误。扩展方法在这里描述:

原来我根本不需要将它转换成 multipolygon。关键是 DbGeography.Union

https://msdn.microsoft.com/en-us/library/system.data.spatial.dbgeography.union(v=vs.110).aspx

else if (db.GSDDistricts.Any(x => x.Code == code && x.Area.Disjoint(gsdDistrict.Area)))
{
    var district = db.GSDDistricts.FirstOrDefault(x => x.Code == code);
    Program.LogWithGreenConsoleColour($"Adding another area for {district.Name}");
    district.Area = district.Area.Union(gsdDistrict.Area);
    db.SaveChanges();                      
}

加载到 Google 地图时看起来像这样,上面是红线而不是蓝绿色。