public bool AddProjectLayer(int Project, ResTBPostGISType resTBPostGISType)
        {
            ResTBPostGISLayer resTBPostGISLayer = new ResTBPostGISLayer();
            bool visibleAtStartup = false;

            switch (resTBPostGISType)
            {
            case ResTBPostGISType.Perimeter:
                resTBPostGISLayer = new ResTBPerimeterLayer(Project);
                visibleAtStartup  = true;
                break;

            case ResTBPostGISType.MitigationMeasure:
                resTBPostGISLayer = new ResTBMitigationMeasureLayer(Project);
                break;

            case ResTBPostGISType.DamagePotential:
                resTBPostGISLayer = new ResTBDamagePotentialLayer(Project);
                break;

            case ResTBPostGISType.ResilienceBefore:
                resTBPostGISLayer = new ResTBResilienceLayer(Project, true);
                break;

            case ResTBPostGISType.ResilienceAfter:
                resTBPostGISLayer = new ResTBResilienceLayer(Project, false);
                break;

            case ResTBPostGISType.RiskMap:
                resTBPostGISLayer = new ResTBRiskMapLayer(Project);
                break;

            case ResTBPostGISType.RiskMapAfter:
                resTBPostGISLayer = new ResTBRiskMapLayer(Project, false);
                break;
            }

            return(AddProjectLayer(resTBPostGISLayer, visibleAtStartup));
        }
