-
Notifications
You must be signed in to change notification settings - Fork 0
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
Nsi processor #29
Nsi processor #29
Changes from 31 commits
917bb16
6d63b42
a0559c2
cbf06d8
2163411
e090217
d0ac729
bb16055
17c84dd
e6dced2
c5d1843
72f0d32
a09045c
0819886
a7a1415
7b91ac3
92148cd
0c4b9ad
2cc7a8f
2d466f4
b3f0687
aa7aeee
f73e7ad
9b9b553
10ae683
f11ff66
69f8171
c6dacc3
611b66e
d4584e8
c390072
6cb2f02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Text; | ||
using System.Text.Json; | ||
using USACE.HEC.Geography; | ||
|
||
namespace USACE.HEC.Consequences; | ||
|
||
public class NSIStreamingProcessor : IBBoxStreamingProcessor | ||
{ | ||
public void Process(BoundingBox boundingBox, Action<IConsequencesReceptor> ConsequenceReceptorProcess) | ||
public async Task Process(BoundingBox boundingBox, Action<IConsequencesReceptor> ConsequenceReceptorProcess) | ||
{ | ||
Structure s = new Structure(); | ||
ConsequenceReceptorProcess(s); | ||
await ProcessStream(boundingBox, ConsequenceReceptorProcess); | ||
} | ||
|
||
private static string ConstructURL(BoundingBox boundingBox, string directive) | ||
{ | ||
StringBuilder url = new StringBuilder(); | ||
|
||
url.Append("https://nsi.sec.usace.army.mil/nsiapi/structures?bbox="); | ||
url.Append(boundingBox.NSIFormat()); | ||
|
||
// directive to specify collection or stream | ||
url.Append(directive); | ||
|
||
return url.ToString(); | ||
} | ||
|
||
|
||
/* | ||
static async Task Test() | ||
// this method processes an entire batch from the NSI at once rather than streaming them one by one | ||
// was tested against ProcessStream and took used significantly more memory with similar times, so we opted to use ProcessStream instead | ||
private static async Task ProcessCollection(BoundingBox boundingBox, Action<IConsequencesReceptor> ConsequenceReceptorProcess) | ||
{ | ||
// Define the API endpoint | ||
string apiEndpoint = "https://nsi.sec.usace.army.mil/nsiapi/structures?bbox=-81.58418,30.25165,-81.58161,30.26939,-81.55898,30.26939,-81.55281,30.24998,-81.58418,30.25165"; | ||
string apiUrl = ConstructURL(boundingBox, "&fmt=fc"); | ||
|
||
// Create an instance of HttpClient | ||
using (HttpClient client = new HttpClient()) | ||
using HttpClient client = new HttpClient(); | ||
try | ||
{ | ||
try | ||
{ | ||
// Make the GET request | ||
HttpResponseMessage response = await client.GetAsync(apiEndpoint); | ||
string jsonResponse = await client.GetStringAsync(apiUrl); | ||
|
||
// Check if the request was successful | ||
response.EnsureSuccessStatusCode(); | ||
using JsonDocument doc = JsonDocument.Parse(jsonResponse); | ||
|
||
// Read and process the response content | ||
string responseBody = await response.Content.ReadAsStringAsync(); | ||
JsonElement featureCollection = doc.RootElement.GetProperty("features"); | ||
foreach (JsonElement structure in featureCollection.EnumerateArray()) | ||
{ | ||
// access the properties of each structure | ||
JsonElement propertiesElement = structure.GetProperty("properties"); | ||
Structure s = JsonSerializer.Deserialize<Structure>(propertiesElement.GetRawText()); | ||
Check warning on line 46 in Consequences/Consequences/NSIStreamingProcessor.cs GitHub Actions / CI
Check warning on line 46 in Consequences/Consequences/NSIStreamingProcessor.cs GitHub Actions / CI
Check warning on line 46 in Consequences/Consequences/NSIStreamingProcessor.cs GitHub Actions / CI
Check warning on line 46 in Consequences/Consequences/NSIStreamingProcessor.cs GitHub Actions / CI
Check warning on line 46 in Consequences/Consequences/NSIStreamingProcessor.cs GitHub Actions / CI
Check warning on line 46 in Consequences/Consequences/NSIStreamingProcessor.cs GitHub Actions / CI
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look into how to ignore this warning |
||
|
||
// Output the response content (for demonstration purposes) | ||
Console.WriteLine("API Response:"); | ||
Console.WriteLine(responseBody); | ||
// apply the action to the deserialized structure | ||
ConsequenceReceptorProcess(s); | ||
} | ||
catch (HttpRequestException e) | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine($"Request error: {e.Message}"); | ||
} | ||
} | ||
|
||
// processes a stream of JSONs from the NSI one by one as opposed to loading in the entire batch | ||
private static async Task ProcessStream(BoundingBox boundingBox, Action<IConsequencesReceptor> ConsequenceReceptorProcess) | ||
{ | ||
string apiURL = ConstructURL(boundingBox, "&fmt=fs"); | ||
|
||
using HttpClient httpClient = new(); | ||
try | ||
{ | ||
HttpResponseMessage response = await httpClient.GetAsync(apiURL); | ||
response.EnsureSuccessStatusCode(); | ||
|
||
using StreamReader reader = new(await response.Content.ReadAsStreamAsync()); | ||
|
||
// read in a line from the stream on by one | ||
// each individual structure JSON is contained in one line | ||
string line; | ||
while ((line = await reader.ReadLineAsync()) != null) | ||
{ | ||
// Handle any errors that occur during the request | ||
Console.WriteLine($"Request error: {e.Message}"); | ||
using JsonDocument structure = JsonDocument.Parse(line); | ||
JsonElement propertiesElement = structure.RootElement.GetProperty("properties"); | ||
Structure s = JsonSerializer.Deserialize<Structure>(propertiesElement.GetRawText()); | ||
Check warning on line 78 in Consequences/Consequences/NSIStreamingProcessor.cs GitHub Actions / CI
Check warning on line 78 in Consequences/Consequences/NSIStreamingProcessor.cs GitHub Actions / CI
Check warning on line 78 in Consequences/Consequences/NSIStreamingProcessor.cs GitHub Actions / CI
Check warning on line 78 in Consequences/Consequences/NSIStreamingProcessor.cs GitHub Actions / CI
Check warning on line 78 in Consequences/Consequences/NSIStreamingProcessor.cs GitHub Actions / CI
Check warning on line 78 in Consequences/Consequences/NSIStreamingProcessor.cs GitHub Actions / CI
|
||
|
||
// apply the action to the deserialized structure | ||
ConsequenceReceptorProcess(s); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine($"Request error: {e.Message}"); | ||
} | ||
} | ||
*/ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Serialization; | ||
using System.Text.Json; | ||
using USACE.HEC.Consequences; | ||
using USACE.HEC.Geography; | ||
using USACE.HEC.Hazards; | ||
using USACE.HEC.Results; | ||
|
||
namespace ConsequencesTest; | ||
|
||
public class NSIStreamingProcessorTest | ||
{ | ||
[Fact] | ||
|
@@ -25,4 +20,25 @@ public void Test() | |
consoleWriter.Write(r); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. await sp.Process |
||
} | ||
|
||
[Fact] | ||
public void JSON_Deserialize() | ||
{ | ||
string jsonString = """{"type": "Feature","geometry": {"type": "Point","coordinates": [-81.563689, 30.265468]},"properties":{"fd_id":498054345,"bid":"862W7C8P+5GM-0-0-0-0","occtype":"RES1-1SNB","st_damcat":"RES","bldgtype":"M","found_type":"S","cbfips":"120310159233002","pop2amu65":2,"pop2amo65":0,"pop2pmu65":1,"pop2pmo65":0,"sqft":1195,"num_story":1,"ftprntid":"none_242828","ftprntsrc":null,"students":0,"found_ht":0.5,"val_struct":124643.72,"val_cont":62321.8604,"val_vehic":27000,"source":"P","med_yr_blt":2004,"firmzone":null,"o65disable":0.26,"u65disable":0.05,"x":-81.56368862,"y":30.265468241,"ground_elv":27.445583771209716,"ground_elv_m":8.365413665771484}}"""; | ||
Structure? s = new Structure(); | ||
using (JsonDocument doc = JsonDocument.Parse(jsonString)) | ||
{ | ||
// Extract the "properties" section | ||
JsonElement root = doc.RootElement; | ||
JsonElement propertiesElement = root.GetProperty("properties"); | ||
|
||
// Convert the "properties" section to a JSON string | ||
string propertiesJson = propertiesElement.GetRawText(); | ||
|
||
// Deserialize the "properties" section into the Properties class | ||
s = JsonSerializer.Deserialize<Structure>(propertiesJson); | ||
|
||
} | ||
Assert.Equal(498054345, s?.Name); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using USACE.HEC.Consequences; | ||
using USACE.HEC.Geography; | ||
|
||
internal class Program | ||
{ | ||
private static async Task Main(string[] args) | ||
{ | ||
// city block in Sunset District, SF | ||
Location upperLeft1 = new Location { X = -122.475275, Y = 37.752394 }; | ||
Location lowerRight1 = new Location { X = -122.473523, Y = 37.750642 }; | ||
BoundingBox boundingBox1 = new BoundingBox(upperLeft1, lowerRight1); | ||
|
||
|
||
Location upperLeft2 = new Location { X = -122.5, Y = 37.8 }; | ||
Location lowerRight2 = new Location { X = -122, Y = 37.3 }; | ||
BoundingBox boundingBox2 = new BoundingBox(upperLeft2, lowerRight2); | ||
|
||
IBBoxStreamingProcessor sp = new NSIStreamingProcessor(); | ||
|
||
int count = 0; | ||
var watch = System.Diagnostics.Stopwatch.StartNew(); | ||
await sp.Process(boundingBox2, (IConsequencesReceptor s) => { | ||
//Console.WriteLine(((Structure)s).Name); | ||
count++; | ||
}); | ||
watch.Stop(); | ||
var elapsedMs = watch.ElapsedMilliseconds; | ||
|
||
Console.WriteLine(count); | ||
Console.WriteLine("Time elapsed: " + elapsedMs.ToString() + " milliseconds"); | ||
Console.Read(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Consequences\Consequences.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simplify new()