⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 包含详细例子的c#创建shape文件的开源码
💻 CS
📖 第 1 页 / 共 2 页
字号:
            // free resources
            ShapeLib.SHPClose(hShp);
            Console.WriteLine("\nPress any key to continue...");
            Console.ReadLine();
        }

        private static void CreateDBF()
        {
            Console.WriteLine("\n*****Creating dbf*****\n");
            // create dbase file
            IntPtr hDbf = ShapeLib.DBFCreate(FILENAME);
            if (hDbf.Equals(IntPtr.Zero))
            {
                Console.WriteLine("Error:  Unable to create {0}.dbf!", FILENAME);
                return;
            }

            // add some fields 
            int iRet = ShapeLib.DBFAddField(hDbf, "recID", ShapeLib.DBFFieldType.FTInteger, 2, 0);
            iRet = ShapeLib.DBFAddField(hDbf, "textField", ShapeLib.DBFFieldType.FTString, 25, 0);
            iRet = ShapeLib.DBFAddField(hDbf, "dblField", ShapeLib.DBFFieldType.FTDouble, 8, 4);
            iRet = ShapeLib.DBFAddField(hDbf, "boolField", ShapeLib.DBFFieldType.FTLogical, 1, 0);
            iRet = ShapeLib.DBFAddField(hDbf, "dateField", ShapeLib.DBFFieldType.FTDate, 8, 0);

            // populate
            Random r = new Random();
            for (int iShape = 0; iShape < NSHAPES; iShape++)
            {
                int iField = 0;
                iRet = (ShapeLib.DBFWriteIntegerAttribute(hDbf, iShape, iField++, iShape * 10));
                iRet = (ShapeLib.DBFWriteStringAttribute(hDbf, iShape, iField++, "Hello World"));
                iRet = (ShapeLib.DBFWriteDoubleAttribute(hDbf, iShape, iField++, (100 * r.NextDouble())));
                iRet = (ShapeLib.DBFWriteLogicalAttribute(hDbf, iShape, iField++, iShape % 2 == 0));
                iRet = (ShapeLib.DBFWriteDateAttribute(hDbf, iShape, iField++, DateTime.Now));
            }

            // set a few null values
            iRet = ShapeLib.DBFWriteNULLAttribute(hDbf, 0, 0);
            iRet = ShapeLib.DBFWriteNULLAttribute(hDbf, 1, 1);
            iRet = ShapeLib.DBFWriteNULLAttribute(hDbf, 2, 2);
            iRet = ShapeLib.DBFWriteNULLAttribute(hDbf, 1, 3);
            iRet = ShapeLib.DBFWriteNULLAttribute(hDbf, 0, 4);

            // modify a value
            iRet = (ShapeLib.DBFWriteStringAttribute(hDbf, 2, 1, "Greetings, Earthlings"));

            // close the file handle then reopen (only so we can test DBFOpen)
            ShapeLib.DBFClose(hDbf);
            hDbf = ShapeLib.DBFOpen(FILENAME, "rb+");

            // verify the table structure
            int recCount = ShapeLib.DBFGetRecordCount(hDbf);
            int fieldCount = ShapeLib.DBFGetFieldCount(hDbf);
            Console.WriteLine("Record Count: {0}", recCount);
            Console.WriteLine("Field Count: {0}\n", fieldCount);

            ShapeLib.DBFFieldType[] fieldTypes = new ShapeLib.DBFFieldType[NFIELDS];
            string[] fieldNames = new string[NFIELDS];
            int fieldWidth = 0;
            int numDecimals = 0;
            for (int iField = 0; iField < fieldCount; iField++)
            {
                StringBuilder sb = new StringBuilder(12);
                fieldTypes[iField] = ShapeLib.DBFGetFieldInfo(hDbf, iField, sb, ref fieldWidth, ref numDecimals);
                fieldNames[iField] = sb.ToString();

                Console.WriteLine("-----Field {0}-----", iField + 1);
                Console.WriteLine("Field Name: {0}", fieldNames[iField]);
                Console.WriteLine("Field Type: {0}", fieldTypes[iField]);
                Console.WriteLine("Field Width: {0}", fieldWidth);
                Console.WriteLine("Num Decimals: {0}", numDecimals);
                char c = (char)ShapeLib.DBFGetNativeFieldType(hDbf, iField);
                Console.WriteLine("Native Type: {0}", c);
                Console.WriteLine("\nPress any key to continue...");
                Console.ReadLine();
            }

            // verify records were written correctly
            Console.WriteLine("\n-----Data Values-----");
            for (int iShape = 0; iShape < recCount; iShape++)
            {
                for (int iField = 0; iField < fieldCount; iField++)
                {
                    switch ((ShapeLib.DBFFieldType)fieldTypes[iField])
                    {
                        case (ShapeLib.DBFFieldType.FTDouble):
                            if (ShapeLib.DBFIsAttributeNULL(hDbf, iShape, iField) == 0)
                            {
                                double val = ShapeLib.DBFReadDoubleAttribute(hDbf, iShape, iField);
                                Console.WriteLine("{0}({1}): {2}", fieldNames[iField], iShape, val);
                            }
                            else
                                Console.WriteLine("{0}({1}) Is Null", fieldNames[iField], iShape);
                            break;

                        case (ShapeLib.DBFFieldType.FTLogical):
                            if (ShapeLib.DBFIsAttributeNULL(hDbf, iShape, iField) == 0)
                            {
                                bool val = ShapeLib.DBFReadLogicalAttribute(hDbf, iShape, iField);
                                Console.WriteLine("{0}({1}): {2}", fieldNames[iField], iShape, val.ToString());
                            }
                            else
                                Console.WriteLine("{0}({1}) Is Null", fieldNames[iField], iShape);
                            break;

                        case (ShapeLib.DBFFieldType.FTInteger):
                            if (ShapeLib.DBFIsAttributeNULL(hDbf, iShape, iField) == 0)
                            {
                                int val = ShapeLib.DBFReadIntegerAttribute(hDbf, iShape, iField);
                                Console.WriteLine("{0}({1}): {2}", fieldNames[iField], iShape, val);
                            }
                            else
                                Console.WriteLine("{0}({1}) Is Null", fieldNames[iField], iShape);
                            break;

                        case (ShapeLib.DBFFieldType.FTDate):
                            if (ShapeLib.DBFIsAttributeNULL(hDbf, iShape, iField) == 0)
                            {
                                DateTime val = ShapeLib.DBFReadDateAttribute(hDbf, iShape, iField);
                                Console.WriteLine("{0}({1}): {2}", fieldNames[iField], iShape, val.ToLongDateString());
                            }
                            else
                                Console.WriteLine("{0}({1}) Is Null", fieldNames[iField], iShape);
                            break;

                        case (ShapeLib.DBFFieldType.FTInvalid):
                            Console.WriteLine("Field type is invalid");
                            break;

                        default:
                            if (ShapeLib.DBFIsAttributeNULL(hDbf, iShape, iField) == 0)
                            {
                                string val = ShapeLib.DBFReadStringAttribute(hDbf, iShape, iField);
                                Console.WriteLine("{0}({1}): {2}", fieldNames[iField], iShape, val);
                            }
                            else
                                Console.WriteLine("{0}({1}) Is Null", fieldNames[iField], iShape);
                            break;
                    }
                }
            }

            // check DBFGetFieldIndex function
            iRet = ShapeLib.DBFGetFieldIndex(hDbf, "nonexistant");
            Console.WriteLine("nonexistant is field #{0}", iRet);
            iRet = ShapeLib.DBFGetFieldIndex(hDbf, "dblField");
            Console.WriteLine("dblField is field #{0}", iRet);

            // check DBFCloneEmpty function
            string newFile = FILENAME + "_clone";
            IntPtr hDbfClone = ShapeLib.DBFCloneEmpty(hDbf, newFile);
            if (hDbfClone.Equals(IntPtr.Zero))
                Console.WriteLine("Error:  Unable to create {0}.dbf!", newFile);
            else
            {
                // test Tuple Read/Write 
                int iShape = NSHAPES;
                for (int iDest = 0; iDest < NSHAPES; iDest++)
                {
                    IntPtr pTuple = ShapeLib.DBFReadTuple(hDbf, --iShape);
                    // write to clone in reverse order
                    // note that iDest must increase monotonically from zero
                    ShapeLib.DBFWriteTuple(hDbfClone, iDest, pTuple);
                    Console.WriteLine("Tuple [{0}]->[{1}] = \"{2}\"",
                        iShape.ToString(),
                        iDest.ToString(),
                        Marshal.PtrToStringAnsi(pTuple));
                }
                // release resources
                ShapeLib.DBFClose(hDbfClone);
            }

            // release resources
            ShapeLib.DBFClose(hDbf);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -