public void Open() { Connection c = Connection.Create(@"..\..\..\testdata\mike11\novomr6.xns11"); CrossSectionDataFactory cd = new CrossSectionDataFactory(); var xsecs = cd.Open(@"..\..\..\testdata\mike11\novomr6.xns11", null); CrossSectionFactory cf = new CrossSectionFactory(); var v= new CrossSectionPointList(); v.Add(new CrossSectionPoint(0,5)); v.Add(new CrossSectionPoint(1,2)); v.Add(new CrossSectionPoint(2,2)); v.Add(new CrossSectionPoint(3,5)); cf.BuildOpen("tempo"); cf.SetRawPoints(v); var cs = cf.GetCrossSection(); CrossSectionData d = new CrossSectionData(); d.Add(cs); d.Connection = Connection.Create(@"c:\temp\new.xns11"); CrossSectionFactory cdd = new CrossSectionFactory(); }
/// <summary> /// Example of how to loop over all cross sections /// </summary> /// <param name="csData">Cross section data object</param> public static void LoopAllCrossSections(CrossSectionData csData) { // Loop over all cross sections foreach (ICrossSection cs in csData) { // Check if cross section has raw data: XSBaseRaw xsBaseRaw = cs.BaseCrossSection as XSBaseRaw; if (xsBaseRaw == null) { continue; // It dit not have raw data } // Add additional 0.4 to all z values in the raw points foreach (ICrossSectionPoint xsPoint in xsBaseRaw.Points) { xsPoint.Z += 0.4; } // Update all markers to defaul xsBaseRaw.UpdateMarkersToDefaults(true, true, true); // Calculates the processed levels, storage areas, radii, etc, ie, fill in all // ProcessedXXX properties. xsBaseRaw.CalculateProcessedData(); } }
/// <summary> /// Modify cross section /// </summary> /// <param name="csData">Cross section data from the CrossSection example data</param> public static void ModifyCrossSectionProcessed(CrossSectionData csData) { ICrossSection crossSection = csData.FindCrossSection(new Location("river B", 1000), "1"); if (crossSection == null) { throw new Exception("Cross section not found at location"); } // This is a cross section with raw data XSBase xsBase = crossSection.BaseCrossSection; if (xsBase == null) { throw new Exception("Not a cross section with processed data"); } for (int i = 0; i < xsBase.NumberOfProcessedLevels; i++) { xsBase.ProcessedResistanceFactors[i] *= 1.1; } // Do not call CalculateProcessedData in this case, because that will reset the // processed data. Instead set the protected flag, such that data is not updated // automatically xsBase.ProcessedDataProtected = true; }
public void Open() { Connection c = Connection.Create(@"..\..\..\testdata\mike11\novomr6.xns11"); CrossSectionDataFactory cd = new CrossSectionDataFactory(); var xsecs = cd.Open(@"..\..\..\testdata\mike11\novomr6.xns11", null); CrossSectionFactory cf = new CrossSectionFactory(); var v = new CrossSectionPointList(); v.Add(new CrossSectionPoint(0, 5)); v.Add(new CrossSectionPoint(1, 2)); v.Add(new CrossSectionPoint(2, 2)); v.Add(new CrossSectionPoint(3, 5)); cf.BuildOpen("tempo"); cf.SetRawPoints(v); var cs = cf.GetCrossSection(); CrossSectionData d = new CrossSectionData(); d.Add(cs); d.Connection = Connection.Create(@"c:\temp\new.xns11"); CrossSectionFactory cdd = new CrossSectionFactory(); }
/// <summary> /// Load an xns11 file, add a cross section and save the file again, /// adding a "-csModPro" to the end of the filename. /// </summary> /// <param name="xns11Filepath">Filepath to xns11 file from the CrossSection example data</param> public static void ModifyCrossSectionProcessedAndSave(string xns11Filepath) { // Load cross section data Diagnostics diagnostics = new Diagnostics("Errors"); CrossSectionDataFactory csDataFactory = new CrossSectionDataFactory(); CrossSectionData csData = csDataFactory.Open(Connection.Create(xns11Filepath), diagnostics); // Modify cross section ModifyCrossSectionProcessed(csData); // Save the cross section as a new file name csData.Connection.FilePath.FileNameWithoutExtension += "-csModPro"; CrossSectionDataFactory.Save(csData); }
/// <summary> /// Example of how to load an xns11 file, add a cross section and save the /// file again, adding a "-csAdd" to the end of the new filename. /// </summary> /// <param name="xns11Filepath">Filepath to xns11 file</param> public static void AddCrossSectionAndSave(string xns11Filepath) { // Load cross section data Diagnostics diagnostics = new Diagnostics("Errors"); CrossSectionDataFactory csDataFactory = new CrossSectionDataFactory(); CrossSectionData csData = csDataFactory.Open(Connection.Create(xns11Filepath), diagnostics); // Add cross section CreateAndAddOpenCrossSection(csData); CreateAndAddCircularCrossSection(csData); // Save the cross section as a new file name csData.Connection.FilePath.FileNameWithoutExtension += "-csAdd"; CrossSectionDataFactory.Save(csData); }
public void CrossSectionExamplesLoopAllCrossSections() { string xns11Filepath = Path.Combine(ExampleBase.ExampleRoot, @"CrossSection\CrossSections.xns11"); // Load cross section data Diagnostics diagnostics = new Diagnostics("Errors"); CrossSectionDataFactory csDataFactory = new CrossSectionDataFactory(); CrossSectionData csData = csDataFactory.Open(Connection.Create(xns11Filepath), diagnostics); // Loop over all cross sections CrossSectionExamples.LoopAllCrossSections(csData); // Save the cross section as a new file name csData.Connection.FilePath.FileNameWithoutExtension += "-csLoop"; CrossSectionDataFactory.Save(csData); }
/// <summary> /// Create a new circular cross section and add it to the CrossSectionData object /// </summary> /// <param name="csData">Cross section data object</param> public static void CreateAndAddCircularCrossSection(CrossSectionData csData) { // Creates a class representing a cross section with raw data attached. CrossSectionFactory builder = new CrossSectionFactory(); builder.BuildCircular(2); // Defines the location of the current cross section. The Z-coordinate // is the bottom level of the cross section (unless defined by the // raw data (the open cross sections)). builder.SetLocation(new ZLocation("pipe A", 10) { Z = 0 }); // Set flow resistance FlowResistance flowResistance = new FlowResistance(); flowResistance.ResistanceDistribution = ResistanceDistribution.ExponentVarying; flowResistance.ResistanceValue = 75; flowResistance.ResistanceTopValue = 85; flowResistance.ExpDepExponent = 1.5; flowResistance.Formulation = ResistanceFormulation.Manning_M; builder.SetFlowResistance(flowResistance); // Get cross section from builder CrossSectionLocated cs = builder.GetCrossSection(); cs.TopoID = "1"; // Calculates the processed levels, storage areas, radii, etc, ie, fill in all // ProcessedXXX properties. cs.BaseCrossSection.CalculateProcessedData(); // Validates the data. The constraints are that the levels and the areas after sorting // must be monotonically increasing. IDiagnostics diagnostics = cs.Validate(); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception(String.Format("Number of errors: {0}", diagnostics.Errors.Count)); } // Add the cross section csData.Add(cs); }
/// <summary> /// Example of how to navigate and find cross sections. /// <para> /// There are two ways to get cross sections: Find or Get /// </para> /// <para> /// Find will search the cross section data object for a cross /// section at the specified location. If no cross section is /// found, null is returned. /// </para> /// <para> /// Get will search the cross section data object for a cross /// section at the specified location. If no cross section is /// found, it will interpolate/extrapolate if possible, and only /// if that interpolation/extrapolation is not possible, null /// is returned. The get methods are used by the engine. /// </para> /// <para> /// Users that want to update or in other ways process the cross /// sections should always use the Find methods. /// </para> /// </summary> /// <param name="xns11Filepath">Filepath to xns11 file</param> public static void Navigation(string xns11Filepath) { // Load cross section data Diagnostics diagnostics = new Diagnostics("Errors"); CrossSectionDataFactory csDataFactory = new CrossSectionDataFactory(); CrossSectionData csData = csDataFactory.Open(Connection.Create(xns11Filepath), diagnostics); ICrossSection crossSection; // Find a cross section on a given location crossSection = csData.FindCrossSection(new Location("LINDSKOV", 1370), "TOPO-95"); if (crossSection == null) { throw new Exception("Could not find cross section at location"); } // Find a cross section closest to a given location crossSection = csData.FindClosestCrossSection(new Location("LINDSKOV", 1250), "TOPO-95"); if (crossSection == null || crossSection.Location.Chainage != 1172) { throw new Exception("Expected cross section at chainage 1172"); } // Find all cross sections inside a given location span IList <ICrossSection> css = csData.FindCrossSectionsForLocationSpan(new LocationSpan("LINDSKOV", 1000, 2000), "TOPO-95", true); if (css.Count != 6) { throw new Exception("Expected 6 cross sections"); } // Find cross sections that are neighbours to a given locatin. ICrossSection csAfter; ICrossSection csBefore; csData.FindNeighborCrossSections(new Location("LINDSKOV", 1250), "TOPO-95", true, out csBefore, out csAfter); if (csBefore == null || csBefore.Location.Chainage != 1172) { throw new Exception("Expected cross section at chainage 1172"); } if (csAfter == null || csAfter.Location.Chainage != 1370) { throw new Exception("Expected cross section at chainage 1370"); } }
/// <summary> /// Export from xns11 to text file /// </summary> public static void Export(string xns11FileName, string txtFileName) { // Open cross section file CrossSectionDataFactory crossSectionDataFactory = new CrossSectionDataFactory(); CrossSectionData csData = crossSectionDataFactory.Open(Connection.Create(xns11FileName), null); // Open text file to write to StreamWriter writer = new StreamWriter(txtFileName); // Loop over all cross sections foreach (ICrossSection crossSection in csData) { // Export only those with raw data. // Any other filtering can be added here. if (!(crossSection.BaseCrossSection is XSBaseRaw)) { continue; } XSBaseRaw xsBase = crossSection.BaseCrossSection as XSBaseRaw; // Write cross section info-line. // First value in inf- line is a "what-to-do when importing" flag. // 0: Do nothing // 1: Update existing/create new if not existing writer.WriteLine("{0},{1},{2},{3},{4},{5}", "1", crossSection.Location.ID.EnQuote(), crossSection.Location.Chainage.ToString("R", CultureInfo.InvariantCulture), crossSection.TopoID.EnQuote(), crossSection.Location.Z.ToString("R", CultureInfo.InvariantCulture), xsBase.Points.Count); // Write coordinates of all raw points for (int i = 0; i < xsBase.Points.Count; i++) { ICrossSectionPoint p = xsBase.Points[i]; writer.WriteLine("{0},{1}", p.X.ToString("R", CultureInfo.InvariantCulture), p.Z.ToString("R", CultureInfo.InvariantCulture)); } } writer.Close(); }
/// <summary> /// Modify cross section /// </summary> /// <param name="csData">Cross section data from the CrossSection example data</param> public static void ModifyCrossSectionRaw(CrossSectionData csData) { ICrossSection crossSection = csData.FindCrossSection(new Location("river B", 1000), "1"); if (crossSection == null) { throw new Exception("Cross section not found at location"); } // This is a cross section with raw data XSBaseRaw xsBaseRaw = crossSection.BaseCrossSection as XSBaseRaw; if (xsBaseRaw == null) { throw new Exception("Not a cross section with raw data"); } // This cross section has 3 points. // Update the first two points xsBaseRaw.Points[1].X = 0.5; xsBaseRaw.Points[1].Z = 0.5; xsBaseRaw.Points[2] = new CrossSectionPoint(0.5, 0.0); // Add two additional points xsBaseRaw.Points.Add(new CrossSectionPoint(1.0, 0.2)); xsBaseRaw.Points.Add(new CrossSectionPoint(1.0, 1.0)); // Update marker to default xsBaseRaw.UpdateMarkersToDefaults(true, true, true); // Calculates the processed levels, storage areas, radii, etc, ie, fill in all // ProcessedXXX properties. xsBaseRaw.CalculateProcessedData(); // Validates the data. The constraints are that the levels and the areas after sorting // must be monotonically increasing. IDiagnostics diagnostics = xsBaseRaw.Validate(); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception(String.Format("Number of errors: {0}", diagnostics.Errors.Count)); } }
/// <summary> /// Reads the cross sections from an .xns-file /// </summary> /// <param name="xnsFile"></param> public void ReadCrossSections(string xnsFile) { CrossSectionDataFactory cd = new CrossSectionDataFactory(); xsecs = cd.Open(xnsFile, null); //Now loop the cross sections foreach (var cs in xsecs) { XSOpen Opencs = cs.BaseCrossSection as XSOpen; if (Opencs != null) { //Create a HydroNumerics.MikeSheTools.Mike11.CrossSection from the M11-CrossSection CrossSection MyCs = new CrossSection(cs); CombineNetworkAndCrossSections(MyCs); } } }
/// <summary> /// Adds a new cross section to the CrossSectionData object /// </summary> /// <param name="csData">Cross section data object</param> public static void AddCrossSection(CrossSectionData csData) { // Note: Raw data must be ordered from left to right in order for hydraulic radius to be processed correctly // Creates a class representing a cross section with raw data attached. CrossSectionFactory builder = new CrossSectionFactory(); builder.BuildOpen(""); // Defines the location of the current cross section. The Z-coordinate // is the bottom level of the cross section (unless defined by the // raw data (the open cross sections)). builder.SetLocation(new ZLocation("river B", 500) { Z = 0 }); // Create a number of points CrossSectionPointList points = new CrossSectionPointList(); points.Add(new CrossSectionPoint(-1.0, 2.0)); points.Add(new CrossSectionPoint(0.0, 1.0)); points.Add(new CrossSectionPoint(0.0, 0.0)); points.Add(new CrossSectionPoint(1.0, 0.0)); points.Add(new CrossSectionPoint(1.0, 1.0)); points.Add(new CrossSectionPoint(2.0, 2.0)); points.Add(new CrossSectionPoint(3.0, 2.0)); // dummy point, outside right levee bank marker // Sets the markers at left/right side and lowest point. builder.SetRawPoints(points); builder.SetLeftLeveeBank(points[0]); builder.SetLowestPoint(points[3]); builder.SetRightLeveeBank(points[5]); // Set flow resistance FlowResistance flowResistance = new FlowResistance(); flowResistance.ResistanceDistribution = ResistanceDistribution.Uniform; flowResistance.ResistanceValue = 1; flowResistance.Formulation = ResistanceFormulation.Relative; builder.SetFlowResistance(flowResistance); builder.SetRadiusType(RadiusType.ResistanceRadius); // Get cross section from builder CrossSectionLocated cs = builder.GetCrossSection(); cs.TopoID = "1"; // Calculates the processed levels, storage areas, radii, etc, ie, fill in all // ProcessedXXX properties. cs.BaseCrossSection.CalculateProcessedData(); // Validates the data. The constraints are that the levels and the areas after sorting // must be monotonically increasing. IDiagnostics diagnostics = cs.Validate(); if (diagnostics.ErrorCountRecursive > 0) { throw new Exception(String.Format("Number of errors: {0}", diagnostics.Errors.Count)); } // Add the cross section csData.Add(cs); }
/// <summary> /// Import from text file to xns11 file /// </summary> /// <param name="txtFileName">Path and name of text file to import</param> /// <param name="xns11FileName">Path and name of xns11 file to create or update</param> /// <param name="xns11NewFileName">Path and name of xns11 file to write to</param> public static void Import(string txtFileName, string xns11FileName, string xns11NewFileName = null) { StreamReader reader = new StreamReader(txtFileName); CrossSectionDataFactory crossSectionDataFactory = new CrossSectionDataFactory(); CrossSectionData csData; if (File.Exists(xns11FileName)) { csData = crossSectionDataFactory.Open(Connection.Create(xns11FileName), null); } else { csData = new CrossSectionData(); } if (string.IsNullOrEmpty(xns11NewFileName)) { xns11NewFileName = xns11FileName; } string line; // Read cross section info-line while ((line = reader.ReadLine()) != null) { // Split cross section info-line string[] split = line.SplitQuoted(',', '"'); // extract info from info-line Location location = new Location(split[1], double.Parse(split[2], CultureInfo.InvariantCulture)); string topoId = split[3]; double datum = double.Parse(split[4], CultureInfo.InvariantCulture); int numRawPoints = int.Parse(split[5]); // Check if this cross section is to be processed if (split[0] == "1") { ICrossSection cs = csData.FindCrossSection(location, topoId); CrossSectionPointList points = new CrossSectionPointList(); // Read raw points for (int i = 0; i < numRawPoints; i++) { line = reader.ReadLine(); if (line == null) // end-of-file { throw new EndOfStreamException("File ended prematurely"); } string[] coords = line.Split(','); double x = double.Parse(coords[0], CultureInfo.InvariantCulture); double z = double.Parse(coords[1], CultureInfo.InvariantCulture); points.Add(new CrossSectionPoint(x, z)); } // Check if cross section already exists if (cs != null) { // Check if it is a cross section with raw points XSBaseRaw xsBaseRaw = cs.BaseCrossSection as XSBaseRaw; if (xsBaseRaw == null) { throw new Exception("Cannot modify raw points of a cross section without raw points: " + location.ToString() + ", " + topoId); } // replace datum (in case datum changes) cs.Location.Z = datum; // Replace points xsBaseRaw.Points = points; // Set default markers xsBaseRaw.UpdateMarkersToDefaults(true, true, true); // Recalculate processed data xsBaseRaw.ProcessingLevelsSpecs.NoOfLevels = 50; xsBaseRaw.CalculateProcessedData(); } else { // Create a new cross section CrossSectionFactory builder = new CrossSectionFactory(); builder.BuildOpen(""); // Defines the location of the current cross section. The Z-coordinate // for an open cross section with raw data is a Z-offset, and usually zero. builder.SetLocation(new ZLocation(location.ID, location.Chainage) { Z = datum }); // Set raw points and default markers builder.SetRawPoints(points); builder.SetDefaultMarkers(); // Define resistance properties as relative FlowResistance flowResistance = new FlowResistance(); flowResistance.Formulation = ResistanceFormulation.Relative; flowResistance.ResistanceDistribution = ResistanceDistribution.Uniform; flowResistance.ResistanceValue = 1; builder.SetFlowResistance(flowResistance); builder.SetRadiusType(RadiusType.ResistanceRadius); // Get the cross section CrossSectionLocated csLocated = builder.GetCrossSection(); // Set topo-id csLocated.TopoID = topoId; // now, calculate processed data csLocated.BaseCrossSection.ProcessingLevelsSpecs.Option = ProcessingOption.AutomaticLevels; csLocated.BaseCrossSection.ProcessingLevelsSpecs.NoOfLevels = 0; csLocated.BaseCrossSection.CalculateProcessedData(); // Store cross section in database csData.Add(csLocated); } } else // this cross section should not be processed { // Skip line containing raw points for (int i = 0; i < numRawPoints; i++) { line = reader.ReadLine(); if (line == null) // end-of-file { throw new EndOfStreamException("File ended prematurely"); } } } } csData.Connection = Connection.Create(xns11NewFileName); CrossSectionDataFactory.Save(csData); }