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

时间:2018-05-04 20:38:32

标签: c# entity-framework gis qgis dbgeography

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

https://stackoverflow.com/a/49225753/3850405

地图来源:

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 示例。

enter image description here

当前代码:

型号:

/// <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 Server使用左手方向,瑞典几乎所有来源都使用右手方向。扩展方法在这里描述:

https://stackoverflow.com/a/49454154/3850405

1 个答案:

答案 0 :(得分:0)

原来我根本不需要将其转换为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地图时会出现这种情况,红线代替上面的蓝绿色。

enter image description here