Skip to content

Ground_Engine: Consolidate method for a Borehole #3372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions Ground_Engine/Compute/ConsolidateStrata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2024, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using BH.Engine.Base;
using BH.oM.Base.Attributes;
using BH.oM.Ground;
using System.Transactions;
using System.Reflection;
using ICSharpCode.Decompiler.CSharp.Syntax;
using System.Globalization;
using System.Data;

namespace BH.Engine.Ground
{
public static partial class Compute
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/
[Description("A method that takes a Borehole, and consolidates the Strata sequentially by combining them based on a specific property.")]
[Input("borehole", "The Borehole to consolidate the strata for.")]
[Input("propertyCompare", "The property of the Strata to consolidate such as ObservedGeology, InterpretedGeology or Legend.")]
[Input("decimals", "The number of decimals to display the depth ranges.")]
[Output("b", "The consolidated Borehole.")]
public static Borehole ConsolidateStrata(Borehole borehole, string propertyCompare, int decimals)
{
if (borehole.IsValid())
return null;


Borehole consolidatedBorehole = borehole.ShallowClone();

List<Stratum> strata = consolidatedBorehole.Strata;
// Add first strata so the strings are formatted the same as the consolidated borehole
List<Stratum> consolidatedStrata = new List<Stratum>() { strata[0].RangeProperties(null, propertyCompare, true, decimals) };

for (int i = 1; i < strata.Count; i++)
{
Stratum consolidatedStratum = consolidatedStrata.Last();
Stratum stratum = strata[i];
// Check whether the strings are equal based on the propertyCompare
if (string.Equals(Base.Query.PropertyValue(stratum, propertyCompare), Base.Query.PropertyValue(strata[i - 1], propertyCompare)))
{
// Update ConsolidatedStratum to include next stratum
consolidatedStratum = RangeProperties(stratum, consolidatedStratum, propertyCompare, false, decimals);
consolidatedStrata[consolidatedStrata.Count - 1] = consolidatedStratum;
}
else
{
// Add new line
consolidatedStrata.Add(strata[i].RangeProperties(null, propertyCompare, true, decimals));
}
}

consolidatedBorehole.SetPropertyValue("Strata", consolidatedStrata);

return consolidatedBorehole;
}

/***************************************************/
/**** Private Methods ****/
/***************************************************/

private static Stratum RangeProperties(this Stratum stratum, Stratum consolidatedStratum, string propName, bool initial, int decimals)
{
Stratum updatedStratum = stratum.ShallowClone();
double top = stratum.Top;
double bottom = stratum.Bottom;

// Properties to skip over
List<string> skipProp = new List<string>() { "Id", "Top", "Bottom", "Properties", "BHoM_Guid", "Name", "Fragments", "Tags", "CustomData" };

Check warning on line 94 in Ground_Engine/Compute/ConsolidateStrata.cs

View check run for this annotation

BHoMBot-CI / code-compliance

Ground_Engine/Compute/ConsolidateStrata.cs#L94

The use of CustomData within the code is discouraged except in circumstances where volatile data is being used. - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsUsingCustomData
skipProp.Add(propName);

if (!initial)
{
updatedStratum.Top = consolidatedStratum.Top;
updatedStratum.Bottom = stratum.Bottom;
}

string topRounded = top.ToString($"N{decimals}", CultureInfo.InvariantCulture);
string bottomRounded = bottom.ToString($"N{decimals}", CultureInfo.InvariantCulture);

foreach (PropertyInfo property in typeof(Stratum).GetProperties())
{
if (!skipProp.Any(x => x.Equals(property.Name)))
{
string summary = "";
if (initial)
summary = $"{topRounded}m - {bottomRounded}m: {stratum.PropertyValue(property.Name)}";
else
summary = $"{consolidatedStratum.PropertyValue(property.Name)}\n" +
$"{topRounded}m - {bottomRounded}m: {stratum.PropertyValue(property.Name)}";

updatedStratum.SetPropertyValue(property.Name, summary);
}
}

if (!updatedStratum.Properties.IsNullOrEmpty())
{
List<IStratumProperty> properties = new List<IStratumProperty>();
for (int i = 0; i < stratum.Properties.Count; i++)
{
IStratumProperty property = updatedStratum.Properties[i];
if (property is StratumReference)
{
StratumReference consolidatedReference = null;
if (!initial)
consolidatedReference = consolidatedStratum.Properties.OfType<StratumReference>().First();
StratumReference updatedReference = (StratumReference)property.ShallowClone();
foreach (PropertyInfo prop in typeof(StratumReference).GetProperties())
{
if (!skipProp.Any(x => x.Equals(prop.Name)))
{
string summary = "";
if (initial)
summary = $"{topRounded}m - {bottomRounded}m: {updatedReference.PropertyValue(prop.Name)}";
else
summary = $"{consolidatedReference.PropertyValue(prop.Name)}\n" +
$"{topRounded}m - {bottomRounded}m: {updatedReference.PropertyValue(prop.Name)}";

updatedReference.SetPropertyValue(prop.Name, summary);
}
}
properties.Add(updatedReference);
}
}

updatedStratum.Properties = properties;
}

return updatedStratum;
}

/***************************************************/

}
}


2 changes: 1 addition & 1 deletion Ground_Engine/Create/Borehole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static Borehole Borehole(string id, Point top = null, Point bottom = null
{
if (string.IsNullOrWhiteSpace(id))
{
Compute.RecordError("The id is null or whitespace.");
Base.Compute.RecordError("The id is null or whitespace.");
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion Ground_Engine/Create/ContaminantSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static ContaminantSample ContaminantSample(string id, double top, string
{
if (string.IsNullOrWhiteSpace(chemical))
{
Compute.RecordError("The chemical name is null or whitespace.");
Base.Compute.RecordError("The chemical name is null or whitespace.");
return null;
}

Expand Down
Loading