//从源文件获取字段信息 public List <R_FieldInf> GetFieldInfs(string path) { List <R_FieldInf> FieldInfs = new List <R_FieldInf>(); string dbfpath = path; #region 该部分使用FastDBF获取字段名称 返回List<string> fieldNames DbfFile dbf = new DbfFile(Encoding.Default); dbf.Open(dbfpath, FileMode.Open); DbfHeader dh = dbf.Header; List <string> fieldNames = new List <string>(); int fieldCount = dh.ColumnCount; for (int index = 0; index < fieldCount; index++) { fieldNames.Add(dh[index].Name); } dbf.Close(); #endregion #region 该部分使用Shapelib获取字段类型 返回List<string> fieldTypes //获取字段类型 IntPtr hDbf = ShapeLib.DBFOpen(dbfpath, "rb+");//"rb"(只读)"rb+"(读/写) int pointCount = ShapeLib.DBFGetRecordCount(hDbf); List <string> fieldTypes = new List <string>(); StringBuilder stringBuilder = new StringBuilder(20); int pnWidth = 10; int pnDecimals = 10; for (int index = 0; index < fieldCount; index++) { string type = TypeConvert(ShapeLib.DBFGetFieldInfo(hDbf, index, stringBuilder, ref pnWidth, ref pnDecimals).ToString()); fieldTypes.Add(type); } ShapeLib.DBFClose(hDbf); #endregion //实例化类型 for (int index = 0; index < fieldCount; index++) { FieldInfs.Add(new R_FieldInf(fieldNames[index], fieldTypes[index])); } return(FieldInfs); }
public static void ConvertToOra(string[] args) { string pkValue = string.Empty; if (args == null || args.Length < 4) { Console.Write("\nUsage: shape2ora <username/password>@dbalias> <spatial_table_name> <shape_col> <shapefile> <srid> \n\nPress any key to exit"); Console.ReadLine(); return; } string connectionstring = Utils.ParseConnectionString(args[0].ToString()); string spatial_table = args[1].ToString(); string shape_col = args[2].ToString(); string inShapeFile = args[3].ToString(); string strSRID = "NULL"; if (args.Length == 5) { strSRID = args[4].ToString(); } if (inShapeFile.ToUpper().EndsWith(".SHP")) { inShapeFile = inShapeFile.Substring(0, inShapeFile.Length - 4); } IntPtr hShp = ShapeLib.SHPOpen(inShapeFile, "rb"); IntPtr hDbf = ShapeLib.DBFOpen(inShapeFile, "rb"); if (hDbf.Equals(IntPtr.Zero)) { Console.WriteLine("\nCould not open {0}.dbf\nProbable cause: You do not have permissions or filename is incorrect\n\nPress any key to exit", inShapeFile); Console.ReadLine(); return; } OracleConnection oracon = new OracleConnection(connectionstring); oracon.Open(); try { //Check if table exists bool tabExists = TabExists(oracon, spatial_table); //Get dbf info int recCount = ShapeLib.DBFGetRecordCount(hDbf); // get shape info double[] minXY = new double[4]; double[] maxXY = new double[4]; int nEntities = 0; ShapeLib.ShapeType shapeType = 0; ShapeLib.SHPGetInfo(hShp, ref nEntities, ref shapeType, minXY, maxXY); string sqlCreateTab = @"CREATE TABLE " + spatial_table.ToUpper() + "("; bool flag = true; for (int i = 0; i < nEntities; i++) { if (flag) { sqlCreateTab = sqlCreateTab + GetShapeColumnNames(ref hDbf); sqlCreateTab = sqlCreateTab + shape_col + "\tMDSYS.SDO_GEOMETRY\n)"; if (!tabExists) { //EXECUTE table creation sql ExecuteStatement(oracon, sqlCreateTab); } flag = false; } #region Deal With Geometry IntPtr pshpObj = ShapeLib.SHPReadObject(hShp, i); // Get the SHPObject associated with our IntPtr pshpObj // We create a new SHPObject in managed code, then use Marshal.PtrToStructure // to copy the unmanaged memory pointed to by pshpObj into our managed copy. ShapeLib.SHPObject shpObj = new ShapeLib.SHPObject(); Marshal.PtrToStructure(pshpObj, shpObj); //Number of parts int nParts = shpObj.nParts; //Part starts List int[] partStarts = new int[nParts]; //Coordinate arrays double[] xCoord = new double[shpObj.nVertices]; double[] yCoord = new double[shpObj.nVertices]; double[] zCoord = new double[shpObj.nVertices]; double[] MCoord = new double[shpObj.nVertices]; // Use Marshal.Copy to copy the memory pointed // to by shpObj.padfX and shpObj.padfX (each an IntPtr) to an actual array. Marshal.Copy(shpObj.padfX, xCoord, 0, shpObj.nVertices); Marshal.Copy(shpObj.padfY, yCoord, 0, shpObj.nVertices); if (shapeType == ShapeLib.ShapeType.MultiPointM || shapeType == ShapeLib.ShapeType.PointM || shapeType == ShapeLib.ShapeType.PolygonM || shapeType == ShapeLib.ShapeType.PolyLineM) { Marshal.Copy(shpObj.padfM, zCoord, 0, shpObj.nVertices); } if (shapeType == ShapeLib.ShapeType.MultiPointZ || shapeType == ShapeLib.ShapeType.PointZ || shapeType == ShapeLib.ShapeType.PolygonZ || shapeType == ShapeLib.ShapeType.PolyLineZ) { Marshal.Copy(shpObj.padfZ, zCoord, 0, shpObj.nVertices); } if (nParts > 0) { Marshal.Copy(shpObj.paPartStart, partStarts, 0, nParts); } string sqlInsertShape = " MDSYS.SDO_GEOMETRY("; string gType = GetGTYPE(shapeType, nParts); string elem_info = "NULL"; string sdo_point = "NULL"; if (shapeType == ShapeLib.ShapeType.Point || shapeType == ShapeLib.ShapeType.PointM || shapeType == ShapeLib.ShapeType.PointZ) { sdo_point = "MDSYS.SDO_POINT_TYPE(" + xCoord[0].ToString() + "," + yCoord[0].ToString(); if (shapeType == ShapeLib.ShapeType.PointZ) { sdo_point = sdo_point + "," + zCoord[0].ToString(); } else if (shapeType == ShapeLib.ShapeType.PointM) { sdo_point = sdo_point + "," + MCoord[0].ToString(); } else { sdo_point = sdo_point + ", NULL"; } sdo_point = sdo_point + ")"; } else { elem_info = GetElemInfoString(shapeType, nParts, partStarts, shpObj.nVertices); } string vert_String = GetVerticesString(shapeType, xCoord, yCoord, zCoord, MCoord); //Construct the geometry statement sqlInsertShape = sqlInsertShape + gType + "," + strSRID + "," + sdo_point + "," + elem_info + "," + vert_String + ")"; # endregion #region Deal with Attributes string[] attrs = InsAttrSQL(ref hDbf, i); string insStatement = "INSERT INTO " + spatial_table.ToUpper() + "\n" + "(" + attrs[0] + "," + shape_col + ")\n" + " VALUES (" + attrs[1] + "," + sqlInsertShape + ")"; //Do the insert ExecuteStatement(oracon, insStatement); #endregion } //Create user_sdo_geom_metadata and spatial index for a new table if (!tabExists) { string usgm = GetUSGMString(shapeType, spatial_table, shape_col, minXY, maxXY); ExecuteStatement(oracon, usgm); string spidx = GetSPIDXString(spatial_table, shape_col); ExecuteStatement(oracon, spidx); } // free resources ShapeLib.SHPClose(hShp); ShapeLib.DBFClose(hDbf); Console.Write("Done.\nPress any key to exit"); Console.ReadLine(); }
public List <InputFields> GetFieldValues(List <R_FieldInf> fieldInfs, FieldMapping vitalFields, string path) { List <InputFields> result = new List <InputFields>(); string dbfpath = path; IntPtr hDbf = ShapeLib.DBFOpen(dbfpath, "rb+"); int FieldCount = fieldInfs.Count(); int PointCount = ShapeLib.DBFGetRecordCount(hDbf); for (int pi = 0; pi < PointCount; pi++) { InputFields point = new InputFields(); string other = "{"; for (int i = 0; i < FieldCount; i++) { if (fieldInfs[i].FieldName == vitalFields.ID) { IntPtr FieldvaluePtr = ShapeLib.DBFReadStringAttribute(hDbf, pi, i); string FieldValue = Marshal.PtrToStringAnsi(FieldvaluePtr, 255).Replace("\0", ""); point.ID = FieldValue; } else { if (fieldInfs[i].FieldName == vitalFields.Name) { IntPtr FieldvaluePtr = ShapeLib.DBFReadStringAttribute(hDbf, pi, i); string FieldValue = Marshal.PtrToStringAnsi(FieldvaluePtr, 255).Replace("\0", ""); point.Name = FieldValue; } else { if (fieldInfs[i].FieldName == vitalFields.Address) { IntPtr FieldvaluePtr = ShapeLib.DBFReadStringAttribute(hDbf, pi, i); string FieldValue = Marshal.PtrToStringAnsi(FieldvaluePtr, 255).Replace("\0", ""); point.Addresss = FieldValue; } else { if (fieldInfs[i].FieldName == vitalFields.Xcoordinate) { IntPtr FieldvaluePtr = ShapeLib.DBFReadStringAttribute(hDbf, pi, i); string FieldValue = Marshal.PtrToStringAnsi(FieldvaluePtr, 255).Replace("\0", ""); point.X = FieldValue; } else { if (fieldInfs[i].FieldName == vitalFields.Ycoordinate) { IntPtr FieldvaluePtr = ShapeLib.DBFReadStringAttribute(hDbf, pi, i); string FieldValue = Marshal.PtrToStringAnsi(FieldvaluePtr, 255).Replace("\0", ""); point.Y = FieldValue; } else { if (fieldInfs[i].FieldType == "text") { IntPtr FieldvaluePtr = ShapeLib.DBFReadStringAttribute(hDbf, pi, i); string FieldValue = Marshal.PtrToStringAnsi(FieldvaluePtr, 255).Replace("\0", ""); other += "\"" + fieldInfs[i].FieldName + "\":\"" + FieldValue + "\","; } else { if (fieldInfs[i].FieldType == "integer") { int FieldValue = ShapeLib.DBFReadIntegerAttribute(hDbf, pi, i); other += "\"" + fieldInfs[i].FieldName + "\":" + FieldValue + ","; } else { double FieldValue = ShapeLib.DBFReadDoubleAttribute(hDbf, pi, i); other += "\"" + fieldInfs[i].FieldName + "\":" + FieldValue + ","; } } } } } } } } other = other.Substring(0, other.Length - 1) + "}"; point.Other = other; result.Add(point); } ShapeLib.DBFClose(hDbf); return(result); }