Esempio n. 1
0
        private void filter_true()
        {
            string path = textBox_folder.Text;

            comboStart = dataGridView1.Rows[0].Cells[0].Value.ToString();
            comboEnd   = dataGridView1.Rows[0].Cells[1].Value.ToString();

            string save_LM    = "UFL_OPIP_LM" + "_" + comboStart + "_" + comboEnd;
            string save_PS    = "UFL_OPIP_PS" + "_" + comboStart + "_" + comboEnd;
            string temp_LM    = "UFL_OPIP_LM";
            string temp_PS    = "UFL_OPIP_PS";
            string psFilePath = path + "\\" + save_PS;
            string lmFilePath = path + "\\" + save_LM;
            int    dRcnt      = dataGridView1.Rows.Count - 1;
            int    dCcnt      = dataGridView1.Columns.Count;

            textBox_status.AppendText("Shape파일을 생성합니다..." + "\r\n" + "\r\n");

            Driver     driver      = Ogr.GetDriverByName("ESRI Shapefile");
            DataSource data_source = driver.CreateDataSource(path, new string[] { "ENCODING=UTF-8" });

            OSGeo.OSR.SpatialReference srs = new OSGeo.OSR.SpatialReference("");
            srs.ImportFromEPSG(5186);

            // MessageBox.Show( dataGridView1.Rows[1].Cells[5].Value.ToString());

            System.IO.FileInfo fi = new System.IO.FileInfo(psFilePath + ".shp");
            if (fi.Exists)
            {
                MessageBox.Show("파일이 있습니다");
                File.Delete(psFilePath + ".prj");
                File.Delete(psFilePath + ".shp");
                File.Delete(psFilePath + ".dbf");
                File.Delete(psFilePath + ".shx");
            }
            else
            {
                //Ps
                var layer = data_source.CreateLayer(temp_PS, srs, wkbGeometryType.wkbPoint, new string[] { "ENCODING=UTF-8" });
                textBox_status.AppendText(psFilePath + ".shp 파일 생성" + "\r\n");
                //string x_temp = dataGridView1.Rows[0].Cells[2].Value.ToString();

                FieldDefn ftr_cde     = new FieldDefn("FTR_CDE", FieldType.OFTString);
                FieldDefn hjd_cde     = new FieldDefn("HJD_CDE", FieldType.OFTString);
                FieldDefn pip_dep     = new FieldDefn("PIP_DEP", FieldType.OFTReal);
                FieldDefn start_point = new FieldDefn("시점", FieldType.OFTString);
                FieldDefn end_point   = new FieldDefn("종점", FieldType.OFTString);
                FieldDefn field_x     = new FieldDefn("X", FieldType.OFTReal);
                FieldDefn field_y     = new FieldDefn("Y", FieldType.OFTReal);
                FieldDefn field_z     = new FieldDefn("Z", FieldType.OFTReal);

                layer.CreateField(ftr_cde, 1);
                layer.CreateField(hjd_cde, 1);
                layer.CreateField(pip_dep, 1);
                layer.CreateField(start_point, 1);
                layer.CreateField(end_point, 1);
                layer.CreateField(field_x, 1);
                layer.CreateField(field_y, 1);
                layer.CreateField(field_z, 1);

                FeatureDefn ftr = layer.GetLayerDefn();
                //FeatureDefn ftr = new FeatureDefn(null);
                ftr.SetGeomType(layer.GetLayerDefn().GetGeomType());
                Feature  ipFeature = new Feature(ftr);
                string   wktPointZ = "";
                Geometry ipGeom    = null;
                for (int i = 0; i < dRcnt; ++i)
                {
                    wktPointZ = String.Format("POINT Z({0} {1} {2})", dataGridView1.Rows[i].Cells[2].Value.ToString(), dataGridView1.Rows[i].Cells[3].Value.ToString(),
                                              dataGridView1.Rows[i].Cells[4].Value.ToString());
                    ipGeom = Ogr.CreateGeometryFromWkt(ref wktPointZ, srs);
                    ipFeature.SetGeometry(ipGeom);
                    ipFeature.SetField("FTR_CDE", "SF900");
                    ipFeature.SetField("HJD_CDE", "");
                    ipFeature.SetField("PIP_DEP", "");
                    ipFeature.SetField("시점", dataGridView1.Rows[i].Cells[0].Value.ToString());
                    ipFeature.SetField("종점", dataGridView1.Rows[i].Cells[1].Value.ToString());
                    ipFeature.SetField("X", dataGridView1.Rows[i].Cells[2].Value.ToString());
                    ipFeature.SetField("Y", dataGridView1.Rows[i].Cells[3].Value.ToString());
                    ipFeature.SetField("Z", dataGridView1.Rows[i].Cells[4].Value.ToString());
                    layer.CreateFeature(ipFeature);
                    progressBar1.PerformStep();
                }
                layer.CommitTransaction();
                layer.SyncToDisk();
                layer.Dispose();
                data_source.Dispose();

                System.IO.FileInfo fil = new System.IO.FileInfo(path + "\\" + temp_PS + ".shp");
                if (fil.Exists)
                {
                    System.IO.File.Move(path + "\\" + temp_PS + ".shp", psFilePath + ".shp");
                    System.IO.File.Move(path + "\\" + temp_PS + ".cpg", psFilePath + ".cpg");
                    System.IO.File.Move(path + "\\" + temp_PS + ".dbf", psFilePath + ".dbf");
                    System.IO.File.Move(path + "\\" + temp_PS + ".prj", psFilePath + ".prj");
                    System.IO.File.Move(path + "\\" + temp_PS + ".shx", psFilePath + ".shx");
                }
            }


            //LM
            System.IO.FileInfo fLM = new System.IO.FileInfo(lmFilePath + ".shp");
            if (fLM.Exists)
            {
                MessageBox.Show(lmFilePath + "이 있습니다");
                File.Delete(lmFilePath + ".prj");
                File.Delete(lmFilePath + ".shp");
                File.Delete(lmFilePath + ".dbf");
                File.Delete(lmFilePath + ".shx");
            }
            else
            {
                //LM
                DataSource data_source2 = driver.CreateDataSource(path, new string[] { "ENCODING=UTF-8" });

                var layer = data_source2.CreateLayer(temp_LM, srs, wkbGeometryType.wkbLineString, new string[] { "ENCODING=UTF-8" });
                textBox_status.AppendText(lmFilePath + ".shp 파일 생성" + "\r\n");
                FieldDefn ftr_cde     = new FieldDefn("FTR_CDE", FieldType.OFTString);
                FieldDefn hjd_cde     = new FieldDefn("HJD_CDE", FieldType.OFTString);
                FieldDefn pip_dep     = new FieldDefn("PIP_DEP", FieldType.OFTReal);
                FieldDefn start_point = new FieldDefn("시점", FieldType.OFTString);
                FieldDefn end_point   = new FieldDefn("종점", FieldType.OFTString);
                FieldDefn field_x     = new FieldDefn("X", FieldType.OFTReal);
                FieldDefn field_y     = new FieldDefn("Y", FieldType.OFTReal);
                FieldDefn field_z     = new FieldDefn("Z", FieldType.OFTReal);

                layer.CreateField(ftr_cde, 1);
                layer.CreateField(hjd_cde, 1);
                layer.CreateField(pip_dep, 1);
                layer.CreateField(start_point, 1);
                layer.CreateField(end_point, 1);
                layer.CreateField(field_x, 1);
                layer.CreateField(field_y, 1);
                layer.CreateField(field_z, 1);
                FeatureDefn ftr = layer.GetLayerDefn();
                //FeatureDefn ftr = new FeatureDefn(null);

                ftr.SetGeomType(layer.GetLayerDefn().GetGeomType());
                Feature  ipFeature = new Feature(ftr);
                Geometry ipGeom    = null;
                string   lineWKT   = "LINESTRING (";
                for (int i = 0; i < dRcnt; ++i)
                {
                    string s_x = dataGridView1.Rows[i].Cells[2].Value.ToString();
                    string s_y = dataGridView1.Rows[i].Cells[3].Value.ToString();
                    if (i == dRcnt - 1)
                    {
                        lineWKT = lineWKT + s_x + " " + s_y + ")";
                        progressBar1.PerformStep();
                    }
                    else
                    {
                        lineWKT = lineWKT + s_x + " " + s_y + ",";
                        progressBar1.PerformStep();
                    }
                }
                ipGeom = Ogr.CreateGeometryFromWkt(ref lineWKT, srs);
                ipFeature.SetGeometry(ipGeom);
                ipFeature.SetField("FTR_CDE", "SF900");
                ipFeature.SetField("HJD_CDE", "");
                ipFeature.SetField("PIP_DEP", "");
                ipFeature.SetField("시점", dataGridView1.Rows[0].Cells[0].Value.ToString());
                ipFeature.SetField("종점", dataGridView1.Rows[0].Cells[1].Value.ToString());
                layer.CreateFeature(ipFeature);
                layer.CommitTransaction();
                layer.SyncToDisk();
                layer.Dispose();
                data_source2.Dispose();

                System.IO.FileInfo film = new System.IO.FileInfo(path + "\\" + temp_LM + ".shp");
                if (film.Exists)
                {
                    System.IO.File.Move(path + "\\" + temp_LM + ".shp", lmFilePath + ".shp");
                    System.IO.File.Move(path + "\\" + temp_LM + ".cpg", lmFilePath + ".cpg");
                    System.IO.File.Move(path + "\\" + temp_LM + ".dbf", lmFilePath + ".dbf");
                    System.IO.File.Move(path + "\\" + temp_LM + ".prj", lmFilePath + ".prj");
                    System.IO.File.Move(path + "\\" + temp_LM + ".shx", lmFilePath + ".shx");
                }
                textBox_status.AppendText("\r\n" + "Shape파일 생성이 완료되었습니다.");
            }
        }