Example #1
0
        /// <summary>
        /// Re-Generate all intensity maps for current project
        /// </summary>
        private void GenerateAllIntensityMaps()
        {
            List <HazardMap> allHazardMaps = new List <HazardMap>();

            using (ResTBContext db = new ResTBContext())
            {
                //remove ALL intensities
                var intensitiesToDelete = db.Intensities
                                          .Include(i => i.Project)
                                          .Where(i => i.Project.Id == CurrentProject.Id);
                if (intensitiesToDelete.Any())
                {
                    db.Intensities.RemoveRange(intensitiesToDelete);
                    db.SaveChanges();
                }

                allHazardMaps = db.HazardMaps
                                .Include(m => m.NatHazard)
                                .Where(m => m.Project.Id == CurrentProject.Id).ToList();
            }

            List <NatHazard> allNatHazards = allHazardMaps
                                             .Select(h => h.NatHazard)
                                             .Distinct()
                                             .OrderBy(n => n.ID).ToList();

            foreach (NatHazard natHazard in allNatHazards)              //foreach NatHazard
            {
                List <bool> beforeActions = allHazardMaps
                                            .Where(i => i.NatHazard.ID == natHazard.ID)
                                            .Select(i => i.BeforeAction)
                                            .Distinct()
                                            .OrderByDescending(a => a).ToList();

                foreach (bool beforeMeasure in beforeActions)           //foreach BeforeAction/AfterAction
                {
                    GenerateIntensityMaps(beforeMeasure, natHazard.ID);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Delete all damage extents of project in the database
        /// </summary>
        /// <param name="projectId"></param>
        private void DeleteDamageExtentsFromDB()
        {
            int projectId = CurrentProject.Id;

            if (projectId < 1)
            {
                return;
            }

            string query =
                $"delete " +
                $"from \"DamageExtent\" " +
                $"where \"DamageExtent\".\"MappedObjectId\" in " +
                $"(select damageextent.\"MappedObjectId\" " +
                $"from \"DamageExtent\" as damageextent, \"Intensity\" as intensity " +
                $"where intensity.\"Project_Id\" = {projectId} " +
                $"and damageextent.\"IntensityId\" = intensity.\"ID\") ";

            using (ResTBContext db = new ResTBContext())
            {
                var result = db.Database.ExecuteSqlCommand(query);
            }
        }
Example #3
0
        /// <summary>
        /// create a calc instance for a given project id
        /// </summary>
        /// <param name="projectId">project id</param>
        public Calc(int projectId)
        {
            if (projectId < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(projectId));
            }

            Project project = new Project()
            {
                Id = projectId
            };

            this.CurrentProject = project;

            using (ResTBContext db = new ResTBContext())
            {
                IkClasses = db.IntensitaetsKlassen.AsNoTracking().OrderBy(i => i.Value).ToList();
                if (IkClasses.Count != 3)
                {
                    throw new InvalidOperationException($"GenerateIntensityMaps: IKClasses count = {IkClasses.Count}. Expected: 3");
                }
            }
        }
Example #4
0
        /// <summary>
        /// Generate intensity maps for current project, beforeAction & natHazard
        /// </summary>
        /// <param name="beforeAction"></param>
        /// <param name="natHazardId"></param>
        private void GenerateIntensityMaps(bool beforeAction, int natHazardId)
        {
            int projectId = CurrentProject.Id;

            List <HazardMap> hazardMaps = new List <HazardMap>();

            using (ResTBContext db = new ResTBContext())
            {
                //Load hazard maps
                hazardMaps = db.HazardMaps
                             .Include(m => m.Project.Intesities)
                             .Where(m => m.Project.Id == projectId && m.BeforeAction == beforeAction && m.NatHazard.ID == natHazardId)
                             .ToList();

                if (!hazardMaps.Any())
                {
                    return;
                }

                foreach (HazardMap hazardMap in hazardMaps)
                {
                    int hazardIndex = hazardMap.Index;

                    Dictionary <IKClasses, IntensityDegree> IkIntensityMap = new Dictionary <IKClasses, IntensityDegree>();

                    IkIntensityMap.Add(IkClasses[0], Ik05Map[hazardIndex]);
                    IkIntensityMap.Add(IkClasses[1], Ik20Map[hazardIndex]);
                    IkIntensityMap.Add(IkClasses[2], Ik50Map[hazardIndex]);

                    foreach (KeyValuePair <IKClasses, IntensityDegree> kvp in IkIntensityMap)
                    {
                        if (kvp.Value == IntensityDegree.zero)  //skip zero intensities
                        {
                            continue;
                        }

                        string query =
                            $"insert into \"Intensity\" (\"BeforeAction\" , \"IntensityDegree\" , \"NatHazard_ID\" , \"Project_Id\" ,\"IKClasses_ID\", geometry ) " +
                            $"select hm.\"BeforeAction\" , {(int)kvp.Value}, hm.\"NatHazard_ID\" , hm.\"Project_Id\" , {kvp.Key.ID} , ST_Multi(hm.geometry) " +
                            $"from \"HazardMap\" hm " +
                            $"where hm.\"Project_Id\" = {projectId} and hm.\"BeforeAction\" = {beforeAction} and hm.\"NatHazard_ID\" = {natHazardId} and hm.\"Index\" = {hazardIndex} ";
                        var result = db.Database.ExecuteSqlCommand(query);
                    }
                }

                //Merging
                foreach (var ikClass in IkClasses)
                {
                    foreach (int intDegree in new List <int>()
                    {
                        0, 1, 2
                    })
                    {
                        var intensities = db.Intensities.Where(i => i.IKClasses.ID == ikClass.ID &&
                                                               (int)i.IntensityDegree == intDegree &&
                                                               i.BeforeAction == beforeAction &&
                                                               i.Project.Id == projectId &&
                                                               i.NatHazard.ID == natHazardId);

                        if (intensities.Any() && intensities.Count() > 1)
                        {
                            string ids   = string.Join(", ", intensities.Select(i => i.ID.ToString()));
                            string query =
                                $"insert into \"Intensity\" (\"BeforeAction\" , \"IntensityDegree\" , \"NatHazard_ID\" , \"Project_Id\" ,\"IKClasses_ID\", geometry ) " +
                                $"select \"BeforeAction\" , \"IntensityDegree\"  , \"NatHazard_ID\", \"Project_Id\", \"IKClasses_ID\" , ST_Multi(ST_Union(geometry)) " +
                                $"from \"Intensity\" i " +
                                $"where i.\"ID\" in ({ids}) " +
                                $"group by \"BeforeAction\", \"NatHazard_ID\", \"Project_Id\", \"IntensityDegree\", \"IKClasses_ID\" ";
                            var result = db.Database.ExecuteSqlCommand(query);

                            //delete the origins
                            query  = $"delete from \"Intensity\" i where i.\"ID\" in ({ids}) ";
                            result = db.Database.ExecuteSqlCommand(query);
                        }
                    }
                }
            } //dbContext
        }
Example #5
0
        /// <summary>
        /// Get the merged Objectparameter for a MappedObject
        /// </summary>
        /// <param name="mapObjID">mapped object ID</param>
        /// <returns>merged objectparameter</returns>
        public static Objectparameter GetMergedObjectparameter(int mapObjID)
        {
            MappedObject mapObj = null;

            using (ResTBContext db = new ResTBContext())
            {
                mapObj = db.MappedObjects.AsNoTracking()
                         .Include(m => m.FreeFillParameter)
                         .Include(m => m.Objectparameter.HasProperties)
                         .Include(m => m.Objectparameter.MotherOtbjectparameter.HasProperties)
                         .Include(m => m.Objectparameter.MotherOtbjectparameter.ObjectparameterPerProcesses)
                         .Include(m => m.Objectparameter.MotherOtbjectparameter.ResilienceFactors.Select(r => r.ResilienceWeights))
                         .Include(m => m.Objectparameter.ResilienceFactors.Select(r => r.ResilienceWeights))
                         .Include(m => m.ResilienceValues.Select(rv => rv.ResilienceWeight).Select(rw => rw.ResilienceFactor))
                         .Where(m => m.ID == mapObjID)
                         .FirstOrDefault();

                if (mapObj == null)
                {
                    return(null);
                }

                // default take everything from original
                Objectparameter mergedObjParam = ShallowCopyEntity <Objectparameter>(mapObj.Objectparameter);

                // look for free fill properties
                if (mapObj.Objectparameter.MotherOtbjectparameter != null)
                {
                    mergedObjParam.HasProperties = mapObj.Objectparameter.MotherOtbjectparameter.HasProperties;
                    mergedObjParam.ObjectparameterPerProcesses = mapObj.Objectparameter.MotherOtbjectparameter.ObjectparameterPerProcesses;
                    mergedObjParam.FeatureType       = mapObj.Objectparameter.MotherOtbjectparameter.FeatureType;
                    mergedObjParam.ResilienceFactors = mapObj.Objectparameter.MotherOtbjectparameter.ResilienceFactors;
                }
                else
                {
                    mergedObjParam.HasProperties = mapObj.Objectparameter.HasProperties;
                    mergedObjParam.ObjectparameterPerProcesses = mapObj.Objectparameter.ObjectparameterPerProcesses;
                    mergedObjParam.FeatureType       = mapObj.Objectparameter.FeatureType;
                    mergedObjParam.ResilienceFactors = mapObj.Objectparameter.ResilienceFactors;
                }
                mergedObjParam.ResilienceFactors = mergedObjParam.ResilienceFactors.OrderBy(r => r.ID).ToList();

                // copy all free fill properties to merged parameter, wenn free fill has a value
                foreach (ObjectparameterHasProperties ohp in mergedObjParam.HasProperties.Where(m => m.isOptional == true))
                {
                    if (mapObj.FreeFillParameter != null)
                    {
                        // Get the Property from the Free Fill Object
                        PropertyInfo FreeFillProperty = ohp.Property.Split('.').Select(s => mapObj.FreeFillParameter.GetType().GetProperty(s)).FirstOrDefault();
                        // Get the Property from the Merged Object
                        PropertyInfo MergedProperty = ohp.Property.Split('.').Select(s => mergedObjParam.GetType().GetProperty(s)).FirstOrDefault();

                        // Assign FreeFill PropertyValue to Merged PropertyValue
                        if (FreeFillProperty.GetValue(mapObj.FreeFillParameter) != null)
                        {
                            MergedProperty.SetValue(mergedObjParam, FreeFillProperty.GetValue(mapObj.FreeFillParameter));
                        }
                    }
                }

                //Add all ResilienceFactors to merged object
                foreach (ResilienceFactor resFac in mergedObjParam.ResilienceFactors)
                {
                    foreach (ResilienceWeight resWeight in resFac.ResilienceWeights)
                    {
                        var resValue = new ResilienceValues()
                        {
                            MappedObject      = mapObj,
                            ResilienceWeight  = resWeight,
                            OverwrittenWeight = resWeight.Weight,
                            Value             = 0
                        };

                        if (mapObj.ResilienceValues.Any(rv => rv.ResilienceWeight.ID == resWeight.ID))
                        {
                            var mappedResValue = mapObj.ResilienceValues.Where(rv => rv.ResilienceWeight.ID == resWeight.ID).Single();

                            resValue = mappedResValue;
                        }

                        mergedObjParam.ResilienceValuesMerged.Add(resValue);
                    }
                }

                return(mergedObjParam);
            }
        }
Example #6
0
        /// <summary>
        /// Transfer resilience values between mapped objects
        /// </summary>
        /// <param name="fromID">from mapped object ID</param>
        /// <param name="toID">to mapped object ID</param>
        /// <param name="onlyAfterAction">flag</param>
        /// <returns></returns>
        public static async Task <bool> CopyResilience(int fromID, int toID, bool onlyAfterAction = false)
        {
            using (ResTBContext db = new ResTBContext())
            {
                var fromMapObj = await db.MappedObjects.AsNoTracking()
                                 .Include(m => m.FreeFillParameter)
                                 .Include(m => m.ResilienceValues.Select(rv => rv.ResilienceWeight).Select(rw => rw.ResilienceFactor))
                                 .Where(m => m.ID == fromID)
                                 .FirstOrDefaultAsync();

                var toMapObj = await db.MappedObjects
                               .Include(m => m.FreeFillParameter)
                               .Include(m => m.ResilienceValues.Select(rv => rv.ResilienceWeight).Select(rw => rw.ResilienceFactor))
                               .Where(m => m.ID == toID)
                               .FirstOrDefaultAsync();

                if (fromMapObj != null && toMapObj != null)
                {
                    //toMapObj.ResilienceValues.Clear();  // only reference of mappedObject is deleted in db !!!!

                    if (onlyAfterAction)
                    {
                        db.ResilienceValues.RemoveRange(toMapObj.ResilienceValues.Where(r => r.ResilienceWeight.BeforeAction == false)); //delete only afterAction
                    }
                    else
                    {
                        db.ResilienceValues.RemoveRange(toMapObj.ResilienceValues); //delete all
                    }
                    await db.SaveChangesAsync();

                    var resWeightIDsfromMapObj = fromMapObj.ResilienceValues.Select(rv => rv.ResilienceWeight.ID);

                    var resWeightsFromMapObj = db.ResilienceWeights.Where(r => resWeightIDsfromMapObj.Contains(r.ID));

                    foreach (ResilienceValues resVal in fromMapObj.ResilienceValues)
                    {
                        ResilienceWeight resWeight = await resWeightsFromMapObj.SingleAsync(r => r.ID == resVal.ResilienceWeight.ID);

                        if (onlyAfterAction)
                        {
                            if ((bool)resWeight.BeforeAction)
                            {
                                continue;
                            }
                        }

                        toMapObj.ResilienceValues.Add(
                            new ResilienceValues()
                        {
                            OverwrittenWeight = resVal.OverwrittenWeight,
                            Value             = resVal.Value,
                            ResilienceWeight  = resWeight
                        }
                            );
                    }

                    await db.SaveChangesAsync();
                }
            }
            return(true);
        }