AO 由坐标创建一个多边形

创建一个环的 多边形

        IGeometryCollection CreatPolygon(List<jsonpoint> lstPts)
        {
            IPoint[] ComPoints = new IPoint[lstPts.Count];
            for (int j = 0; j < lstPts.Count; j++)
            {
                ISpatialReferenceFactory fatory = new SpatialReferenceEnvironmentClass();
                ComPoints[j] = new PointClass();
                ComPoints[j].SpatialReference = fatory.CreateGeographicCoordinateSystem(4326);
                ComPoints[j].X = lstPts[j].x;
                ComPoints[j].Y = lstPts[j].y;
                ComPoints[j].Project(sdeSpatialreference);
            }

            ISegmentCollection pSegments;
            ILine pLine;
            IRing pRing;
            pSegments = new RingClass();
            object Missing = Type.Missing;
            for (int k = 0; k < lstPts.Count - 1; k++)
            {
                pLine = new LineClass();
                pLine.PutCoords(ComPoints[k], ComPoints[k + 1]);
                pSegments.AddSegment(pLine as ISegment, ref Missing, ref Missing);
            }
            pRing = pSegments as IRing;

            pRing.Close();

            IGeometryCollection pPolygon = new PolygonClass();
            pPolygon.AddGeometry(pRing, ref Missing, ref Missing);

            return pPolygon;

        }

多个环的多边形

  IGeometryCollection CreatPolygon(List<jsonpoint>[] pRings)
        {
            IGeometryCollection pPolygon = new PolygonClass();
            for (int m = 0; m < pRings.Length; m++)
            {
                IPoint[] ComPoints = new IPoint[pRings[m].Count];
                for (int j = 0; j < pRings[m].Count; j++)
                {
                    ISpatialReferenceFactory fatory = new SpatialReferenceEnvironmentClass();
                    ComPoints[j] = new PointClass();
                    ComPoints[j].SpatialReference = fatory.CreateGeographicCoordinateSystem(4326);
                    ComPoints[j].X = pRings[m][j].x;
                    ComPoints[j].Y = pRings[m][j].y;
                    ComPoints[j].Project(sdeSpatialreference);
                }

                ISegmentCollection pSegments;
                ILine pLine;
                IRing pRing;
                pSegments = new RingClass();
                object Missing = Type.Missing;
                for (int k = 0; k < pRings[m].Count - 1; k++)
                {
                    pLine = new LineClass();
                    pLine.PutCoords(ComPoints[k], ComPoints[k + 1]);
                    pSegments.AddSegment(pLine as ISegment, ref Missing, ref Missing);
                }
                pRing = pSegments as IRing;

                pRing.Close();

                pPolygon.AddGeometry(pRing, ref Missing, ref Missing);
            }

            return pPolygon;
        }

完整示例

        private void AddNewFeatures(IFeatureClass featureclass)
        {
            IPoint[] pts = new IPoint[4];
            for (int i = 0; i < 4; i++)
            {
                pts[i] = new PointClass();
            }
            pts[0].X = 121.480;
            pts[0].Y = 29.890;
            pts[1].X = 121.490;
            pts[1].Y = 29.890;
            pts[2].X = 121.490;
            pts[2].Y = 29.900;
            pts[3].X = 121.480;
            pts[3].Y = 29.900;


            ISegmentCollection pSegments;
            ILine pLine;
            IRing pRing;
            pSegments = new RingClass();
            object Missing = Type.Missing;
            for (int j = 3; j > 1; j++)
            {
                pLine = new LineClass();
                pLine.PutCoords(pts[j], pts[j- 1]);
                pSegments.AddSegment(pLine as ISegment, ref Missing, ref Missing);
            }


            pRing = pSegments as IRing;

            pRing.Close();

            IGeometryCollection pPolygon = new PolygonClass();
            pPolygon.AddGeometry(pRing, ref Missing, ref Missing);


            IFeatureBuffer featurebuffer = featureclass.CreateFeatureBuffer();
            IFeatureCursor featurecursor = featureclass.Insert(true);
            featurebuffer.Shape = (IPolygon)pPolygon;
            featurebuffer.set_Value(featurebuffer.Fields.FindField("CODE"), "0_0");

            object featureOid = featurecursor.InsertFeature(featurebuffer);
            featurecursor.Flush();
            Marshal.ReleaseComObject(featurecursor);
           // pCursor = null;
        }

 

posted on 2014-10-15 10:03  markygis  阅读(602)  评论(1编辑  收藏  举报