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;
            }
        }