Skip to content

Commit

Permalink
Merge branch 'sprint-release' into dev-eb
Browse files Browse the repository at this point in the history
  • Loading branch information
Corgam committed Jul 13, 2024
2 parents 0958d22 + 3d29d11 commit 4b6dae1
Show file tree
Hide file tree
Showing 18 changed files with 557 additions and 497 deletions.
21 changes: 0 additions & 21 deletions backend/api-gateway/Controllers/APIGatewayController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,6 @@ public async Task<IActionResult> LoadLocationData([FromBody, Required] LocationD

}

// Helper method to generate mock data
private List<DatasetItem> GenerateMockData(string mapId, int count)
{
var random = new Random();
var data = new List<DatasetItem>();

for (int i = 0; i < count; i++)
{
var item = new DatasetItem
{
Id = Guid.NewGuid().ToString(), // Generate a unique ID
Key = $"{mapId} Item {i + 1} (distance)",
Value = $"{random.Next(50, 1000)} m",
MapId = mapId
};
data.Add(item);
}

return data;
}

/// <summary>
/// Gets the dataset viewport data based on the provided parameters.
/// </summary>
Expand Down
19 changes: 15 additions & 4 deletions backend/api-gateway/Models/LocationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,28 @@ public class Coordinate

public class LocationDataResponse
{
public List<DatasetItem> IndividualData { get; set; } = new List<DatasetItem>();
public List<DatasetItem> GeneralData { get; set; } = new List<DatasetItem>();
public List<DatasetItem> IndividualData { get; set; } = [];
public List<DatasetItem> SelectionData { get; set; } = [];
}

public class DatasetItem
{
public string Id { get; set; } = string.Empty;
public string DisplayName { get; set; } = string.Empty;
public string DatasetId { get; set; } = string.Empty;

public double[] Coordinate { get; set; } = [];
public List<double[]> PolygonCoordinates { get; set; } = [];
public List<SubdataItem> Subdata { get; set; } = [];
public string Value { get; set; } = string.Empty; // some items may not have subdata and should instead be directly displayed with a value

}

public class SubdataItem
{
public string Key { get; set; } = string.Empty;

public string Value { get; set; } = string.Empty;
public string MapId { get; set; } = string.Empty; // Optional -> for "open in map" functionality

}


Expand Down
45 changes: 21 additions & 24 deletions backend/lib/BieMetadata/MetadataDbHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class MetadataDbHelper
private string mMetaDataDbUrl;

private IMongoDatabase mDatabase;

public bool Connected { get; private set; }

public MetadataDbHelper()
Expand Down Expand Up @@ -58,7 +58,7 @@ public bool CreateConnection()
return metadataObject;
}

public bool UpdateMetadata(string dataset, string tableName, int numberOfLines, BoundingBox boundingBox)
public bool UpdateMetadata(string dataset, MetadataObject.TableData tableData)
{
// Load the collection
var collection = mDatabase.GetCollection<MetadataObject>("datasets");
Expand All @@ -74,31 +74,28 @@ public bool UpdateMetadata(string dataset, string tableName, int numberOfLines,
return false;
}

// Load the existing table
var existingTable = metadataObject.additionalData.Tables.Find(t => t.Name == tableName);
if (existingTable == null)
// Load and remove any existing table
var existingTable = metadataObject.additionalData.Tables.Find(t => t.Name == tableData.Name);
if (existingTable != null)
{
// Create a new table object if not present
var newTable = new MetadataObject.TableData()
{
Name = tableName,
NumberOfLines = numberOfLines,
BoundingBox = boundingBox
};
metadataObject.additionalData.Tables.Add(newTable);
collection.ReplaceOne(g => g.basicData.DatasetId == dataset, metadataObject);
return true;
metadataObject.additionalData.Tables.Remove(existingTable);
}

// Table info already exists, for now just choose the larger number of lines number.
existingTable.NumberOfLines = existingTable.NumberOfLines < numberOfLines
? numberOfLines
: existingTable.NumberOfLines;

// always write the current Bounding box
existingTable.BoundingBox = boundingBox;

// Insert the new Table object.
metadataObject.additionalData.Tables.Add(tableData);
collection.ReplaceOne(g => g.basicData.DatasetId == dataset, metadataObject);
return true;
}
}

public bool UpdateMetadata(string dataset, string tableName, int numberOfLines, BoundingBox boundingBox)
{
return UpdateMetadata(dataset,
new MetadataObject.TableData()
{
Name = tableName,
NumberOfLines = numberOfLines,
BoundingBox = new BoundingBox(),
RowHeaders = new List<string>()
});
}
}
17 changes: 15 additions & 2 deletions backend/lib/BieMetadata/MetadataObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,25 @@ public class AdditionalData
/// </summary>
public class TableData
{
// The name of the .yaml file
/// <summary>
/// the name of the table
/// </summary>
public string Name { get; set; } = string.Empty;
// The number of lines of data in that file.

/// <summary>
/// the number of lines in the table
/// </summary>
public int NumberOfLines { get; set; } = 0;

/// <summary>
/// the bounding box of the geomtry data in the table
/// </summary>
public BoundingBox? BoundingBox { get; set; }

/// <summary>
/// the headers of the dataset. Should NOT include the special Location header.
/// </summary>
public List<string> RowHeaders { get; set; }
}

