예제 #1
0
        private void CreateToEachShp(DataGridViewRowCollection rows, bool isSaveToSource)
        {
            var shpDir = this.txtShpDir.Text.Trim();

            for (int i = 0; i < rows.Count; i++)
            {
                try
                {
                    var path = rows[i].Cells["colFilePath2"].Value.ToString();
                    if (isSaveToSource)
                    {
                        shpDir = Path.GetDirectoryName(path);
                    }

                    var name       = rows[i].Cells["colFileName2"].Value.ToString();
                    var resultPath = Path.Combine(shpDir, name + ".shp");
                    if (File.Exists(path))
                    {
                        RedLineManager.TxtToShp(path, resultPath);
                    }

                    rows[i].Cells["colResultPath2"].Value = resultPath;
                    rows[i].Cells["colResult2"].Value     = "成功";
                }
                catch (Exception ex)
                {
                    rows[i].Cells["colResult2"].Value = ex.Message.Contains("GDAL_DATA")
                        ? "失败:要确保正常txt转shapefile,请将本工具放置在不含中文的目录中(" + ex.Message + "),且勿删除Data文件夹"
                        : "失败:" + ex.Message;
                }
                this.progressBar1.Value++;
                Application.DoEvents();
            }
        }
        private void btnRun_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count < 1)
            {
                return;
            }

            ChangedView(true);
            try
            {
                _stopRunning = false;
                var rows = dataGridView1.Rows;
                progressBar1.Maximum = rows.Count;
                foreach (DataGridViewRow row in rows)
                {
                    row.Cells["colResult"].Value = string.Empty;
                }
                Application.DoEvents();

                var layerName      = cmbLayers.SelectedItem.ToString();
                var layer          = dataSourceSelector1.GetLayerByName(layerName);
                var idFieldName    = cmbIdField.SelectedItem.ToString();
                var txtFileDir     = txtTextFileDir.Text.Trim();
                var featureDefn    = layer.GetLayerDefn();
                var idField        = featureDefn.GetFieldDefn(featureDefn.GetFieldIndex(idFieldName));
                var sqlFormat      = idField.GetFieldType() == FieldType.OFTString ? "{0} = '{1}'" : "{0} = {1}";
                var fileNameFormat = cmbNameField.SelectedIndex > 0 ? "{0}_{1}.txt" : "{0}.txt";

                foreach (DataGridViewRow row in rows)
                {
                    if (Convert.ToBoolean(row.Cells["colCheck"].Value) == true)
                    {
                        try
                        {
                            var id       = row.Cells["colID"].Value.ToString();
                            var name     = row.Cells["colName"].Value.ToString();
                            var filePath = Path.Combine(txtFileDir, string.Format(fileNameFormat, id, name));
                            var features = GdalHelper.GetFeatures(layer, string.Format(sqlFormat, idFieldName, id));
                            var txtInfo  = RedLineManager.GetProjInfoFromFeatures(features.ToArray());
                            RedLineManager.ToTxtFile(filePath, txtInfo);

                            row.Cells["colResultPath"].Value = filePath;
                            row.Cells["colResult"].Value     = "成功";
                        }
                        catch (Exception ex)
                        {
                            row.Cells["colResult"].Value = "失败:" + ex.Message;
                        }
                    }
                    progressBar1.Value++;
                    Application.DoEvents();
                    if (_stopRunning)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error); }
            ChangedView(false);
        }
예제 #3
0
        private void CreateToOneShp(DataGridViewRowCollection rows)
        {
            var shpInfos = new List <ShpSourceInfo>();
            var shpDir   = this.txtShpDir.Text.Trim();

            for (int i = 0; i < rows.Count; i++)
            {
                try
                {
                    var path = rows[i].Cells["colFilePath2"].Value.ToString();
                    if (File.Exists(path))
                    {
                        var           projInfo = RedLineManager.GetProjInfoFromTxt(path);
                        ShpSourceInfo shpInfo  = null;
                        foreach (var info in shpInfos)
                        {
                            if (info.Wkid == projInfo.Wkid)
                            {
                                shpInfo = info; break;
                            }
                        }
                        Layer      layer;
                        DataSource dataSource;
                        if (shpInfo == null)
                        {
                            var shpName = $"DH{projInfo.ProjZone}_{projInfo.Wkid}_{DateTime.Now:yyyy_MM_dd_HH_mm_ss}.shp";
                            var shpPath = Path.Combine(shpDir, shpName);
                            dataSource = RedLineManager.CreateShapefileSource(shpPath);
                            layer      = RedLineManager.CreateRedLineLayer(dataSource, projInfo.Wkid, shpPath);
                            shpInfo    = new ShpSourceInfo {
                                Wkid = projInfo.Wkid, ShpPath = shpPath
                            };
                            shpInfos.Add(shpInfo);
                        }
                        else
                        {
                            var shpPath = shpInfo.ShpPath;
                            dataSource = Ogr.Open(shpPath, 1);
                            layer      = dataSource.GetLayerByName(Path.GetFileNameWithoutExtension(shpPath));
                        }
                        RedLineManager.ToLayer(projInfo, layer);
                        rows[i].Cells["colResultPath2"].Value = shpInfo.ShpPath;
                        rows[i].Cells["colResult2"].Value     = "成功";
                    }
                }
                catch (Exception ex)
                {
                    rows[i].Cells["colResult2"].Value = ex.Message.Contains("GDAL_DATA")
                        ? "失败:要确保正常txt转shapefile,请将本工具放置在不含中文的目录中(" + ex.Message + "),且勿删除Data文件夹"
                        : "失败:" + ex.Message;
                }
                this.progressBar1.Value++;
                Application.DoEvents();
            }
        }
        private void btnRun_Click(object sender, EventArgs e)//转换
        {
            bool   isSaveToSource = this.cbSaveToSourceShpDir.Checked;
            string txtDir         = null;

            if (!isSaveToSource)
            {
                if (!Directory.Exists(txtDir = this.txtTextFileDir.Text.Trim()))
                {
                    MessageBox.Show(@"TXT保存目录不存在,请选择正确的TXT保存目录!", @"坐标文件转换", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            ChangedView(true);
            var rows = this.dataGridView1.Rows;

            this.progressBar1.Maximum = rows.Count;

            foreach (DataGridViewRow row in rows)
            {
                row.Cells["colResult"].Value = "";
            }

            for (int i = 0; i < rows.Count; i++)
            {
                try
                {
                    var path = rows[i].Cells["colFilePath"].Value.ToString();
                    var name = rows[i].Cells["colFileName"].Value.ToString();
                    if (isSaveToSource)
                    {
                        txtDir = Path.GetDirectoryName(path);
                    }
                    var resultPath = Path.Combine(txtDir, name + ".txt");
                    if (File.Exists(path))
                    {
                        RedLineManager.ShpToTxt(path, resultPath);
                    }

                    rows[i].Cells["colResultPath"].Value = resultPath;
                    rows[i].Cells["colResult"].Value     = "成功";
                }
                catch (Exception ex)
                {
                    rows[i].Cells["colResult"].Value = "失败:" + ex.Message;
                }
                this.progressBar1.Value++;
                Application.DoEvents();
            }
            ChangedView(false);
        }