Infrastructure/OpenAuth.App/ServiceApp/LayerManagerApp/LayerApp.cs

1822 lines
68 KiB
C#
Raw Normal View History

2025-01-10 10:14:58 +08:00
using System.Data;
using System.IO.Compression;
using System.Text;
using DocumentFormat.OpenXml.InkML;
2025-01-10 10:14:58 +08:00
using Infrastructure;
using Infrastructure.Utils;
using Microsoft.AspNetCore.Http;
2025-04-14 10:57:27 +08:00
using Microsoft.AspNetCore.Mvc;
2025-01-10 10:14:58 +08:00
using Microsoft.Extensions.Options;
using NetTopologySuite;
using NetTopologySuite.Features;
2025-01-10 10:14:58 +08:00
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
2025-01-10 10:14:58 +08:00
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.FormScheme.FormHelpers;
2025-01-10 10:14:58 +08:00
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
2025-01-10 10:14:58 +08:00
using OpenAuth.App.ServiceApp.LayerManagerApp.Request;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;
2025-04-17 14:48:05 +08:00
using Shapefile = NetTopologySuite.IO.Esri.Shapefile;
2025-01-10 10:14:58 +08:00
namespace OpenAuth.App.ServiceApp.LayerManagerApp;
public class LayerApp : SqlSugarBaseApp<DmLayer, SugarDbContext>
{
private readonly string _filePath;
private readonly ISqlSugarClient _client;
public LayerApp(ISugarUnitOfWork<SugarDbContext> unitWork,
ISimpleClient<DmLayer> repository, IAuth auth,
IOptions<AppSetting> setOptions, ISqlSugarClient client) : base(unitWork, repository, auth)
{
_client = client;
_filePath = setOptions.Value.UploadPath;
if (string.IsNullOrEmpty(_filePath))
{
_filePath = AppContext.BaseDirectory;
}
}
public async Task<Response<bool>> UploadExcel(LayerReq req)
{
using var db = Repository.AsSugarClient();
var excelPath = Path.Combine(_filePath, req.FilePath);
// excel名称
var excelFileName = Path.GetFileNameWithoutExtension(excelPath);
// 使用NPOI读取Excel文件
using (var fileStream = new FileStream(excelPath, FileMode.Open, FileAccess.Read))
{
IWorkbook workbook;
if (Path.GetExtension(excelPath).Equals(".xls"))
{
workbook = new HSSFWorkbook(fileStream);
}
else
{
workbook = new XSSFWorkbook(fileStream);
}
//
var sheet = workbook.GetSheetAt(0);
// 获取总行数
var rowCount = sheet.LastRowNum;
if (rowCount < 3)
{
throw new Exception("只有两行表头");
}
// 获取总列数
var columnCount = sheet.GetRow(0).LastCellNum;
var headers = new List<DataTableAttr>();
// 遍历表头
var chineseRow = sheet.GetRow(0);
var englishRow = sheet.GetRow(1);
for (var j = 0; j < columnCount; j++)
{
var header = new DataTableAttr
{
InitName = chineseRow.GetCell(j).ToString(), // 中文表头
RefName = englishRow.GetCell(j).ToString()?.ToLower(), // 英文表头
// 字段类型
Type = "string",
// 原始字段名称
Name = englishRow.GetCell(j).ToString(),
// 字段长度
Length = 64
};
headers.Add(header);
}
var keys = headers.Select(a => a.RefName).ToList();
if (!keys.Contains("lng") || !keys.Contains("lat"))
{
throw new Exception("缺少经纬度字段");
}
var typeBuilder = db.DynamicBuilder().CreateClass(req.DataTable,
new SugarTable() { TableName = req.DataTable, TableDescription = req.ServerName + "图斑" });
//添加主键
typeBuilder.CreateProperty("id", typeof(string), new SugarColumn()
{
IsPrimaryKey = true,
IsIdentity = false,
ColumnDataType = "varchar",
Length = 36,
ColumnDescription = "主键",
});
//添加主键
typeBuilder.CreateProperty("geometry", typeof(string), new SugarColumn()
{
IsPrimaryKey = false,
IsIdentity = false,
// ColumnDataType = "geometry(GEOMETRY)",
ColumnDataType = string.Concat("geometry(GEOMETRY,",
req.SpatialRef.AsSpan(req.SpatialRef.LastIndexOf(":", StringComparison.Ordinal) + 1),
")"),
ColumnDescription = "图斑",
});
headers.ForEach(u =>
{
if (!u.RefName.Equals("lng") && !u.RefName.Equals("lat"))
{
typeBuilder.CreateProperty(u.RefName, typeof(string), new SugarColumn()
{
IsPrimaryKey = false,
IsIdentity = false,
IsNullable = true,
Length = u.Length,
ColumnDescription = u.InitName,
});
}
});
// 开启事务
await db.Ado.BeginTranAsync(IsolationLevel.ReadCommitted);
// 创建表
//db.CodeFirst.InitTables(typeBuilder.BuilderType());
var cols = new string[headers.Count];
for (var i = 0; i < headers.Count; i++)
{
if (headers[i].RefName.Equals("lng"))
{
cols[i] = "geometry";
}
else if (headers[i].RefName.Equals("lat"))
{
cols[i] = "id";
}
else
{
cols[i] = headers[i].RefName;
}
}
var objs = new List<Dictionary<string, object>>(1001);
var num = 0;
// 遍历数据行
for (var i = 2; i < rowCount; i++)
{
var row = sheet.GetRow(i);
if (row == null)
{
continue;
}
var data = new Dictionary<string, object>();
// 遍历列
string lat = null, lng = null;
for (var j = 0; j < columnCount; j++)
{
var cell = row.GetCell(j);
if (cell == null)
{
continue;
}
var cellValue = cell.ToString();
switch (headers[j].RefName)
{
case "lat":
lat = cellValue;
continue;
case "lng":
lng = cellValue;
continue;
default:
data[headers[j].RefName] = cellValue;
break;
}
}
if (lat != null && lng != null)
{
var geography =
NtsGeometryServices.Instance.CreateGeometryFactory(
int.Parse(req.SpatialRef.Substring(
req.SpatialRef.IndexOf(":", StringComparison.Ordinal) + 1)));
var point = geography.CreatePoint(new Coordinate(double.Parse(lng), double.Parse(lat)));
data["geometry"] = point.AsText();
}
data["id"] = Guid.NewGuid().ToString();
objs.Add(data);
if (num++ == 999)
{
await db.Insertable(objs)
.AS(req.DataTable) // 指定目标表名
.InsertColumns(cols) // 指定要插入的列
.ExecuteCommandAsync();
num = 0;
objs.Clear();
}
}
await db.Insertable(objs)
.AS(req.DataTable) // 指定目标表名
.InsertColumns(cols) // 指定要插入的列
.ExecuteCommandAsync();
await db.Ado.CommitTranAsync();
workbook.Close();
// 关于图层发布失败后,如何处理?
// excel 矢量点发布图层
var response = await GeoUtil.CreateStoreAndLayer("", req.SpatialRef, req.DataTable, "");
// todo 保存图层信息
var dmLayer = new DmLayer()
{
Id = Guid.NewGuid().ToString(),
SeverName = req.ServerName,
SpatialRef = req.SpatialRef,
SeverType = req.SeverType, // 服务类型
StoreType = "", //存储类型
DataSourceType = req.DataSourceType,
TableRef = req.DataTable,
ShapeType = req.ShapeType,
StyleName = req.StyleName, // 样式名称
Description = req.Description, // 描述
Screenshot = req.Screenshot // 截图
};
await Repository.AsSugarClient().Insertable(dmLayer).ExecuteCommandAsync();
}
return new Response<bool>
{
Result = true
};
}
public async Task<Response<bool>> UploadShape(LayerReq req)
{
try
{
}
catch (Exception ex)
{
throw new CommonException(ex.Message, 500);
}
return new Response<bool>
{
Result = true
};
}
2025-04-14 10:57:27 +08:00
public Response<Dictionary<string, object>> TableDataByTableName(string tablename, string type, string keyword, string keyvalue, int page, int pagesize)
{
2025-12-25 14:52:31 +08:00
Dictionary<string, object> result = new Dictionary<string, object>();
// string sql = $"select column_name as name from INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'public' AND table_name ='" + tablename + "'";
2025-04-30 13:58:32 +08:00
string sqlcom = $"SELECT \r\n\r\n\r\n c.column_name AS \"columnName\", pgd.description AS \"comment\" \r\nFROM \r\n information_schema.columns c\r\nLEFT JOIN \r\n " +
2025-12-25 14:52:31 +08:00
$"pg_catalog.pg_description pgd ON pgd.objoid = c.table_name::regclass::oid \r\n AND pgd.objsubid = c.ordinal_position\r\nWHERE \r\n c.table_name ='" + tablename + "';";
2025-05-13 15:20:21 +08:00
//var cloumnlist = _client.Ado.SqlQuery<string>(sql);
2025-04-30 13:58:32 +08:00
var coment = _client.Ado.SqlQuery<dynamic>(sqlcom);
2025-05-13 14:51:52 +08:00
//result.Add("head", cloumnlist);
2025-04-30 13:24:35 +08:00
result.Add("headName", coment);
2025-04-14 10:57:27 +08:00
string names = "";
string geoms = "";
2025-04-30 13:58:32 +08:00
for (int i = 0; i < coment.Count; i++)
2025-12-25 14:52:31 +08:00
{
var name = coment[i].columnName;
2025-04-14 10:57:27 +08:00
if (name.Contains("geom"))
{
geoms = name;
2025-12-25 14:52:31 +08:00
2025-04-14 10:57:27 +08:00
}
else
{
2025-12-25 14:52:31 +08:00
names = names + "\"" + name + "\",";
2025-04-14 10:57:27 +08:00
}
}
string sql1 = $"select " + names + "ST_AsText(" + geoms + ") as " + geoms + $" from " + tablename + " where 1=1 ";
2025-05-15 15:10:24 +08:00
string sql2 = "";
2025-04-14 10:57:27 +08:00
if (string.IsNullOrEmpty(keyword) || string.IsNullOrEmpty(keyvalue))
{
2025-12-25 14:52:31 +08:00
sql1 = sql1 + "limit " + pagesize + " offset " + (page - 1) * pagesize;
2025-04-14 10:57:27 +08:00
}
else
{
sql1 = sql1 + " and " + keyword + " like '%" + keyvalue + "%'" + "limit " + pagesize + " offset " + (page - 1) * pagesize;
2025-05-15 15:10:24 +08:00
sql2 = " and " + keyword + " like '%" + keyvalue + "%'";
2025-04-14 10:57:27 +08:00
}
2025-05-15 15:10:24 +08:00
string sqlcount = $"select count(*) from " + tablename + " where 1=1 ";
2025-12-25 14:52:31 +08:00
var count = _client.Ado.GetInt(sqlcount + sql2);
2025-04-14 10:57:27 +08:00
var result1 = _client.Ado.SqlQuery<dynamic>(sql1);
2025-12-25 14:52:31 +08:00
result.Add("data", result1);
2025-05-15 15:10:24 +08:00
result.Add("total", count);
2025-04-14 10:57:27 +08:00
return new Response<Dictionary<string, object>>
{
Result = result
};
}
2025-12-25 14:52:31 +08:00
public Response<MemoryStream> TempeleteByTableName(string tablename, int type, string shpFilePath, string shpFilePathzip)
2025-04-14 10:57:27 +08:00
{
Response<MemoryStream> response = new Response<MemoryStream>();
string sql = $"select column_name as name from INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'public' AND table_name ='" + tablename + "'";
var cloumnlist = _client.Ado.SqlQuery<string>(sql);
2025-04-17 14:48:05 +08:00
if (type == 0)
{
try
{
HSSFWorkbook workbook = new HSSFWorkbook();
IFont font = workbook.CreateFont(); //创建一个字体样式对象
font.FontName = "Microsoft YaHei"; //和excel里面的字体对应
font.Boldweight = (short)FontBoldWeight.Bold; //字体加粗
font.FontHeightInPoints = 12; //字体大小
ICellStyle style1 = workbook.CreateCellStyle(); //创建样式对象
style1.BorderBottom = BorderStyle.Thin;
style1.BorderLeft = BorderStyle.Thin;
style1.BorderRight = BorderStyle.Thin;
style1.BorderTop = BorderStyle.Thin;
style1.Alignment = HorizontalAlignment.Center;
style1.VerticalAlignment = VerticalAlignment.Center;
style1.SetFont(font); //将字体样式赋给样式对象
// 创建一个数值格式的CellStyle
ICellStyle numericStyle = workbook.CreateCellStyle();
// 创建一个数据格式对象
IDataFormat dataFormat = workbook.CreateDataFormat();
var font11 = workbook.CreateFont();
font11.FontName = "Microsoft YaHei";
font11.FontHeightInPoints = 12;
// 设置单元格格式为数值型
numericStyle.DataFormat = dataFormat.GetFormat("0.00");
numericStyle.BorderBottom = BorderStyle.Thin;
numericStyle.BorderLeft = BorderStyle.Thin;
numericStyle.BorderRight = BorderStyle.Thin;
numericStyle.BorderTop = BorderStyle.Thin;
numericStyle.Alignment = HorizontalAlignment.Center;
numericStyle.VerticalAlignment = VerticalAlignment.Center;
numericStyle.SetFont(font11);
ISheet sheet = workbook.CreateSheet("Sheet0");
IRow rowHeader = sheet.CreateRow(0);
rowHeader.Height = 20 * 30;
2025-12-25 14:52:31 +08:00
for (int i = 0; i < cloumnlist.Count + 1; i++)
2025-04-17 14:48:05 +08:00
{
2025-04-26 14:12:14 +08:00
if (i == cloumnlist.Count)
{
rowHeader.CreateCell(i);
rowHeader.Cells[i].CellStyle = style1;
rowHeader.Cells[i].SetCellValue("lng");
}
2025-12-25 14:52:31 +08:00
else
{
2025-04-26 14:12:14 +08:00
var header = cloumnlist[i];
if (header.Contains("geom"))
{
rowHeader.CreateCell(i);
rowHeader.Cells[i].CellStyle = style1;
rowHeader.Cells[i].SetCellValue("lat");
}
2025-12-25 14:52:31 +08:00
else
{
2025-04-26 14:12:14 +08:00
rowHeader.CreateCell(i);
rowHeader.Cells[i].CellStyle = style1;
rowHeader.Cells[i].SetCellValue(header);
}
2025-12-25 14:52:31 +08:00
2025-04-26 14:12:14 +08:00
}
2025-12-25 14:52:31 +08:00
2025-04-17 14:48:05 +08:00
}
response.Result = new MemoryStream();
workbook.Write(response.Result);
workbook = null;
response.Result.Close();
response.Result.Dispose();
response.Code = 200;
response.Message = "获取成功";
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.Message;
}
}
if (type == 1)
2025-04-14 10:57:27 +08:00
{
2025-04-17 14:48:05 +08:00
List<IFeature> features = new List<IFeature>();
var attributes = new AttributesTable();
string wkt = "POINT(117.968055555555 35.4761111111111)";
var geometry = ParseGeometry(wkt);
2025-04-14 10:57:27 +08:00
for (int i = 0; i < cloumnlist.Count; i++)
{
2025-12-25 14:52:31 +08:00
2025-04-14 10:57:27 +08:00
var header = cloumnlist[i];
2025-04-17 14:48:05 +08:00
if (header.Contains("geom"))
{
continue;
}
attributes.Add(header, 11);
2025-04-14 10:57:27 +08:00
}
IFeature feature = new Feature(geometry, attributes);
2025-04-17 14:48:05 +08:00
features.Add(feature);
// 导出 SHP 文件及其关联文件
ExportToShapefileFour(shpFilePath, features);
// 将文件打包成 ZIP
CreateZipFromShapefiles(shpFilePath, shpFilePathzip);
2025-04-14 10:57:27 +08:00
}
2025-04-17 14:48:05 +08:00
return response;
}
public Geometry ParseGeometry(string wkt)
2025-04-17 14:48:05 +08:00
{
if (string.IsNullOrEmpty(wkt))
2025-04-14 10:57:27 +08:00
{
2025-04-17 14:48:05 +08:00
return null;
2025-04-14 10:57:27 +08:00
}
2025-04-17 14:48:05 +08:00
var reader = new WKTReader();
return reader.Read(wkt);
2025-04-14 10:57:27 +08:00
}
2025-04-17 14:48:05 +08:00
public void ExportToShapefileFour(string shpPath, List<IFeature> features)
{
//var geometryFactory = new GeometryFactory();
//// 写入 CPG 文件(定义字符编码)
//var cpgPath = System.IO.Path.ChangeExtension(shpPath, "cpg");
//File.WriteAllText(cpgPath, "UTF-8", Encoding.UTF8);
//// 获取 SHP 文件头
//var header = ShapefileDataWriter.GetHeader(features[0], features.Count);
//// 创建 ShapefileDataWriter
//var shapeFileWriter = new ShapefileDataWriter(shpPath, geometryFactory)
//{
// Header = header
//};
//// 写入 SHP 文件
//shapeFileWriter.Write(features);
2025-04-14 10:57:27 +08:00
2025-04-17 14:48:05 +08:00
// 写入 CPG 文件(定义字符编码)
string prjStr =
@"GEOGCS[""GCS_China_Geodetic_Coordinate_System_2000"",DATUM[""D_China_2000"",SPHEROID[""CGCS2000"",6378137.0,298.257222101]],PRIMEM[""Greenwich"",0.0],UNIT[""Degree"",0.0174532925199433]]";
var cpgPath = Path.ChangeExtension(shpPath, "prj");
File.WriteAllText(cpgPath, prjStr, Encoding.UTF8);
Shapefile.WriteAllFeatures(features, shpPath, encoding: Encoding.UTF8);
}
public void CreateZipFromShapefiles(string shpPath, string zipPath)
2025-04-14 10:57:27 +08:00
{
2025-04-17 14:48:05 +08:00
var files = new List<string>
{
Path.ChangeExtension(shpPath, "cpg"),
shpPath,
Path.ChangeExtension(shpPath, "shx"),
Path.ChangeExtension(shpPath, "dbf"),
Path.ChangeExtension(shpPath, "prj")
};
2025-04-14 10:57:27 +08:00
2025-04-17 14:48:05 +08:00
using (var zipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Create))
{
foreach (var file in files)
{
if (File.Exists(file))
{
zipArchive.CreateEntryFromFile(file, Path.GetFileName(file));
}
2025-04-14 10:57:27 +08:00
}
}
2025-04-17 14:48:05 +08:00
}
public async Task<bool> UploadExcel1(string tableName, IFormFile file)
{
if (file == null || file.Length == 0)
{
return false;
}
try
{
using (var stream = new MemoryStream())
{
// 将文件复制到内存流
file.CopyTo(stream);
stream.Position = 0;
2025-04-14 10:57:27 +08:00
2025-04-17 14:48:05 +08:00
IWorkbook workbook;
if (Path.GetExtension(file.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
{
workbook = new XSSFWorkbook(stream); // 处理 .xlsx 文件
}
else
{
workbook = new HSSFWorkbook(stream);
}
//
var sheet = workbook.GetSheetAt(0);
// 获取总行数
var rowCount = sheet.LastRowNum;
if (rowCount < 1)
{
throw new Exception("没有数据");
}
var currentRow1 = sheet.GetRow(0);
var cols = new string[currentRow1.LastCellNum];
for (int i = 0; i < currentRow1.LastCellNum; i++)
{
var cell1 = currentRow1.GetCell(i);
cols[i] = cell1.ToString();
}
var result = new List<string[]>();
List<Dictionary<String, Object>> listmap = new List<Dictionary<String, Object>>();
// 遍历每一行
2025-04-30 13:24:35 +08:00
var maxid = nextid(tableName, "Id");
2025-04-17 14:48:05 +08:00
for (int row = 1; row <= rowCount; row++)
{
2025-04-30 13:24:35 +08:00
maxid++;
2025-04-17 14:48:05 +08:00
var currentRow = sheet.GetRow(row);
if (currentRow == null) continue;
Dictionary<String, Object> map = new Dictionary<String, Object>();
2025-04-26 14:12:14 +08:00
string lat = null, lng = null;
2025-04-30 13:24:35 +08:00
2025-04-17 14:48:05 +08:00
for (int col = 0; col < currentRow.LastCellNum; col++)
{
2025-04-26 14:12:14 +08:00
var cell = currentRow.GetCell(col);
if (cell == null)
{
continue;
}
2025-04-17 14:48:05 +08:00
string head = cols[col];
2025-04-26 14:12:14 +08:00
var cellValue = cell.ToString();
if ("id".Equals(head.ToLower()))
{
2025-04-30 13:24:35 +08:00
map.Add(head, maxid);
2025-04-26 14:12:14 +08:00
}
switch (head)
{
case "lat":
lat = cellValue;
continue;
case "lng":
lng = cellValue;
continue;
case "Id":
continue;
case "id":
continue;
case "ID":
continue;
default:
map.Add(head, cellValue);
break;
}
}
// 遍历列
2025-12-25 14:52:31 +08:00
2025-04-26 14:12:14 +08:00
if (lat != null && lng != null)
{
var geography =
NtsGeometryServices.Instance.CreateGeometryFactory(
int.Parse("4326"));
var point = geography.CreatePoint(new Coordinate(double.Parse(lng), double.Parse(lat)));
map["geom"] = point.AsText();
2025-04-17 14:48:05 +08:00
}
listmap.Add(map);
2025-12-25 14:52:31 +08:00
}
2025-04-17 14:48:05 +08:00
_client.Insertable(listmap).AS(tableName).ExecuteCommand();
await GeoUtil.CreateStoreAndLayer("", "EPSG:4326", tableName, "");
// 返回结果
return true;
}
}
catch (Exception ex)
{
2025-12-25 14:52:31 +08:00
return false;
2025-04-17 14:48:05 +08:00
}
2025-04-14 10:57:27 +08:00
}
2025-12-25 14:52:31 +08:00
public bool UpdateTableData(UpdateTableReq req)
{
2025-04-14 10:57:27 +08:00
string tableName = req.TableName;
2025-04-17 14:48:05 +08:00
string id = "";
2025-04-14 10:57:27 +08:00
var list = req.list;
string sql = "update " + tableName + " set ";
string sql1 = "";
2025-04-17 14:48:05 +08:00
string num = "";
2025-04-30 13:24:35 +08:00
2025-04-17 14:48:05 +08:00
for (int i = 0; i < list.Count; i++)
{
var map = list[i];
if ("id".Equals(map["name"].ToString().ToLower()))
{
num = map["name"].ToString();
id = map["value"].ToString();
continue;
}
if (map["name"].ToString().ToLower().Contains("time"))
{
continue;
}
if (map["value"] != null)
{
sql1 = sql1 + "\"" + map["name"] + "\" = '" + map["value"] + "',";
}
2025-04-14 10:57:27 +08:00
}
2025-12-25 14:52:31 +08:00
var sql2 = sql1.Substring(0, sql1.Length - 1) + " where 1=1 and \"" + num + "\"=" + id;
2025-04-14 10:57:27 +08:00
_client.Ado.ExecuteCommand(sql + sql2);
return true;
}
2025-04-30 13:24:35 +08:00
public bool AddTableData(UpdateTableReq req)
{
string tableName = req.TableName;
string id = "";
var list = req.list;
string sql = "insert into " + tableName + " ( ";
string sql1 = "";
string sql2 = "";
string num = "";
2025-04-30 13:42:15 +08:00
for (int j = 0; j < list.Count; j++)
{
var map = list[j];
if ("id".Equals(map["name"].ToString().ToLower()))
{
id = map["name"].ToString();
}
}
2025-12-25 14:52:31 +08:00
var maxid = nextid(req.TableName, id);
2025-04-30 13:24:35 +08:00
for (int i = 0; i < list.Count; i++)
{
2025-12-25 14:52:31 +08:00
maxid++;
2025-04-30 13:24:35 +08:00
var map = list[i];
if ("id".Equals(map["name"].ToString().ToLower()))
{
2025-04-30 13:42:15 +08:00
sql1 = sql1 + "\"" + map["name"] + "\",";
2025-04-30 13:24:35 +08:00
sql2 = sql2 + maxid + ",";
continue;
}
if (map["name"].ToString().ToLower().Contains("time"))
{
continue;
}
if (map["value"] != null)
{
2025-04-30 13:42:15 +08:00
sql1 = sql1 + "\"" + map["name"] + "\",";
2025-04-30 13:24:35 +08:00
sql2 = sql2 + "'" + map["value"] + "'" + ",";
}
}
var sql3 = sql1.Substring(0, sql1.Length - 1) + ") values(" + sql2.Substring(0, sql2.Length - 1) + ")";
_client.Ado.ExecuteCommand(sql + sql3);
return true;
}
2025-12-25 14:52:31 +08:00
public bool UpdateTableData1(UpdateTableReq req)
2025-04-17 14:48:05 +08:00
{
string tableName = req.TableName;
string id = "";
var list = req.list;
string sql = "update " + tableName + " set ";
2025-12-25 14:52:31 +08:00
2025-04-17 14:48:05 +08:00
string num = "";
for (int i = 0; i < list.Count; i++)
{
var map = list[i];
string sql1 = "";
foreach (KeyValuePair<string, object> kvp in map)
{
string key = kvp.Key;
object value = kvp.Value;
if ("id".Equals(key.ToString().ToLower()))
{
num = key;
id = value.ToString();
continue;
}
2025-04-17 16:06:24 +08:00
if (key.ToLower().Contains("time"))
2025-04-17 14:48:05 +08:00
{
continue;
}
if (value != null)
{
sql1 = sql1 + "\"" + key + "\" = '" + value + "',";
}
// 处理键值对
}
var sql2 = sql1.Substring(0, sql1.Length - 1) + " where 1=1 and \"" + num + "\"=" + id;
_client.Ado.ExecuteCommand(sql + sql2);
}
2025-12-25 14:52:31 +08:00
2025-04-17 14:48:05 +08:00
return true;
}
2025-04-26 14:12:14 +08:00
public async Task<Response<Dictionary<string, object>>> UploadExcel2(string tableName, IFormFile file)
2025-04-17 14:48:05 +08:00
{
if (file == null || file.Length == 0)
{
2025-04-26 14:12:14 +08:00
return null;
2025-04-17 14:48:05 +08:00
}
try
{
using (var stream = new MemoryStream())
{
2025-04-26 14:12:14 +08:00
List<dynamic> dynamics = new List<dynamic>();
List<Dictionary<string, object>> dicts = new List<Dictionary<string, object>>();
2025-04-30 13:24:35 +08:00
List<Dictionary<string, object>> errordicts = new List<Dictionary<string, object>>();
2025-04-17 14:48:05 +08:00
// 将文件复制到内存流
file.CopyTo(stream);
stream.Position = 0;
IWorkbook workbook;
if (Path.GetExtension(file.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
{
workbook = new XSSFWorkbook(stream); // 处理 .xlsx 文件
}
else
{
workbook = new HSSFWorkbook(stream);
}
//
var sheet = workbook.GetSheetAt(0);
// 获取总行数
var rowCount = sheet.LastRowNum;
if (rowCount < 1)
{
throw new Exception("没有数据");
}
2025-04-30 13:24:35 +08:00
2025-12-25 14:52:31 +08:00
2025-04-17 14:48:05 +08:00
var currentRow1 = sheet.GetRow(0);
var cols = new string[currentRow1.LastCellNum];
2025-04-30 13:24:35 +08:00
string ids = "";
2025-04-17 14:48:05 +08:00
for (int i = 0; i < currentRow1.LastCellNum; i++)
{
var cell1 = currentRow1.GetCell(i);
cols[i] = cell1.ToString();
2025-05-15 15:10:24 +08:00
if (cell1.ToString().ToLower().Equals("gid"))
2025-04-30 13:24:35 +08:00
{
2025-12-25 14:52:31 +08:00
ids = cell1.ToString();
2025-04-30 13:24:35 +08:00
}
2025-04-17 14:48:05 +08:00
}
var result = new List<string[]>();
List<Dictionary<String, Object>> listmap = new List<Dictionary<String, Object>>();
// 遍历每一行
2025-04-30 13:24:35 +08:00
var maxid = nextid(tableName, ids);
var tablelist = tablestruc(tableName);
2025-04-17 14:48:05 +08:00
for (int row = 1; row <= rowCount; row++)
{
2025-04-30 13:24:35 +08:00
maxid++;
2025-04-17 14:48:05 +08:00
var currentRow = sheet.GetRow(row);
if (currentRow == null) continue;
Dictionary<String, Object> map = new Dictionary<String, Object>();
2025-04-26 14:12:14 +08:00
Dictionary<string, object> dict = new Dictionary<string, object>();
2025-04-30 13:24:35 +08:00
Dictionary<string, object> errorData = new Dictionary<string, object>();
2025-04-26 14:12:14 +08:00
// 读取列
var flag = false;
string lat = null, lng = null;
2025-04-30 13:24:35 +08:00
var flag1 = false;
2025-04-17 14:48:05 +08:00
for (int col = 0; col < currentRow.LastCellNum; col++)
{
2025-12-25 14:52:31 +08:00
2025-04-26 14:12:14 +08:00
var cell = currentRow.GetCell(col);
if (cell == null)
{
continue;
}
2025-04-17 14:48:05 +08:00
string head = cols[col];
2025-04-26 14:12:14 +08:00
var cellValue = cell.ToString();
2025-04-30 13:24:35 +08:00
for (int i = 0; i < tablelist.Count; i++)
2025-04-26 14:12:14 +08:00
{
2025-04-30 13:24:35 +08:00
string cloName = tablelist[i].ColumnName;
string cloType = tablelist[i].Type;
2025-12-25 14:52:31 +08:00
if (head.ToLower().Equals(cloName.ToLower()) && "gid".Equals(head.ToLower()))
2025-04-30 13:24:35 +08:00
{
dict.Add(head, cellValue);
errorData.Add(head, cellValue);
if (cloType.Contains("int"))
{
map.Add(head, maxid);
}
2025-12-25 14:52:31 +08:00
if (cloType.Contains("character"))
{
2025-04-30 13:24:35 +08:00
map.Add(head, Guid.NewGuid().ToString());
}
break;
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
}
2025-12-25 14:52:31 +08:00
if (head.Contains("geom"))
{
2025-04-30 13:24:35 +08:00
break;
}
if (head.Contains("time"))
{
break;
}
if (head.Equals(cloName))
{
2025-12-25 14:52:31 +08:00
var dictres = dealType(cloName, cloType, head, cellValue);
2025-04-30 13:24:35 +08:00
if ((bool)dictres["flag"])
{
map.Add(head, dictres["data"]);
dict.Add(head, cellValue);
errorData.Add(head, cellValue);
}
2025-12-25 14:52:31 +08:00
else
{
errorData.Add(head, "error" + cellValue);
2025-04-30 13:24:35 +08:00
dict.Add(head, cellValue);
flag1 = true;
}
}
2025-12-25 14:52:31 +08:00
2025-04-26 14:12:14 +08:00
}
2025-12-25 14:52:31 +08:00
2025-04-26 14:12:14 +08:00
switch (head)
{
case "lat":
lat = cellValue;
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
dict.Add(head, cellValue);
errorData.Add(head, cellValue);
2025-04-26 14:12:14 +08:00
continue;
case "lng":
lng = cellValue;
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
dict.Add(head, cellValue);
errorData.Add(head, cellValue);
2025-04-26 14:12:14 +08:00
continue;
2025-12-25 14:52:31 +08:00
2025-04-26 14:12:14 +08:00
default:
2025-12-25 14:52:31 +08:00
2025-04-26 14:12:14 +08:00
break;
}
2025-12-25 14:52:31 +08:00
2025-04-17 14:48:05 +08:00
2025-04-30 13:24:35 +08:00
}
if (flag1)
{
errordicts.Add(errorData);
continue;
2025-04-26 14:12:14 +08:00
}
// 遍历列
if (lat != null && lng != null)
{
var geography =
NtsGeometryServices.Instance.CreateGeometryFactory(
int.Parse("4326"));
var point = geography.CreatePoint(new Coordinate(double.Parse(lng), double.Parse(lat)));
map.Add("geom", point.AsText());
2025-12-25 14:52:31 +08:00
2025-04-26 14:12:14 +08:00
}
var geometryForWgs84 = map["geom"];
string sql = $"select column_name as name from INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'public' AND table_name ='" + tableName + "'";
var cloumnlist = _client.Ado.SqlQuery<string>(sql);
string names = "";
string geoms = "";
for (int l = 0; l < cloumnlist.Count; l++)
{
var name = cloumnlist[l];
if (name.Contains("geom"))
{
geoms = name;
}
else
{
names = names + "\"" + name + "\",";
}
}
string sql1 = $"select " + names + "ST_AsText(" + geoms + ") as " + geoms + $" from " + tableName + " where geom='" + geometryForWgs84 + "'";
var obj = _client.Ado.SqlQuerySingle<dynamic>(sql1);
if (obj != null)
{
dynamics.Add(obj);
dict.Add("geom", geometryForWgs84);
flag = true;
}
if (flag)
{
dicts.Add(dict);
continue;
2025-04-17 14:48:05 +08:00
}
listmap.Add(map);
}
_client.Insertable(listmap).AS(tableName).ExecuteCommand();
2025-04-26 14:12:14 +08:00
2025-04-17 14:48:05 +08:00
// 返回结果
2025-04-26 14:12:14 +08:00
Dictionary<string, object> res = new Dictionary<string, object>();
res.Add("nowData", dicts);
res.Add("originData", dynamics);
2025-04-30 13:24:35 +08:00
res.Add("errorData", errordicts);
2025-04-26 14:12:14 +08:00
return new Response<Dictionary<string, object>>
{
Result = res
};
2025-04-17 14:48:05 +08:00
}
}
catch (Exception ex)
{
2025-04-30 13:24:35 +08:00
return new Response<Dictionary<string, object>>
{
Result = null,
Message = ex.Message,
Code = 500
};
2025-04-17 14:48:05 +08:00
}
2025-04-26 14:12:14 +08:00
2025-04-17 14:48:05 +08:00
}
2025-04-14 10:57:27 +08:00
[HttpPost]
2025-04-30 13:24:35 +08:00
public async Task<Response<Dictionary<string, object>>> AddUploadExcel(UploadExcelReq req)
2025-04-14 10:57:27 +08:00
{
2025-12-25 14:52:31 +08:00
2025-04-17 14:48:05 +08:00
if (req.file == null || req.file.Length == 0)
{
2025-04-30 13:24:35 +08:00
return null;
2025-04-17 14:48:05 +08:00
}
try
{
using (var stream = new MemoryStream())
{
2025-04-26 14:12:14 +08:00
string sq2 = "delete from " + req.tableName;
_client.Ado.ExecuteCommand(sq2);
2025-04-17 14:48:05 +08:00
// 将文件复制到内存流
req.file.CopyTo(stream);
stream.Position = 0;
2025-04-30 13:24:35 +08:00
List<Dictionary<string, object>> errordicts = new List<Dictionary<string, object>>();
2025-04-17 14:48:05 +08:00
IWorkbook workbook;
if (Path.GetExtension(req.file.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
{
workbook = new XSSFWorkbook(stream); // 处理 .xlsx 文件
}
else
{
workbook = new HSSFWorkbook(stream);
}
//
var sheet = workbook.GetSheetAt(0);
// 获取总行数
var rowCount = sheet.LastRowNum;
if (rowCount < 1)
{
throw new Exception("没有数据");
}
var currentRow1 = sheet.GetRow(0);
var cols = new string[currentRow1.LastCellNum];
2025-04-30 13:24:35 +08:00
string ids = "";
2025-04-17 14:48:05 +08:00
for (int i = 0; i < currentRow1.LastCellNum; i++)
{
var cell1 = currentRow1.GetCell(i);
cols[i] = cell1.ToString();
2025-05-15 15:10:24 +08:00
if (cell1.ToString().ToLower().Equals("gid"))
2025-04-30 13:24:35 +08:00
{
ids = cell1.ToString();
}
2025-04-17 14:48:05 +08:00
}
var result = new List<string[]>();
List<Dictionary<String, Object>> listmap = new List<Dictionary<String, Object>>();
// 遍历每一行
2025-04-30 13:24:35 +08:00
var maxid = nextid(req.tableName, ids);
var tablelist = tablestruc(req.tableName);
// 遍历每一行
2025-04-17 14:48:05 +08:00
for (int row = 1; row <= rowCount; row++)
{
2025-04-30 13:24:35 +08:00
maxid++;
2025-04-17 14:48:05 +08:00
var currentRow = sheet.GetRow(row);
if (currentRow == null) continue;
Dictionary<String, Object> map = new Dictionary<String, Object>();
2025-04-30 13:24:35 +08:00
Dictionary<string, object> errorData = new Dictionary<string, object>();
2025-04-26 14:12:14 +08:00
string lat = null, lng = null;
2025-04-30 13:24:35 +08:00
var flag1 = false;
2025-04-17 14:48:05 +08:00
for (int col = 0; col < currentRow.LastCellNum; col++)
{
2025-04-26 14:12:14 +08:00
var cell = currentRow.GetCell(col);
if (cell == null)
{
continue;
}
2025-04-17 14:48:05 +08:00
string head = cols[col];
2025-04-26 14:12:14 +08:00
var cellValue = cell.ToString();
2025-04-30 13:24:35 +08:00
for (int i = 0; i < tablelist.Count; i++)
2025-04-26 14:12:14 +08:00
{
2025-04-30 13:24:35 +08:00
string cloName = tablelist[i].ColumnName;
string cloType = tablelist[i].Type;
2025-05-15 15:10:24 +08:00
if (head.ToLower().Equals(cloName.ToLower()) && "gid".Equals(head.ToLower()))
2025-04-30 13:24:35 +08:00
{
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
errorData.Add(head, cellValue);
if (cloType.Contains("int"))
{
map.Add(head, maxid);
}
if (cloType.Contains("character"))
{
map.Add(head, Guid.NewGuid().ToString());
}
break;
}
if (head.Contains("geom"))
{
break;
}
if (head.Contains("time"))
{
break;
}
if (head.Equals(cloName))
{
var dictres = dealType(cloName, cloType, head, cellValue);
if ((bool)dictres["flag"])
{
map.Add(head, dictres["data"]);
errorData.Add(head, cellValue);
}
else
{
errorData.Add(head, "error" + cellValue);
flag1 = true;
}
}
2025-04-26 14:12:14 +08:00
}
2025-04-30 13:24:35 +08:00
2025-04-26 14:12:14 +08:00
switch (head)
{
case "lat":
lat = cellValue;
2025-04-30 13:24:35 +08:00
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
errorData.Add(head, cellValue);
2025-04-26 14:12:14 +08:00
continue;
case "lng":
lng = cellValue;
2025-04-30 13:24:35 +08:00
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
errorData.Add(head, cellValue);
2025-04-26 14:12:14 +08:00
continue;
2025-04-30 13:24:35 +08:00
2025-04-26 14:12:14 +08:00
default:
2025-04-30 13:24:35 +08:00
2025-04-26 14:12:14 +08:00
break;
}
2025-04-17 14:48:05 +08:00
2025-04-26 14:12:14 +08:00
}
2025-04-30 13:24:35 +08:00
if (flag1)
{
errordicts.Add(errorData);
continue;
}
2025-04-26 14:12:14 +08:00
if (lat != null && lng != null)
{
var geography =
NtsGeometryServices.Instance.CreateGeometryFactory(
int.Parse("4326"));
var point = geography.CreatePoint(new Coordinate(double.Parse(lng), double.Parse(lat)));
map["geom"] = point.AsText();
2025-04-17 14:48:05 +08:00
}
listmap.Add(map);
}
_client.Insertable(listmap).AS(req.tableName).ExecuteCommand();
//await GeoUtil.CreateStoreAndLayer("", "EPSG:4326", req.tableName, "");
// 返回结果
2025-04-30 13:24:35 +08:00
Dictionary<string, object> res = new Dictionary<string, object>();
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
res.Add("errorData", errordicts);
return new Response<Dictionary<string, object>>
{
Result = res
};
2025-04-17 14:48:05 +08:00
}
}
catch (Exception ex)
{
2025-04-30 13:24:35 +08:00
return new Response<Dictionary<string, object>>
{
Result = null,
Message = ex.Message,
Code = 500
};
2025-04-17 14:48:05 +08:00
}
}
2025-05-15 15:10:24 +08:00
2025-12-25 14:52:31 +08:00
public async Task<Response<dynamic>> GetSldFilePath(string tablename)
{
2025-05-15 15:10:24 +08:00
Response<dynamic> response = new Response<dynamic>();
string sql = $"select * from sldfile_record where name='" + tablename + "'";
var filepath = _client.Ado.SqlQuerySingle<dynamic>(sql);
response.Result = filepath;
return response;
}
2025-12-25 14:52:31 +08:00
public async Task<Response<bool>> uploadSldStyle(string styleName, string tablename, string filepath, string fileid)
2025-04-17 14:48:05 +08:00
{
Response<bool> response = new Response<bool>();
2025-12-25 14:52:31 +08:00
2025-04-17 14:48:05 +08:00
var sldpath = Path.Combine(_filePath, filepath);
2025-05-15 15:10:24 +08:00
var flag = await GeoUtil.ExistStyle("", tablename);
var flag1 = false;
if (flag)
{
2025-12-25 14:52:31 +08:00
flag1 = await GeoUtil.UpdateSldStyle(filepath, tablename);
2025-05-15 15:10:24 +08:00
await GeoUtil.UpdateStyle("", tablename, tablename);
}
2025-12-25 14:52:31 +08:00
else
{
flag1 = await GeoUtil.PublishSldStyle(sldpath, tablename);
2025-05-15 15:10:24 +08:00
await GeoUtil.UpdateStyle("", tablename, tablename);
}
2025-12-25 14:52:31 +08:00
2025-05-15 15:10:24 +08:00
string sql = $"select count(*) from sldfile_record where name='" + tablename + "'";
var count = _client.Ado.GetInt(sql);
if (count > 0)
{
2025-12-25 14:52:31 +08:00
string updatesql = $"update sldfile_record set filepath='" + filepath + "',styleName='" + tablename + "',fileid='" + fileid + "' where name='" + tablename + "'";
2025-05-15 15:10:24 +08:00
_client.Ado.ExecuteCommand(updatesql);
}
2025-12-25 14:52:31 +08:00
else
{
2025-05-15 15:10:24 +08:00
string id = Guid.NewGuid().ToString();
2025-12-25 14:52:31 +08:00
string insertsql = $"insert into sldfile_record(id,name,filepath,stylename,fileid) values('" + id + "','" + tablename + "','" + filepath + "','" + tablename + "'," + fileid + "')";
2025-05-15 15:10:24 +08:00
_client.Ado.ExecuteCommand(insertsql);
}
2025-04-17 14:48:05 +08:00
if (flag1)
{
response.Message = "发布成功";
response.Result = flag1;
return response;
}
2025-12-25 14:52:31 +08:00
else
{
2025-04-17 14:48:05 +08:00
response.Message = "发布失败";
response.Result = false;
2025-05-15 15:10:24 +08:00
response.Code = 500;
2025-04-17 14:48:05 +08:00
return response;
}
}
2025-04-30 13:24:35 +08:00
public async Task<Response<Dictionary<string, object>>> UploadShape2(string zipFilePath, string tableName
2025-04-17 14:48:05 +08:00
)
{
try
{
string sq2 = "delete from " + tableName;
_client.Ado.ExecuteCommand(sq2);
string srd = "";
using var db = Repository.AsSugarClient();
// 开启事务
// 取得文件完全路径
zipFilePath = Path.Combine(_filePath, zipFilePath);
// 确保ZIP文件存在
if (!File.Exists(zipFilePath))
{
throw new FileNotFoundException("ZIP文件未找到。");
}
// 打开ZIP存档 取除后缀之前的字符串
var extractPath = zipFilePath.Substring(0, zipFilePath.LastIndexOf(".", StringComparison.Ordinal)) +
"extract";
// 解压文件
await UnZip(zipFilePath, extractPath);
var searchPattern = "*.shp"; // 设置你想要遍历的文件后缀名
var fileName = Directory.GetFiles(extractPath, searchPattern, SearchOption.AllDirectories);
if (fileName.IsEmpty())
{
throw new Exception("压缩文件中无shp文件");
}
2025-04-30 13:24:35 +08:00
List<Dictionary<string, object>> errordicts = new List<Dictionary<string, object>>();
2025-04-17 14:48:05 +08:00
var shpFileName = fileName[0];
var shpFile = Path.Combine(extractPath, shpFileName);
using (var dataReader = new ShapefileDataReader(shpFile, GeometryFactory.Default))
{
var dbfReader = dataReader.DbaseHeader;
List<string> headlist = new List<string>();
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
string ids = "";
2025-04-17 14:48:05 +08:00
foreach (var dbaseFieldDescriptor in dbfReader.Fields)
{
headlist.Add(dbaseFieldDescriptor.Name);
2025-05-15 15:10:24 +08:00
if ("gid".Equals(dbaseFieldDescriptor.Name.ToLower()))
2025-04-30 13:24:35 +08:00
{
ids = dbaseFieldDescriptor.Name;
}
2025-04-17 14:48:05 +08:00
}
2025-04-30 13:24:35 +08:00
2025-04-17 14:48:05 +08:00
Console.WriteLine("记录数" + dataReader.RecordCount);
//throw new Exception("失败");
var objs = new List<Dictionary<string, object>>(1001);
var num = 0;
int count = 0;
2025-04-30 13:24:35 +08:00
var maxid = nextid(tableName, ids);
var tablelist = tablestruc(tableName);
2025-04-17 14:48:05 +08:00
while (dataReader.Read())
{
2025-04-30 13:24:35 +08:00
maxid++;
2025-04-17 14:48:05 +08:00
count++;
Console.WriteLine("记录行:" + count);
2025-04-30 13:24:35 +08:00
var map = new Dictionary<string, object>();
var errorData = new Dictionary<string, object>();
var flag1 = false;
2025-04-17 14:48:05 +08:00
// 读取列
for (int i = 0; i < dataReader.FieldCount; i++)
{
var colName = dataReader.GetName(i);
2025-05-15 15:10:24 +08:00
if (dataReader.GetValue(i) == null)
{
map.Add(colName, dataReader.GetValue(i));
errorData.Add(colName, dataReader.GetValue(i));
continue;
}
2025-04-30 13:24:35 +08:00
for (int l = 0; l < tablelist.Count; l++)
{
string cloName = tablelist[l].ColumnName;
string cloType = tablelist[l].Type;
2025-05-15 15:10:24 +08:00
if (colName.ToLower().Equals(cloName.ToLower()) && "gid".Equals(colName.ToLower()))
2025-04-30 13:24:35 +08:00
{
errorData.Add(colName, dataReader.GetValue(i));
if (cloType.Contains("int"))
{
map.Add(colName, maxid);
}
if (cloType.Contains("character"))
{
map.Add(colName, Guid.NewGuid().ToString());
}
break;
}
2025-04-17 14:48:05 +08:00
if (colName.ToLower().Contains("time"))
{
break;
}
if (colName.ToLower().Equals("geometry"))
{
var geometry = (Geometry)dataReader.GetValue(i);
2025-04-17 14:48:05 +08:00
var geometryForWgs84 = GeometryFactory.Default.WithSRID(4326)
.CreateGeometry(geometry);
2025-04-30 13:24:35 +08:00
map.Add("geom", geometryForWgs84.AsText());
errorData.Add("geom", geometryForWgs84.AsText());
2025-04-17 14:48:05 +08:00
break;
}
2025-05-15 15:10:24 +08:00
if (colName.ToLower().Equals(cloName.ToLower()))
2025-04-17 14:48:05 +08:00
{
2025-04-30 13:24:35 +08:00
var dictres = dealType(cloName, cloType, "", dataReader.GetValue(i).ToString());
if ((bool)dictres["flag"])
{
map.Add(colName, dictres["data"]);
errorData.Add(colName, dataReader.GetValue(i));
}
else
{
errorData.Add(colName, dataReader.GetValue(i));
flag1 = true;
}
2025-04-17 14:48:05 +08:00
}
}
2025-04-30 13:24:35 +08:00
2025-04-17 14:48:05 +08:00
}
2025-04-30 13:24:35 +08:00
objs.Add(map);
2025-04-17 14:48:05 +08:00
if (num++ == 999)
{
await db.Insertable(objs)
.AS(tableName) // 指定目标表名
.ExecuteCommandAsync();
num = 0;
objs.Clear();
}
}
// 读取末尾
await db.Insertable(objs)
.AS(tableName) // 指定目标表名
.ExecuteCommandAsync();
}
await db.Ado.CommitTranAsync();
2025-04-30 13:24:35 +08:00
Dictionary<string, object> res = new Dictionary<string, object>();
res.Add("errorData", errordicts);
return new Response<Dictionary<string, object>>
{
Result = res
};
2025-04-17 14:48:05 +08:00
}
catch (Exception ex)
{
throw new CommonException(ex.Message, 500);
}
}
2025-12-25 14:52:31 +08:00
public async Task<Response<bool>> UploadShape3(string zipFilePath, string tableName
)
2025-04-17 14:48:05 +08:00
{
try
{
2025-12-25 14:52:31 +08:00
2025-04-17 14:48:05 +08:00
string srd = "";
using var db = Repository.AsSugarClient();
// 开启事务
2025-04-14 10:57:27 +08:00
2025-04-17 14:48:05 +08:00
// 取得文件完全路径
zipFilePath = Path.Combine(_filePath, zipFilePath);
// 确保ZIP文件存在
if (!File.Exists(zipFilePath))
{
throw new FileNotFoundException("ZIP文件未找到。");
}
2025-04-14 10:57:27 +08:00
2025-04-17 14:48:05 +08:00
// 打开ZIP存档 取除后缀之前的字符串
var extractPath = zipFilePath.Substring(0, zipFilePath.LastIndexOf(".", StringComparison.Ordinal)) +
"extract";
// 解压文件
await UnZip(zipFilePath, extractPath);
var searchPattern = "*.shp"; // 设置你想要遍历的文件后缀名
var fileName = Directory.GetFiles(extractPath, searchPattern, SearchOption.AllDirectories);
if (fileName.IsEmpty())
2025-04-14 10:57:27 +08:00
{
2025-04-17 14:48:05 +08:00
throw new Exception("压缩文件中无shp文件");
}
var shpFileName = fileName[0];
var shpFile = Path.Combine(extractPath, shpFileName);
using (var dataReader = new ShapefileDataReader(shpFile, GeometryFactory.Default))
{
var dbfReader = dataReader.DbaseHeader;
List<string> headlist = new List<string>();
foreach (var dbaseFieldDescriptor in dbfReader.Fields)
2025-04-14 10:57:27 +08:00
{
2025-04-17 14:48:05 +08:00
headlist.Add(dbaseFieldDescriptor.Name);
2025-04-14 10:57:27 +08:00
}
2025-04-17 14:48:05 +08:00
Console.WriteLine("记录数" + dataReader.RecordCount);
//throw new Exception("失败");
var objs = new List<Dictionary<string, object>>(1001);
var num = 0;
int count = 0;
2025-04-30 13:24:35 +08:00
var maxid = nextid(tableName, "Id");
2025-04-17 14:48:05 +08:00
while (dataReader.Read())
{
count++;
2025-04-30 13:24:35 +08:00
maxid++;
2025-04-17 14:48:05 +08:00
Console.WriteLine("记录行:" + count);
var insertObj = new Dictionary<string, object>();
// 读取列
for (int i = 0; i < dataReader.FieldCount; i++)
{
var colName = dataReader.GetName(i);
for (int k = 0; k < headlist.Count; k++)
{
var header = headlist[k];
if (colName.ToLower().Contains("time"))
{
break;
}
if (colName.ToLower().Equals("geometry"))
{
var geometry = (Geometry)dataReader.GetValue(i);
2025-04-17 14:48:05 +08:00
var geometryForWgs84 = GeometryFactory.Default.WithSRID(4326)
.CreateGeometry(geometry);
insertObj.Add("geom", geometryForWgs84.AsText());
break;
}
if (header.Equals(colName))
{
2025-04-30 13:24:35 +08:00
if (header.Equals("Id"))
{
insertObj.Add(header, maxid);
break;
}
else
{
insertObj.Add(header, dataReader.GetValue(i));
break;
}
2025-04-17 14:48:05 +08:00
}
}
}
objs.Add(insertObj);
if (num++ == 999)
{
await db.Insertable(objs)
.AS(tableName) // 指定目标表名
.ExecuteCommandAsync();
num = 0;
objs.Clear();
}
}
// 读取末尾
await db.Insertable(objs)
.AS(tableName) // 指定目标表名
.ExecuteCommandAsync();
2025-04-14 10:57:27 +08:00
}
2025-04-17 14:48:05 +08:00
await db.Ado.CommitTranAsync();
2025-04-14 10:57:27 +08:00
await GeoUtil.CreateStoreAndLayer("", "EPSG:4326", tableName, "");
}
2025-04-17 14:48:05 +08:00
catch (Exception ex)
{
throw new CommonException(ex.Message, 500);
}
return new Response<bool>
{
Result = true
};
}
private static async Task UnZip(string zipFilePath, string extractPath)
{
await Task.Run(() =>
{
Directory.CreateDirectory(extractPath);
// 设置字符编码
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var gbk = Encoding.GetEncoding("utf-8");
using (var archive = ZipFile.OpenRead(zipFilePath))
{
// 列举ZIP文件中的条目
foreach (var entry in archive.Entries)
{
var xxx = gbk.GetString(Encoding.Default.GetBytes(entry.FullName));
Console.WriteLine(xxx);
}
// 提取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("文件解压失败");
}
});
}
2025-12-25 14:52:31 +08:00
public async Task<Response<Dictionary<string, object>>> UploadShape1(string zipFilePath, string tableName
2025-04-17 14:48:05 +08:00
)
{
try
{
2025-12-25 14:52:31 +08:00
2025-04-17 14:48:05 +08:00
string srd = "";
using var db = Repository.AsSugarClient();
// 开启事务
// 取得文件完全路径
zipFilePath = Path.Combine(_filePath, zipFilePath);
// 确保ZIP文件存在
if (!File.Exists(zipFilePath))
{
throw new FileNotFoundException("ZIP文件未找到。");
}
// 打开ZIP存档 取除后缀之前的字符串
var extractPath = zipFilePath.Substring(0, zipFilePath.LastIndexOf(".", StringComparison.Ordinal)) +
"extract";
// 解压文件
await UnZip(zipFilePath, extractPath);
var searchPattern = "*.shp"; // 设置你想要遍历的文件后缀名
var fileName = Directory.GetFiles(extractPath, searchPattern, SearchOption.AllDirectories);
if (fileName.IsEmpty())
{
throw new Exception("压缩文件中无shp文件");
}
2025-04-14 10:57:27 +08:00
2025-04-17 14:48:05 +08:00
var shpFileName = fileName[0];
var shpFile = Path.Combine(extractPath, shpFileName);
List<dynamic> dynamics = new List<dynamic>();
List<Dictionary<string, object>> dicts = new List<Dictionary<string, object>>();
2025-04-30 13:24:35 +08:00
List<Dictionary<string, object>> errordicts = new List<Dictionary<string, object>>();
2025-04-17 14:48:05 +08:00
using (var dataReader = new ShapefileDataReader(shpFile, GeometryFactory.Default))
{
var dbfReader = dataReader.DbaseHeader;
List<string> headlist = new List<string>();
2025-04-30 13:24:35 +08:00
string ids = "";
2025-04-17 14:48:05 +08:00
foreach (var dbaseFieldDescriptor in dbfReader.Fields)
{
headlist.Add(dbaseFieldDescriptor.Name);
2025-05-15 15:10:24 +08:00
if ("gid".Equals(dbaseFieldDescriptor.Name.ToLower()))
2025-04-30 13:24:35 +08:00
{
ids = dbaseFieldDescriptor.Name;
}
2025-04-17 14:48:05 +08:00
}
Console.WriteLine("记录数" + dataReader.RecordCount);
//throw new Exception("失败");
var objs = new List<Dictionary<string, object>>(1001);
var num = 0;
int count = 0;
2025-04-30 13:24:35 +08:00
var maxid = nextid(tableName, ids);
var tablelist = tablestruc(tableName);
2025-04-17 14:48:05 +08:00
while (dataReader.Read())
{
count++;
2025-04-30 13:24:35 +08:00
maxid++;
2025-04-17 14:48:05 +08:00
Console.WriteLine("记录行:" + count);
2025-04-30 13:24:35 +08:00
var map = new Dictionary<string, object>();
var errorData = new Dictionary<string, object>();
var flag1 = false;
2025-04-17 14:48:05 +08:00
// 读取列
Dictionary<string, object> dict = new Dictionary<string, object>();
2025-12-25 14:52:31 +08:00
// 读取列
var flag = false;
for (int i = 0; i < dataReader.FieldCount; i++)
2025-04-17 14:48:05 +08:00
{
var colName = dataReader.GetName(i);
2025-12-25 14:52:31 +08:00
if (dataReader.GetValue(i) == null)
2025-05-15 15:10:24 +08:00
{
map.Add(colName, dataReader.GetValue(i));
errorData.Add(colName, dataReader.GetValue(i));
continue;
}
2025-04-30 13:24:35 +08:00
for (int l = 0; l < tablelist.Count; l++)
{
string cloName = tablelist[l].ColumnName;
string cloType = tablelist[l].Type;
2025-05-15 15:10:24 +08:00
if (colName.ToLower().Equals(colName.ToLower()) && "gid".Equals(colName.ToLower()))
2025-04-30 13:24:35 +08:00
{
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
errorData.Add(colName, dataReader.GetValue(i));
if (cloType.Contains("int"))
{
map.Add(colName, maxid);
}
if (cloType.Contains("character"))
{
map.Add(colName, Guid.NewGuid().ToString());
}
break;
}
if (colName.ToLower().Contains("time"))
{
break;
}
if (colName.ToLower().Equals("geometry"))
{
var geometry = (Geometry)dataReader.GetValue(i);
2025-04-30 13:24:35 +08:00
var geometryForWgs84 = GeometryFactory.Default.WithSRID(4326)
.CreateGeometry(geometry);
map.Add("geom", geometryForWgs84.AsText());
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
errorData.Add("geom", geometryForWgs84.AsText());
break;
}
2025-05-15 15:10:24 +08:00
if (colName.ToLower().Equals(colName.ToLower()))
2025-04-30 13:24:35 +08:00
{
var dictres = dealType(cloName, cloType, "", dataReader.GetValue(i).ToString());
if ((bool)dictres["flag"])
{
map.Add(colName, dictres["data"]);
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
errorData.Add(colName, dataReader.GetValue(i));
}
else
{
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
errorData.Add(colName, dataReader.GetValue(i));
flag1 = true;
}
}
}
2025-04-17 14:48:05 +08:00
if (colName.ToLower().Equals("geometry"))
{
var geometry = (Geometry)dataReader.GetValue(i);
2025-04-17 14:48:05 +08:00
var geometryForWgs84 = GeometryFactory.Default.WithSRID(4326)
.CreateGeometry(geometry);
string sql = $"select column_name as name from INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'public' AND table_name ='" + tableName + "'";
var cloumnlist = _client.Ado.SqlQuery<string>(sql);
2025-12-25 14:52:31 +08:00
2025-04-17 14:48:05 +08:00
string names = "";
string geoms = "";
for (int l = 0; l < cloumnlist.Count; l++)
{
var name = cloumnlist[l];
if (name.Contains("geom"))
{
geoms = name;
2025-04-14 10:57:27 +08:00
2025-04-17 14:48:05 +08:00
}
else
{
names = names + "\"" + name + "\",";
}
}
string sql1 = $"select " + names + "ST_AsText(" + geoms + ") as " + geoms + $" from " + tableName + " where geom='" + geometryForWgs84.AsText() + "'";
var obj = _client.Ado.SqlQuerySingle<dynamic>(sql1);
2025-12-25 14:52:31 +08:00
if (obj != null)
2025-04-17 14:48:05 +08:00
{
dynamics.Add(obj);
dict.Add("geom", geometryForWgs84.AsText());
flag = true;
}
2025-12-25 14:52:31 +08:00
2025-04-17 14:48:05 +08:00
}
2025-12-25 14:52:31 +08:00
else
{
2025-04-17 14:48:05 +08:00
dict.Add(colName, dataReader.GetValue(i));
}
2025-12-25 14:52:31 +08:00
2025-04-17 14:48:05 +08:00
2025-04-30 13:24:35 +08:00
}
if (flag1)
{
errordicts.Add(errorData);
continue;
2025-04-17 14:48:05 +08:00
}
if (flag)
{
dicts.Add(dict);
continue;
}
2025-04-30 13:24:35 +08:00
objs.Add(map);
2025-04-17 14:48:05 +08:00
if (num++ == 999)
{
await db.Insertable(objs)
.AS(tableName) // 指定目标表名
.ExecuteCommandAsync();
num = 0;
objs.Clear();
}
}
// 读取末尾
await db.Insertable(objs)
.AS(tableName) // 指定目标表名
.ExecuteCommandAsync();
}
await db.Ado.CommitTranAsync();
Dictionary<string, object> res = new Dictionary<string, object>();
res.Add("nowData", dicts);
res.Add("originData", dynamics);
2025-04-30 13:24:35 +08:00
res.Add("errorData", errordicts);
2025-04-17 14:48:05 +08:00
return new Response<Dictionary<string, object>>
{
Result = res
};
}
catch (Exception ex)
{
throw new CommonException(ex.Message, 500);
}
2025-12-25 14:52:31 +08:00
2025-04-14 10:57:27 +08:00
}
2025-12-25 14:52:31 +08:00
public int nextid(string tablename, string id)
{
2025-04-30 13:24:35 +08:00
string sql = "select max(\"" + id + "\") from " + tablename;
var maxid = _client.Ado.GetInt(sql);
return maxid;
}
2025-12-25 14:52:31 +08:00
public List<dynamic> tablestruc(string tableName)
{
2025-04-30 13:24:35 +08:00
string tableStruc = $"\t\t\r\nSELECT \r\n c.column_name AS \"ColumnName\",\r\n c.data_type AS \"Type\"\r\n\r\nFROM \r\n " +
$" information_schema.columns c\r\nLEFT JOIN \r\n pg_catalog.pg_description pgd ON pgd.objoid = c.table_name::regclass::oid \r\n " +
$" AND pgd.objsubid = c.ordinal_position\r\nWHERE \r\n c.table_name = '" + tableName + "'; ";
2025-12-25 14:52:31 +08:00
return _client.Ado.SqlQuery<dynamic>(tableStruc);
2025-04-30 13:24:35 +08:00
}
2025-12-25 14:52:31 +08:00
public Dictionary<string, object> dealType(string cloum, string type, string head, string data)
{
2025-04-30 13:24:35 +08:00
Dictionary<string, object> dict = new Dictionary<string, object>();
string str = "";
bool flag = false;
double d = 0.0;
2025-05-15 15:10:24 +08:00
float f = 0.0f;
2025-04-30 13:24:35 +08:00
int i = 0;
2025-12-25 14:52:31 +08:00
Object obj = null;
2025-04-30 13:24:35 +08:00
switch (type)
{
case "real":
2025-12-25 14:52:31 +08:00
flag = float.TryParse(data, out f);
2025-05-15 15:10:24 +08:00
obj = f;
2025-04-30 13:24:35 +08:00
break;
case "integer":
flag = Int32.TryParse(data, out i);
obj = i;
break;
case "numeric":
flag = double.TryParse(data, out d);
obj = d;
break;
case "bigint":
flag = Int32.TryParse(data, out i);
obj = i;
break;
case "double precision":
flag = double.TryParse(data, out d);
obj = d;
break;
case "character varying":
2025-12-25 14:52:31 +08:00
flag = true;
obj = data;
2025-04-30 13:24:35 +08:00
break;
2025-05-15 15:10:24 +08:00
case "date":
flag = true;
obj = new DateTime();
break;
case "timestamp without time zone":
flag = true;
obj = new Timestamp();
break;
2025-12-25 14:52:31 +08:00
2025-04-30 13:24:35 +08:00
default:
flag = false;
break;
}
dict.Add("flag", flag);
dict.Add("data", obj);
return dict;
}
2025-01-10 10:14:58 +08:00
}