示例#1
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (null == tempPath)
                {
                    FolderBrowserDialog dlgFolder = new FolderBrowserDialog();
                    if (dlgFolder.ShowDialog() == DialogResult.OK)
                    {
                        tempPath = dlgFolder.SelectedPath + "\\";
                    }
                    else //No output folder chosen, exit
                    {
                        return;
                    }
                }
                int count = lstToAddLayers.Items.Count;
                for (int i = 0; i < count; i++)
                {
                    MapWinGIS.Shapefile sf = new MapWinGIS.ShapefileClass();
                    string featureName     = lstToAddLayers.Items[i].ToString();
                    string strSql          = null;

                    switch (frmConnection.DbType)
                    {
                    case "MySQL 5.0":
                        strSql = "select Shape from " + featureName;
                        MySql.Data.MySqlClient.MySqlDataAdapter daMySql = new MySql.Data.MySqlClient.MySqlDataAdapter(strSql, (MySqlConnection)frmConnection.SqlConnection);
                        DataSet dsMySql = new DataSet();
                        daMySql.Fill(dsMySql);
                        DataTable dtMySql = dsMySql.Tables[0];

                        int rowCountMySql = dtMySql.Rows.Count;

                        if (rowCountMySql > 0)
                        {
                            MapWinGIS.Shape shp = new MapWinGIS.Shape();
                            shp.CreateFromString((string)dtMySql.Rows[i][0]);
                            sf.CreateNew(tempPath + featureName + ".shp", shp.ShapeType);
                            sf.StartEditingShapes(true, null);
                            int stupidx = 0;
                            sf.EditInsertShape(shp, ref stupidx);

                            for (int j = 1; j < rowCountMySql; j++)
                            {
                                MapWinGIS.Shape newShp = new MapWinGIS.Shape();
                                newShp.CreateFromString((string)dtMySql.Rows[j][0]);
                                int veryStupidRefForTheFunEditInsertShape = sf.NumShapes;
                                sf.EditInsertShape(newShp, ref veryStupidRefForTheFunEditInsertShape);
                            }
                            sf.StopEditingShapes(true, true, null);
                        }
                        break;
                    //case "Oracle 9i":

                    //    break;
                    //case "Oracle 10g":
                    //    strSql = "select \"Shape\" from OPENHYDRO." + "\"" + featureName + "\"";
                    //    OracleDataAdapter daOra = new OracleDataAdapter(strSql, (OracleConnection)frmConnection.SqlConnection);
                    //    DataSet dsOra = new DataSet();
                    //    daOra.Fill(dsOra);
                    //    DataTable dtOra = dsOra.Tables[0];

                    //    int rowCountOra = dtOra.Rows.Count;

                    //    if (rowCountOra > 0)
                    //    {

                    //        MapWinGIS.Shape shp = new MapWinGIS.Shape();
                    //        shp.CreateFromString((string)dtOra.Rows[i][0]);
                    //        sf.CreateNew(tempPath + featureName + ".shp", shp.ShapeType);
                    //        sf.StartEditingShapes(true, null);
                    //        int stupidx = 0;
                    //        sf.EditInsertShape(shp, ref stupidx);

                    //        for (int j = 1; j < rowCountOra; j++)
                    //        {
                    //            MapWinGIS.Shape newShp = new MapWinGIS.Shape();
                    //            newShp.CreateFromString((string)dtOra.Rows[j][0]);
                    //            int veryStupidRefForTheFunEditInsertShape = sf.NumShapes;
                    //            sf.EditInsertShape(newShp, ref veryStupidRefForTheFunEditInsertShape);
                    //        }
                    //        sf.StopEditingShapes(true, true, null);
                    //    }

                    //    break;
                    case "PostGIS":

                        strSql = "select f_table_name from geometry_columns";
                        IDbDataAdapter daPgsql = new Npgsql.NpgsqlDataAdapter(strSql, (Npgsql.NpgsqlConnection)frmConnection.SqlConnection);
                        DataSet        dsPg    = new DataSet();
                        daPgsql.Fill(dsPg);
                        DataTable dtPg = dsPg.Tables[0];

                        //store user name and password information for future usage:
                        string username = frmConnection.Username;
                        string password = frmConnection.Password;

                        for (int x = 0; x < dtPg.Rows.Count; x++)
                        {
                            //Export all the features into shapefiles and then open them in MapWindow:
                            string fName  = (string)dtPg.Rows[x][0];
                            string strCmd = " -u " + username + " -P " + password + " postgis " + fName + " -f " + tempPath + fName + ".shp";
                            if (lstToAddLayers.Items.Contains(fName))
                            {
                                System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
                                info.FileName        = "pgsql2shp.exe ";
                                info.Arguments       = strCmd;
                                info.CreateNoWindow  = true;
                                info.UseShellExecute = false;
                                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                                proc.StartInfo = info;
                                proc.Start();
                                proc.WaitForExit();
                            }
                        }

                        break;

                    default:
                        break;
                    }
                }
                this.Hide();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }