Skip to content

Commit

Permalink
fixing warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jackschonherr authored and Brennan1994 committed Oct 2, 2024
1 parent 7f417e8 commit a861309
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 121 deletions.
5 changes: 3 additions & 2 deletions Consequences/Consequences/IStreamingProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using USACE.HEC.Geography;
using System.Diagnostics.CodeAnalysis;
using USACE.HEC.Geography;

namespace USACE.HEC.Consequences;
public interface IStreamingProcessor
{
public void Process<T>(Action<IConsequencesReceptor> consequenceReceptorProcess) where T : IConsequencesReceptor, new();
public void Process<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(Action<IConsequencesReceptor> consequenceReceptorProcess) where T : IConsequencesReceptor, new();
}
17 changes: 2 additions & 15 deletions Consequences/Consequences/NSIStreamingProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,7 @@ private static async Task ProcessCollection(BoundingBox boundingBox, Action<ICon
// access the properties of each structure
JsonElement propertiesElement = structure.GetProperty("properties");

// disable the warnings generated by Deserialize
#pragma warning disable IL2026
#pragma warning disable IL3050
Structure s = JsonSerializer.Deserialize<Structure>(propertiesElement.GetRawText());
#pragma warning restore IL2026
#pragma warning restore IL3050

Structure s = JsonSerializer.Deserialize(propertiesElement.GetRawText(), SourceGenerationContext.Default.Structure);
// apply the action to the deserialized structure
ConsequenceReceptorProcess(s);
}
Expand Down Expand Up @@ -81,14 +75,7 @@ private static async Task ProcessStream(BoundingBox boundingBox, Action<IConsequ
{
using JsonDocument structure = JsonDocument.Parse(line);
JsonElement propertiesElement = structure.RootElement.GetProperty("properties");

// disable the warnings generated by Deserialize
#pragma warning disable IL2026
#pragma warning disable IL3050
Structure s = JsonSerializer.Deserialize<Structure>(propertiesElement.GetRawText());
#pragma warning restore IL2026
#pragma warning restore IL3050

Structure s = JsonSerializer.Deserialize(propertiesElement.GetRawText(), SourceGenerationContext.Default.Structure);
// apply the action to the deserialized structure
ConsequenceReceptorProcess(s);
}
Expand Down
14 changes: 10 additions & 4 deletions Consequences/Consequences/Structure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,40 @@ public Result Compute(IHazard hazard)
{
resultItems.Add(new ResultItem {
ResultName = "Depth",
Result = hazard.Get<float>(HazardParameter.Depth)
ResultValue = hazard.Get<float>(HazardParameter.Depth)
});
}

if (hazard.Has(HazardParameter.Velocity))
{
resultItems.Add(new ResultItem {
ResultName = "Velocity",
Result = hazard.Get<float>(HazardParameter.Velocity)
ResultValue = hazard.Get<float>(HazardParameter.Velocity)
});
}

if (hazard.Has(HazardParameter.ArrivalTime))
{
resultItems.Add(new ResultItem {
ResultName = "ArrivalTime",
Result = hazard.Get<DateTime>(HazardParameter.ArrivalTime)
ResultValue = hazard.Get<DateTime>(HazardParameter.ArrivalTime)
});
}

if (hazard.Has(HazardParameter.ArrivalTime2ft))
{
resultItems.Add(new ResultItem {
ResultName = "ArrivalTime2ft",
Result = hazard.Get<DateTime>(HazardParameter.ArrivalTime2ft)
ResultValue = hazard.Get<DateTime>(HazardParameter.ArrivalTime2ft)
});
}

return new Result([.. resultItems]);
}
}

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Structure))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
}
2 changes: 1 addition & 1 deletion Consequences/Results/ConsoleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public string WriteString(Result res)
CheckIfSameHeaders(res);
foreach (string header in headers)
{
object val = res.Fetch(header).Result;
object val = res.Fetch(header).ResultValue;
output.Append(val.ToString());
if (header != headers.Last())
{
Expand Down
3 changes: 1 addition & 2 deletions Consequences/Results/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public ResultItem Fetch(string name)
for (int i = 0; i < ResultItems.Length; i++)
if (ResultItems[i].ResultName == name)
return ResultItems[i];
// return empty ResultItem if not found
return new ResultItem { ResultName = name };
throw new ArgumentException($"{name} not found in result");
}
}
2 changes: 1 addition & 1 deletion Consequences/Results/ResultItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
public struct ResultItem
{
public string ResultName;
public object Result;
public object ResultValue;
}
7 changes: 4 additions & 3 deletions Consequences/Results/Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json.Serialization;
using USACE.HEC.Consequences;

