/// <summary> /// 生产SHP /// </summary> /// <param name="matrix"></param> /// <param name="fullname"></param> private void ToSHP(ByteMatrix matrix, string fullname) { double size = 6; SHPHandle hSHP = null; DBFHandle hDBF = null; try { SHPT shptype = SHPT.POLYGON; // 创建SHP文件 hSHP = SHPHandle.Create(fullname, shptype); if (hSHP == null) { throw new Exception("Unable to create SHP:" + fullname); } // 创建DBF文件 hDBF = DBFHandle.Create(fullname); if (hDBF == null) { throw new Exception("Unable to create DBF:" + fullname); } if (hDBF.AddField("_row", FT.Integer, 10, 0) < 0) { throw new Exception("DBFHandle.AddField(_row,Integer,10,0) failed."); } if (hDBF.AddField("_col", FT.Integer, 10, 0) < 0) { throw new Exception("DBFHandle.AddField(_col,Integer,10,0) failed."); } // 绘制二维码 int record_index = 0; for (int row = 0; row < matrix.Width; row++) { for (int col = 0; col < matrix.Height; col++) { if (matrix[row, col] == 0) { continue; } // 构造SHPRecord SHPRecord record = new SHPRecord() { ShapeType = shptype }; // 分段开始 record.Parts.Add(record.Points.Count); // 添加4个角点 double ox = row * size + 300; double oy = matrix.Height * size - col * size; record.Points.Add(new double[] { ox, oy, 0, 0 }); record.Points.Add(new double[] { ox + size, oy, 0, 0 }); record.Points.Add(new double[] { ox + size, oy + size, 0, 0 }); record.Points.Add(new double[] { ox, oy + size, 0, 0 }); // 写图形 int num_parts = record.NumberOfParts; // 总共分为几段 int[] parts = record.Parts.ToArray(); // 每一个分段的起始节点索引 int num_points = record.NumberOfPoints; // 所有节点总数 double[] xs = new double[num_points]; // 所有节点X坐标 double[] ys = new double[num_points]; // 所有节点Y坐标 double[] zs = new double[num_points]; // 所有节点Z坐标 double[] ms = new double[num_points]; // 所有节点M坐标 for (int n = 0; n < num_points; n++) { xs[n] = record.Points[n][0]; // X坐标 ys[n] = record.Points[n][1]; // Y坐标 zs[n] = record.Points[n][2]; // Z值 ms[n] = record.Points[n][3]; // M值 } // PS: 节点 "逆时针"是加 "顺时针"是减 SHPObject shpobj = SHPObject.Create(shptype, // 图形类别 -1, // 图形ID -1表示新增 num_parts, // 总共分为几段 parts, // 每一个分段的起始节点索引 null, // 每段的类别 num_points, // 所有节点总数 xs, // 所有节点的X坐标 ys, // 所有节点的Y坐标 zs, // 所有节点的Z值 ms); // 所有节点的M值 hSHP.WriteObject(-1, shpobj); // 写属性 //hDBF.WriteNULLAttribute(record_index, 0); hDBF.WriteDoubleAttribute(record_index, 0, row); hDBF.WriteDoubleAttribute(record_index, 1, col); record_index++; } } } catch (Exception) { throw; } finally { if (hSHP != null) { hSHP.Close(); } if (hDBF != null) { hDBF.Close(); } } }
/// <summary> /// Write a small multipoint file. /// </summary> static void Test_WriteMultiPoints( SHPT nSHPType, string pszFilename ) { SHPHandle hSHPHandle; SHPObject psShape; double[] x = new double[4], y = new double[4]; double[] z = new double[4], m = new double[4]; int i, iShape; hSHPHandle = SHPHandle.Create( pszFilename, nSHPType ); for( iShape = 0; iShape < 3; iShape++ ) { for( i = 0; i < 4; i++ ) { x[i] = iShape * 10 + i + 1.15; y[i] = iShape * 10 + i + 2.25; z[i] = iShape * 10 + i + 3.35; m[i] = iShape * 10 + i + 4.45; } psShape = SHPObject.Create( nSHPType, -1, 0, null, null, 4, x, y, z, m ); hSHPHandle.WriteObject( -1, psShape ); psShape = null; } hSHPHandle.Close(); }
/// <summary> /// Write a small point file. /// </summary> static void Test_WritePoints( SHPT nSHPType, string pszFilename ) { SHPHandle hSHPHandle; SHPObject psShape; double[] x = new double[1], y = new double[1]; double[] z = new double[1], m = new double[1]; hSHPHandle = SHPHandle.Create( pszFilename, nSHPType ); x[0] = 1.0; y[0] = 2.0; z[0] = 3.0; m[0] = 4.0; psShape = SHPObject.Create( nSHPType, -1, 0, null, null, 1, x, y, z, m ); hSHPHandle.WriteObject( -1, psShape ); psShape = null; x[0] = 10.0; y[0] = 20.0; z[0] = 30.0; m[0] = 40.0; psShape = SHPObject.Create( nSHPType, -1, 0, null, null, 1, x, y, z, m ); hSHPHandle.WriteObject( -1, psShape ); psShape = null; hSHPHandle.Close(); }
/// <summary> /// Write a small arc or polygon file. /// </summary> static void Test_WriteArcPoly( SHPT nSHPType, string pszFilename ) { SHPHandle hSHPHandle; SHPObject psShape; double[] x = new double[100], y = new double[100]; double[] z = new double[100], m = new double[100]; int[] anPartStart = new int[100]; int[] anPartType = new int[100], panPartType; int i, iShape; hSHPHandle = SHPHandle.Create( pszFilename, nSHPType ); if( nSHPType == SHPT.MULTIPATCH ) panPartType = anPartType; else panPartType = null; for( iShape = 0; iShape < 3; iShape++ ) { x[0] = 1.0; y[0] = 1.0+iShape*3; x[1] = 2.0; y[1] = 1.0+iShape*3; x[2] = 2.0; y[2] = 2.0+iShape*3; x[3] = 1.0; y[3] = 2.0+iShape*3; x[4] = 1.0; y[4] = 1.0+iShape*3; for( i = 0; i < 5; i++ ) { z[i] = iShape * 10 + i + 3.35; m[i] = iShape * 10 + i + 4.45; } psShape = SHPObject.Create( nSHPType, -1, 0, null, null, 5, x, y, z, m ); hSHPHandle.WriteObject( -1, psShape ); psShape = null; } /* -------------------------------------------------------------------- */ /* Do a multi part polygon (shape). We close it, and have two */ /* inner rings. */ /* -------------------------------------------------------------------- */ x[0] = 0.0; y[0] = 0.0; x[1] = 0; y[1] = 100; x[2] = 100; y[2] = 100; x[3] = 100; y[3] = 0; x[4] = 0; y[4] = 0; x[5] = 10; y[5] = 20; x[6] = 30; y[6] = 20; x[7] = 30; y[7] = 40; x[8] = 10; y[8] = 40; x[9] = 10; y[9] = 20; x[10] = 60; y[10] = 20; x[11] = 90; y[11] = 20; x[12] = 90; y[12] = 40; x[13] = 60; y[13] = 40; x[14] = 60; y[14] = 20; for( i = 0; i < 15; i++ ) { z[i] = i; m[i] = i*2; } anPartStart[0] = 0; anPartStart[1] = 5; anPartStart[2] = 10; anPartType[0] = (int)SHPP.RING; anPartType[1] = (int)SHPP.INNERRING; anPartType[2] = (int)SHPP.INNERRING; psShape = SHPObject.Create( nSHPType, -1, 3, anPartStart, panPartType, 15, x, y, z, m ); hSHPHandle.WriteObject( -1, psShape ); psShape = null; hSHPHandle.Close(); }