Search code examples
c#entity-frameworkgisqgisdbgeography

Concatenate two C# DbGeography Polygons into a single multipolygon


I have downloaded a map with Swedish districts as Shape files from an official source. I then used QGIS to convert the data into geojson and then converted the geojson to DbGeography like this:

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

Map source:

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

The districts can have two or more areas but are not multipolygon but polygon where each area share the same key (code). Using other official sources I have gotten a multipolygon directly but not from this source. When I save it to my database I would like to do it right and only have the code and name properties once and store it as a multipolygon. I use Entity Framework to store the information to my database.

How can I concatenate the two or more polygons into a multipolygon?

Example for Stora Hammars distrikt with code 101019.

enter image description here

Current code:

Model:

/// <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; }
}

Method:

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");
        }
    }
}

The MakePolygonValid()extension method is to solve ring orientation error due to that SQL Server uses left-handed orientation and almost all sources in Sweden use right-handed orientation. The extension method is described here:

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


Solution

  • Turns out I did not have to convert it into a multipolygon at all. The key was 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();                      
    }
    

    Looks like this when loaded into Google Maps, red line instead of teal above.

    enter image description here