using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using ESRI.ArcGIS.Geodatabase; using KGIS.Framework.Utils; using KGIS.Framework.Utils.Helper; namespace Kingo.Plugin.MakeTaskPackage.View { /// /// FrmCustomExtent.xaml 的交互逻辑 /// public partial class FrmXZQSelected : BaseWindow { public IFeatureClass ImportFeatureClass { get; set; } public string CustomLayerPath { get; set; } public string CustomLayerName { get; set; } public List KeyFieldsList = new List(); public Dictionary geometrys = new Dictionary (); public string KeyField = ""; public string WhereStr = ""; public Action GetCustomXZQInfo; public FrmXZQSelected() { InitializeComponent(); } private void FrmXZQSelected_Loaded(object sender, RoutedEventArgs e) { if (ImportFeatureClass == null) return; IFeatureCursor featureCursor = null; try { DataTable dtNew =null; DataTable dtLayer = null; if (CustomLayerPath.ToLower().Contains(".gdb") || CustomLayerPath.ToLower().Contains(".mdb")) { if(CustomLayerPath.ToLower().Contains(".gdb")) CustomLayerPath = CustomLayerPath.Substring(0, CustomLayerPath.LastIndexOf(".gdb")+4); dtLayer = Common.GdalShapeOperate.Instance.GDBToDataTable(CustomLayerPath, CustomLayerName); } else { dtLayer = Common.GdalShapeOperate.Instance.ShapeToDataTable(CustomLayerPath); } if (dtLayer != null) { dtNew = dtLayer.Clone(); foreach (DataRow s in dtLayer.Rows) { dtNew.ImportRow(s);//导入旧数据 } if (dtNew != null) { GetCobFields(dtNew); dtNew.Columns.Add("IsValid", typeof(bool)); foreach (System.Data.DataRow item in dtNew.Rows) { item["IsValid"] = false; } } } this.Dispatcher.Invoke(() => { this.gridData.ItemsSource = null; this.gridData.ItemsSource = dtNew; this.gridData.Columns[0].AllowEditing = DevExpress.Utils.DefaultBoolean.True; this.combFields.SelectedIndex = 0; if (this.gridData.Columns["Shape"] != null) this.gridData.Columns["Shape"].Visible = false; if (this.gridData.Columns["SHAPE"] != null) this.gridData.Columns["SHAPE"].Visible = false; }); } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (featureCursor != null) Marshal.FinalReleaseComObject(featureCursor); } } /// /// 获取下拉列表字段 /// /// private void GetCobFields(DataTable table) { for (int i = 0; i < table.Columns.Count; i++) { DataColumn dc = table.Columns[i]; if (dc.ColumnName.ToLower() == "shape") continue; Type fieldType = dc.DataType; string whereStr = ""; switch (fieldType.Name) { case "Double": case "Decimal": case "Int32": case "Int64": whereStr = dc.ColumnName + "={0}"; break; case "String": whereStr = dc.ColumnName+ "='{0}'"; break; default: whereStr = dc.ColumnName + "='{0}'"; break; } AddComboxItem(dc.Caption, dc.ColumnName, whereStr); } } private void AddComboxItem(string name, string code, string whereStr) { ComboxItemClass itemClass = new ComboxItemClass() { Name = name, Code = code, WhereStr = whereStr }; this.combFields.Items.Add(itemClass); } public class ComboxItemClass { public string Name { get; set; } public string Code { get; set; } public string WhereStr { get; set; } } private void btnOK_Click(object sender, RoutedEventArgs e) { KeyField = (this.combFields.SelectedItem as ComboxItemClass).Code; WhereStr = (this.combFields.SelectedItem as ComboxItemClass).WhereStr; DataTable dt = this.gridData.ItemsSource as DataTable; foreach (DataRow item in dt.Rows) { if (item["IsValid"].ToString() == "True") { if (!KeyFieldsList.Contains(item[KeyField].ToString())) KeyFieldsList.Add(item[KeyField].ToString()); if (!geometrys.Keys.Contains(item[KeyField].ToString())) geometrys.Add(item[KeyField].ToString(), item["SHAPE"] as OSGeo.OGR.Geometry); } } Type fieldType = dt.Columns[KeyField].DataType; if (fieldType.Name.ToLower() != "double" && fieldType.Name.ToLower() != "decimal") { var data = (from a in dt.AsEnumerable() group a by a.Field(KeyField) into g where g.Count() > 1 select new { value = g.Key }).ToList(); if (data != null && data.Count != 0) { MessageHelper.Show(string.Format("生成文件名称{0}字段值不唯一!请重新选择字段!", KeyField)); return; } } if (GetCustomXZQInfo != null) GetCustomXZQInfo(); this.Close(); } private void btnCancel_Click(object sender, RoutedEventArgs e) { this.Close(); } private void CheckBox_Click(object sender, RoutedEventArgs e) { try { if (gridData.SelectedItem != null) { DataRowView dr = (gridData.SelectedItem as DataRowView); if ((sender as CheckBox).IsChecked == true) { dr["IsValid"] = true; } else { dr["IsValid"] = false; } } } catch (Exception ex) { LogAPI.Debug(ex); } } } }