Exemplo n.º 1
0
        private void generateShapeInfo()
        {
            if (!this.hShpIsOpen)
            {
                hShp = ShapeLib.SHPOpenW(this.filePath, this.access);
            }
            if (hShp.Equals(IntPtr.Zero))
            {
                MessageBox.Show("Oops - hShp = 0");
            }
            else
            {
                // get shape info and verify shapes were created correctly
                double[]           minB      = new double[4];
                double[]           maxB      = new double[4];
                int                nEntities = 0;
                ShapeLib.ShapeType shapeType = ShapeLib.ShapeType.PolyLine;
                ShapeLib.SHPGetInfo(hShp, ref nEntities, ref shapeType, minB, maxB);
                this.numberOfShapes = nEntities;
                this.shapeType      = shapeType;
                this.minB           = minB;
                this.maxB           = maxB;

                ShapeLib.SHPClose(hShp);
                this.hShpIsOpen      = false;
                this.isShapeInfoRead = true;
            }
        }
Exemplo n.º 2
0
        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();
            }