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 12, 2024
2 parents 831778a + 5df611b commit 0958d22
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 31 deletions.
2 changes: 1 addition & 1 deletion backend/metadata-database/init-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const datasets = [
LongDescription: `The building models have a 3D object of each building plus additional information on its dimentions.`,
MinZoomLevel: 11,
MarkersThreshold: 17,
DisplayProperty: "",
DisplayProperty: [],
Tables: [],
},
},
Expand Down
11 changes: 6 additions & 5 deletions backend/src/BIE.DataPipeline/DbHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ public void SetInfo(string tableName, string columnNames)
/// Creates a Table in the database based on the name given in the description. Returns false if unable to.
/// </summary>
/// <param name="description"></param>
internal bool CreateTable(DataSourceDescription description)
/// <param name="header"> the header string to be used in creation (only used for SHAPE)</param>
internal bool CreateTable(DataSourceDescription description, string header)
{
try
{
Console.WriteLine("Creating Table...");
var db = Database.Instance;


var query = GetCreationQuery(description);
var query = GetCreationQuery(description, header);

var cmd = db.CreateCommand(query);
db.Execute(cmd);
Expand Down Expand Up @@ -231,7 +232,7 @@ USING GEOMETRY_AUTO_GRID
}
catch (Exception e)
{
Console.WriteLine("Error while creating Table:");
Console.WriteLine("Error while creating Indexes:");
Console.Error.WriteLine(e);
return false;
}
Expand Down Expand Up @@ -282,7 +283,7 @@ FROM dbo.{tableName}
}


private string GetCreationQuery(DataSourceDescription description)
private string GetCreationQuery(DataSourceDescription description, string header)
{
if (description.source.data_format == "SHAPE")
{
Expand All @@ -291,7 +292,7 @@ IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{desc
BEGIN
CREATE TABLE {description.table_name} (
Id INT PRIMARY KEY IDENTITY(1,1),
Location GEOMETRY,
{header},
Area FLOAT
);
END";
Expand Down
10 changes: 10 additions & 0 deletions backend/src/BIE.DataPipeline/Import/CityGmlImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ public bool ReadLine(out string nextLine)
}
}

public string GetCreationHeader()
{
return "";
}

public string GetInsertHeader()
{
return "";
}

private Geometry UtmCoordinatesToGeometry(string utmCoordinates)
{
//Console.WriteLine(utmCoordinates);
Expand Down
10 changes: 10 additions & 0 deletions backend/src/BIE.DataPipeline/Import/CsvImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ public bool ReadLine(out string nextLine)
}
}

public string GetCreationHeader()
{
return headerString;
}

public string GetInsertHeader()
{
return headerString;
}


private static string RemoveLastComma(string input)
{
Expand Down
4 changes: 4 additions & 0 deletions backend/src/BIE.DataPipeline/Import/IImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ namespace BIE.DataPipeline.Import
internal interface IImporter
{
public bool ReadLine(out string nextLine);

public string GetCreationHeader();

public string GetInsertHeader();
}
}
68 changes: 45 additions & 23 deletions backend/src/BIE.DataPipeline/Import/ShapeImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public ShapeImporter(DataSourceDescription? dataSourceDescription)

SetupParser();

ReadFileHeader();

// Define the source and target coordinate systems
var utmZone32 = CoordinateSystemWktReader
.Parse("PROJCS[\"WGS 84 / UTM zone 32N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS" +
Expand Down Expand Up @@ -74,14 +72,19 @@ private void SetupParser()
dbfStream.Position = 0;

mParser = Shapefile.CreateDataReader(
new ShapefileStreamProviderRegistry(
new ByteStreamProvider(StreamTypes.Shape, shpStream),
new ByteStreamProvider(StreamTypes.Data, dbfStream),
true,
true),
GeometryFactory.Default);
new ShapefileStreamProviderRegistry(
new ByteStreamProvider(StreamTypes
.Shape,
shpStream),
new ByteStreamProvider(StreamTypes
.Data,
dbfStream),
true,
true),
GeometryFactory.Default);

mHeader = mParser.DbaseHeader;

}

private void ExtractShapeFilesFromZip(ZipArchive zipArchive, MemoryStream shpStream, MemoryStream dbfStream)
Expand Down Expand Up @@ -125,10 +128,24 @@ public bool ReadLine(out string nextLine)
// Append geometry as WKT (Well-Known Text)
var geometry = mParser.Geometry;
geometry = ConvertUtmToLatLong(geometry);


// for (int i = 1; i < mHeader.Fields.Length; i++)
// {
// Console.Write($" {mParser.GetValue(i)};");
// }
// Console.WriteLine();

nextLine = $"GEOMETRY::STGeomFromText('{geometry.AsText()}', 4326)";
// nextLine = $"GEOMETRY::STGeomFromText('POLYGON (11.060226859896797 49.496927347229494, 11.060276626123832 49.49695803564076)')', 4326)";

for (int i = 1; i < mHeader.Fields.Length + 1; i++)
{
var value = (string)mParser.GetValue(i);
nextLine += $", \'{(value != "" ? value : "null")}\'";
}

var area = CalculateAreaInSquareMeters(geometry);

nextLine = $"GEOMETRY::STGeomFromText('{geometry.AsText()}', 4326),"+area;
nextLine += $", {CalculateAreaInSquareMeters(geometry)}";

return true;
}
Expand Down Expand Up @@ -172,20 +189,25 @@ private double CalculateAreaInSquareMeters(Geometry geometry)
}


private string[] ReadFileHeader()
/// <summary>
/// Returns the Header needed for Table creation
/// starts with a comma
/// </summary>
/// <returns></returns>
public string GetCreationHeader()
{
var fieldCount = mHeader.NumFields;
var res = new string[fieldCount];

// Append column names
for (int i = 0; i < fieldCount; i++)
{
res[i] = mHeader.Fields[i].Name;
}

return res;
return mHeader.Fields.Aggregate("Location GEOMETRY", (current, field) => current + $", {field.Name} VARCHAR(255)");
}

/// <summary>
/// get the header used for inserting
/// </summary>
/// <returns></returns>
public string GetInsertHeader()
{
return mHeader.Fields.Aggregate("Location", (current, field) => current + $", {field.Name}");
}

/// <summary>
/// Conver UTM coordinates to Latitude and Longitude
/// </summary>
Expand Down Expand Up @@ -213,4 +235,4 @@ private Geometry ConvertUtmToLatLong(Geometry polygon)
return polygon;
}
}
}
}
6 changes: 4 additions & 2 deletions backend/src/BIE.DataPipeline/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@

case "SHAPE":
importer = new ShapeImporter(description);
dbHelper.SetInfo(description.table_name, "Location ,Area");

dbHelper.SetInfo(description.table_name, importer.GetInsertHeader() + ",Area");

break;
case "CITYGML":
importer = new CityGmlImporter(description, dbHelper);
Expand All @@ -96,7 +98,7 @@
return 1;
}

if (!dbHelper.CreateTable(description))
if (!dbHelper.CreateTable(description, importer.GetCreationHeader()))
{
return 0;
}
Expand Down

0 comments on commit 0958d22

Please sign in to comment.