/// <summary> /// Utility method to write shapefiles /// </summary> /// <param name="filename">Output Shapefile name</param> private void writeShape(string filename) { IntPtr shphandle = ShapeLib.SHPCreate(filename,ShapeLib.ShapeType.PolyLine); double[] lat = new double[m_multiline.Count]; double[] lon = new double[m_multiline.Count]; int i=0; foreach(MeasureLine line in m_multiline) { lat[i] = line.StartLatitude.Degrees; lon[i] = line.StartLongitude.Degrees; i++; } ShapeLib.SHPObject poly = ShapeLib.SHPCreateSimpleObject(ShapeLib.ShapeType.Polygon,m_multiline.Count,lon,lat,null); ShapeLib.SHPWriteObject(shphandle,0,poly); ShapeLib.SHPDestroyObject(poly); ShapeLib.SHPClose(shphandle); }
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 static IGeometry GetGeometry2D(ShapeLib.SHPObject obj) { if (obj == null) { return(null); } int parts = obj.nParts; int verts = obj.nVertices; int part = 0, nextp = 0; double[] X = new double[verts]; double[] Y = new double[verts]; int[] pStart = new int[parts]; unsafe { int *pstart = (int *)obj.paPartStart.ToPointer(); //ShapeLib.PartType * pType =(ShapeLib.PartType *)obj.paPartType.ToPointer(); double *x = (double *)obj.padfX; double *y = (double *)obj.padfY; for (int i = 0; i < verts; i++) { X[i] = x[i]; Y[i] = y[i]; } for (int i = 0; i < parts; i++) { pStart[i] = pstart[i]; } } switch (obj.shpType) { case ShapeLib.ShapeType.Point: case ShapeLib.ShapeType.PointM: case ShapeLib.ShapeType.PointZ: return(new Point(X[0], Y[0])); case ShapeLib.ShapeType.PolyLine: case ShapeLib.ShapeType.PolyLineM: case ShapeLib.ShapeType.PolyLineZ: IPolyline polyline = new Polyline(); IPath path = null; for (int v = 0; v < verts; v++) { if (path != null && nextp == v) { polyline.AddPath(path); path = null; } if (path == null) { if (parts <= part + 1) { nextp = verts; } else { nextp = pStart[part + 1]; part++; } path = new gView.Framework.Geometry.Path(); } path.AddPoint(new Point(X[v], Y[v])); } polyline.AddPath(path); X = Y = null; return(polyline); case ShapeLib.ShapeType.Polygon: case ShapeLib.ShapeType.PolygonM: case ShapeLib.ShapeType.PolygonZ: IPolygon polygon = new Polygon(); IRing ring = null; for (int v = 0; v < verts; v++) { if (ring != null && nextp == v) { polygon.AddRing(ring); ring = null; } if (ring == null) { if (parts <= part + 1) { nextp = verts; } else { nextp = pStart[part + 1]; part++; } ring = new Ring(); } ring.AddPoint(new Point(X[v], Y[v])); } polygon.AddRing(ring); X = Y = null; return(polygon); } return(null); }