1

I have a set of points from GPS I want to convert them to polygons..I have two approaches to do this.1- convert points to polygon..I do this with below code:

        private IPolygon PolygonFromPoint()
    {

        try
        {
            IGeometryBridge2 pGeoBrg = new GeometryEnvironment() as IGeometryBridge2;
            IPointCollection4 pPointColl = new PolygonClass();

            int numPoints = pList.Count;
            WKSPoint[] aWKSPointBuffer = new WKSPoint[numPoints];
            for (int i = 0; i < pList.Count; i++)
            {
                WKSPoint A = new WKSPoint();
                A.X = pList[i].X;
                A.Y = pList[i].Y;
                aWKSPointBuffer[i] = A;
            }
            pGeoBrg.SetWKSPoints(pPointColl, ref aWKSPointBuffer);
            IPolygon pPointColl2 = pPointColl as IPolygon;
            pPointColl2.SpatialReference = spatialrefrence;
            OutputPolygon = pPointColl2;
            status = true;
            return pPointColl2;
        }
        catch (Exception e)
        {
            MessageBox.Show("PolygonFromPoint: " + e.Message);
            status = false;
            return null;
        }
    }

This works some times but some times I get the error that says the polygon is not colosed.I could not manage to fix this problem

Another way is converting to closed polyline and then convert them to a polygon.I managed to convert them to polyline using geoprosseor but can not convert it to polygons.(I can not use feature to polygon either because of licence problam).

So what ways do you suggest me to do this task?I can not find any samples or something like that.Can you please help me do this?

I am using ArcObjects 10.2.1 and C# thanks a lot

8
  • 1
    If the points aren't in proper order to generate a polygon, then even if method succeeds, you might not get the polygon you really wanted. This is more a "garbage in, garbage out" issue than anything else.
    – Vince
    Commented Jul 24, 2014 at 18:12
  • @Vince Thanks for your answer..so you suggest me the second way..converting them to closed polyline and then convert polyline to polygon?Can you help me with this method?I Can easily convert points to closed polylines using geoprocessing but how can I convert line to polygon?exept using feature to polygon? Commented Jul 24, 2014 at 18:25
  • A polygon is composed of an ordered list of ordinates. The perimeter of a polygon must be a simple line (cannot cross itself and can only touch at a single point). Polylines do not usually have a simple requirement, so you will have the same exact issue converting them to polygons. Bad data is bad data; you won't gain anything from an intermediate line type.
    – Vince
    Commented Jul 24, 2014 at 19:09
  • @Vince But how feature to polygon do this for me?I can use the same points (and closed polyline that is created by them) in feature to polygon and it gives me a polygon..But I can not use it in my application.:(If I make sure that my polyline does not have a cross and is closed in only one point how can I convert it to polygon? Commented Jul 24, 2014 at 19:30
  • Hi Majid, do you want it to exactly match the points?? If you're after a polygon from the points consider making them a multipoint and then use ITopologicalOperator.ConvexHull to generate a bounding polygon... this will cover all the points but some may not be on the boundary. Commented Jul 24, 2014 at 23:25

1 Answer 1

2

Using your code:

private IPolygon BoundingPolygon()
{
    try
    {
        IGeometryBridge2 pGeoBrg = new GeometryEnvironment() as IGeometryBridge2;
        IPointCollection4 pPointColl = new MultipointClass(); // edited here

        int numPoints = pList.Count;
        WKSPoint[] aWKSPointBuffer = new WKSPoint[numPoints];
        for (int i = 0; i < pList.Count; i++)
        {
            WKSPoint A = new WKSPoint();
            A.X = pList[i].X;
            A.Y = pList[i].Y;
            aWKSPointBuffer[i] = A;
        }
        pGeoBrg.SetWKSPoints(pPointColl, ref aWKSPointBuffer);

        // edits here
        IGeometry pGeom = (IMultipoint)pPointColl;
        pGeom.SpatialReference = spatialrefrence;
        ITopologicalOperator pTopOp = (ITopologicalOperator)pGeom;
        IPolygon pPointColl2 = (IPolygon)pTopOp.ConvexHull();

        pPointColl2.SpatialReference = spatialrefrence;
        // OutputPolygon = pPointColl2; maybe you don't need this line as the object is not used
        status = true;
        return pPointColl2;
    }
    catch (Exception e)
    {
        MessageBox.Show("PolygonFromPoint: " + e.Message);
        status = false;
        return null;
    }
}

This is from memory; it's been a while since I dealt with multipoints and I think it's right.. I think you can go straight from point collection to geometry with the multipoint type without needing to go via geometry/geometrycollection/geometry.

This will create a geometry that looks like Minimum Bounding Geometry, which as I indicated before encloses all the points but may not pass through each point. To insert the rest you can iterate through the ones not on the boundary and using IHitTest find the segment that they are closest to and insert the point there. It would be necessary to update the geometry and refresh the hittest for each vertex inserted or some fairly interesting and useless results will be generated.

1
  • Thanks so much dear friend..I'll try this code and I hope finally I can fix my problem. Commented Jul 25, 2014 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.