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();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义