森林草原湿地荒漠调查
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

342 lines
17 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KGIS.Framework.Utils;
using System.IO;
using UIShell.OSGi.Utility;
using System.Collections;
using DevExpress.Mvvm.Native;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
namespace Kingo.PluginServiceInterface.Helper
{
public static class DealVCTErrorFileHelper
{
public static bool CheckAndDeleteVCTError(string directoryPath, string xzqdm)
{
try
{
if (string.IsNullOrEmpty(directoryPath) || !Directory.Exists(directoryPath)) return false;
if (string.IsNullOrEmpty(xzqdm)) return false;
DirectoryInfo dir = new DirectoryInfo(directoryPath);
FileInfo[] fileSet = dir.GetFiles("*.VCT", SearchOption.TopDirectoryOnly);
if (fileSet == null || fileSet.Length == 0)
{
LogAPI.Debug("所选择的更新数据包\\标准格式数据文件夹下不存在vct文件!");
//MessageHelper.Show("所选择的更新数据包\\标准格式数据文件夹下不存在vct文件!");
return false;
}
foreach (FileInfo file in fileSet)
{
string strjson1 = File.ReadAllText(file.FullName, Encoding.Default);
List<string> lines = strjson1.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
int startIndex = lines.IndexOf("PolygonBegin");
int endIndex = lines.IndexOf("PolygonEnd");
if (startIndex != -1 && endIndex != -1)
{
Dictionary<int, Dictionary<int, string>> keyValuePairs = new Dictionary<int, Dictionary<int, string>>();
int startstr = lines[startIndex + 1].ToInt();
for (int i = startIndex + 1; i < endIndex; i++)
{
if ((startstr + 1).ToString() == lines[i].ToString() || ("unknown" == lines[i + 2].ToString()))
{
if ((startstr + 1).ToString() == lines[i].ToString())
startstr = startstr + 1;
else if ("unknown" == lines[i + 2].ToString())
startstr = lines[i].ToInt();
}
if (keyValuePairs.ContainsKey(startstr))
{
keyValuePairs[startstr].Add(i, lines[i].ToString());
}
else
{
Dictionary<int, string> keyValues = new Dictionary<int, string>() { { i, lines[i].ToString() } };
keyValuePairs.Add(startstr, keyValues);
}
}
//检查是否合法
Dictionary<int, string> keyValueError = new Dictionary<int, string>();
foreach (var item in keyValuePairs)
{
Dictionary<int, string> valuePairs = item.Value;
int begin = valuePairs.FirstOrDefault().Key;
int end = valuePairs.LastOrDefault().Key;
if (valuePairs[end] != "0")
{
keyValueError.Add(valuePairs.FirstOrDefault().Key, valuePairs.FirstOrDefault().Value);
continue;
}
List<string> values = new List<string>(valuePairs.Values);
if (!values.Contains("21"))
{
keyValueError.Add(valuePairs.FirstOrDefault().Key, valuePairs.FirstOrDefault().Value);
continue;
}
int index = values.FindIndex(x => x == "21");
int pointCount = values[index + 1].ToInt();
if (pointCount < 1)
{
keyValueError.Add(valuePairs.FirstOrDefault().Key, valuePairs.FirstOrDefault().Value);
continue;
}
if (values.Count - 1 < index + 2 || values.Count - 1 == index + 2)
{
keyValueError.Add(valuePairs.FirstOrDefault().Key, valuePairs.FirstOrDefault().Value);
continue;
}
string point = "";
for (int k = index + 2; k < values.Count - 1; k++)
{
if (k < values.Count - 2) point += values[k] + ",";
else point += values[k];
}
if (string.IsNullOrEmpty(point) || point.StartsWith("0,") || point.EndsWith(",0"))
{
keyValueError.Add(valuePairs.FirstOrDefault().Key, valuePairs.FirstOrDefault().Value);
continue;
}
List<string> listPoints = new List<string>(point.Split(','));
if (listPoints.Count != pointCount)
{
keyValueError.Add(valuePairs.FirstOrDefault().Key, valuePairs.FirstOrDefault().Value);
continue;
}
}
//检查结果倒序删除
if (keyValueError.Count > 0)
{
var sortedDictionary = keyValueError.OrderByDescending(kvp => kvp.Key).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
foreach (var pair in sortedDictionary)
{
int attributeIndex = lines.IndexOf(x => x.StartsWith(pair.Value + "," + xzqdm));
if (attributeIndex == -1)//标识码为空的情况
{
attributeIndex = lines.IndexOf(x => x.StartsWith(pair.Value + ",,"));
}
if (attributeIndex > -1)
{
lines.RemoveAt(attributeIndex);
var needDelete = keyValuePairs.FirstOrDefault(x => x.Key == pair.Value.ToInt());
foreach (var item in needDelete.Value.OrderByDescending(kvp => kvp.Key))
{
lines.RemoveAt(item.Key);
}
}
}
}
//lines1.RemoveAt(1);
File.WriteAllLines(file.FullName, lines, Encoding.GetEncoding("gb2312"));
}
else
{
Console.WriteLine("Polygon data not found.");
}
}
return true;
}
catch (Exception ex)
{
LogAPI.Debug("VCT前置错误检查处理 发生异常:" + ex.Message);
LogAPI.Debug("VCT前置错误检查处理 发生异常:" + ex.StackTrace);
return false;
}
}
public static bool CheckAndDeleteVCTErrorByTable(string directoryPath, string xzqdm, string tablePath)
{
try
{
if (string.IsNullOrEmpty(directoryPath) || !Directory.Exists(directoryPath)) return false;
if (string.IsNullOrEmpty(xzqdm)) return false;
if (string.IsNullOrEmpty(tablePath) || !File.Exists(tablePath)) return false;
DirectoryInfo dir = new DirectoryInfo(directoryPath);
FileInfo[] fileSet = dir.GetFiles("*.VCT", SearchOption.TopDirectoryOnly);
if (fileSet == null || fileSet.Length == 0)
{
LogAPI.Debug("所选择的更新数据包\\标准格式数据文件夹下不存在vct文件!");
//MessageHelper.Show("所选择的更新数据包\\标准格式数据文件夹下不存在vct文件!");
return false;
}
List<string> errorID = GetTableVCTErrorID(tablePath);
if (errorID == null || errorID.Count == 0)
{
LogAPI.Debug("所选择的更新数据包\\标准格式数据文件夹下不存在所选择表格中的VCT错误信息,无需修改!");
return true;
}
foreach (FileInfo file in fileSet)
{
string strjson1 = File.ReadAllText(file.FullName, Encoding.Default);
if (string.IsNullOrEmpty(strjson1))
{
LogAPI.Debug($"所选择的{file.FullName}文件为空!");
continue;
}
List<string> lines = strjson1.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
int startIndex = lines.IndexOf("PolygonBegin");
int endIndex = lines.IndexOf("PolygonEnd");
if (startIndex != -1 && endIndex != -1)
{
Dictionary<int, Dictionary<int, string>> keyValuePairs = new Dictionary<int, Dictionary<int, string>>();
int startstr = lines[startIndex + 1].ToInt();
for (int i = startIndex + 1; i < endIndex; i++)
{
if ((startstr + 1).ToString() == lines[i].ToString() || ("unknown" == lines[i + 2].ToString()))
{
if ((startstr + 1).ToString() == lines[i].ToString())
startstr = startstr + 1;
else if ("unknown" == lines[i + 2].ToString())
startstr = lines[i].ToInt();
}
if (keyValuePairs.ContainsKey(startstr))
{
keyValuePairs[startstr].Add(i, lines[i].ToString());
}
else
{
Dictionary<int, string> keyValues = new Dictionary<int, string>() { { i, lines[i].ToString() } };
keyValuePairs.Add(startstr, keyValues);
}
}
//检查是否合法
Dictionary<int, string> keyValueError = new Dictionary<int, string>();
foreach (var item in keyValuePairs)
{
Dictionary<int, string> valuePairs = item.Value;
int begin = valuePairs.FirstOrDefault().Key;
int end = valuePairs.LastOrDefault().Key;
if (errorID.Contains(valuePairs[begin]))
{
keyValueError.Add(valuePairs.FirstOrDefault().Key, valuePairs.FirstOrDefault().Value);
continue;
}
}
//检查结果倒序删除
if (keyValueError.Count > 0)
{
var sortedDictionary = keyValueError.OrderByDescending(kvp => kvp.Key).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
foreach (var pair in sortedDictionary)
{
int attributeIndex = lines.IndexOf(x => x.StartsWith(pair.Value + "," + xzqdm));
if (attributeIndex == -1)//标识码为空的情况
{
attributeIndex = lines.IndexOf(x => x.StartsWith(pair.Value + ",,"));
}
if (attributeIndex > -1)
{
lines.RemoveAt(attributeIndex);
var needDelete = keyValuePairs.FirstOrDefault(x => x.Key == pair.Value.ToInt());
foreach (var item in needDelete.Value.OrderByDescending(kvp => kvp.Key))
{
lines.RemoveAt(item.Key);
}
}
}
}
//lines1.RemoveAt(1);
File.WriteAllLines(file.FullName, lines, Encoding.GetEncoding("gb2312"));
}
else
{
Console.WriteLine("Polygon data not found.");
}
}
return true;
}
catch (Exception ex)
{
LogAPI.Debug("VCT前置错误检查处理 发生异常:" + ex.Message);
LogAPI.Debug("VCT前置错误检查处理 发生异常:" + ex.StackTrace);
return false;
}
}
private static List<string> GetTableVCTErrorID(string tablePath)
{
List<string> errorID = new List<string>();
try
{
try
{
using (FileStream fileStream = new FileStream(tablePath, FileMode.Open, FileAccess.Read)) { }
}
catch
{
LogAPI.Debug("所选表格被占用!");
return errorID;
}
using (FileStream fileStream = new FileStream(tablePath, FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = new XSSFWorkbook(fileStream);
ISheet sheet = workbook.GetSheetAt(0);
for (int rowIndex = 1; rowIndex <= sheet.LastRowNum; rowIndex++)
{
IRow inputRow = sheet.GetRow(rowIndex);
if (inputRow == null) continue;
ICell inputCell = inputRow.GetCell(3);
if (inputCell == null || string.IsNullOrEmpty(inputCell.ToString())) continue;
if (inputCell.ToString().StartsWith("VCT文件"))
{
var split = inputCell.ToString().Split(':');
if (split == null || split.Length < 1) continue;
foreach (var item in split)
{
if (item.Contains("请检查标识码为"))
{
string tempId = item.Replace("请检查标识码为", "");
bool isNum = int.TryParse(tempId, out int id);
if (isNum)
{
errorID.Add(id.ToString());
break;
}
}
else
{
bool isNum = int.TryParse(item, out int id);
if (isNum)
{
errorID.Add(id.ToString());
break;
}
}
}
}
}
}
return errorID;
}
catch (Exception ex)
{
LogAPI.Debug("获取表格中错误ID 发生异常:" + ex.Message);
LogAPI.Debug("获取表格中错误ID 发生异常:" + ex.StackTrace);
return errorID;
}
}
}
public class VCTPolygonMessage
{
}
}