namespace USACE.HEC.Results;
public class Utilities
{
public static Result ConsequenceReceptorToResult<T>(IConsequencesReceptor cr) where T: IConsequencesReceptor
public static Result ConsequenceReceptorToResult<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(IConsequencesReceptor cr) where T: IConsequencesReceptor
{
List<ResultItem> resultItems = [];

Expand All @@ -15,7 +16,7 @@ public static Result ConsequenceReceptorToResult<T>(IConsequencesReceptor cr) wh
ResultItem item;
JsonPropertyNameAttribute jsonPropertyAttribute = property.GetCustomAttribute<JsonPropertyNameAttribute>();
item.ResultName = jsonPropertyAttribute != null ? jsonPropertyAttribute.Name : property.Name;
item.Result = property.GetValue(cr);
item.ResultValue = property.GetValue(cr);
resultItems.Add(item);
}

Expand Down
6 changes: 3 additions & 3 deletions ConsequencesTest/ConsoleWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
namespace ConsequencesTest;
public class ConsoleWrite
{
static ResultItem r1 = new ResultItem { ResultName = "Depth", Result = 1.03f };
static ResultItem r2 = new ResultItem { ResultName = "Velocity", Result = 2.02f };
static ResultItem r3 = new ResultItem { ResultName = "ArrivalTime2ft", Result = new DateTime() };
static ResultItem r1 = new ResultItem { ResultName = "Depth", ResultValue = 1.03f };
static ResultItem r2 = new ResultItem { ResultName = "Velocity", ResultValue = 2.02f };
static ResultItem r3 = new ResultItem { ResultName = "ArrivalTime2ft", ResultValue = new DateTime() };
static ResultItem[] resultItems = { r1, r2, r3 };
Result res = new Result(resultItems);
[Fact]
Expand Down
28 changes: 14 additions & 14 deletions ConsequencesTest/StructureTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public void Compute_SimpleDepth()
ResultItem item1 = res.Fetch("Depth");

Assert.Equal("Depth", item1.ResultName);
Assert.Equal(typeof(float), item1.Result.GetType());
Assert.Equal(4.56f, item1.Result);
Assert.Equal(typeof(float), item1.ResultValue.GetType());
Assert.Equal(4.56f, item1.ResultValue);
}

[Fact]
Expand Down Expand Up @@ -47,10 +47,10 @@ public void Compute_ArrayDepth_CorrectConsoleOutput()

ResultItem depthItem = res.Fetch("Depth");
Assert.Equal("Depth", depthItem.ResultName);
Assert.Equal(typeof(float), depthItem.Result.GetType());
Assert.Equal(depthHazard.Get<float>(HazardParameter.Depth), depthItem.Result);
Assert.Equal(typeof(float), depthItem.ResultValue.GetType());
Assert.Equal(depthHazard.Get<float>(HazardParameter.Depth), depthItem.ResultValue);

expectedConsoleOutput += depthItem.Result.ToString() + "\r\n";
expectedConsoleOutput += depthItem.ResultValue.ToString() + "\r\n";
actualOutput += cw.WriteString(res);
}
}
Expand Down Expand Up @@ -89,22 +89,22 @@ public void Compute_ArrayLifeLoss_CorrectConsoleOutput()

ResultItem depthItem = res.Fetch("Depth");
Assert.Equal("Depth", depthItem.ResultName);
Assert.Equal(typeof(float), depthItem.Result.GetType());
Assert.Equal(lifeLossHazard.Get<float>(HazardParameter.Depth), depthItem.Result);
Assert.Equal(typeof(float), depthItem.ResultValue.GetType());
Assert.Equal(lifeLossHazard.Get<float>(HazardParameter.Depth), depthItem.ResultValue);

ResultItem velocityItem = res.Fetch("Velocity");
Assert.Equal("Velocity", velocityItem.ResultName);
Assert.Equal(typeof(float), velocityItem.Result.GetType());
Assert.Equal(lifeLossHazard.Get<float>(HazardParameter.Velocity), velocityItem.Result);
Assert.Equal(typeof(float), velocityItem.ResultValue.GetType());
Assert.Equal(lifeLossHazard.Get<float>(HazardParameter.Velocity), velocityItem.ResultValue);

