Skip to content
Draft
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
24 changes: 24 additions & 0 deletions AOTTest/AOTTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<None Include="..\GeoConsequences\bin\Release\net8.0\win-x64\publish\GeoConsequences.dll" Link="GeoConsequences.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

<Content Include="C:\Users\HEC\Downloads\GDAL\GDAL\bin64\*.dll">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These guys will always fail because we don't have the GDAL Build. We're going to need to set up some system to check if GDAL exists. Check if it's the right GDAL (optionally) , and if we don't, download it. This is a problem FDA has to solve too. We'll solve it together.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="C:\Users\HEC\Downloads\GDAL\GDAL\common\data\*.db">
<Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
35 changes: 35 additions & 0 deletions AOTTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;

internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("===========================TESTING GEOCONSEQUENCES NATIVE LIBRARY===========================");
TestImport.InitializeGDAL();

Console.WriteLine("===========================READING FROM THE NSI=============================================");
TestImport.ReadNSI(-122.48, 37.76, -122.479, 37.759, 0);

Console.WriteLine("===========================READING FROM THE NSI AND WRITING TO SHAPEFILE====================");
IntPtr path = Marshal.StringToHGlobalAnsi(@"C:\repos\consequences\AOTTest\generated");
IntPtr driver = Marshal.StringToHGlobalAnsi("ESRI Shapefile");
int projection = 3310;
IntPtr x = Marshal.StringToHGlobalAnsi("x");
IntPtr y = Marshal.StringToHGlobalAnsi("y");
TestImport.WriteNSIToShapefile(path, driver, projection, x, y, -122.48, 37.76, -122.479, 37.759);
}
}

public class TestImport
{
[DllImport("GeoConsequences.dll")]
public static extern int ReadNSI(double ulX, double ulY, double lrX, double lrY, int actionIndex);

[DllImport("GeoConsequences.dll")]
public static extern int InitializeGDAL();

[DllImport("GeoConsequences.dll")]
public static extern int WriteNSIToShapefile(IntPtr outputPath, IntPtr driverName, int projection, IntPtr xField, IntPtr yField, double ulX, double ulY, double lrX, double lrY);

}
8 changes: 8 additions & 0 deletions AOTTest/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"AOTTest": {
"commandName": "Project",
"nativeDebugging": true
}
}
}
22 changes: 20 additions & 2 deletions Consequences.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D661B25B-F61F-48AA-B626-09DDFC8FB7E6}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.github\workflows\Release.yml = .github\workflows\Release.yml
.github\workflows\Integration.yml = .github\workflows\Integration.yml
.github\workflows\Release.yml = .github\workflows\Release.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsequencesTest", "ConsequencesTest\ConsequencesTest.csproj", "{95BCC49B-7780-41E9-8365-C51B5E1B3D5E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScratchPaper", "ScratchPaper\ScratchPaper.csproj", "{31071E1B-DA08-40CF-8F16-95982E85B45D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geospatial", "Geospatial\Geospatial.csproj", "{CB7204A4-C01A-4C1C-9BEB-C82F95EEA214}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Geospatial", "Geospatial\Geospatial.csproj", "{CB7204A4-C01A-4C1C-9BEB-C82F95EEA214}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AOTTest", "AOTTest\AOTTest.csproj", "{D0C41ACF-5F5B-4E3C-B86D-606C1019B91F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeoConsequences", "GeoConsequences\GeoConsequences.csproj", "{AAF76EBA-C136-4BEA-B292-308DEBA45753}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventTest", "EventTest\EventTest.csproj", "{B90625C2-2628-4451-8EE9-A1F537129CAE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -40,6 +46,18 @@ Global
{CB7204A4-C01A-4C1C-9BEB-C82F95EEA214}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB7204A4-C01A-4C1C-9BEB-C82F95EEA214}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB7204A4-C01A-4C1C-9BEB-C82F95EEA214}.Release|Any CPU.Build.0 = Release|Any CPU
{D0C41ACF-5F5B-4E3C-B86D-606C1019B91F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0C41ACF-5F5B-4E3C-B86D-606C1019B91F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0C41ACF-5F5B-4E3C-B86D-606C1019B91F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0C41ACF-5F5B-4E3C-B86D-606C1019B91F}.Release|Any CPU.Build.0 = Release|Any CPU
{AAF76EBA-C136-4BEA-B292-308DEBA45753}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AAF76EBA-C136-4BEA-B292-308DEBA45753}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAF76EBA-C136-4BEA-B292-308DEBA45753}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAF76EBA-C136-4BEA-B292-308DEBA45753}.Release|Any CPU.Build.0 = Release|Any CPU
{B90625C2-2628-4451-8EE9-A1F537129CAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B90625C2-2628-4451-8EE9-A1F537129CAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B90625C2-2628-4451-8EE9-A1F537129CAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B90625C2-2628-4451-8EE9-A1F537129CAE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion Consequences/Consequences.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<IsAotCompatible>true</IsAotCompatible>
<PublishAot>true</PublishAot>
Expand Down
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
{
}
35 changes: 35 additions & 0 deletions Consequences/ExternalMethods/NSI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Runtime.InteropServices;
using USACE.HEC.Consequences;
using USACE.HEC.Geography;

namespace USACE.HEC.ExternalMethods;

public class NSI
{
/// <summary>
/// Specify (x,y) coordinates for upper left and lower right corners of the bounding box, as well
/// as an index corresponding to a specific action from a preset list of actions that we define,
/// to be performed on each structure that gets read (TODO)
/// </summary>
[UnmanagedCallersOnly(EntryPoint = "ReadNSI")]
public static int Read(double ulX, double ulY, double lrX, double lrY, int actionIndex)
{
Location upperLeft = new Location { X = ulX, Y = ulY };
Location lowerRight = new Location { X = lrX, Y = lrY };
BoundingBox boundingBox = new BoundingBox(upperLeft, lowerRight);

NSIStreamingProcessor sp = new NSIStreamingProcessor();

// going forward a user will be able to specify an action on the structures
int count = 0;
Task task = sp.Process(boundingBox, (IConsequencesReceptor s) => {
Console.WriteLine(((Structure)s).Name);
Console.WriteLine("hi");
count++;
});

task.Wait();

return count;
}
}
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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate this. We can relax this explosion later if we want, but defaulting to exploding on the error rather than hiding it is probably good practice.

}
}
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;
}
8 changes: 4 additions & 4 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,10 +16,9 @@ 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);
}

return new Result([.. resultItems]);
}
}
2 changes: 1 addition & 1 deletion ConsequencesTest/ConsequencesTest.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

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
Loading