添加工具类
parent
698faff527
commit
9c3b676ef7
|
|
@ -14,6 +14,8 @@
|
|||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
<PackageReference Include="DocXCore.Standard" Version="1.0.1" />
|
||||
<PackageReference Include="EnyimMemcachedCore" Version="2.6.4" />
|
||||
<PackageReference Include="GDAL" Version="3.10.0" />
|
||||
<PackageReference Include="GDAL.Native" Version="3.10.0" />
|
||||
<PackageReference Include="log4net" Version="2.0.15" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
namespace Infrastructure.Utils;
|
||||
|
||||
public class BoundingBox
|
||||
{
|
||||
public double MinX { get; set; }
|
||||
public double MinY { get; set; }
|
||||
public double MaxX { get; set; }
|
||||
public double MaxY { get; set; }
|
||||
|
||||
public BoundingBox(double minX, double minY, double maxX, double maxY)
|
||||
{
|
||||
MinX = minX;
|
||||
MinY = minY;
|
||||
MaxX = maxX;
|
||||
MaxY = maxY;
|
||||
}
|
||||
|
||||
public static BoundingBox Merge(BoundingBox bbox1, BoundingBox bbox2)
|
||||
{
|
||||
return new BoundingBox(
|
||||
Math.Min(bbox1.MinX, bbox2.MinX),
|
||||
Math.Min(bbox1.MinY, bbox2.MinY),
|
||||
Math.Max(bbox1.MaxX, bbox2.MaxX),
|
||||
Math.Max(bbox1.MaxY, bbox2.MaxY)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
namespace Infrastructure.Utils;
|
||||
|
||||
public static class DateTimeExtensions
|
||||
{
|
||||
public static int GetWeekOfYear(this DateTime time)
|
||||
{
|
||||
// 获取当前文化的日历
|
||||
System.Globalization.CultureInfo cultureInfo = System.Globalization.CultureInfo.CurrentCulture;
|
||||
// 使用 CalendarWeekRule.FirstFourDayWeek 规则计算周数
|
||||
return cultureInfo.Calendar.GetWeekOfYear(time, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Infrastructure.Utils;
|
||||
|
||||
public sealed class GeoContext
|
||||
{
|
||||
public readonly string GeoserverUrl;
|
||||
public readonly string JobwebUrl;
|
||||
public readonly string Workspace;
|
||||
public readonly string Username;
|
||||
public readonly string Password;
|
||||
public readonly string DbSchema;
|
||||
public readonly string DbStoreName;
|
||||
|
||||
public readonly string TiffStoreNamePrefix;
|
||||
public readonly string TiffDir;
|
||||
public readonly string ConnectionString;
|
||||
|
||||
private static readonly Lazy<GeoContext> Lazy = new(() => new GeoContext());
|
||||
|
||||
public static GeoContext Instance => Lazy.Value;
|
||||
|
||||
public GeoContext()
|
||||
{
|
||||
// 创建配置构建器
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||
.AddJsonFile(
|
||||
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json",
|
||||
optional: true)
|
||||
.AddEnvironmentVariables();
|
||||
// 构建配置
|
||||
var configuration = builder.Build();
|
||||
// 读取连接字符串
|
||||
var connectionString = configuration.GetConnectionString("OpenAuthDBContext");
|
||||
var geoserver = configuration["GeoServer:GeoserverUrl"];
|
||||
var jobwebUrl = configuration["GeoServer:JobwebUrl"];
|
||||
var workspace = configuration["Geoserver:Workspace"];
|
||||
var username = configuration["Geoserver:Username"];
|
||||
var password = configuration["Geoserver:Password"];
|
||||
var dbSchema = configuration["Geoserver:DbSchema"];
|
||||
var dbStoreName = configuration["Geoserver:DbStoreName"];
|
||||
var tiffStoreNamePrefix = configuration["Geoserver:TiffStoreNamePrefix"];
|
||||
var tiffDir = configuration["Geoserver:TiffDir"];
|
||||
ConnectionString = connectionString;
|
||||
GeoserverUrl = geoserver;
|
||||
JobwebUrl = jobwebUrl;
|
||||
Workspace = workspace;
|
||||
Username = username;
|
||||
Password = password;
|
||||
DbSchema = dbSchema;
|
||||
DbStoreName = dbStoreName;
|
||||
TiffStoreNamePrefix = tiffStoreNamePrefix;
|
||||
TiffDir = tiffDir;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,160 @@
|
|||
using System.IO.Compression;
|
||||
using System.Text;
|
||||
using NetTopologySuite.Geometries;
|
||||
using NetTopologySuite.IO;
|
||||
|
||||
namespace Infrastructure.Utils;
|
||||
|
||||
public class ShapeUtil
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="shpPath"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public static List<Dictionary<string, object>> getRows(string shpPath)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 支持
|
||||
/// </summary>
|
||||
/// <param name="shpPath"></param>
|
||||
/// <returns></returns>
|
||||
public static List<Dictionary<string, object>> ReadAllGeometry(string shpPath)
|
||||
{
|
||||
// 取得后缀,如果是zip,则解压
|
||||
var fileExtension = Path.GetExtension(shpPath);
|
||||
if (!fileExtension.Equals(".shp") && !fileExtension.Equals(".zip"))
|
||||
{
|
||||
throw new Exception("不支持的文件格式");
|
||||
}
|
||||
|
||||
// 设置字符编码
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
var gbk = Encoding.GetEncoding("gbk");
|
||||
if (fileExtension == ".zip")
|
||||
{
|
||||
var extractPath = Path.GetDirectoryName(shpPath) + "\\temp";
|
||||
Directory.CreateDirectory(extractPath);
|
||||
|
||||
using (var archive = ZipFile.OpenRead(shpPath))
|
||||
{
|
||||
// 列举ZIP文件中的条目
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
Console.WriteLine("zip 包含文件:");
|
||||
var fileName = gbk.GetString(Encoding.Default.GetBytes(entry.FullName));
|
||||
Console.WriteLine(fileName);
|
||||
}
|
||||
|
||||
// 提取ZIP文件中的所有文件到指定目录
|
||||
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
if (entry.Name != string.Empty)
|
||||
{
|
||||
// 确保完整路径存在 entry.FullName 是否可以编码
|
||||
var fixedEntryName = entry.FullName.Replace("/", "");
|
||||
var destinationPath = Path.GetFullPath(Path.Combine(extractPath, fixedEntryName));
|
||||
Console.WriteLine("解压文件路径:" + destinationPath);
|
||||
if (!destinationPath.StartsWith(Path.GetFullPath(extractPath) + Path.DirectorySeparatorChar))
|
||||
{
|
||||
throw new UnauthorizedAccessException("试图提取的文件超出了目标文件夹的路径边界。");
|
||||
}
|
||||
|
||||
// 提取条目到目标路径
|
||||
entry.ExtractToFile(destinationPath, overwrite: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 遍历解压目录,是否有shp后缀的文件
|
||||
if (!Directory.Exists(extractPath))
|
||||
{
|
||||
throw new Exception("文件解压失败");
|
||||
}
|
||||
|
||||
// 查找
|
||||
var dirInfo = new DirectoryInfo(extractPath);
|
||||
// zip 压缩包中只有一个shp文件
|
||||
shpPath = dirInfo.GetFiles("*.shp", SearchOption.AllDirectories)[0].FullName;
|
||||
}
|
||||
|
||||
var data = ParseShape(shpPath, null);
|
||||
var tifPath = data.First()["Image"].ToString();
|
||||
if (CheckForGarbledText(tifPath))
|
||||
{
|
||||
// 乱码,需要解码
|
||||
data = ParseShape(shpPath, gbk);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
private static List<Dictionary<string, object>> ParseShape(string shpPath, Encoding code)
|
||||
{
|
||||
using var dataReader = code == null
|
||||
? new ShapefileDataReader(shpPath, GeometryFactory.Default)
|
||||
: new ShapefileDataReader(shpPath, GeometryFactory.Default, code);
|
||||
Console.WriteLine("记录数" + dataReader.RecordCount);
|
||||
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
|
||||
while (dataReader.Read())
|
||||
{
|
||||
Dictionary<string, object> row = new Dictionary<string, object>();
|
||||
// 读取列
|
||||
for (int i = 0, j = 0; i < dataReader.FieldCount; i++)
|
||||
{
|
||||
var colName = dataReader.GetName(i);
|
||||
var value = dataReader.GetValue(i);
|
||||
row.Add(colName, value);
|
||||
}
|
||||
|
||||
rows.Add(row);
|
||||
}
|
||||
|
||||
return rows;
|
||||
// return NetTopologySuite.IO.Esri.Shapefile.ReadAllGeometries(shpPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否乱码
|
||||
/// </summary>
|
||||
/// <param name="content"></param>
|
||||
/// <returns></returns>
|
||||
private static bool CheckForGarbledText(string content)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 检查是否有不可打印的字符
|
||||
foreach (char c in content)
|
||||
{
|
||||
if (char.IsControl(c) && !char.IsWhiteSpace(c))
|
||||
{
|
||||
return true; // 发现不可打印的控制字符
|
||||
}
|
||||
}
|
||||
// 检查是否有不常见的字符
|
||||
foreach (char c in content)
|
||||
{
|
||||
if (c > 0x7F && !char.IsLetterOrDigit(c) && !char.IsPunctuation(c) && !char.IsSymbol(c) &&
|
||||
!char.IsWhiteSpace(c))
|
||||
{
|
||||
return true; // 发现不常见的字符
|
||||
}
|
||||
}
|
||||
|
||||
return false; // 没有发现乱码
|
||||
}
|
||||
catch (DecoderFallbackException)
|
||||
{
|
||||
return true; // 解码失败,存在乱码
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return true; // 其他异常,可能表示存在乱码
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue