using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using Kingo.RuleCheck.CheckHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Kingo.RuleCheck.XJRuleCheck
{
public abstract class RuleCheckBase_DTB
{
public CheckHelper.MDBHelper mdbHelper { get; set; }
///
/// 质检外业任务图斑标识码
///
public string TBBSM { get; set; }
///
/// 任务包数据路径
///
public string SourcePath { get; set; }
///
/// 过程数据路径
///
public string ProcessDataPath { get; set; }
///
/// 是否为首轮执行
///
public bool FirstRun { get; set; } = true;
///
/// 变更范围矢量集合
///
public Dictionary BGFWDic { get; set; }
///
/// 人工错误(漏斗式)
///
public List AllRGError { get; set; } = new List();
///
/// 要执行规则
///
public List> ExcuteRules { get; set; }
///
/// 不执行规则
///
public List DisExecuteRules { get; set; }
public IFeatureWorkspace Workspace { get; set; }
public int Wkid { get; set; }
public List LayerNames { get; set; }
public List DataSetNames { get; set; }
public Dictionary DicFeatureClass { get; set; }
public IWorkspace sourceWorkSpace;
protected List StartCheck(string sourePath, IWorkspace workSpace, List ruleNames)
{
this.SourcePath = sourePath;
this.Workspace = workSpace as IFeatureWorkspace;
this.OpenConnection(sourePath);
DataTable dtMetadata = mdbHelper.ExecuteDataTable("select * from WJMetadata");
if (dtMetadata != null && dtMetadata.Rows.Count > 0)
{
this.Wkid = Convert.ToInt32(dtMetadata.Rows[0]["WKID"]);
}
ConvertBGFW();
return Check(ruleNames);
}
protected List StartCheck(string tbbsm, string sourePath, Dictionary dicFeatureClass, List ruleNames)
{
this.SourcePath = sourePath;
this.DicFeatureClass = dicFeatureClass;
return Check(ruleNames);
}
protected List StartCheck(string sourePath, Dictionary workspace, List ruleNames)
{
this.sourceWorkSpace = OpenWorkspace(sourePath);
this.Workspace = this.sourceWorkSpace as IFeatureWorkspace;
this.SourcePath = sourePath;
this.DicFeatureClass = workspace;
this.OpenConnection(sourePath);
return Check(ruleNames);
}
protected List StartCheck(IWorkspace workspace, List ruleNames, List layerNames, List dataSetNames)
{
this.Workspace = workspace as IFeatureWorkspace;
this.LayerNames = layerNames;
this.DataSetNames = dataSetNames;
return Check(ruleNames);
}
public abstract List ExcuteCheck(string sourePath, IWorkspace workspace);
public abstract List ExcuteCheck(string sourePath, Dictionary dicFeatureClass);
public abstract List ExcuteCheck(IWorkspace workspace, List layerNames, List dataSetNames);
public abstract List ExcuteCheck(string tbbsm, string sourePath, Dictionary dicFeatureClass);
private void OpenConnection(string connectionString)
{
//this.connection = connection = new SQLiteConnection("Data Source=" + connectionString);
//this.connection.Open();
this.mdbHelper = new CheckHelper.MDBHelper(connectionString);
this.mdbHelper.connOpen();
}
///
/// 变更范围矢量化
///
private void ConvertBGFW()
{
BGFWDic = new Dictionary();
string sql = "select * from bgfw";
DataTable dt = mdbHelper.ExecuteDataTable(sql);
if (dt == null || dt.Rows.Count <= 0)
{
return;
}
foreach (DataRow item in dt.Rows)
{
string bsm = item["bsm"].ToString();
IGeometry geometry = GeometryConvertHelper.ConvertWKTToIGeometry(item["geometry"].ToString());
if (BGFWDic.ContainsKey(bsm))
{
BGFWDic[bsm].Shape = geometry;
}
else
{
BGFWDic.Add(bsm, new BGFWEntity() { BSM = bsm, Shape = geometry });
}
}
}
private void DisposeConnect()
{
if (this.mdbHelper != null)
{
this.mdbHelper.DisConn();
}
}
private List Check(List ruleNames)
{
List rst = new List();
try
{
foreach (var item in ruleNames)
{
if (ExcuteRules != null && ExcuteRules.Count > 0 && ExcuteRules.FirstOrDefault(x => x.Item1.Equals(item)) == null)
continue;
if (DisExecuteRules != null && DisExecuteRules.Contains(item))
continue;
MethodInfo method = this.GetType().GetMethod(item);
if (method != null)
{
//DateTime dt = DateTime.Now;
object ruleEntity = method.Invoke(this, null);
//Console.WriteLine($"{item}执行时间:{(DateTime.Now - dt).TotalSeconds}");
if (ruleEntity != null)
{
if (ruleEntity is RuleEntity)
{
rst.Add(ruleEntity as RuleEntity);
}
else if (ruleEntity is List)
{
rst.AddRange(ruleEntity as List);
}
}
}
else
{
}
}
return rst;
}
catch (Exception ex)
{
throw ex;
}
finally
{
this.DisposeConnect();
}
}
private IWorkspace OpenWorkspace(string sourcePath)
{
AccessWorkspaceFactoryClass factoryClass = new AccessWorkspaceFactoryClass();
IPropertySet result = new PropertySetClass();
result.SetProperty("DATABASE", sourcePath);
return factoryClass.Open(result, 0);
}
///
/// 求两个图形相交部分
///
/// 图形1
/// 图形2
///
public IGeometry InterSect(IGeometry pGeo1, IGeometry pGeo2)
{
try
{
IGeometry result = null;
if (pGeo1 == null || pGeo2 == null)
return null;
ITopologicalOperator topo = pGeo1 as ITopologicalOperator;
if (topo != null)
{
topo.Simplify();
ITopologicalOperator topo2 = pGeo2 as ITopologicalOperator;
topo2.Simplify();
if (pGeo1.GeometryType == esriGeometryType.esriGeometryPoint || pGeo2.GeometryType == esriGeometryType.esriGeometryPoint)
{
result = topo.Intersect(pGeo2, esriGeometryDimension.esriGeometry0Dimension);
}
else if (pGeo1.GeometryType == esriGeometryType.esriGeometryPolyline || pGeo2.GeometryType == esriGeometryType.esriGeometryPolyline)
{
result = topo.Intersect(pGeo2, esriGeometryDimension.esriGeometry1Dimension);
}
else
{
result = topo.Intersect(pGeo2, pGeo2.Dimension);
}
if (result != null)
{
ITopologicalOperator resultTopo = result as ITopologicalOperator;
resultTopo.Simplify();
}
return result;
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}
}
public class BGFWEntity
{
public string BSM { get; set; }
public IGeometry Shape { get; set; }
}
}