| | |
| | | using DataLoader.Model; |
| | | using Npgsql; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Collections.ObjectModel; |
| | | using System.Data.Common; |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Reflection; |
| | | using System.Windows.Forms; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | |
| | | namespace DataLoader.CS |
| | | { |
| | | public class Importor |
| | | { |
| | | private static PostgreHelper _ph = null; |
| | | |
| | | public static PostgreHelper Helper |
| | | #region 加载 |
| | | /// <summary> |
| | | /// 获取文件 |
| | | /// </summary> |
| | | public static void GetFiles(ObservableCollection<ViewData> viewDatas, string path) |
| | | { |
| | | get |
| | | { |
| | | if (_ph == null) _ph = new PostgreHelper(DbEnum.langfang); |
| | | GetSubFiles(viewDatas, path); |
| | | |
| | | return _ph; |
| | | string[] directories = Directory.GetDirectories(path); |
| | | if (null == directories || directories.Length == 0) return; |
| | | |
| | | foreach (string sub in directories) |
| | | { |
| | | if (IsGdbFile(sub)) |
| | | { |
| | | SetSysMeta(viewDatas, sub, "gdb"); |
| | | continue; |
| | | } |
| | | if (IsOsgbFile(sub)) |
| | | { |
| | | SetSysMeta(viewDatas, sub, "osgb"); |
| | | continue; |
| | | } |
| | | |
| | | GetFiles(viewDatas, sub); |
| | | } |
| | | } |
| | | |
| | | private static readonly string insertMeta = "insert into lf.sys_meta (eventid, metaid, dirid, depid, verid, name, type, guid, path, sizes, tab, rows, create_user, create_time) values (@eventid, @metaid, @dirid, @depid, @verid, @name, @type, @guid, @path, @sizes, @tab, @rows, @create_user, now()) returning id"; |
| | | |
| | | public static string GetFilePathByGuid(string guid) |
| | | /// <summary> |
| | | /// 设置元数据 |
| | | /// </summary> |
| | | private static void SetSysMeta(ObservableCollection<ViewData> viewDatas, string path, string ext) |
| | | { |
| | | string sql = "select path from lf.sys_meta where guid = @guid limit 1"; |
| | | ViewData vd = new ViewData(); |
| | | vd.ID = viewDatas.Count + 1; |
| | | vd.FilePath = path; |
| | | vd.Ext = "." + ext; |
| | | vd.Status = "准备"; |
| | | viewDatas.Add(vd); |
| | | |
| | | DbParameter dp = new NpgsqlParameter("@guid", guid); |
| | | object obj = Helper.GetScalar(sql, dp); |
| | | SetSysMeta(vd); |
| | | |
| | | return obj == null ? null : obj.ToString(); |
| | | DirectoryInfo di = new DirectoryInfo(vd.FilePath); |
| | | vd.Meta.name = di.Name; |
| | | vd.Meta.type = ext; |
| | | } |
| | | |
| | | public static bool IsFileExists(string guid) |
| | | /// <summary> |
| | | /// 获取子文件 |
| | | /// </summary> |
| | | private static void GetSubFiles(ObservableCollection<ViewData> viewDatas, string path) |
| | | { |
| | | string sql = "select count(*) from lf.sys_meta where guid = @guid"; |
| | | string[] files = Directory.GetFiles(path); |
| | | if (null == files || files.Length == 0) return; |
| | | |
| | | DbParameter dp = new NpgsqlParameter("@guid", guid); |
| | | object obj = Helper.GetScalar(sql, dp); |
| | | |
| | | return obj != null && Convert.ToInt32(obj) > 0; |
| | | } |
| | | |
| | | public static int InsertMeta(SysMeta meta) |
| | | { |
| | | List<DbParameter> list = Tool.GetParams<SysMeta>(insertMeta, meta); |
| | | |
| | | object obj = Helper.GetScalar(insertMeta, list.ToArray()); |
| | | |
| | | return obj == null ? 0 : Convert.ToInt32(obj); |
| | | } |
| | | |
| | | public static void GetFiles(ObservableCollection<ViewData> viewDatas, string source) |
| | | { |
| | | string[] files = Directory.GetFiles(source); |
| | | for (int i = 0, c = files.Length; i < c; i++) |
| | | foreach (string file in files) |
| | | { |
| | | string ext = Path.GetExtension(file).ToLower(); |
| | | if (!StaticData.ALL_EXTENSION.Contains(ext) || IsExcludeFile(file)) continue; |
| | | |
| | | ViewData vd = new ViewData(); |
| | | vd.ID = i + 1; |
| | | vd.FilePath = files[i]; |
| | | vd.ID = viewDatas.Count + 1; |
| | | vd.FilePath = file; |
| | | vd.Ext = ext; |
| | | vd.Status = "准备"; |
| | | viewDatas.Add(vd); |
| | | |
| | | SetSysMeta(vd); |
| | | |
| | | FileInfo fi = new FileInfo(vd.FilePath); |
| | | vd.Meta.name = fi.Name; |
| | | vd.Meta.type = fi.Extension.ToLower().Replace(".", ""); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 设置元数据 |
| | | /// </summary> |
| | | private static void SetSysMeta(ViewData vd) |
| | | { |
| | | FileInfo fi = new FileInfo(vd.FilePath); |
| | | long sizes = GetFileSizes(vd); |
| | | |
| | | vd.Meta = new SysMeta(); |
| | | vd.Meta.name = fi.Name; |
| | | vd.Meta.dirid = 1; |
| | | vd.Meta.depid = 1; |
| | | vd.Meta.verid = 0; |
| | | vd.Meta.type = fi.Extension.ToLower().Replace(".", ""); |
| | | vd.Meta.sizes = Tool.SizeToMb(fi.Length); |
| | | vd.Meta.create_user = Tool.UserId; |
| | | SysMeta meta = new SysMeta(); |
| | | meta.eventid = Guid.NewGuid().ToString(); |
| | | meta.metaid = 0; |
| | | meta.verid = 0; |
| | | meta.guid = null; |
| | | meta.path = vd.FilePath; // * |
| | | meta.sizes = Tools.SizeToMb(sizes); |
| | | meta.tab = null; |
| | | meta.rows = 0; |
| | | meta.create_user = CommonProp.UserId; |
| | | meta.create_time = DateTime.Now; |
| | | meta.bak = null; |
| | | meta.geom = null; |
| | | meta.layer = null; |
| | | meta.depcode = CommonProp.Depcode; |
| | | meta.dircode = CommonProp.Dircode; |
| | | meta.ismeta = 0; |
| | | meta.sensortype = CommonProp.SensorType; |
| | | meta.acq_time = CommonProp.AcqTime; |
| | | |
| | | vd.Meta = meta; |
| | | vd.Sizes = Tools.FormatBytes(sizes); |
| | | } |
| | | |
| | | public static void ImportFiles(ObservableCollection<ViewData> viewDatas, string target) |
| | | /// <summary> |
| | | /// 获取文件大小 |
| | | /// </summary> |
| | | private static long GetFileSizes(ViewData vd) |
| | | { |
| | | int start = 1; |
| | | foreach (ViewData vd in viewDatas) |
| | | if (File.Exists(vd.FilePath)) |
| | | { |
| | | switch (vd.Ext) |
| | | { |
| | | case StaticData.MPT: |
| | | return GetFileSizes(GetExtFiles(vd, StaticData.MPT_EXT)); |
| | | case StaticData.JPG: |
| | | return GetFileSizes(GetExtFiles(vd, StaticData.JPG_EXT)); |
| | | case StaticData.JP2: |
| | | return GetFileSizes(GetExtFiles(vd, StaticData.JP2_EXT)); |
| | | case StaticData.IMG: |
| | | return GetFileSizes(GetExtFiles(vd, StaticData.IMG_EXT)); |
| | | case StaticData.TIF: |
| | | return GetFileSizes(GetExtFiles(vd, StaticData.TIF_EXT)); |
| | | case StaticData.TIFF: |
| | | return GetFileSizes(GetExtFiles(vd, StaticData.TIFF_EXT)); |
| | | case StaticData.SHP: |
| | | return GetFileSizes(GetExtFiles(vd, StaticData.SHP_EXT)); |
| | | default: |
| | | FileInfo fi = new FileInfo(vd.FilePath); |
| | | return fi.Length; |
| | | } |
| | | } |
| | | |
| | | if (Directory.Exists(vd.FilePath)) |
| | | { |
| | | string[] files = Directory.GetFiles(vd.FilePath, "*", SearchOption.AllDirectories); |
| | | return GetFileSizes(files); |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取文件大小 |
| | | /// </summary> |
| | | private static long GetFileSizes(string[] files) |
| | | { |
| | | if (null == files || files.Length == 0) return 0; |
| | | |
| | | long sizes = 0; |
| | | foreach (string file in files) |
| | | { |
| | | FileInfo fi = new FileInfo(file); |
| | | sizes += fi.Length; |
| | | } |
| | | |
| | | return sizes; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取SHP文件集合 |
| | | /// </summary> |
| | | private static string[] GetExtFiles(ViewData vd, List<string> exts) |
| | | { |
| | | List<string> files = new List<string>(); |
| | | files.Add(vd.FilePath); |
| | | |
| | | foreach (string ext in exts) |
| | | { |
| | | string file = vd.FilePath.Replace(vd.Ext, ext); |
| | | if (File.Exists(file)) |
| | | { |
| | | files.Add(file); |
| | | } |
| | | } |
| | | |
| | | return files.ToArray(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 是否为GDB文件 |
| | | /// </summary> |
| | | private static bool IsGdbFile(string path) |
| | | { |
| | | if (!Directory.Exists(path)) return false; |
| | | |
| | | DirectoryInfo info = new DirectoryInfo(path); |
| | | if (!info.Name.ToLower().EndsWith(StaticData.GDB)) return false; |
| | | |
| | | string gdb = Path.Combine(path, StaticData.NGDB); |
| | | |
| | | return File.Exists(gdb); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 是否为OSGB文件 |
| | | /// </summary> |
| | | private static bool IsOsgbFile(string path) |
| | | { |
| | | if (!Directory.Exists(path)) return false; |
| | | |
| | | string meta = Path.Combine(path, "metadata.xml"); |
| | | string data = Path.Combine(path, "Data"); |
| | | |
| | | return File.Exists(meta) && Directory.Exists(data); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 是/否为排除文件 |
| | | /// </summary> |
| | | private static bool IsExcludeFile(string file) |
| | | { |
| | | bool isExclude = false; |
| | | |
| | | string name = Path.GetFileName(file).ToLower(); |
| | | foreach (string ext in StaticData.MAPPER_EXCLUDE_EXT) |
| | | { |
| | | if (name.EndsWith(ext)) |
| | | { |
| | | isExclude = true; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | return isExclude; |
| | | } |
| | | #endregion |
| | | |
| | | #region 导入 |
| | | /// <summary> |
| | | /// 导入文件 |
| | | /// </summary> |
| | | public static void ImportFiles(ObservableCollection<ViewData> viewDatas) |
| | | { |
| | | Parallel.ForEach(viewDatas, (vd, ParallelLoopState) => |
| | | { |
| | | try |
| | | { |
| | | vd.Status = "生成MD5码..."; |
| | | string guid = MD5Helper.GetMD5Hash(vd.FilePath); |
| | | if (IsFileExists(guid)) |
| | | vd.Meta.guid = GetFilesMD5(vd); |
| | | |
| | | if (!string.IsNullOrEmpty(vd.Meta.guid) && DBHelper.IsFileExists(vd.Meta.guid)) |
| | | { |
| | | vd.Status = "已存在!"; |
| | | continue; |
| | | return; |
| | | } |
| | | |
| | | vd.Status = "插入数据库..."; |
| | | start = GetSubPath(target, start); |
| | | SysMeta mf = GetMetaFile(vd, start, guid); |
| | | |
| | | int metaId = InsertMeta(mf); |
| | | if (metaId == 0) |
| | | { |
| | | vd.Status = "元数据出错!"; |
| | | continue; |
| | | } |
| | | vd.Status = "读取栅格信息..."; |
| | | ReadRasterInfo(vd); |
| | | |
| | | vd.Status = "复制文件..."; |
| | | CopyFile(vd.FilePath, Path.Combine(target, mf.path)); |
| | | CopyFiles(vd); |
| | | |
| | | vd.Status = "完成。"; |
| | | vd.Status = "元数据入库..."; |
| | | int id = DBHelper.InsertMeta(vd.Meta); |
| | | vd.Status = id > 0 ? "成功。" : "失败!"; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | vd.Status = "失败!"; |
| | | LogOut.Error(ex.StackTrace); |
| | | vd.Status = string.Format("失败:{0}!", ex.Message); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取文件的MD5 |
| | | /// </summary> |
| | | private static string GetFilesMD5(ViewData vd) |
| | | { |
| | | if ("1" != Tools.GetSetting("GetMD5")) return null; |
| | | |
| | | if (File.Exists(vd.FilePath)) |
| | | { |
| | | if (vd.Ext == StaticData.SHP) |
| | | { |
| | | return GetFilesMD5(GetExtFiles(vd, StaticData.SHP_EXT)); |
| | | } |
| | | else |
| | | { |
| | | return MD5Helper.GetMD5Hash(vd.FilePath); |
| | | } |
| | | } |
| | | |
| | | if (Directory.Exists(vd.FilePath)) |
| | | { |
| | | string[] files = Directory.GetFiles(vd.FilePath, "*", SearchOption.AllDirectories); |
| | | return GetFilesMD5(files); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取多个文件的MD5码 |
| | | /// </summary> |
| | | private static string GetFilesMD5(string[] files) |
| | | { |
| | | if (null == files || files.Length == 0) return null; |
| | | |
| | | List<string> list = new List<string>(); |
| | | foreach (string file in files) |
| | | { |
| | | string md5 = MD5Helper.GetMD5Hash(file); |
| | | if (string.IsNullOrEmpty(md5)) list.Add(md5); |
| | | } |
| | | |
| | | if (list.Count == 0) return null; |
| | | |
| | | string str = string.Join(",", list.ToArray()); |
| | | byte[] bytes = Encoding.Unicode.GetBytes(str); |
| | | |
| | | return MD5Helper.GetMD5Hash(bytes); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 读取栅格信息 |
| | | /// </summary> |
| | | private static void ReadRasterInfo(ViewData vd) |
| | | { |
| | | if (!StaticData.RASTER_EXT.Contains(vd.Ext) || !File.Exists(vd.FilePath)) return; |
| | | |
| | | GdalHelper.Instance.ReadRasterInfo(vd); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 复制文件 |
| | | /// </summary> |
| | | private static void CopyFiles(ViewData vd) |
| | | { |
| | | if (string.IsNullOrEmpty(vd.Meta.guid)) vd.Meta.guid = vd.Meta.eventid; |
| | | |
| | | switch (vd.Ext) |
| | | { |
| | | case StaticData.MPT: |
| | | copyMultiFile(vd, StaticData.MPT_EXT); |
| | | break; |
| | | case StaticData.JPG: |
| | | copyMultiFile(vd, StaticData.JPG_EXT); |
| | | break; |
| | | case StaticData.JP2: |
| | | copyMultiFile(vd, StaticData.JP2_EXT); |
| | | break; |
| | | case StaticData.IMG: |
| | | copyMultiFile(vd, StaticData.IMG_EXT); |
| | | break; |
| | | case StaticData.TIF: |
| | | copyMultiFile(vd, StaticData.TIF_EXT); |
| | | break; |
| | | case StaticData.TIFF: |
| | | copyMultiFile(vd, StaticData.TIFF_EXT); |
| | | break; |
| | | case StaticData.SHP: |
| | | copyMultiFile(vd, StaticData.SHP_EXT); |
| | | break; |
| | | case StaticData.OSGB: |
| | | copyFolderFile(vd); |
| | | break; |
| | | case StaticData.GDB: |
| | | copyFolderFile(vd); |
| | | break; |
| | | default: |
| | | copySingleFile(vd); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | private static int GetSubPath(string target, int start = 1) |
| | | /// <summary> |
| | | /// 复制文件夹 |
| | | /// </summary> |
| | | private static int copyFolderFile(ViewData vd) |
| | | { |
| | | if (!Directory.Exists(vd.FilePath)) return -1; |
| | | |
| | | int count = 0; |
| | | vd.Meta.path = Path.Combine("" + GetSubPath(), vd.Meta.guid); |
| | | string targetFolder = Path.Combine(CommonProp.TargetPath, vd.Meta.path); |
| | | if (Directory.Exists(targetFolder)) return 0; |
| | | |
| | | string[] files = Directory.GetFiles(vd.FilePath, "*", SearchOption.AllDirectories); |
| | | foreach (string source in files) |
| | | { |
| | | try |
| | | { |
| | | string target = source.Replace(vd.FilePath, targetFolder); |
| | | count++; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.StackTrace); |
| | | } |
| | | } |
| | | |
| | | return count; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 复制多个文件 |
| | | /// </summary> |
| | | private static int copyMultiFile(ViewData vd, List<string> exts) |
| | | { |
| | | int count = copySingleFile(vd); |
| | | if (count < 0) return -1; |
| | | |
| | | string target = Path.Combine(CommonProp.TargetPath, vd.Meta.path); |
| | | foreach (string ext in exts) |
| | | { |
| | | try |
| | | { |
| | | File.Copy(vd.FilePath.Replace(vd.Ext, ext), target.Replace(vd.Ext, ext), true); |
| | | count++; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.StackTrace); |
| | | } |
| | | } |
| | | |
| | | return count; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 复制单文件 |
| | | /// </summary> |
| | | private static int copySingleFile(ViewData vd) |
| | | { |
| | | if (!File.Exists(vd.FilePath)) return -1; |
| | | |
| | | vd.Meta.path = Path.Combine("" + GetSubPath(), vd.Meta.guid + vd.Ext); |
| | | string target = Path.Combine(CommonProp.TargetPath, vd.Meta.path); |
| | | if (File.Exists(target)) return 0; |
| | | |
| | | try |
| | | { |
| | | File.Copy(vd.FilePath, target, true); |
| | | |
| | | return 1; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.StackTrace); |
| | | return -1; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取子目录 |
| | | /// </summary> |
| | | private static int GetSubPath() |
| | | { |
| | | int start = 0; |
| | | int.TryParse(Tools.GetSetting("StartPath"), out start); |
| | | |
| | | while (true) |
| | | { |
| | | string path = Path.Combine(target, start.ToString()); |
| | | string path = Path.Combine(CommonProp.TargetPath, start.ToString()); |
| | | if (!Directory.Exists(path)) |
| | | { |
| | | Directory.CreateDirectory(path); |
| | |
| | | } |
| | | |
| | | string[] files = Directory.GetFiles(path); |
| | | if (files.Length < 2001) |
| | | if (files.Length <= StaticData.MAX_FILES) |
| | | { |
| | | return start; |
| | | } |
| | |
| | | start++; |
| | | } |
| | | } |
| | | |
| | | private static SysMeta GetMetaFile(ViewData vd, int subPath, string guid) |
| | | { |
| | | SysMeta mf = new SysMeta(); |
| | | mf.eventid = Guid.NewGuid().ToString(); |
| | | mf.metaid = 0; |
| | | mf.dirid = vd.Meta.dirid; |
| | | mf.depid = vd.Meta.depid; |
| | | mf.verid = vd.Meta.verid; |
| | | mf.name = vd.Meta.name; |
| | | mf.type = vd.Meta.type; |
| | | mf.guid = guid; |
| | | mf.path = subPath + "\\" + mf.name; |
| | | mf.sizes = vd.Meta.sizes; |
| | | mf.tab = null; |
| | | mf.rows = 0; |
| | | mf.create_user = vd.Meta.create_user; |
| | | |
| | | return mf; |
| | | } |
| | | |
| | | private static void CopyFile(string source, string target) |
| | | { |
| | | if (File.Exists(source)) |
| | | { |
| | | File.Copy(source, target, true); |
| | | } |
| | | } |
| | | #endregion |
| | | } |
| | | } |