using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.DataSourcesGDB; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using KGIS.Framework.AE; using KGIS.Framework.AE.GPHelper; using KGIS.Framework.DBOperator; using KGIS.Framework.Maps; using KGIS.Framework.Platform; using KGIS.Framework.Platform.Helper; using KGIS.Framework.Utils; using KGIS.Framework.Utils.Helper; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Odbc; using System.Globalization; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using System.Windows.Data; namespace Kingo.Plugin.BGResultManager.View { /// /// FrmJZCGImport.xaml 的交互逻辑 /// public partial class FrmJZCGImport : BaseWindow { /// /// 举证包路径 /// string dbPath = string.Empty; public string CGType { get; set; } public FrmJZCGImport(IHookHelper hookHelper) { InitializeComponent(); } private void btnLoad_Click(object sender, RoutedEventArgs e) { try { ProgressHelper.ShowProcessBar("正在导入..."); IFeatureClass featureClass = MapsManager.Instance.MapService.GetFeatureClassByName("JZCG"); if (this.txtbzcg != null && !string.IsNullOrWhiteSpace(this.txtbzcg.Text) && string.IsNullOrWhiteSpace(dbPath)) { dbPath = this.txtbzcg.Text; } if (string.IsNullOrWhiteSpace(dbPath)) { ProgressHelper.CloseProcessBar(); MessageHelper.ShowTips("未选择导入数据!"); return; } DataTable rowData = null; string geofield = ""; rowData = SQLiteDBOperate.Instance.ExceDataTable(dbPath, "select * from BGDC2020"); geofield = "TBFW";//图形字段 if (rowData != null && rowData.Rows.Count > 0) { InsertPolygonToTYSDJZ(rowData, "JZCG", geofield, "", radTH.IsChecked == true ? true : false, featureClass); ProgressHelper.CloseProcessBar(); MessageHelper.ShowTips("导入完成!"); } else { ProgressHelper.CloseProcessBar(); MessageHelper.ShowTips("成果查询为空!"); } this.Close(); } catch (Exception ex) { LogAPI.Debug(ex); ProgressHelper.CloseProcessBar(); MessageHelper.ShowError("举证包数据导入失败:" + ex.Message); } } private void btnClose_Click(object sender, RoutedEventArgs e) { this.Close(); } /// /// 统一时点举证转图形并插入当前工程 /// /// 数据源 /// 图层名 /// 图形字段 /// 字段映射 /// 目标库路径 /// 是否替换 /// 数据源类型 public void InsertPolygonToTYSDJZ(DataTable dtSource, string featureName, string shapCol, string targetPath, bool replace, IFeatureClass featureClass) { IFeatureCursor insertfeaCursor = null; IFeatureCursor updatefeaCursor = null; IFeature pUpdatefeature = null; insertfeaCursor = featureClass.Insert(true); IFeatureBuffer feaBuffer = featureClass.CreateFeatureBuffer(); try { ProgressHelper.ShowProcessBar("正在导入..."); foreach (DataRow item in dtSource.Rows) { IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = string.Format("TBBH='{0}'", item["JCBH"].ToString().TrimEnd()); updatefeaCursor = featureClass.Update(queryFilter, false); if ((pUpdatefeature = updatefeaCursor.NextFeature()) != null) { if (replace)//替换 { foreach (DataColumn col in item.Table.Columns) { //TBFW字段太长,而且无参考意义 if (col.ColumnName == "TBFW" || col.ColumnName == "SHAPE" || col.ColumnName == "JCBH") { continue; } int fieldindex = SetFeatureValue(ref pUpdatefeature, item, col); if (fieldindex == -1) { continue; } } SetDBPath(null, pUpdatefeature); updatefeaCursor.UpdateFeature(pUpdatefeature); Marshal.ReleaseComObject(pUpdatefeature); } else//跳过 { continue; } } else { IGeometry geometry = null; ISpatialReference spatialReference = (featureClass as IGeoDataset).SpatialReference; geometry = GetPolygonFromWkt(item[shapCol].ToString()); //fs = KGIS.Framework.AE.FeatureAPI.Identify(geometry, pDLTBBGfeatureLayer);//判断举证成果是否压盖到地类图斑变更层,未压盖到不进行导入 //if (fs == null || fs.Count == 0) //{ // continue; //} ITopologicalOperator topo = geometry as ITopologicalOperator; if (!topo.IsSimple) topo.Simplify(); feaBuffer.Shape = geometry; int t_fieldindex = -1; //for (int i = 0; i < fs.Count; i++) //{ // IFeature newfeature = fs[i]; // int jcbhIndex = newfeature.Fields.FindField("JCBH"); // t_fieldindex = feaBuffer.Fields.FindField("TBBH"); // if (jcbhIndex >= 0) // { // feaBuffer.set_Value(t_fieldindex, newfeature.get_Value(jcbhIndex).ToString()); // } foreach (DataColumn col in item.Table.Columns) { if (col.ColumnName == "TBFW" ) { continue; } t_fieldindex = SetFeaBufferValue(ref feaBuffer, item, col); if (t_fieldindex == -1) { continue; } } //} //保存hcdb路径 SetDBPath(feaBuffer, null); insertfeaCursor.InsertFeature(feaBuffer); } } updatefeaCursor.Flush(); insertfeaCursor.Flush(); //修复要素,防止SHAPE_AREA为负数 GeoprocessorHelper.RepairGeo(featureClass, true); } catch (Exception ex) { throw ex; } finally { if (updatefeaCursor != null) Marshal.ReleaseComObject(updatefeaCursor); if (insertfeaCursor != null) Marshal.ReleaseComObject(insertfeaCursor); if (feaBuffer != null) Marshal.ReleaseComObject(feaBuffer); if (pUpdatefeature != null) Marshal.ReleaseComObject(pUpdatefeature); //if (featureClass != null) // Marshal.ReleaseComObject(featureClass); //if (pDLTBBGfeatureLayer != null) // Marshal.ReleaseComObject(pDLTBBGfeatureLayer); //if (pDLTBBGfeatureLayer != null) // Marshal.ReleaseComObject(pDLTBBGfeatureLayer); } } private int SetFeaBufferValue(ref IFeatureBuffer feaBuffer,DataRow row, DataColumn col) { int t_fieldindex = feaBuffer.Fields.FindField(col.ColumnName); if (t_fieldindex == -1 && col.ColumnName == "JCBH") { t_fieldindex = feaBuffer.Fields.FindField("TBBH"); } if (t_fieldindex == -1 && col.ColumnName == "JCMJ") { t_fieldindex = feaBuffer.Fields.FindField("TBMJ"); } if (t_fieldindex == -1 && col.ColumnName == "BGDL") { t_fieldindex = feaBuffer.Fields.FindField("WYRDDL"); } if (t_fieldindex == -1 && col.ColumnName == "JZRY") { t_fieldindex = feaBuffer.Fields.FindField("DHRY"); } if (t_fieldindex == -1) return t_fieldindex; double mj = 0; if (col.ColumnName == "XXTBKD" || col.ColumnName == "TBMJ" || col.ColumnName == "JCMJ") { if (row[col.ColumnName] != null && !string.IsNullOrWhiteSpace(row[col.ColumnName].ToString())) { mj = Convert.ToDouble(row[col.ColumnName].ToString()); } feaBuffer.set_Value(t_fieldindex, mj); } else { feaBuffer.set_Value(t_fieldindex, row[col.ColumnName] is System.DBNull ? "" : row[col.ColumnName].ToString().TrimEnd()); } return t_fieldindex; } private int SetFeatureValue(IFeatureBuffer feaBuffer, ref IFeature feature, DataRow row, DataColumn col) { int t_fieldindex = feaBuffer.Fields.FindField(col.ColumnName); if (t_fieldindex == -1 && col.ColumnName == "JCBH") { t_fieldindex = feaBuffer.Fields.FindField("TBBH"); } if (t_fieldindex == -1 && col.ColumnName == "JCMJ") { t_fieldindex = feaBuffer.Fields.FindField("TBMJ"); } if (t_fieldindex == -1 && col.ColumnName == "BGDL") { t_fieldindex = feaBuffer.Fields.FindField("WYRDDL"); } if (t_fieldindex == -1 && col.ColumnName == "JZRY") { t_fieldindex = feaBuffer.Fields.FindField("DHRY"); } if (t_fieldindex == -1) return t_fieldindex; double mj = 0; if (col.ColumnName == "XXTBKD" || col.ColumnName == "TBMJ") { if (row[col.ColumnName] != null && !string.IsNullOrWhiteSpace(row[col.ColumnName].ToString())) { mj = Convert.ToDouble(row[col.ColumnName].ToString()); } feature.set_Value(t_fieldindex, mj); } else { feature.set_Value(t_fieldindex, row[col.ColumnName] is System.DBNull ? "" : row[col.ColumnName].ToString().TrimEnd()); } return t_fieldindex; } private int SetFeatureValue(ref IFeature feature, DataRow row, DataColumn col) { int t_fieldindex = feature.Fields.FindField(col.ColumnName); if (t_fieldindex == -1 && col.ColumnName == "JCBH") { t_fieldindex = feature.Fields.FindField("TBBH"); } if (t_fieldindex == -1 && col.ColumnName == "JCMJ") { t_fieldindex = feature.Fields.FindField("TBMJ"); } if (t_fieldindex == -1 && col.ColumnName == "BGDL") { t_fieldindex = feature.Fields.FindField("WYRDDL"); } if (t_fieldindex == -1 && col.ColumnName == "JZRY") { t_fieldindex = feature.Fields.FindField("DHRY"); } if (t_fieldindex == -1) return t_fieldindex; double mj = 0; if (col.ColumnName == "XXTBKD" || col.ColumnName == "TBMJ") { if (row[col.ColumnName] != null && !string.IsNullOrWhiteSpace(row[col.ColumnName].ToString())) { mj = Convert.ToDouble(row[col.ColumnName].ToString()); } feature.set_Value(t_fieldindex, mj); } else { feature.set_Value(t_fieldindex, row[col.ColumnName] is System.DBNull ? "" : row[col.ColumnName].ToString().TrimEnd()); } return t_fieldindex; } private ISpatialReference ChangeLayerRef(IFeatureClass pFeatureClass, int wkid) { try { //QI到IGeoDataset IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset; //QI到IGeoDatasetSchemaEdit IGeoDatasetSchemaEdit pGeoDatasetSchemaEdit = pGeoDataset as IGeoDatasetSchemaEdit; if (pGeoDatasetSchemaEdit.CanAlterSpatialReference == true) { ISpatialReferenceFactory2 pSpaRefFactory = new SpatialReferenceEnvironmentClass(); ISpatialReference spatialReference = GeoDBAPI.CreteSpatialReference(wkid); if (spatialReference is IGeographicCoordinateSystem) { IGeographicCoordinateSystem pNewGeoSys = pSpaRefFactory.CreateGeographicCoordinateSystem(wkid); pGeoDatasetSchemaEdit.AlterSpatialReference(pNewGeoSys); return pNewGeoSys as ISpatialReference; } else { IProjectedCoordinateSystem pNewGeoSys2 = pSpaRefFactory.CreateProjectedCoordinateSystem(wkid); pGeoDatasetSchemaEdit.AlterSpatialReference(pNewGeoSys2); return pNewGeoSys2 as ISpatialReference; } } return null; } catch (Exception Err) { return null; } } public string GetNestJsonValue(JEnumerable jToken, string key) { System.Collections.IEnumerator enumerator = jToken.GetEnumerator(); while (enumerator.MoveNext()) { JToken jc = (JToken)enumerator.Current; if (jc is JObject || ((JProperty)jc).Value is JObject) { return GetNestJsonValue(jc.Children(), key); } else { if (((JProperty)jc).Name == key) { return ((JProperty)jc).Value.ToString(); } } } return null; } /// /// 保存三种举证db路径 /// /// /// public void SetDBPath(IFeatureBuffer featureBuffer, IFeature feature) { if (featureBuffer != null && featureBuffer.Fields.FindField("BZFJPATH") >= 0) { featureBuffer.set_Value(featureBuffer.Fields.FindField("BZFJPATH"), dbPath); } else if (feature != null && feature.Fields.FindField("BZFJPATH") >= 0) { feature.set_Value(feature.Fields.FindField("BZFJPATH"), dbPath); } } public IGeometry GetPolygonFromWkt(string pointsStr) { //OSGeo.OGR.Ogr.RegisterAll(); IGeometry geometry = new PolygonClass(); OSGeo.OGR.Geometry rstGeometry = OSGeo.OGR.Geometry.CreateFromWkt(pointsStr); byte[] geometryBytes = new byte[rstGeometry.WkbSize()]; rstGeometry.ExportToWkb(geometryBytes); IGeometryFactory3 factory = new GeometryEnvironment() as IGeometryFactory3; int bytesLen = geometryBytes.Length; factory.CreateGeometryFromWkbVariant(geometryBytes, out geometry, out bytesLen); IPolygon polygon = geometry as IPolygon; polygon.Close(); ITopologicalOperator pBoundaryTop = polygon as ITopologicalOperator; pBoundaryTop.Simplify(); return geometry; } /// /// JSON字符串转成IGeometry /// private ESRI.ArcGIS.Geometry.IGeometry GeometryFromJsonString(string strJson, ESRI.ArcGIS.Geometry.esriGeometryType type) { return GeometryFromJsonString(strJson, type, false, false); } /// /// JSON字符串转成IGeometry /// private ESRI.ArcGIS.Geometry.IGeometry GeometryFromJsonString(string strJson, ESRI.ArcGIS.Geometry.esriGeometryType type, bool bHasZ, bool bHasM) { ESRI.ArcGIS.esriSystem.IJSONReader jsonReader = new ESRI.ArcGIS.esriSystem.JSONReaderClass(); jsonReader.ReadFromString(strJson); ESRI.ArcGIS.Geometry.JSONConverterGeometryClass jsonCon = new ESRI.ArcGIS.Geometry.JSONConverterGeometryClass(); return jsonCon.ReadGeometry(jsonReader, type, bHasZ, bHasM); } /// ///选择db /// /// private void SelectDB(string type) { try { string Filter = ""; TextBox textBox = null; Filter = "举证数据|*.db"; textBox = this.txtbzcg; KGIS.Framework.Utils.Dialog.OpenFileDialog openFileDialog = new KGIS.Framework.Utils.Dialog.OpenFileDialog(); openFileDialog.Title = "选择文件"; openFileDialog.Filter = Filter; openFileDialog.FileName = string.Empty; openFileDialog.FilterIndex = 1; openFileDialog.RestoreDirectory = true; openFileDialog.DefaultExt = "VCT"; if (openFileDialog.ShowDialog()) { textBox.Text = openFileDialog.FileName; dbPath = openFileDialog.FileName; } } catch (Exception ex) { LogAPI.Debug(ex.Message); } } private void btnBZ_Click(object sender, RoutedEventArgs e) { SelectDB("BZ"); } } }