【问题标题】:Concatenate two C# DbGeography Polygons into a single multipolygon将两个 C# DbGeography 多边形连接成一个多多边形
【发布时间】:2018-05-04 20:38:31
【问题描述】:

我从官方来源下载了一张包含瑞典地区的地图作为 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 将信息存储到我的数据库中。

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

Stora Hammars distrikt 的示例,代码为 101019。

当前代码:

型号:

/// <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()extension 方法是为了解决环方向错误,因为 SQL Server 使用左手方向,而瑞典几乎所有的源都使用右手方向。扩展方法在这里描述:

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

【问题讨论】:

    标签: c# entity-framework gis qgis dbgeography


    【解决方案1】:

    事实证明,我根本不需要将其转换为 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();                      
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多