using ExportMap.db; using ExportMap.Models; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace ExportMap.cs { public class LasUtils { /// /// 获取路径 /// public static string GetPath(int id) { return Path.Combine(SGUtils.LFData, "3d\\3dtiles\\las", id.ToString()); } /// /// 获取发布地址 /// public static string GetReleaseUrl(string path) { return "http://{host}/LFData/" + path.Replace("\\", "/"); } /// /// 生成 /// /// XYZ参数 /// 错误信息 /// 数据发布ID集合 public static List Generate(XYZArgs args, ref string err) { try { List list = XYZUtils.SelectMetas(args.ids, "and type in ('las', 'laz')"); if (null == list || list.Count == 0) return null; string tilerPath = Tools.GetSetting("tilerPath"); string uploadFolder = Tools.GetSetting("uploadFolder"); List ids = new List(); foreach (SysMeta meta in list) { string lasPath = Path.Combine(uploadFolder, meta.path); if (!File.Exists(lasPath)) continue; meta.ismeta = 0; // 0-倾斜摄影数据 string outPath = GetPath(meta.id); if (Directory.Exists(outPath)) Tools.DelPath(outPath); if (!Directory.Exists(outPath)) Directory.CreateDirectory(outPath); if ("laz" == meta.type) lasPath = toLas(lasPath, Path.Combine(outPath, meta.id + ".las")); string cmd = string.Format("{0}\\gocesiumtiler.exe -i \"{1}\" -o \"{2}\" -e {3} -z {4} -g -s", tilerPath, lasPath, outPath, args.srid, args.z); err = Tools.ExecCmd(cmd, false, false); string jsonFile = findTileset(meta, outPath); if ("laz" == meta.type && File.Exists(lasPath)) File.Delete(lasPath); if (File.Exists(jsonFile)) { string path = jsonFile.Replace(Tools.GetSetting("lfData") + "\\", ""); int pubid = InsertToDB(meta, args, path); if (pubid > 0) ids.Add(pubid); } } return ids; } catch (Exception ex) { LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); err = ex.Message; return null; } } /// /// laz 转换为 las /// private static string toLas(string lazPath, string outPath) { string lasPath = outPath.Replace("laz", "laz"); string tilerPath = Tools.GetSetting("tilerPath"); string cmd = string.Format("{0}\\laszip64.exe -i \"{1}\" -o \"{2}\"", tilerPath, lazPath, lasPath); Tools.ExecCmd(cmd, false, false); return lasPath; } /// /// 查找tileset.json /// private static string findTileset(SysMeta meta, string path) { PathRename(meta, path); DirectoryInfo di = new DirectoryInfo(path); FileInfo[] fis = di.GetFiles("tileset.json", SearchOption.AllDirectories); return null == fis || 0 == fis.Length ? null : fis[0].FullName; } /// /// 路径重命名 /// private static void PathRename(SysMeta meta, string path) { try { string subPath = Path.Combine(path, meta.guid); if (Directory.Exists(subPath)) { string newPath = Path.Combine(path, meta.id.ToString()); Directory.Move(subPath, newPath); } } catch { } } /// /// 插入数据库 /// private static int InsertToDB(SysMeta meta, XYZArgs args, string path) { if (PubDBHelper.IsPublish(meta.id)) return 1; SysPublish sys = Tools.NewPublish(meta, args, GetReleaseUrl(path), path); int pubid = PubDBHelper.InsertPublish(sys); if (pubid > 0) { sys.id = pubid; PubDBHelper.InsertLayer(sys, meta); PubDBHelper.InsertMetaPub(meta.id, pubid, args.userId); } return pubid; } /// /// 读取Las坐标系 /// public static List ReadLasCs(XYZArgs args, ref string err) { List list = XYZUtils.SelectMetas(args.ids, "and type in ('las', 'laz')"); if (null == list || list.Count == 0) return null; string uploadFolder = Tools.GetSetting("uploadFolder"); List ids = new List(); foreach (SysMeta meta in list) { string lasPath = Path.Combine(uploadFolder, meta.path); if (!File.Exists(lasPath)) continue; int epsg = 0; try { epsg = Tools.get_las_cs(lasPath.Replace("\\", "/")); } catch (Exception ex) { LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); epsg = -1; } ids.Add(epsg); } return ids; } } }