Exemplo n.º 1
0
        /// <summary>
        /// Create a Polyline from two Points
        /// </summary>
        /// <param name="pointFrom">Start point</param>
        /// <param name="pointTo">End point</param>
        /// <returns>Esri poliline</returns>
        public static IPolyline CreatePolylineFromPoints(IPoint pointFrom, IPoint pointTo)
        {
            if (pointFrom == null || pointTo == null)
            {
                return(null);
            }

            WKSPoint[] segmentWksPoints = new WKSPoint[2];
            segmentWksPoints[0].X = pointFrom.X;
            segmentWksPoints[0].Y = pointFrom.Y;
            segmentWksPoints[1].X = pointTo.X;
            segmentWksPoints[1].Y = pointTo.Y;

            IPointCollection4 trackLine = new PolylineClass();

            IGeometryBridge2 m_geometryBridge = new GeometryEnvironmentClass();

            m_geometryBridge.AddWKSPoints(trackLine, ref segmentWksPoints);


            var result = trackLine as IPolyline;

            if (pointFrom.SpatialReference != null && pointTo.SpatialReference != null && pointFrom.SpatialReference == pointTo.SpatialReference)
            {
                result.SpatialReference = pointFrom.SpatialReference;
            }

            return(result);
        }
Exemplo n.º 2
0
        private List <IGeometry> splitGeometry(IGeometry geo, bool mergePolys)
        {
            IGeometry geoM = null;

            if (mergePolys)
            {
                geoM = mergeSmallGeos(geo);
            }
            else
            {
                geoM = geo;
            }
            List <IGeometry>    geoLst  = new List <IGeometry>();
            IPolygon4           poly4   = (IPolygon4)geoM;
            IGeometryCollection geoColl = (IGeometryCollection)poly4.ConnectedComponentBag;

            for (int i = 0; i < geoColl.GeometryCount; i++)
            {
                IGeometry geo2 = geoColl.get_Geometry(i);
                IEnvelope env = geo2.Envelope;
                double    xmax = env.XMax;
                double    xmin = env.XMin;
                double    ymax = env.YMax;
                double    ymin = env.YMin;
                double    xRange = xmax - xmin;
                double    yRange = ymax - ymin;
                double    nMinX, nMinY, nMaxX, nMaxY;
                if (xRange > yRange)
                {
                    nMinY = ymin - 1;
                    nMaxY = ymax + 1;
                    nMinX = xmin + (xRange / 2);
                    nMaxX = nMinX;
                }
                else
                {
                    nMinX = xmin - 1;
                    nMaxX = xmax + 1;
                    nMinY = ymin + (yRange / 2);
                    nMaxY = nMinY;
                }
                WKSPoint[]        wksPoint         = new WKSPoint[2];
                IPointCollection4 pointCollection4 = new PolylineClass();
                wksPoint[0].X = nMinX;
                wksPoint[0].Y = nMinY;
                wksPoint[1].X = nMaxX;
                wksPoint[1].Y = nMaxY;
                IGeometryBridge2 geometryBridge2 = new GeometryEnvironmentClass();
                geometryBridge2.AddWKSPoints(pointCollection4, ref wksPoint);
                IPolyline polyline = pointCollection4 as ESRI.ArcGIS.Geometry.IPolyline;
                polyline.SpatialReference = geo2.SpatialReference;
                ITopologicalOperator4 tp   = (ITopologicalOperator4)geo2;
                IGeometry             geoL = null;
                IGeometry             geoR = null;
                try
                {
                    tp.Cut(polyline, out geoL, out geoR);
                    if (((IArea)geoL).Area > maxarea)
                    {
                        geoLst.AddRange(splitGeometry(geoL, false));
                    }
                    else
                    {
                        geoLst.Add(geoL);
                    }
                    if (((IArea)geoR).Area > maxarea)
                    {
                        geoLst.AddRange(splitGeometry(geoR, false));
                    }
                    else
                    {
                        geoLst.Add(geoR);
                    }
                }
                catch (Exception e)
                {
                    geoLst.Add(geo2);
                    Console.WriteLine(e.ToString());
                }
            }
            return(geoLst);
        }
 private List<IGeometry> splitGeometry(IGeometry geo,bool mergePolys)
 {
     IGeometry geoM = null;
     if (mergePolys)
     {
         geoM = mergeSmallGeos(geo);
     }
     else
     {
         geoM = geo;
     }
     List<IGeometry> geoLst = new List<IGeometry>();
     IPolygon4 poly4 = (IPolygon4)geoM;
     IGeometryCollection geoColl = (IGeometryCollection)poly4.ConnectedComponentBag;
     for (int i = 0; i < geoColl.GeometryCount; i++)
     {
         IGeometry geo2 = geoColl.get_Geometry(i);
         IEnvelope env = geo2.Envelope;
         double xmax = env.XMax;
         double xmin = env.XMin;
         double ymax = env.YMax;
         double ymin = env.YMin;
         double xRange = xmax - xmin;
         double yRange = ymax - ymin;
         double nMinX, nMinY, nMaxX, nMaxY;
         if (xRange > yRange)
         {
             nMinY = ymin - 1;
             nMaxY = ymax + 1;
             nMinX = xmin + (xRange / 2);
             nMaxX = nMinX;
         }
         else
         {
             nMinX = xmin - 1;
             nMaxX = xmax + 1;
             nMinY = ymin + (yRange / 2);
             nMaxY = nMinY;
         }
         WKSPoint[] wksPoint = new WKSPoint[2];
         IPointCollection4 pointCollection4 = new PolylineClass();
         wksPoint[0].X = nMinX;
         wksPoint[0].Y = nMinY;
         wksPoint[1].X = nMaxX;
         wksPoint[1].Y = nMaxY;
         IGeometryBridge2 geometryBridge2 = new GeometryEnvironmentClass();
         geometryBridge2.AddWKSPoints(pointCollection4, ref wksPoint);
         IPolyline polyline = pointCollection4 as ESRI.ArcGIS.Geometry.IPolyline;
         polyline.SpatialReference = geo2.SpatialReference;
         ITopologicalOperator4 tp = (ITopologicalOperator4)geo2;
         IGeometry geoL = null;
         IGeometry geoR = null;
         try
         {
             tp.Cut(polyline, out geoL, out geoR);
             if (((IArea)geoL).Area > maxarea)
             {
                 geoLst.AddRange(splitGeometry(geoL,false));
             }
             else
             {
                 geoLst.Add(geoL);
             }
             if (((IArea)geoR).Area > maxarea)
             {
                 geoLst.AddRange(splitGeometry(geoR,false));
             }
             else
             {
                 geoLst.Add(geoR);
             }
         }
         catch (Exception e)
         {
             geoLst.Add(geo2);
             Console.WriteLine(e.ToString());
         }
     }
     return geoLst;
 }