[GIS] The best way to convert points to polygons using ArcObjects

arcgis-10.2arcobjectsgeometry-conversionpointpolygon

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

Best Answer

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.