Drauka
Drauka

Reputation: 1217

Create a DbGeography Polygon from a collection of DbGeography Points

Can anyone tell me how I can create a DbGeography object of type 'Polygon' from a Collection of DbGeography objects of type 'POINT'

So far I've got this which creates the polygon but I'm missing how the initial step.

1. DbGeography multipoint = DbGeography.MultiPointFromText("MULTIPOINT(53.095124 -0.864716, 53.021255 -1.337128, 52.808019 -1.345367, 52.86153 -1.018524)", 4326)

2. DbGeometry temp_multipoint = DbGeometry.MultiPointFromBinary(multipoint.AsBinary(), 4326)

3. DbGeography polygon = DbGeography.PolygonFromBinary(temp_multipoint.ConvexHull.AsBinary(), 4326); (RESULT)

The problem is creating the initial multipoint geography object from a list of DbGeography(POINTS)

Upvotes: 4

Views: 10340

Answers (3)

Drauka
Drauka

Reputation: 1217

I did eventually find out how to create a polygon from multiple points without having to create it from WKT. The below explanation is slightly simplified but nonetheless

    var PolygonFromMultiplePoints = new DbGeography();

    using (var db = new LocationContext())
    {
        //Select Locations known to be within a certain area which should define the polygon.
        foreach (var item in db.Locations)
        {
            PolygonFromMultiplePoints.Union(item.GeoLocation);
        }
    }            
    var temp_multipointgeometry = DbGeometry.MultiPointFromBinary(PolygonFromMultiplePoints.AsBinary(), DbGeometry.DefaultCoordinateSystemId);            
    PolygonFromMultiplePoints = DbGeography.PolygonFromBinary(temp_multipointgeometry.ConvexHull.AsBinary(), DbGeography.DefaultCoordinateSystemId);

Code sample assumes that you already have a collection of dbgeography stored as points in the database. My database information was derived from importing locations from GeoNames.

Upvotes: 2

Eric
Eric

Reputation: 2283

You should really do it in C#, adding points in an iterative way for a couple of reasons:

  • You need to ensure that the end position is the same as the start.
  • You need to ensure that the points are added clockwise, due to ring orientation. You can flip the ring around and go counterclockwise (sqlGeography.ReorientObject();).

You're dealing with spatial data, but the construction of complex shapes was never meant for T-SQL. At worst put it in a CLR function.

Upvotes: 0

Rich
Rich

Reputation: 398

Create each point as a DbGeography object using WKT:

DbGeography point1 = DbGeography.FromText("POINT(53.095124 -0.864716)", 4326);
DbGeography point2 = DbGeography.FromText("POINT(53.021255 -1.337128)", 4326);
DbGeography point3 = DbGeography.FromText("POINT(52.808019 -1.345367)", 4326);
...
DbGeography polygon = DbGeography.PolygonFromText("POLYGON((53.095124 -0.864716, 53.021255 -1.337128, 52.808019 -1.345367, 53.095124 -0.864716))", 4326);

Two things to note:

  • The WKT format is Longitude then Latitude, not the more intuitive Lat, Long
  • For a polygon, the LAST point must match the FIRST point to "close it off"

Hope this helps - I also battled to learn the polygon stuff!

See this article for extra tips on the WKT format: http://en.wikipedia.org/wiki/Well-known_text

Upvotes: 6

Related Questions