功能可以实现,但存在问题,layer.intersection 很慢,不知道原因,有大佬知道可以在评论区留言
/// <summary>
/// 单层内相交检查
/// </summary>
/// <param name="inputDataSource"></param>
/// <param name="input">检查层</param>
/// <param name="output">结果层</param>
/// <param name="processMethod">反馈进度 第一个参数为当前进度和第二个参数为当前处理的Fid</param>
public static void Overlap(DataSource inputDataSource, Layer input,Layer output,Action<long,long> processMethod)
{
input.SetAttributeFilter(null);
input.SetSpatialFilter(null);
input.ResetReading();
string layername = ReadWrite.OGR_L_GetName(input);
#region Memory Driver
Driver driver = Ogr.GetDriverByName("Memory");
if (driver == null)
{
Console.write("Memory驱动错误,请检查");
}
DataSource dataSource = driver.CreateDataSource(null,null);
if (dataSource is null)
{
Console.write("Memory文件创建失败,请检查");
}
#endregion
Layer Layer1 = null;
Layer Layer2 = null;
string[] p = new string[4] { "SKIP_FAILURES=YES", "PROMOTE_TO_MULTI=YES", "INPUT_PREFIX=1", "METHOD_PREFIX=2" };
long current = 0;
Feature feature = null;
long[] features= CommonOp.GetFeatures(input);
for (int i = 0; i < features.Length; i++)
{
long fid = features[i];
feature = input.GetFeature(fid);
Geometry geometry = feature.GetGeometryRef();
current++;
//ASC 升序 DESC 降序 _单个字符 %任意数量个字符
string sql = string.Format("SELECT * FROM {0} WHERE FID = {1}", layername, fid);
Layer1 = inputDataSource.ExecuteSQL(sql, null, null);
//复制到memory layer中
Layer1 = dataSource.CopyLayer(Layer1, "method1", null);
long cnt1 = Layer1.GetFeatureCount(1);
sql = string.Format("SELECT * FROM {0} WHERE NOT FID = {1}", layername, fid);
Layer2 = inputDataSource.ExecuteSQL(sql, geometry, null);
//复制到memory layer中
Layer2 = dataSource.CopyLayer(Layer2, "method2", null);
long cnt2 = Layer2.GetFeatureCount(1);
if (cnt2 == 0)
{
dataSource.DeleteLayer(0);
dataSource.DeleteLayer(1);
//inputDataSource.ReleaseResultSet(Layer2);
continue;
}
//这步调试要花费4秒左右,不知道原因,暂未解决
int ret= Layer2.Intersection(Layer1, output, p, null, null);
dataSource.DeleteLayer(0);
dataSource.DeleteLayer(1);
//inputDataSource.ReleaseResultSet(Layer2);
//显示进度
processMethod(current, fid);
}
dataSource.Dispose();
output.SyncToDisk();
}