建立索引

     private void createIndex(int width,int height,string savePath){
            IFeatureLayer featLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
            List<IField> listfield = new List<IField>();
            for (int i = 0; i < featLayer.FeatureClass.Fields.FieldCount; i++)
            {
                listfield.Add(featLayer.FeatureClass.Fields.Field[i]);
            }
            IField selectField = featLayer.featureclass.fileds.get_field(0);
            IFeatureClass fclass = featLayer.FeatureClass;
            IFeatureCursor fc = fclass.Search(null, false);
            IFeature feat = fc.NextFeature();

            List<IFeature> listFeatures = new List<IFeature>();
            while (feat != null)
            {
                listFeatures.Add(feat);
                feat = fc.NextFeature();
            }
            string layername = ((IDataset)fclass).Name;

            LayerSpatialIndex lsi = new LayerSpatialIndex();
            lsi.LayerName = layername;
            //lsi.LayerField = selectField.Name;
            lsi.LayerFields = listfield.ConvertAll(o => o.Name);

            IEnvelope env = null;
            listFeatures.ForEach(o =>
            {
                if (env == null)
                {
                    env = o.Extent.Envelope;
                }
                else
                {
                    env.Union(o.Extent.Envelope);
                }
            });
            double upperleftX = env.UpperLeft.X;
            double upperleftY = env.UpperLeft.Y;
            double lowerrightX = env.LowerRight.X;
            double lowerrightY = env.LowerRight.Y;


            lsi.UpperLeftX = upperleftX;
            lsi.UpperLeftY = upperleftY;
            lsi.LowerRightX = lowerrightX;
            lsi.LowerRightY = lowerrightY;

            lsi.listIndexes = new List<ValueIndex>();
            int userType = 1;
            if (listFeatures.Count < 255)
            {
                userType = 1;
            }
            else if (listFeatures.Count < 65535)
            {
                userType = 2;
            }
            else if (listFeatures.Count < 4294967295)
            {
                userType = 4;
            }
            lsi.DataType = userType;


            for (int i = 0; i < listFeatures.Count; i++)
            {
                ValueIndex vi = new ValueIndex();
                vi.Value = new List<string>();
                for (int j = 0; j < listfield.Count; j++)
                {
                    vi.Value.Add(listFeatures[i].get_Value(j).ToString());
                }

                vi.index = i + 2;
                lsi.listIndexes.Add(vi);
            }
            double dX = (lowerrightX - upperleftX) / width;
            double dY = (upperleftY - lowerrightY) / height;

            int preferindex = -1;
            IRelationalOperator perferro = null;

            PixelFormat pf = PixelFormat.Format32bppRgb;
            //if(userType == typeof(ushort))
            //{
            //    pf = PixelFormat.Format16bppGrayScale;
            //}
            //else if(userType == typeof(uint))
            //{
            //    pf = PixelFormat.Format32bppRgb;
            //}
            Random r = new Random();
            List<Color> listColors = listFeatures.ConvertAll<Color>(o => Color.FromArgb(255, Color.FromArgb(r.Next())));
            listColors.Add(Color.FromArgb(255, Color.FromArgb(r.Next())));
            listColors.Add(Color.FromArgb(255, Color.FromArgb(r.Next())));

            Bitmap bmp = new Bitmap(width, height, pf);
            byte[] bt = new byte[width * height];
            DateTime dt1 = DateTime.Now;
            for (int i = 0; i < bt.Length; i++)
            {
                bt[i] = 0;
            }
            try
            {
                for (int i = 0; i < listFeatures.Count; i++)
                {
                    IFeature ifeat = listFeatures[i];


                    IPolygon poly = ifeat.Shape as IPolygon;


                    IGeometryCollection gc = poly as IGeometryCollection;
                    for (int j = 0; j < gc.GeometryCount; j++)
                    {
                        IRing ring = gc.Geometry[j] as IRing;
                        ISegmentCollection sc = ring as ISegmentCollection;
                        for (int k = 0; k < sc.SegmentCount; k++)
                        {
                            DrawBound(sc.Segment[k], upperleftX, lowerrightY, dX, dY, width, height, bt, bmp, listColors[1]);
                        }
                    }
                }
            }
            catch(Exception ex)
            {
            }

            for (int i = 0; i < height; i++)
            {
                int offset = i * width;
                for (int j = 0; j < width; j++)
                {
                    //current
                    if (bt[offset + j] == 1)
                    {
                        continue;
                    }
                    else 
                    {
                        if (j > 0)
                        {
                            if (bt[offset + j - 1] != 1)
                            {
                                bt[offset + j] = bt[offset + j - 1];
                                bmp.SetPixel(j, i, listColors[bt[offset + j - 1]]);
                                continue;
                            }
                        }

                        if (i > 0)
                        {
                            int offset2 = (i - 1) * width;
                            if (bt[offset2 + j] != 1)
                            {
                                bt[offset + j] = bt[offset2 + j];
                                bmp.SetPixel(j, i, listColors[bt[offset2 + j]]);
                                continue;
                            }

                            int start = j;
                            int end = j;

                            while (end < width && bt[offset + end] == 0)
                            {
                                end++;
                            }
                            end--;

                            byte findvalue = 0;
                            bool bFind = false ;
                            for (int m = start; m <= end; m++)
                            {
                                if (bt[offset2 + m] != 1)
                                {
                                    findvalue = bt[offset2 + m];
                                    bFind = true;
                                    break;
                                }
                            }
                            if (bFind)
                            {
                                for (int m = start; m <= end; m++)
                                {
                                    bt[offset + m] = findvalue;
                                    bmp.SetPixel(m, i, listColors[findvalue]);
                                }
                                continue;
                            }
                        }

                        IEnvelope tempenv = new EnvelopeClass();
                        tempenv.XMin = upperleftX + dX * j;
                        tempenv.XMax = tempenv.XMin + dX;
                        tempenv.YMin = lowerrightY + dY * (height-1-i);
                        tempenv.YMax = tempenv.YMin + dY;
                        bool find = false;
                        for (int m = 0; m < listFeatures.Count; m++)
                        {
                            //if (m == preferindex)
                            //{
                            //    continue;
                            //}
                            IRelationalOperator ro = listFeatures[m].Shape as IRelationalOperator;
                            int sit = GetSit(ro, tempenv, m);
                            if (sit > 0)
                            {
                                bmp.SetPixel(j, i, listColors[sit]);
                                bt[i * width + j] = (byte)sit;
                               
                                find = true;
                                break;
                            }
                        }
                        if (!find)
                        {
                            bmp.SetPixel(j, i, Color.FromArgb(0));
                            bt[i * width + j] = (byte)0;

                            //bmp.SetPixel(i, height - j - 1, Color.FromArgb(0));
                            //bt[(height - j - 1) * width + i] = (byte)0;
                        }
                    }
                }
            }               
            lsi.width = width;
            lsi.height = height;
            bmp.Save(savePath + "\\" + layername + ".bmp", ImageFormat.Bmp);

            using (System.IO.FileStream fs = File.Open(savePath + "\\" + layername + ".dat", FileMode.Create))
            {
                fs.Write(bt, 0, bt.Length);
                fs.Close();
            }

            using (System.IO.FileStream fs = File.Open(savePath + "\\" + layername + ".cfg", FileMode.Create))
            {
                System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(LayerSpatialIndex));
                ser.Serialize(fs, lsi);
                fs.Close();
            }

            MessageBox.Show((DateTime.Now - dt1).TotalSeconds.ToString());
        }