/// <summary>
Expand Down
55 changes: 30 additions & 25 deletions backend/src/BIE.Core/BIE.Core.API/ApiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public static class ApiHelper
AUTHORITY[""ESRI"",""102008""]
"; // see http://epsg.io/27700
private static ICoordinateTransformation mTransformation = new CoordinateTransformationFactory().
CreateFromCoordinateSystems(GeographicCoordinateSystem.WGS84,
(CoordinateSystem)CoordinateSystemWktReader.Parse(epsg27700));
CreateFromCoordinateSystems(GeographicCoordinateSystem.WGS84, ProjectedCoordinateSystem.WGS84_UTM(32, true));
private static ICoordinateTransformation transform = new CoordinateTransformationFactory().CreateFromCoordinateSystems(GeographicCoordinateSystem.WGS84, ProjectedCoordinateSystem.WGS84_UTM(32, true));


public static double getDistance(double longitude, double latitude, Point p)
{
Expand All @@ -58,28 +59,32 @@ public static double getDistance(double longitude, double latitude, Point p)

public static double getArea(Polygon p)
{
// convert each point of the polygon into a new coordinate (revert x and y
// convert it into EPSG27700
// return area

var coordinates = p.Coordinates;
Console.WriteLine($"coordinates {coordinates}");

var transformedCoordinates = new Coordinate[coordinates.Length];
// Transform each coordinate
for (int i = 0; i < coordinates.Length; i++)
{
double[] pointWGS84 = { coordinates[i].Y, coordinates[i].X }; // Switch X and Y
double[] pointEPSG27700 = mTransformation.MathTransform.Transform(pointWGS84);
//Console.WriteLine($"FIRST X THEN Y coordinates before transformation (but switched xy) {coordinates[i].X},{coordinates[i].Y}");
double[] pointWGS84 = { coordinates[i].X, coordinates[i].Y };
double[] pointEPSG27700 = transform.MathTransform.Transform(pointWGS84);
transformedCoordinates[i] = new Coordinate(pointEPSG27700[0], pointEPSG27700[1]);
Console.WriteLine($"transformedCoordinates[i] {transformedCoordinates[i]}");
//Console.WriteLine($"transformedCoordinates[i] {transformedCoordinates[i]}");
}
var geometryFactory = new GeometryFactory(new PrecisionModel(), 27700);
var transformedPolygon = new Polygon(new LinearRing(transformedCoordinates), geometryFactory);
Console.WriteLine($"area {transformedPolygon.Area}");
Console.WriteLine($"transformed coords {transformedPolygon.Coordinates}");

// Ensure the polygon is closed
if (!transformedCoordinates[0].Equals2D(transformedCoordinates[transformedCoordinates.Length - 1]))
{
Array.Resize(ref transformedCoordinates, transformedCoordinates.Length + 1);
transformedCoordinates[transformedCoordinates.Length - 1] = transformedCoordinates[0];
}

// Create a polygon with transformed coordinates
var geometryFactory = new GeometryFactory();
var transformedPolygon = geometryFactory.CreatePolygon(transformedCoordinates);

// Return the area of the transformed polygon
// Calculate and return the area
return transformedPolygon.Area;
}

Expand Down Expand Up @@ -132,23 +137,23 @@ public static List<string> ConvertToWktPolygons(List<List<List<double>>> locatio
foreach (var polygon in locations)
{
var wktString = "POLYGON((";
foreach (var point in polygon)
for (int i = 0; i < polygon.Count; i++)
{
var point = polygon[i];
if (point.Count != 2)
{
throw new ArgumentException("Each point should have exactly two coordinates.");
}

var longitude = point[0].ToString(culture);
var latitude = point[1].ToString(culture);
wktString += $"{longitude} {latitude}, ";
var longitude = point[0].ToString(sCultureInfo);
var latitude = point[1].ToString(sCultureInfo);
wktString += $"{longitude} {latitude}";
if (i < polygon.Count - 1)
{
wktString += ", ";
}
}

// Close the polygon by repeating the first point
var firstPoint = polygon[0];
var firstLongitude = firstPoint[0].ToString(culture);
var firstLatitude = firstPoint[1].ToString(culture);
wktString += $"{firstLongitude} {firstLatitude}))";
wktString += "))";

wktPolygons.Add(wktString);
}
Expand Down Expand Up @@ -198,7 +203,7 @@ public static bool BoxIntersection(BoundingBox box1, BoundingBox box2)
/// <param name="tableName">the name of the table to filter</param>
/// <param name="polygon">the polygon string</param>
/// <returns></returns>
public static string FromTableIntersectsPolygon(string tableName, string polygon)
public static string FromTableWhereIntersectsPolygon(string tableName, string polygon)
{
return $@"
FROM dbo.{tableName}
Expand Down
1 change: 1 addition & 0 deletions backend/src/BIE.Core/BIE.Core.API/BIE.Core.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="Accord.MachineLearning" Version="3.8.0" />
<PackageReference Include="Accord.Math" Version="3.8.0" />
<PackageReference Include="LinqStatistics" Version="2.3.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.11.1" />
<PackageReference Include="MongoDB.Driver" Version="2.26.0" />
<PackageReference Include="NetTopologySuite" Version="2.5.0" />
Expand Down
16 changes: 8 additions & 8 deletions backend/src/BIE.Core/BIE.Core.API/BIE.Core.API.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4b6dae1

Please sign in to comment.