Example #2
0
        public int ImportProjectThread(string filename)
        {
            try
            {
                ZipFile.ExtractToDirectory(filename, localDirectory + "\\ImportMap");

                // import the project, add it to the database as a new project with new id
                if (File.Exists(localDirectory + "\\ImportMap\\database.sqlite"))
                {
                    RunGdalImport(localDirectory + "\\ImportMap\\database.sqlite", "projectimport", "project");
                }
                using (var context = new DB.ResTBContext())
                {
                    List <Project> importproject = context.Projects
                                                   .SqlQuery("Select * from projectimport")
                                                   .ToList <Project>();

                    if ((importproject != null) && (importproject.Count > 0))
                    {
                        Project p = new Project();
                        p.Description      = importproject[0].Description;
                        p.CoordinateSystem = importproject[0].CoordinateSystem;

                        // check if name is already there...
                        string  name = importproject[0].Name;
                        Project pold = context.Projects.Where(m => m.Name == name).FirstOrDefault();
                        if (pold != null)
                        {
                            name = name + " (import " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ")";
                        }


                        p.Name   = name;
                        p.Number = importproject[0].Number;
                        context.Projects.Add(p);
                        context.SaveChanges();

                        Events.MapControl_BusyStateChange bc = new Events.MapControl_BusyStateChange();
                        bc.BusyState   = Events.BusyState.Busy;
                        bc.KeyOfSender = "ImportProject";
                        bc.Percent     = 10;
                        bc.Message     = Resources.MapControl_Importing;
                        MapControlTools.On_BusyStateChange(bc);

                        // we have a new project id

                        //import all new Objectparameters
                        RunGdalImport(localDirectory + "\\ImportMap\\database.sqlite", "objectparameterimport", "objectparameter");

                        RunGdalImport(localDirectory + "\\ImportMap\\database.sqlite", "resiliencevaluesimport", "resiliencevalues");

                        RunGdalImport(localDirectory + "\\ImportMap\\database.sqlite", "praimport", "pra");
                        List <PrA> importpra = context.PrAs
                                               .SqlQuery("Select * from praimport")
                                               .ToList <PrA>();
                        foreach (PrA praImp in importpra)
                        {
                            PrA pra = (PrA)praImp.Clone();
                            pra.Project = p;
                            p.PrAs.Add(pra);
                            context.PrAs.Add(pra);
                        }

                        context.SaveChanges();

                        RunGdalImport(localDirectory + "\\ImportMap\\database.sqlite", "protectionmeasureimport", "ProtectionMeasure");
                        List <ProtectionMeasure> importpm = context.ProtectionMeasurements
                                                            .SqlQuery("Select * from protectionmeasureimport")
                                                            .ToList <ProtectionMeasure>();
                        if (importpm?.Count > 0)
                        {
                            ProtectionMeasure pm = (ProtectionMeasure)importpm[0].Clone();
                            pm.Project          = p;
                            p.ProtectionMeasure = pm;
                            context.ProtectionMeasurements.Add(pm);
                            context.SaveChanges();
                        }

                        // create the ids of old objectparameters, so foreign constraint will work.
                        // But don't override existing
                        List <Objectparameter> importop = context.Objektparameter
                                                          .SqlQuery("Select * from objectparameterimport")
                                                          .ToList <Objectparameter>();

                        //
                        foreach (Objectparameter o in importop)
                        {
                            if (context.Objektparameter.Where(m => m.ID == o.ID).ToList().Count == 0)
                            {
                                context.Database.ExecuteSqlCommand("INSERT INTO public.\"Objectparameter\" (\"ID\",\"FeatureType\",\"Value\",\"Floors\",\"Personcount\",\"Presence\",\"NumberOfVehicles\",\"Velocity\",\"Staff\",\"IsStandard\",\"ObjectClass_ID\")\tVALUES (" + o.ID + ",1,0,0,0,0.0,0,0,0,false,1); ");
                            }
                        }



                        ResTBPostGISLayer tempLayer = new ResTBMitigationMeasureLayer(0);
                        if (File.Exists(localDirectory + "\\ImportMap\\" + tempLayer.ExportImportFileName + ".shp"))
                        {
                            RunGdalImport(localDirectory + "\\ImportMap\\" + tempLayer.ExportImportFileName + ".shp", "protectionmeasuregeometryimport");
                            context.Database.ExecuteSqlCommand("insert into \"ProtectionMeasureGeometry\" (project_fk, geometry) select " + p.Id + ", wkb_geometry from protectionmeasuregeometryimport;");
                        }
                        bc             = new Events.MapControl_BusyStateChange();
                        bc.BusyState   = Events.BusyState.Busy;
                        bc.KeyOfSender = "ImportProject";
                        bc.Percent     = 30;
                        bc.Message     = Resources.MapControl_Importing;
                        MapControlTools.On_BusyStateChange(bc);

                        tempLayer = new ResTBPerimeterLayer(0);
                        if (File.Exists(localDirectory + "\\ImportMap\\" + tempLayer.ExportImportFileName + ".shp"))
                        {
                            RunGdalImport(localDirectory + "\\ImportMap\\" + tempLayer.ExportImportFileName + ".shp", "perimeterimport");
                            context.Database.ExecuteSqlCommand("insert into \"Perimeter\" (project_fk, geometry) select " + p.Id + ", wkb_geometry from perimeterimport;");
                        }
                        bc             = new Events.MapControl_BusyStateChange();
                        bc.BusyState   = Events.BusyState.Busy;
                        bc.KeyOfSender = "ImportProject";
                        bc.Percent     = 40;
                        bc.Message     = Resources.MapControl_Importing;
                        MapControlTools.On_BusyStateChange(bc);

                        tempLayer = new ResTBHazardMapLayer(0, false, context.NatHazards.ToList().First(), 0);
                        if (File.Exists(localDirectory + "\\ImportMap\\" + tempLayer.ExportImportFileName + ".shp"))
                        {
                            RunGdalImport(localDirectory + "\\ImportMap\\" + tempLayer.ExportImportFileName + ".shp", "hazardmapimport");
                            context.Database.ExecuteSqlCommand("insert into \"HazardMap\" (\"Project_Id\", \"Index\",\"BeforeAction\",\"NatHazard_ID\",geometry) select " + p.Id + ",\"index\",CASE WHEN beforeacti IS NULL THEN false WHEN beforeacti = 0 THEN false ELSE true END beforeaction ,\"nathazard_\", wkb_geometry from hazardmapimport;");
                        }
                        bc             = new Events.MapControl_BusyStateChange();
                        bc.BusyState   = Events.BusyState.Busy;
                        bc.KeyOfSender = "ImportProject";
                        bc.Percent     = 50;
                        bc.Message     = Resources.MapControl_Importing;
                        MapControlTools.On_BusyStateChange(bc);

                        tempLayer = new ResTBDamagePotentialLayer(0);
                        if (File.Exists(localDirectory + "\\ImportMap\\" + tempLayer.ExportImportFileName + "_Points.shp"))
                        {
                            RunGdalImport(localDirectory + "\\ImportMap\\" + tempLayer.ExportImportFileName + "_Points.shp", "damagepotentialpoints");
                            context.Database.ExecuteSqlCommand("insert into \"MappedObject\" (\"Project_Id\", \"FreeFillParameter_ID\",\"Objectparameter_ID\",point) select " + p.Id + ", freefillpa, objectpara, wkb_geometry from damagepotentialpoints;");
                        }
                        if (File.Exists(localDirectory + "\\ImportMap\\" + tempLayer.ExportImportFileName + "_Lines.shp"))
                        {
                            RunGdalImport(localDirectory + "\\ImportMap\\" + tempLayer.ExportImportFileName + "_Lines.shp", "damagepotentiallines");
                            context.Database.ExecuteSqlCommand("insert into \"MappedObject\" (\"Project_Id\", \"FreeFillParameter_ID\",\"Objectparameter_ID\",line) select " + p.Id + ", freefillpa, objectpara, wkb_geometry from damagepotentiallines;");
                        }

                        List <int> importrvsMOID = context.Database.SqlQuery <int>("select distinct mappedobject_id from resiliencevaluesimport")
                                                   .ToList <int>();


                        bc             = new Events.MapControl_BusyStateChange();
                        bc.BusyState   = Events.BusyState.Busy;
                        bc.KeyOfSender = "ImportProject";
                        bc.Percent     = 60;
                        bc.Message     = Resources.MapControl_Importing;
                        MapControlTools.On_BusyStateChange(bc);



                        if (File.Exists(localDirectory + "\\ImportMap\\" + tempLayer.ExportImportFileName + "_Polygones.shp"))
                        {
                            RunGdalImport(localDirectory + "\\ImportMap\\" + tempLayer.ExportImportFileName + "_Polygones.shp", "damagepotentialpolygones");
                            context.Database.ExecuteSqlCommand("insert into \"MappedObject\" (\"Project_Id\", \"FreeFillParameter_ID\",\"Objectparameter_ID\",polygon) select " + p.Id + ", freefillpa, objectpara, wkb_geometry from damagepotentialpolygones;");
                        }
                        bc             = new Events.MapControl_BusyStateChange();
                        bc.BusyState   = Events.BusyState.Busy;
                        bc.KeyOfSender = "ImportProject";
                        bc.Percent     = 70;
                        bc.Message     = Resources.MapControl_Importing;
                        MapControlTools.On_BusyStateChange(bc);

                        foreach (int moID in importrvsMOID)
                        {
                            string sql = "insert into \"ResilienceValues\" (\"OverwrittenWeight\", \"Value\", \"MappedObject_ID\", \"ResilienceWeight_ID\") " +
                                         "select r.overwrittenweight, r.value, (select \"ID\" " +
                                         "from \"MappedObject\" mo where polygon = ( " +
                                         "select wkb_geometry from damagepotentialpolygones d " +
                                         "where d.id = " + moID + ") " +
                                         "and \"Project_Id\" = " + p.Id + "), r.resilienceweight_id from resiliencevaluesimport r where r.mappedobject_id = " + moID + "; ";
                            context.Database.ExecuteSqlCommand(sql);
                        }



                        List <MappedObject> mos = context.MappedObjects.Where(m => m.Project.Id == p.Id).ToList();

                        // Insert FreeFills and Objectparameters

                        foreach (MappedObject mo in mos)
                        {
                            if (mo.FreeFillParameter != null)
                            {
                                Objectparameter o     = importop.Where(m => m.ID == mo.FreeFillParameter.ID).FirstOrDefault();
                                Objectparameter oCopy = ShallowCopyEntity <Objectparameter>(o);
                                context.Objektparameter.Add(oCopy);
                                mo.FreeFillParameter = oCopy;
                                context.SaveChanges();
                            }
                            if (mo.Objectparameter != null)
                            {
                                Objectparameter o = importop.Where(m => m.ID == mo.Objectparameter.ID).FirstOrDefault();

                                // not a standard objectparameter
                                if (o != null)
                                {
                                    Objectparameter oCopy = ShallowCopyEntity <Objectparameter>(o);

                                    // get the objectclass
                                    int         objClassID = context.Database.SqlQuery <int>("select objectclass_id from objectparameterimport where id = " + o.ID).First();
                                    ObjectClass oc         = context.ObjektKlassen.Find(objClassID);
                                    oCopy.ObjectClass = oc;
                                    // get the motherobject
                                    int             motherObjID = context.Database.SqlQuery <int>("select motherotbjectparameter_id from objectparameterimport where id = " + o.ID).First();
                                    Objectparameter motherObj   = context.Objektparameter.Find(motherObjID);
                                    oCopy.MotherOtbjectparameter = motherObj;

                                    context.Objektparameter.Add(oCopy);
                                    mo.Objectparameter = oCopy;
                                    context.SaveChanges();
                                }
                            }
                        }



                        bc             = new Events.MapControl_BusyStateChange();
                        bc.BusyState   = Events.BusyState.Idle;
                        bc.KeyOfSender = "ImportProject";
                        bc.Percent     = 100;
                        bc.Message     = Resources.MapControl_Importing_Success;
                        MapControlTools.On_BusyStateChange(bc);

                        return(p.Id);
                    }
                }
            }
            catch (Exception e)
            {
                Events.MapControl_Error export_error = new Events.MapControl_Error()
                {
                    ErrorCode = Events.ErrorCodes.ImportExportError, InMethod = "ImportProject", AxMapError = e.ToString()
                };
                On_Error(export_error);
            }
            return(-1);
        }