private static void ExportFeatures(List <IFeature> pFeatures, string pFileName, bool pIsMainFile) { if (pFeatures.Count == 0) { CDebug.Warning("You are trying to export 0 features"); return; } string shapeFileFolder = pIsMainFile ? CProjectData.outputFolder : CProjectData.outputTileSubfolder; shapeFileFolder += "/shp_" + pFileName; Directory.CreateDirectory(shapeFileFolder); //create subfolder // Construct the shapefile name. Don't add the .shp extension or the ShapefileDataWriter will // produce an unwanted shapefile string shapeFileName = Path.Combine(shapeFileFolder, pFileName); string shapeFilePrjName = Path.Combine(shapeFileFolder, $"{pFileName}.prj"); // Create the shapefile var outGeomFactory = GeometryFactory.Default; var writer = new ShapefileDataWriter(shapeFileName, outGeomFactory); var outDbaseHeader = ShapefileDataWriter.GetHeader(pFeatures[0], pFeatures.Count); writer.Header = outDbaseHeader; writer.Write(pFeatures); //Create the projection file using (var streamWriter = new StreamWriter(shapeFilePrjName)) { streamWriter.Write(GeographicCoordinateSystem.WGS84.WKT); } }
/// <summary> /// Merges all exported files into one /// </summary> public static void ExportMain() { return; List <string[]> filesLines = new List <string[]>(); foreach (FileInfo fi in exportedFiles) { filesLines.Add(File.ReadAllLines(fi.FullName)); } if (filesLines.Count == 0) { CDebug.Warning($"CDartTxt: no main output created"); return; } using (StreamWriter writer = File.CreateText($"{CProjectData.outputFolder}\\dart_main.txt")) { writer.WriteLine(HEADER_LINE); foreach (string[] fileLines in filesLines) { int lineNum = 1; //skip header while (lineNum < fileLines.Length) { writer.WriteLine(fileLines[lineNum]); lineNum++; } } } }
public static void ExportMain() { if (!export) { return; } if (exportedTiles.Count == 0) { CDebug.Warning($"CLasExporter: no mainoutput created"); return; } string mainTxtFilePath = mainFilePath + ".txt"; foreach (string filePath in exportedTiles) { //copy content of all exported tile files to one StringBuilder fileContent = new StringBuilder(File.ReadAllText(filePath)); WriteToFile(fileContent, mainTxtFilePath); } //convert to las string cmd = $"{txt2lasCmd} {mainTxtFilePath}"; CCmdController.RunLasToolsCmd(cmd, mainFilePath + ".las"); //delete all txt files File.Delete(mainTxtFilePath); foreach (string filePath in exportedTiles) { File.Delete(filePath); } }
/// <summary> /// Calculates approximate index on branch where the give point should be placed with given tolerance /// </summary> private int GetAproxIndexOfPoint(Vector3 pPoint, float pMaxDiff) { int fromIndex = 0; int toIndex = TreePoints.Count; int selectedIndex = (fromIndex + toIndex) / 2; CTreePoint selectedTreePoint = TreePoints[selectedIndex]; int counter = 0; while (Math.Abs(selectedTreePoint.Z - pPoint.Z) > pMaxDiff && toIndex - fromIndex > 1) { if (selectedTreePoint.Z > pPoint.Z) { fromIndex += (toIndex - fromIndex) / 2; } else { toIndex -= (toIndex - fromIndex) / 2; } selectedIndex = (fromIndex + toIndex) / 2; selectedTreePoint = TreePoints[selectedIndex]; counter++; } if (counter > 20) { CDebug.Warning("GetAproxIndexOfPoint " + pPoint + " = " + counter); } return(selectedIndex); }
public static void ExportTile() { return; if (CTreeManager.Trees.Count == 0) { CDebug.Warning($"CDartTxt: no output created on tile {CProgramStarter.currentTileIndex}"); return; } DateTime start = DateTime.Now; DateTime lastDebug = DateTime.Now; StringBuilder output = new StringBuilder(HEADER_LINE + newLine); for (int i = 0; i < CTreeManager.Trees.Count; i++) { CDebug.Progress(i, CTreeManager.Trees.Count, DEBUG_FREQUENCY, ref lastDebug, start, "Export dart (trees)"); CTree tree = CTreeManager.Trees[i]; string line = GetLine(tree); if (line != null) { output.Append(line + newLine); } } //CDebug.WriteLine(output); WriteToFile(output.ToString()); }
public static void ExportTile() { if (!export) { return; } DateTime exportStart = DateTime.Now; StringBuilder output = new StringBuilder(); string res; DateTime start = DateTime.Now; DateTime lastDebug = DateTime.Now; AddPointsTo(ref output, EClass.Unassigned, ref start); AddPointsTo(ref output, EClass.Ground, ref start); AddPointsTo(ref output, EClass.Building, ref start); if (CTreeManager.GetDetectMethod() == EDetectionMethod.Balls) { AddPointsTo(ref output, EClass.BallsSurface, ref start); AddPointsTo(ref output, EClass.Balls, ref start); AddPointsTo(ref output, EClass.BallsMainPoints, ref start); AddPointsTo(ref output, EClass.BallsCenters, ref start); } //tree points AddTreePointsTo(ref output, true, ref start); //invalid tree points AddTreePointsTo(ref output, false, ref start); if (output.Length == 0) { CDebug.Warning($"CLasExporter: no output created on tile {CProgramStarter.currentTileIndex}"); return; } //1. Create the text file string txtOutputPath = tileFilePath + ".txt"; WriteToFile(output, txtOutputPath); //2. save output path for the main file export exportedTiles.Add(txtOutputPath); //3. Convert the txt to las using LasTools //format: x, y, z, class, user comment (id), R, G, B string cmd = $"{txt2lasCmd} {txtOutputPath}"; CCmdController.RunLasToolsCmd(cmd, tileFilePath + ".las"); CAnalytics.lasExportDuration = CAnalytics.GetDuration(exportStart); }
private static string[] GetReftreeLines(string refTreeXyzPath) { if (!File.Exists(refTreeXyzPath)) { //not errror. all files doesnt have to be specified CDebug.Warning("Reftree " + refTreeXyzPath + " XYZ does not exist."); return(new string[0]); } string[] lines = File.ReadAllLines(refTreeXyzPath); CDebug.WriteLine("load: " + refTreeXyzPath); return(lines); }
/// <summary> /// Reads parsed lines and loads class and point list. /// Result is sorted in descending order. /// </summary> public static List <Tuple <EClass, Vector3> > ParseLines(string[] lines, bool pUseHeader) { CDebug.Step(EProgramStep.ParseLines); //store coordinates to corresponding data strucures based on their class const int DEFAULT_START_LINE = 19; int startLine = pUseHeader && CProjectData.mainHeader != null ? DEFAULT_START_LINE : 0; CDebug.Warning("loading " + lines.Length + " lines!"); int linesToRead = lines.Length; //todo: check that "classic" processed files work correctly without using default class //bool classesCorect = true; List <Tuple <EClass, Vector3> > parsedLines = new List <Tuple <EClass, Vector3> >(); if (useDebugData) { parsedLines = CDebugData.GetStandartTree(); //CDebugData.DefineArray(true, 0); } else { for (int i = startLine; i < linesToRead; i++) { // <class, coordinate> Tuple <EClass, Vector3> c = CLazTxtParser.ParseLine(lines[i], pUseHeader); if (c == null) { continue; } //some files have different class counting. we are interested only in classes in EClass //if(c.Item1 == EClass.Other) //{ // c = new Tuple<EClass, Vector3>(EClass.Vege, c.Item2); // classesCorect = false; //} parsedLines.Add(c); } } //if(!classesCorect) //{ // CDebug.WriteLine("classes not correct. using default class"); //} CDebug.Count("parsedLines", parsedLines.Count); return(parsedLines); }
private static void LoadTrees(List <string> pFileNames) { DateTime loadTreesStartTime = DateTime.Now; DateTime lastDebugTime = DateTime.Now; CDebug.WriteLine("Load reftrees: "); foreach (string fileName in pFileNames) { CDebug.WriteLine(" - " + fileName); } for (int i = 0; i < pFileNames.Count; i++) { if (CProjectData.backgroundWorker.CancellationPending) { return; } string fileName = pFileNames[i]; CDebug.Progress(i, pFileNames.Count, 1, ref lastDebugTime, loadTreesStartTime, "load reftree"); CRefTree deserializedRefTree = CRefTree.Deserialize(fileName); bool isDeserializedTreeValid = deserializedRefTree != null && deserializedRefTree.IsCurrentVersion(); CRefTree refTree = isDeserializedTreeValid ? deserializedRefTree : new CRefTree(fileName, i, TREE_POINT_EXTENT, true); refTree.RefTreeTypeName = fileName; //GetTypeName(fileName); if (!refTree.isValid) { //this reftree is not valid. case for reftrees in 'ignore' folder CDebug.Warning($"Skipping reftree {fileName}"); continue; } //material set durring assigning to tree //refTree.Obj.UseMtl = CMaterialManager.GetRefTreeMaterial(counter); Trees.Add(refTree); CDebug.WriteLine($"Loaded tree: {fileName}. height = {refTree.GetTreeHeight()}"); } CAnalytics.loadReftreesDuration = CAnalytics.GetDuration(loadTreesStartTime); CDebug.Duration("Load reftrees", loadTreesStartTime); CAnalytics.loadedReftrees = Trees.Count; DebugReftrees(); }
public static CRefTree Deserialize(string pFileName) { string filePath = GetRefTreeFilePath(pFileName, pFileName + ".reftree"); CDebug.WriteLine("\nDeserialize. filePath = " + filePath); if (!File.Exists(filePath)) { //not error. reftree files doesnt have to be generated yet CDebug.Warning(".reftree file does not exist."); return(null); } string[] serialisedLines = File.ReadAllLines(filePath); CRefTree refTree = new CRefTree(pFileName, serialisedLines); return(refTree); }
private void AddPoint(Tuple <EClass, Vector3> pParsedLine) { Vector3 point = pParsedLine.Item2; switch (pParsedLine.Item1) { case EClass.Undefined: case EClass.Unassigned: unassigned.Add(point); break; case EClass.Ground: ground.Add(point); break; case EClass.Vege: vege.Add(point); break; case EClass.Building: building.Add(point); break; default: CDebug.Warning($"point class {pParsedLine.Item1} not recognized"); return; } float height = point.Z; if (height < lowestHeight) { lowestHeight = height; } if (height > highestHeight) { highestHeight = height; } }
protected override void OnProcess() { string filePath = GetRefTreeFilePath(fileName, fileName + ".reftree"); CDebug.WriteLine("\nfilePath = " + filePath); if (File.Exists(filePath)) { if (!IsCurrentVersion()) { File.Delete(filePath); CDebug.Warning($"deleting old .reftree file ({filePath})"); } else { CDebug.Error(".reftree file already exists ({filePath})"); } //return; } DateTime processStartTime = DateTime.Now; CDebug.WriteLine("Serialization"); List <string> serializedTree = Serialize(); version = CURRENT_REFTREE_VERSION; using (StreamWriter file = new StreamWriter(filePath, false)) { foreach (string line in serializedTree) { file.WriteLine(line); } } CDebug.Duration("Serialized", processStartTime); CDebug.WriteLine("saved to: " + filePath); }
public CRefTree(string pFileName, string[] pSerializedLines) { DeserialiseMode currentMode = DeserialiseMode.None; fileName = pFileName; isValid = true; branches = new List <CBranch>(); List <CTreePoint> _treepointsOnBranch = new List <CTreePoint>(); //if first line is not version then it is the old version and //the file needs to be regenerated if (pSerializedLines.Length == 0 || pSerializedLines[0] != KEY_VERSION) { return; } foreach (string line in pSerializedLines) { switch (line) { case KEY_VERSION: currentMode = DeserialiseMode.Version; continue; case KEY_TREE_INDEX: currentMode = DeserialiseMode.TreeIndex; continue; case KEY_TREE_POINT_EXTENT: currentMode = DeserialiseMode.TreePointExtent; continue; case KEY_PEAK: currentMode = DeserialiseMode.Peak; continue; case KEY_BRANCHES: currentMode = DeserialiseMode.Branches; continue; case KEY_STEM: currentMode = DeserialiseMode.Stem; branches.Last().SetTreePoints(_treepointsOnBranch); _treepointsOnBranch = new List <CTreePoint>(); continue; case KEY_BOUNDING_BOX: currentMode = DeserialiseMode.BoundingBox; continue; } switch (currentMode) { case DeserialiseMode.Version: version = line; if (!IsCurrentVersion()) { CDebug.Warning($"reftree version {version} is not up to date {CURRENT_REFTREE_VERSION}. Generating new file."); return; } break; case DeserialiseMode.TreeIndex: treeIndex = int.Parse(line); break; case DeserialiseMode.TreePointExtent: treePointExtent = float.Parse(line); break; case DeserialiseMode.Peak: peak = CPeak.Deserialize(line, treePointExtent); stem = new CBranch(this, 0, 0); break; case DeserialiseMode.Branches: if (line.Contains(KEY_BRANCH)) { int branchIndex = branches.Count; if (branchIndex > 0) { branches.Last().SetTreePoints(_treepointsOnBranch); } branches.Add(new CBranch( this, branchIndex * BRANCH_ANGLE_STEP, branchIndex * BRANCH_ANGLE_STEP + BRANCH_ANGLE_STEP)); _treepointsOnBranch = new List <CTreePoint>(); } else { CTreePoint treePointOnBranch = CTreePoint.Deserialize(line, treePointExtent); _treepointsOnBranch.Add(treePointOnBranch); Points.Add(treePointOnBranch.Center); } break; case DeserialiseMode.Stem: _treepointsOnBranch.Add(CTreePoint.Deserialize(line, treePointExtent)); break; case DeserialiseMode.BoundingBox: string[] split = line.Split(null); minBB = new Vector3(float.Parse(split[0]), float.Parse(split[1]), float.Parse(split[2])); maxBB = new Vector3(float.Parse(split[3]), float.Parse(split[4]), float.Parse(split[5])); break; } } stem.SetTreePoints(_treepointsOnBranch); LoadObj(pFileName); }