You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
8.6 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Text;
using WorkData.Entity;
#region 文件说明
/*-----------------------------------------------------
* 版权所有 c 2024 jdfcd
* CLR版本 4.0.30319.42000
* 命名空间 DQ.Construction.NewLook.Utility
*
* 创建者pnpe
* 电子邮件pnpe@qq.com
* 创建时间2024/12/31 10:38:45
*/
#endregion 文件说明
namespace WellWorkDataUI
{
public class ResultDataInfo
{
public int Code { get; set; }
public string Msg { get; set; }
public object Data { get; set; }
}
public class LasHelper
{
//读取las
public static ResultDataInfo LasFileToDataTable(ref DataTable table, string filename, string charset, int rowindex)
{
ResultDataInfo result = new ResultDataInfo();
List<string> columnNames = new List<string>();
try
{
long length = new FileInfo(filename).Length;
using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(filename, FileMode.Open, Guid.NewGuid().ToString()))
using (MemoryMappedViewStream stream = mmf.CreateViewStream(0, length))
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(charset)))
{
int lineindex = 0;
bool isReadHeader = false;
bool isReadBody = false;
string strLine = string.Empty;
while (!reader.EndOfStream)
{
string str = reader.ReadLine();
if (string.IsNullOrWhiteSpace(str))
{
continue;
}
else if (str.StartsWith("~C") || str.StartsWith("~Curve"))
{
isReadHeader = true;
continue;
}
else if (!isReadHeader)
{
continue;
}
else if (str.StartsWith("~A") || str.StartsWith("~Ascii"))
{
isReadBody = true;
table.Columns.Clear();
if (columnNames.Count == 0)
{
result.Code = 0;
result.Msg = $"文件{filename}列读取失败,已跳过文件,{Environment.NewLine}";
return result;
}
//说明表头写完了
for (int i = 0; i < columnNames.Count; i++)
{
table.Columns.Add(columnNames[i]);
}
continue;
}
if (isReadHeader && !isReadBody)
{
//判断表头,对于小数点处理和冒号处理
string colName = str.Replace(":", string.Empty);
colName = colName.Split('.')[0];
if (colName.StartsWith("#"))
{
continue;
}
colName = colName.Trim().ToUpper();
if (!columnNames.Contains(colName))
{
columnNames.Add(colName);
}
else
{
//存在重复列名
table.Columns.Clear();
result.Code = 0;
result.Msg = $"文件{filename}存在重复列名{colName},已跳过文件,{Environment.NewLine}";
return result;
}
continue;
}
strLine = str;
string trimString = string.Join(",", strLine.Split(Constants.StringSplit.EmptySeparator, StringSplitOptions.RemoveEmptyEntries));
string[] array = trimString.Split(Constants.StringSplit.Separator, StringSplitOptions.None);
if (array.Length != columnNames.Count)
{
continue;
}
if (array.Length > 0)
{
table.Rows.Add(array);
}
lineindex++;
}
}
}
catch (Exception e)
{
result.Code = 0;
result.Msg = e.Message;
Console.WriteLine(e.ToString());
}
result.Code = 1;
return result;
}
/// <summary>
/// las文件转List<WellCurve>
/// </summary>
public static IEnumerable<WellCurve> LasFileToList(string jh, string filename, string charset)
{
if (!File.Exists(filename))
{
yield break;
}
using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(filename, FileMode.Open, Guid.NewGuid().ToString()))
using (MemoryMappedViewStream stream = mmf.CreateViewStream(0, new FileInfo(filename).Length))
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(charset)))
{
List<string> columnNames = new List<string>();
bool isReadHeader = false;
bool isReadBody = false;
while (!reader.EndOfStream)
{
string str = reader.ReadLine();
if (string.IsNullOrWhiteSpace(str))
{
continue;
}
if (str.StartsWith("~C") || str.StartsWith("~Curve"))
{
isReadHeader = true;
continue;
}
else if (!isReadHeader)
{
continue;
}
else if (str.StartsWith("~A") || str.StartsWith("~Ascii"))
{
isReadBody = true;
if (columnNames.Count == 0)
{
throw new Exception($"文件{filename} 列名为空!");
}
continue;
}
if (isReadHeader && !isReadBody)
{
string colName = str.Replace(":", string.Empty);
colName = colName.Split('.')[0];
if (colName.StartsWith("#"))
{
continue;
}
colName = colName.Trim().ToUpper();
if (!columnNames.Contains(colName))
{
columnNames.Add(colName);
}
else
{
throw new Exception($"文件{filename} 存在重复列名:{colName}");
}
continue;
}
// 数据体
string trimString = string.Join(",", str.Split(Constants.StringSplit.EmptySeparator, StringSplitOptions.RemoveEmptyEntries));
string[] array = trimString.Split(Constants.StringSplit.Separator, StringSplitOptions.None);
if (array.Length != columnNames.Count)
{
continue;
}
if (!double.TryParse(array[0], out double depth))
{
continue;
}
var dic = new Dictionary<string, string>();
for (int i = 1; i < array.Length; i++)
{
dic[columnNames[i]] = array[i];
}
yield return new WellCurve
{
JH = jh,
DEPTH = depth,
ColumnValues = dic,
};
}
}
}
}
}