private static void ProcessCodes(G g, Codes codeMap, int vertexLabel, Vertex parentTreeNodeVertex) { var codes = codeMap.GetValues(); foreach (var code in codes) { if (!g.TryGetV(code.Code, out var vertex)) { vertex = g.AddV(vertexLabel, code.Code).AddP(P.Name, code.Name); } if (!string.IsNullOrWhiteSpace(code.ParentCode)) { if (!g.TryGetV(code.ParentCode, out var parentVertex)) { var parent = codeMap.GetCode(code.ParentCode); parentVertex = g.AddV(vertexLabel, parent.Code).AddP(P.Name, parent.Name); } vertex.AddE(EL.Child, parentVertex); } else if (parentTreeNodeVertex != null) { vertex.AddE(EL.Child, parentTreeNodeVertex); } } }
public static async Task <List <NatureAreaType> > Load(string connectionString, Codes codes) { const string natureAreaTypeCodeSql = @"SELECT nat.geometry_id AS NatureAreaId, nat.code AS Code, nat.fraction AS Percentage FROM data.codes_geometry nat WHERE nat.code LIKE 'NA-%' and nat.code not LIKE 'NA-BS%' and nat.code not LIKE 'NA-LKM%' GROUP BY nat.geometry_id, nat.code, nat.fraction"; IEnumerable <NatureAreaTypeDto> dtos = null; using (var conn = new NpgsqlConnection(connectionString)) { conn.Open(); dtos = await conn.QueryAsync <NatureAreaTypeDto>(natureAreaTypeCodeSql); } var dict = new Dictionary <string, NatureAreaType>(); var natureAreaPercentages = new Dictionary <(int, string), NatureAreaPercentage>(); foreach (var dto in dtos) { if (!codes.Contains(dto.Code)) { continue; } if (!dict.TryGetValue(dto.Code, out var natureAreaType)) { natureAreaType = new NatureAreaType(dto.Code, codes.GetCode(dto.Code).Name); dict.Add(dto.Code, natureAreaType); } var key = (dto.NatureAreaId, dto.Code); if (natureAreaPercentages.TryGetValue(key, out var natureArea)) { double newPercentage = natureArea.Percentage + (dto.Percentage / 10); if (newPercentage > 1) { Console.WriteLine($"{key.Code} {key.NatureAreaId} exceeds 1 as percentage."); } else { natureArea.Percentage = newPercentage; } } else { var natureAreaPercentage = new NatureAreaPercentage { NatureAreaId = dto.NatureAreaId, Percentage = dto.Percentage / 10 }; natureAreaType.NatureAreas.Add(natureAreaPercentage); natureAreaPercentages.Add(key, natureAreaPercentage); } } return(dict.Values.ToList()); }
public static async Task <List <DescriptionVariable> > Load(string connectionString, Codes descriptionVariableCodes) { Console.WriteLine("Loading DescriptionVariables"); const string descriptionVariableSql = @"SELECT bv.code AS DvCode, nomt.code AS NatCode, bv.geometry_id AS NatureAreaId, nomt.fraction AS NatureAreaPercentage FROM data.codes_geometry bv, data.codes_geometry nomt WHERE bv.geometry_id = nomt.geometry_id and bv.code like 'NA-BS%' and nomt.code like 'NA-%' and nomt.code NOT like 'NA-BS%' and nomt.code NOT like 'NA-LKM%' GROUP BY bv.code, nomt.code, bv.geometry_id, nomt.fraction"; IEnumerable <DescriptionVariableDto> descriptionVariableDtos; using (var conn = new NpgsqlConnection(connectionString)) { conn.Open(); descriptionVariableDtos = await conn.QueryAsync <DescriptionVariableDto>(descriptionVariableSql); } var dict = new Dictionary <string, DescriptionVariable>(); var natureAreas = new Dictionary <(string, int, string), NatureAreaIdTypePercentage>(); foreach (var dto in descriptionVariableDtos) { var codes = dto.DvCode.Split(',', StringSplitOptions.RemoveEmptyEntries); //dto.DvCode = dto.DvCode.Remove(0,3); foreach (var code in codes) { if (!descriptionVariableCodes.Contains(dto.DvCode.ToUpper())) { // Ignored code as it doesn't exist in the code file. continue; } if (!dict.TryGetValue(dto.DvCode, out var descriptionVariable)) { descriptionVariable = new DescriptionVariable(dto.DvCode, descriptionVariableCodes.GetCode(dto.DvCode).Name); dict.Add(dto.DvCode, descriptionVariable); } var key = (dto.DvCode, dto.NatureAreaId, dto.NatCode); // descriptionVariable.NatureAreas.FirstOrDefault(na => na.NatureAreaId == dto.NatureAreaId && na.NatureAreaTypeCode == dto.NatCode); if (natureAreas.TryGetValue(key, out var natureArea)) { double newPercentage = natureArea.NatureAreaPercentage + (dto.NatureAreaPercentage / 10); if (newPercentage > 1) { Console.WriteLine($"{key.DvCode} {key.NatCode} {key.NatureAreaId} exceeds 1 as percentage."); } else { natureArea.NatureAreaPercentage = newPercentage; } } else { natureArea = new NatureAreaIdTypePercentage { NatureAreaId = dto.NatureAreaId, NatureAreaTypeCode = dto.NatCode, NatureAreaPercentage = dto.NatureAreaPercentage / 10 }; descriptionVariable.NatureAreas.Add(natureArea); natureAreas.Add(key, natureArea); } } } Console.WriteLine("Finished loading DescriptionVariables"); return(dict.Values.ToList()); }