public class ValueIndex
    {
        public List<string> Value{get;set;}
        public int index{get;set;}
    }

        private void DrawBound(ISegment Segment, double upperleftX, double lowerrightY, double dX, double dY, int width, int height, byte[] bt, Bitmap bmp, Color c)
        {
            IPoint fromPoint = Segment.FromPoint;
            IPoint toPoint = Segment.ToPoint;
            //throw new NotImplementedException();
            
            //开启DDA算法
            double y1 = fromPoint.Y;
            double y2 = toPoint.Y;


            if (y1 > y2)
            {
                double ytemp = y1;
                y1 = y2;
                y2 = ytemp;
            }


            int starty = (int)((y1 - lowerrightY) / dY);
            int endy = (int)((y2 - lowerrightY) / dY);


            //最后一格
            if (endy == height)
            {
                endy = height - 1;
            }

            double p1X = fromPoint.X;
            double p1Y = fromPoint.Y;
            double p2X = toPoint.X;
            double p2Y = toPoint.Y;

            double firstY = lowerrightY + (starty+1) * dY;
            double lastY = lowerrightY + endy * dY;

            if (starty == endy)
            {
                int x1 = (int)((p1X - upperleftX) / dX);
                int x2 = (int)((p2X - upperleftX) / dX);

                if (x1 < 0)
                {
                    x1 = 0;
                }
                if (x1 >= width)
                {
                    x1 = width - 1;
                }

                if (x2 < 0)
                {
                    x2 = 0;
                }
                if (x2 >= width)
                {
                    x2 = width - 1;
                }

                if(x1 > x2)
                {
                    int xtemp = x1;
                    x1 = x2;
                    x2 = xtemp;
                }

                int offset = (height-1-starty)*width;
                for (int i = x1; i <= x2; i++)
                {
                    bt[offset + i] = 1;
                    try
                    {
                        bmp.SetPixel(i, height - 1 - starty, c);
                    }
                    catch (Exception ex)
                    {


                    }
                }
            }
            else
            {
                int x1 = (int)((p1X - upperleftX) / dX);
                int x2 = (int)((p2X - upperleftX) / dX);


                if (x1 < 0)
                {
                    x1 = 0;
                }
                if (x1 >= width)
                {
                    x1 = width - 1;
                }


                if (x2 < 0)
                {
                    x2 = 0;
                }
                if (x2 >= width)
                {
                    x2 = width - 1;
                }

                double deltaX = (p2X - p1X) * dY / (p2Y - p1Y); 

                if (p1Y > p2Y)
                {
                    double dfirstX = ((firstY - y1) / dY * deltaX + p2X - upperleftX);
                    int ifirstX =  (int)(((firstY - y1)/dY * deltaX + p2X - upperleftX) / dX);
                    int ilastX = (int)(((lastY - y1)/dY * deltaX + p2X - upperleftX) / dX);

                    if (ifirstX < 0)
                    {
                        ifirstX = 0;
                    }
                    if (ifirstX >= width)
                    {
                        ifirstX = width - 1;
                    }

                    if (ilastX < 0)
                    {
                        ilastX = 0;
                    }
                    if (ilastX >= width)
                    {
                        ilastX = width - 1;
                    }

                    int tmpx1 = x2;
                    int tmpx2 = ifirstX;
                    if(tmpx1 > tmpx2)
                    {
                        int tmpx = tmpx1;
                        tmpx1 = tmpx2;
                        tmpx2 = tmpx;
                    }

                    int offset = (height - 1 - starty) * width;

                    for (int i = tmpx1; i <= tmpx2; i++)
                    {
                        bt[offset + i] = 1;
                        try
                        {
                            bmp.SetPixel(i, height - 1 - starty, c);
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    tmpx1 = x1;
                    tmpx2 = ilastX;
                    if (tmpx1 > tmpx2)
                    {
                        int tmpx = tmpx1;
                        tmpx1 = tmpx2;
                        tmpx2 = tmpx;
                    }

                    offset = (height - 1 - endy) * width;
                    for (int i = tmpx1; i <= tmpx2; i++)
                    {
                        bt[offset + i] = 1;
                        try
                        {
                            bmp.SetPixel(i, height - 1 - endy, c);
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    double dSX = dfirstX;
                    for (int i = starty + 1; i < endy; i++)
                    {
                        offset = (height - 1 - i) * width;
                        tmpx1 = (int)(dSX / dX);
                        dSX += deltaX;
                        tmpx2 = (int)(dSX / dX);

                        if (tmpx1 < 0)
                        {
                            tmpx1 = 0;
                        }
                        if (tmpx1 >= width)
                        {
                            tmpx1 = width - 1;
                        }

                        if (tmpx2 < 0)
                        {
                            tmpx2 = 0;
                        }
                        if (tmpx2 >= width)
                        {
                            tmpx2 = width - 1;
                        }

                        if (tmpx1 > tmpx2)
                        {
                            int tmpx = tmpx1;
                            tmpx1 = tmpx2;
                            tmpx2 = tmpx;
                        }
                        for (int j = tmpx1; j <= tmpx2; j++)
                        {
                            bt[offset + j] = 1;
                            try
                            {
                                bmp.SetPixel(j, height - 1 - i, c);
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }
                else
                {
                    double dfirstX = ((firstY - y1) / dY * deltaX + p1X - upperleftX);
                    int ifirstX = (int)(((firstY - y1) / dY * deltaX + p1X - upperleftX) / dX);
                    int ilastX = (int)(((lastY - y1) / dY * deltaX + p1X - upperleftX) / dX);

                    if (ifirstX < 0)
                    {
                        ifirstX = 0;
                    }
                    if (ifirstX >= width)
                    {
                        ifirstX = width - 1;
                    }

                    if (ilastX < 0)
                    {
                        ilastX = 0;
                    }
                    if (ilastX >= width)
                    {
                        ilastX = width - 1;
                    }

                    int tmpx1 = x1;
                    int tmpx2 = ifirstX;
                    if (tmpx1 > tmpx2)
                    {
                        int tmpx = tmpx1;
                        tmpx1 = tmpx2;
                        tmpx2 = tmpx;
                    }

                    int offset = (height - 1 - starty) * width;

                    for (int i = tmpx1; i <= tmpx2; i++)
                    {
                        bt[offset + i] = 1;
                        try
                        {
                            bmp.SetPixel(i, height - 1 - starty, c);
                        }
                        catch (Exception ex)
                        {
                        }
                    }


                    tmpx1 = x2;
                    tmpx2 = ilastX;
                    if (tmpx1 > tmpx2)
                    {
                        int tmpx = tmpx1;
                        tmpx1 = tmpx2;
                        tmpx2 = tmpx;
                    }

                    offset = (height - 1 - endy) * width;
                    for (int i = tmpx1; i <= tmpx2; i++)
                    {
                        bt[offset + i] = 1;
                        try
                        {
                            bmp.SetPixel(i, height - 1 - endy, c);
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    double dSX = dfirstX;
                    for (int i = starty + 1; i < endy; i++)
                    {
                        offset = (height - 1 - i) * width;

                        tmpx1 = (int)(dSX / dX);
                        dSX += deltaX;
                        tmpx2 = (int)(dSX / dX);

                        if (tmpx1 < 0)
                        {
                            tmpx1 = 0;
                        }
                        if (tmpx1 >= width)
                        {
                            tmpx1 = width - 1;
                        }

                        if (tmpx2 < 0)
                        {
                            tmpx2 = 0;
                        }
                        if (tmpx2 >= width)
                        {
                            tmpx2 = width - 1;
                        }

                        if (tmpx1 > tmpx2)
                        {
                            int tmpx = tmpx1;
                            tmpx1 = tmpx2;
                            tmpx2 = tmpx;
                        }
                        for (int j = tmpx1; j <= tmpx2; j++)
                        {
                            bt[offset + j] = 1;
                            try
                            {
                                bmp.SetPixel(j, height - 1 - i, c);
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }
            }


        }



private int GetSit(IRelationalOperator perferro, IEnvelope tempenv, int opindex)
        {
            if (perferro.Contains(tempenv))
            {
                return opindex + 2;
            }
            else if (perferro.Overlaps(tempenv))
            {
                return 1;
            }
            else
            {
                return 0;
            }

           // throw new NotImplementedException();
        }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值