ResultItem arrivalTime2ftItem = res.Fetch("ArrivalTime2ft");
Assert.Equal("ArrivalTime2ft", arrivalTime2ftItem.ResultName);
Assert.Equal(typeof(DateTime), arrivalTime2ftItem.Result.GetType());
Assert.Equal(lifeLossHazard.Get<DateTime>(HazardParameter.ArrivalTime2ft), arrivalTime2ftItem.Result);
Assert.Equal(typeof(DateTime), arrivalTime2ftItem.ResultValue.GetType());
Assert.Equal(lifeLossHazard.Get<DateTime>(HazardParameter.ArrivalTime2ft), arrivalTime2ftItem.ResultValue);

expectedConsoleOutput += depthItem.Result.ToString() + ',';
expectedConsoleOutput += velocityItem.Result.ToString() + ',';
expectedConsoleOutput += arrivalTime2ftItem.Result.ToString() + "\r\n";
expectedConsoleOutput += depthItem.ResultValue.ToString() + ',';
expectedConsoleOutput += velocityItem.ResultValue.ToString() + ',';
expectedConsoleOutput += arrivalTime2ftItem.ResultValue.ToString() + "\r\n";
actualOutput += cw.WriteString(res);
}
}
Expand Down
41 changes: 21 additions & 20 deletions Geospatial/Geospatial.csproj
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Library</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Geospatial.GDALAssist" Version="0.1.0-Beta" />
</ItemGroup>

<!--<ItemGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<IsAotCompatible>true</IsAotCompatible>
<PublishAot>true</PublishAot>
<OutputType>Library</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Geospatial.GDALAssist" Version="0.1.0-Beta" />
</ItemGroup>

<!--<ItemGroup>
<PackageReference Include="Geospatial.GDALAssist" Version="1.0.845-beta" />
</ItemGroup>-->



<ItemGroup>
<ProjectReference Include="..\Consequences\Consequences.csproj" />
</ItemGroup>



<!--<ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Consequences\Consequences.csproj" />
</ItemGroup>



<!--<ItemGroup>
<Reference Include="Geospatial.GDALAssist">
<HintPath>..\..\..\Users\HEC\Downloads\GDAL\Geospatial.GDALAssist.dll</HintPath>
</Reference>
Expand Down
13 changes: 8 additions & 5 deletions Geospatial/SpatialProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json.Serialization;
using OSGeo.OGR;
using USACE.HEC.Consequences;
Expand All @@ -10,12 +11,14 @@ public class SpatialProcessor : IStreamingProcessor
{
private DataSource _dataSource;
private Layer _layer;
public SpatialProcessor(string filePath)
public SpatialProcessor(string filePath, int layerIndex = 0)
{
_dataSource = Ogr.Open(filePath, 0) ?? throw new Exception("Failed to create datasource.");
_layer = _dataSource.GetLayerByIndex(0) ?? throw new Exception("Failed to create layer.");
// only one layer in the files we are dealing with
_layer = _dataSource.GetLayerByIndex(layerIndex) ?? throw new Exception("Failed to create layer.");
}
public void Process<T>(Action<IConsequencesReceptor> consequenceReceptorProcess) where T : IConsequencesReceptor, new()

public void Process<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(Action<IConsequencesReceptor> consequenceReceptorProcess) where T : IConsequencesReceptor, new()
{
Feature feature;
while ((feature = _layer.GetNextFeature()) != null)
Expand All @@ -28,7 +31,7 @@ public SpatialProcessor(string filePath)
{
// IConsequenceReceptors' properties must have the JsonPropertyName tag
// JsonPropertyNames must match the corresponding field names in the driver for a given property
JsonPropertyNameAttribute? jsonPropertyAttribute = property.GetCustomAttribute<JsonPropertyNameAttribute>();
JsonPropertyNameAttribute jsonPropertyAttribute = property.GetCustomAttribute<JsonPropertyNameAttribute>();
string fieldName = jsonPropertyAttribute != null ? jsonPropertyAttribute.Name : property.Name;

// read values from the driver into their corresponding properties in the IConsequencesReceptor
Expand Down
Loading

0 comments on commit a861309

Please sign in to comment.