Beispiel #1
0
        public int ImportProject(string filename)
        {
            try
            {
                Events.MapControl_BusyStateChange bc = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ImportProject";
                bc.Percent     = 0;
                bc.Message     = Resources.Project_Import;
                MapControlTools.On_BusyStateChange(bc);

                if (System.IO.Directory.Exists(localDirectory + "\\ImportMap"))
                {
                    DeleteDirectory(localDirectory + "\\ImportMap");
                }
                System.IO.Directory.CreateDirectory(localDirectory + "\\ImportMap");

                MapControlTools.On_BusyStateChange(bc);

                return(ImportProjectThread(filename));
            }
            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);
        }
Beispiel #2
0
        public bool ExportProject(int Project, string filename)
        {
            try
            {
                if (System.IO.Directory.Exists(localDirectory + "\\ExportMap"))
                {
                    DeleteDirectory(localDirectory + "\\ExportMap");
                }
                System.IO.Directory.CreateDirectory(localDirectory + "\\ExportMap");

                Thread exportProjectThread = new Thread(() => ExportProjectThread(Project, filename));

                Events.MapControl_BusyStateChange bc = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ExportProject";
                bc.Percent     = 0;
                bc.Message     = Resources.Project_Export;

                MapControlTools.On_BusyStateChange(bc);

                exportProjectThread.Start();
                return(true);
            }
            catch (Exception e)
            {
                Events.MapControl_Error export_error = new Events.MapControl_Error()
                {
                    ErrorCode = Events.ErrorCodes.ImportExportError, InMethod = "ExportProject", AxMapError = e.ToString()
                };
                On_Error(export_error);
                return(false);
            }
        }
        /// <summary>
        /// Add all Layers for a project. (Hazard Map only the ones with data)
        /// </summary>
        /// <param name="Project">The project id</param>
        /// <returns></returns>
        public bool AddProjectLayers(int Project)
        {
            Thread addProjectThread = new Thread(() => AddProjectLayersThread(Project));

            Events.MapControl_BusyStateChange bc = new Events.MapControl_BusyStateChange();
            bc.BusyState   = Events.BusyState.BackgroundBusy;
            bc.KeyOfSender = "AddProjectLayers";
            bc.Percent     = 0;
            bc.Message     = Resources.MapControl_AddProjectLayers;

            MapControlTools.On_BusyStateChange(bc);

            addProjectThread.Start();

            return(true);
        }
        public bool Redraw(bool reloadLayers = false)
        {
            Events.MapControl_BusyStateChange bc = new Events.MapControl_BusyStateChange();
            bc             = new Events.MapControl_BusyStateChange();
            bc.BusyState   = Events.BusyState.Busy;
            bc.KeyOfSender = "Redraw";
            bc.Percent     = 0;
            bc.Message     = Resources.MapControl_RedrawMap;
            MapControlTools.On_BusyStateChange(bc);

            if (reloadLayers)
            {
                double percentadd     = 80.0 / MapControlTools.Layers.Count;
                int    currentpercent = 10;

                foreach (ILayer layer in MapControlTools.Layers)
                {
                    Type editingLayerType = layer.GetType();
                    if (editingLayerType == typeof(ResTBDamagePotentialLayer))
                    {
                        OgrLayer o = AxMap.get_OgrLayer(((ResTBDamagePotentialLayer)layer).PointHandle);
                        o.ReloadFromSource();
                        o = AxMap.get_OgrLayer(((ResTBDamagePotentialLayer)layer).LineHandle);
                        o.ReloadFromSource();
                        o = AxMap.get_OgrLayer(((ResTBDamagePotentialLayer)layer).PolygonHandle);
                        o.ReloadFromSource();
                    }
                    else if (editingLayerType == typeof(ResTBRiskMapLayer))
                    {
                        OgrLayer o = AxMap.get_OgrLayer(((ResTBRiskMapLayer)layer).PointHandle);
                        o.ReloadFromSource();
                        o = AxMap.get_OgrLayer(((ResTBRiskMapLayer)layer).LineHandle);
                        o.ReloadFromSource();
                        o = AxMap.get_OgrLayer(((ResTBRiskMapLayer)layer).PolygonHandle);
                        o.ReloadFromSource();
                    }
                    else if (editingLayerType.BaseType != null && editingLayerType.BaseType == typeof(ResTBPostGISLayer))
                    {
                        OgrLayer o = AxMap.get_OgrLayer(((ResTBPostGISLayer)layer).Handle);
                        o.ReloadFromSource();
                    }

                    layer.ShapeCount = MapControlTools.ShapesCount(layer.Name);

                    currentpercent += (int)percentadd;
                    bc              = new Events.MapControl_BusyStateChange();
                    bc.BusyState    = Events.BusyState.Busy;
                    bc.KeyOfSender  = "Redraw";
                    bc.Percent      = currentpercent;
                    bc.Message      = Resources.MapControl_ReloadLayers;
                }
            }

            bc             = new Events.MapControl_BusyStateChange();
            bc.BusyState   = Events.BusyState.Busy;
            bc.KeyOfSender = "Redraw";
            bc.Percent     = 80;
            bc.Message     = Resources.MapControl_RedrawMap;
            MapControlTools.On_BusyStateChange(bc);
            AxMap.Redraw();

            foreach (BaseLayer l in MapControlTools.Layers)
            {
                l.ApplyStyle(AxMap);
            }

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

            //MapControlTools.ReStartSelecting();

            return(true);
        }
        public void AddProjectLayersThread(int Project)
        {
            var gdalUtils = new GdalUtils();

            RasterReprojectCallback callback = new RasterReprojectCallback(null, MapControlTools);

            gdalUtils.GlobalCallback = callback;
            ds = new OgrDatasource();
            if (!ds.Open(dBConnection.GetGdalConnectionString()))
            {
                Events.MapControl_Error error = new Events.MapControl_Error()
                {
                    ErrorCode = Events.ErrorCodes.CouldNotConnectDatabase, InMethod = "AddPostGISLayer", AxMapError = ds.GdalLastErrorMsg
                };
                On_Error(error);
                return;
            }
            else
            {
                Events.MapControl_BusyStateChange bc = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "AddProjectLayersThread";
                bc.Percent     = 10;
                bc.Message     = Resources.MapControl_AddedPerimeter;
                MapControlTools.On_BusyStateChange(bc);


                // RiskMap
                if (!AddProjectLayer(Project, ResTBPostGISType.RiskMap))
                {
                    return;
                }
                if (!AddProjectLayer(Project, ResTBPostGISType.RiskMapAfter))
                {
                    return;
                }

                // Perimeter
                if (!AddProjectLayer(Project, ResTBPostGISType.Perimeter))
                {
                    return;
                }
                if (MapControlTools.ShapesCount(MapControlTools.GetLayerNamesFromPostGISType(ResTBPostGISType.Perimeter).First()) > 0)
                {
                    MapControlTools.ZoomToLayer(MapControlTools.GetLayerNamesFromPostGISType(ResTBPostGISType.Perimeter).First());
                }


                // Hazard Maps
                DB.ResTBContext  db         = new DB.ResTBContext();
                List <HazardMap> hazardMaps = db.HazardMaps.Where(m => m.Project.Id == Project).Include(m => m.NatHazard).OrderBy(m => m.Index).ToList();

                double percentadd     = 50.0 / hazardMaps.Count;
                int    currentpercent = 10;

                foreach (HazardMap hazardMap in hazardMaps.OrderByDescending(m => m.BeforeAction))
                {
                    ResTBHazardMapLayer hazardLayer;
                    if (hazardMap.BeforeAction)
                    {
                        hazardLayer = new ResTBHazardMapLayer(Project, true, hazardMap.NatHazard, hazardMap.Index);
                    }
                    else
                    {
                        hazardLayer = new ResTBHazardMapLayer(Project, false, hazardMap.NatHazard, hazardMap.Index);
                    }


                    currentpercent += (int)percentadd;
                    if (!MapControlTools.Layers.Where(m => m.Name == hazardLayer.Name).Any())
                    {
                        AddProjectLayer(hazardLayer, false);
                        bc             = new Events.MapControl_BusyStateChange();
                        bc.BusyState   = Events.BusyState.Busy;
                        bc.KeyOfSender = "AddProjectLayersThread";
                        bc.Percent     = currentpercent;
                        bc.Message     = Resources.MapControl_AddingHazardMaps;
                        MapControlTools.On_BusyStateChange(bc);
                    }
                }


                // Damage Potential
                if (!AddProjectLayer(Project, ResTBPostGISType.DamagePotential))
                {
                    return;
                }

                // Resiliences

                if (!AddProjectLayer(Project, ResTBPostGISType.ResilienceBefore))
                {
                    return;
                }

                if (!AddProjectLayer(Project, ResTBPostGISType.ResilienceAfter))
                {
                    return;
                }

                currentpercent += 30;
                bc              = new Events.MapControl_BusyStateChange();
                bc.BusyState    = Events.BusyState.Busy;
                bc.KeyOfSender  = "AddProjectLayersThread";
                bc.Percent      = currentpercent;
                bc.Message      = Resources.MapControl_AddedDamagePotentials;
                MapControlTools.On_BusyStateChange(bc);

                // Mitigation Measure
                if (!AddProjectLayer(Project, ResTBPostGISType.MitigationMeasure))
                {
                    return;
                }

                currentpercent += 10;
                bc              = new Events.MapControl_BusyStateChange();
                bc.BusyState    = Events.BusyState.Idle;
                bc.KeyOfSender  = "Project";
                bc.Percent      = 100;
                bc.Message      = Resources.MapControl_ProjectLoaded;// "Project loaded";
                MapControlTools.On_BusyStateChange(bc);


                return;
            }
        }
        public bool AddRasterLayer(string fileLocation, string layerName, bool autoReproject = true, bool overwriteExistingReprojectedFiles = false)
        {
            Image img = new Image();

            if (autoReproject)
            {
                if (!img.Open(fileLocation))
                {
                    Events.MapControl_Error errorFileNotOpen = new Events.MapControl_Error()
                    {
                        ErrorCode = Events.ErrorCodes.CouldNotLoadLayer, InMethod = "AddRasterLayer", AxMapError = img.ErrorMsg[img.LastErrorCode]
                    };
                    On_Error(errorFileNotOpen);
                }

                if (!IsRasterGoogleMercator(img))
                {
                    // Image is not WGS84, so use GdalWrap for reprojection

                    string localData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                    if (!System.IO.Directory.Exists(localData + "\\ResTBDesktop"))
                    {
                        System.IO.Directory.CreateDirectory(localData + "\\ResTBDesktop");
                    }

                    string filename = Path.GetFileName(fileLocation);
                    if (!Directory.Exists(localData + "\\ResTBDesktop\\temp"))
                    {
                        Directory.CreateDirectory(localData + "\\ResTBDesktop\\temp");
                    }

                    // check if file already transformed
                    if ((File.Exists(localData + "\\ResTBDesktop\\temp\\" + filename)) && (!overwriteExistingReprojectedFiles))
                    {
                    }
                    else
                    {
                        string fileLocationThreadSave = (string)fileLocation.Clone();
                        fileLocation = localData + "\\ResTBDesktop\\temp\\" + filename;
                        RasterLayer r = new RasterLayer()
                        {
                            FileName = fileLocation, LayerType = LayerType.CustomLayerRaster, Name = layerName
                        };

                        RasterProjectionWorker rpw = new RasterProjectionWorker();
                        rpw.MapControlTools = MapControlTools;
                        rpw.MapControl_RasterReprojected += Callback_MapControl_RasterReprojected;

                        reprojectThread = new Thread(() => rpw.Run(filename, fileLocationThreadSave, r));

                        Events.MapControl_BusyStateChange bc = new Events.MapControl_BusyStateChange();
                        bc.BusyState   = Events.BusyState.BackgroundBusy;
                        bc.KeyOfSender = Resources.MapControl_Busy_RasterReprojection;
                        bc.Percent     = 0;
                        bc.Message     = Resources.MapControl_Busy_StartRasterReprojection;;

                        MapControlTools.On_BusyStateChange(bc);

                        reprojectThread.Start();

                        return(false);
                    }
                    fileLocation = localData + "\\ResTBDesktop\\temp\\" + filename;
                }
            }

            if (img.Open(fileLocation))
            {
                string proj        = img.GetProjection();
                int    layerHandle = AxMap.AddLayer(img, true);
                if (layerHandle == -1)
                {
                    Events.MapControl_Error imghandle_error = new Events.MapControl_Error()
                    {
                        ErrorCode = Events.ErrorCodes.CouldNotLoadLayer, InMethod = "AddRasterLayer", AxMapError = new GlobalSettings().GdalLastErrorMsg
                    };
                    On_Error(imghandle_error);
                    return(false);
                }

                RasterLayer r = new RasterLayer()
                {
                    FileName = fileLocation, Handle = layerHandle, LayerType = LayerType.CustomLayerRaster, Name = layerName
                };
                MapControlTools.Layers.Add(r);
                MapControlTools.LayerHandlingTool.SetLayerPosition(r.Name, LayerMoveType.BOTTOM);
                Events.MapControl_LayerChange layerchange = new Events.MapControl_LayerChange()
                {
                    LayerChangeReason = Events.LayerChangeReason.AddLayer, Layer = r
                };
                On_LayerChange(layerchange);
                return(true);
            }
            Events.MapControl_Error error = new Events.MapControl_Error()
            {
                ErrorCode = Events.ErrorCodes.CouldNotLoadLayer, InMethod = "AddRasterLayer", AxMapError = img.ErrorMsg[img.LastErrorCode]
            };
            On_Error(error);
            return(false);
        }
Beispiel #7
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);
        }
Beispiel #8
0
        public void ExportProjectThread(int Project, string filename)
        {
            try
            {
                Events.MapControl_BusyStateChange bc = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ExportProjectThread";
                bc.Percent     = 0;
                bc.Message     = Resources.MapControl_Exporting_Layers;
                MapControlTools.On_BusyStateChange(bc);

                if (!ExportProjectLayer((ResTBPostGISLayer)MapControlTools.Layers.Where(m => ((ResTBPostGISLayer)m).ResTBPostGISType == ResTBPostGISType.Perimeter).FirstOrDefault()))
                {
                    return;
                }
                bc             = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ExportProjectThread";
                bc.Percent     = 10;
                bc.Message     = Resources.MapControl_Exporting_Layers + "(" + Resources.Perimeter + ")";
                MapControlTools.On_BusyStateChange(bc);
                if (!ExportProjectLayer((ResTBPostGISLayer)MapControlTools.Layers.Where(m => ((ResTBPostGISLayer)m).ResTBPostGISType == ResTBPostGISType.MitigationMeasure).FirstOrDefault()))
                {
                    return;
                }
                bc             = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ExportProjectThread";
                bc.Percent     = 20;
                bc.Message     = Resources.MapControl_Exporting_Layers + "(" + Resources.MitigationMeasure + ")";
                MapControlTools.On_BusyStateChange(bc);
                if (!ExportProjectLayer((ResTBPostGISLayer)MapControlTools.Layers.Where(m => ((ResTBPostGISLayer)m).ResTBPostGISType == ResTBPostGISType.DamagePotential).FirstOrDefault()))
                {
                    return;
                }
                bc             = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ExportProjectThread";
                bc.Percent     = 30;
                bc.Message     = Resources.MapControl_Exporting_Layers + "(" + Resources.DamagePotential + ")";
                MapControlTools.On_BusyStateChange(bc);
                if (!ExportProjectLayer((ResTBPostGISLayer)MapControlTools.Layers.Where(m => ((ResTBPostGISLayer)m).ResTBPostGISType == ResTBPostGISType.HazardMapBefore).FirstOrDefault()))
                {
                    return;
                }
                bc             = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ExportProjectThread";
                bc.Percent     = 40;
                bc.Message     = Resources.MapControl_Exporting_Layers + "(" + Resources.Hazard_Maps_before_Measure + ")";
                MapControlTools.On_BusyStateChange(bc);
                if (!ExportProjectLayer((ResTBPostGISLayer)MapControlTools.Layers.Where(m => ((ResTBPostGISLayer)m).ResTBPostGISType == ResTBPostGISType.HazardMapAfter).FirstOrDefault()))
                {
                    return;
                }
                bc             = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ExportProjectThread";
                bc.Percent     = 50;
                bc.Message     = Resources.MapControl_Exporting_Layers + "(" + Resources.Hazard_Maps_after_Measure + ")";
                MapControlTools.On_BusyStateChange(bc);
                DBConnectionTool db = new DBConnectionTool(this.AxMap, this.MapControlTools);

                if (!RunGdalTranslate(db.GetGdalConnectionString(), localDirectory + "\\ExportMap\\database.sqlite", "SQLite", "select * from \"Project\" where \"Id\" = " + Project, false, "Project"))
                {
                    return;
                }
                bc             = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ExportProjectThread";
                bc.Percent     = 60;
                bc.Message     = Resources.MapControl_Exporting_Database;
                MapControlTools.On_BusyStateChange(bc);

                if (!RunGdalTranslate(db.GetGdalConnectionString(), localDirectory + "\\ExportMap\\database.sqlite", "SQLite", "select * from \"ProtectionMeasure\" where \"ProjectID\" = " + Project, true, "ProtectionMeasure"))
                {
                    return;
                }
                bc             = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ExportProjectThread";
                bc.Percent     = 70;
                bc.Message     = Resources.MapControl_Exporting_Database;
                MapControlTools.On_BusyStateChange(bc);

                if (!RunGdalTranslate(db.GetGdalConnectionString(), localDirectory + "\\ExportMap\\database.sqlite", "SQLite", "select o.* from \"Objectparameter\" o inner join \"MappedObject\" mo on (mo.\"FreeFillParameter_ID\"  = o.\"ID\" ) where mo.\"Project_Id\" = " + Project + " union select o.* from \"Objectparameter\" o inner join \"MappedObject\" mo on (mo.\"Objectparameter_ID\"  = o.\"ID\" ) where o.\"IsStandard\" = false and mo.\"Project_Id\" = " + Project, true, "Objectparameter"))
                {
                    return;
                }
                bc             = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ExportProjectThread";
                bc.Percent     = 70;
                bc.Message     = Resources.MapControl_Exporting_Database;
                MapControlTools.On_BusyStateChange(bc);

                if (!RunGdalTranslate(db.GetGdalConnectionString(), localDirectory + "\\ExportMap\\database.sqlite", "SQLite", "select o.* from \"ResilienceValues\" o inner join \"MappedObject\" mo on (mo.\"ID\"  = o.\"MappedObject_ID\" ) where mo.\"Project_Id\" = " + Project, true, "ResilienceValues"))
                {
                    return;
                }
                bc             = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ExportProjectThread";
                bc.Percent     = 70;
                bc.Message     = Resources.MapControl_Exporting_Database;
                MapControlTools.On_BusyStateChange(bc);

                if (!RunGdalTranslate(db.GetGdalConnectionString(), localDirectory + "\\ExportMap\\database.sqlite", "SQLite", "select * from \"PrA\" where \"ProjectId\" = " + Project, true, "PrA"))
                {
                    return;
                }
                bc             = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Busy;
                bc.KeyOfSender = "ExportProjectThread";
                bc.Percent     = 70;
                bc.Message     = Resources.MapControl_Exporting_Database;
                MapControlTools.On_BusyStateChange(bc);

                while (openTasks != finishedTasks)
                {
                }


                // Now Zip it...
                if (System.IO.File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }
                ZipFile.CreateFromDirectory(localDirectory + "\\ExportMap", filename);
                bc             = new Events.MapControl_BusyStateChange();
                bc.BusyState   = Events.BusyState.Idle;
                bc.KeyOfSender = "ExportProjectThread";
                bc.Percent     = 100;
                bc.Message     = Resources.MapControl_Exporting_ExportetTo + " " + filename;
                MapControlTools.On_BusyStateChange(bc);

                DeleteDirectory(localDirectory + "\\ExportMap");
            }
            catch (Exception e)
            {
                Events.MapControl_Error export_error = new Events.MapControl_Error()
                {
                    ErrorCode = Events.ErrorCodes.ImportExportError, InMethod = "ExportProject", AxMapError = e.ToString()
                };
                On_Error(export_error);
            }

            return;
        }