Esempio n. 1
0
        private bool RunGdalImport(string inputFilename, string newTableName = null, string tablename = null)
        {
            DBConnectionTool db             = new DBConnectionTool(this.AxMap, this.MapControlTools);
            string           outputFilename = db.GetGdalConnectionString();

            var gdalUtils = new GdalUtils();
            var options   = new[] { "-append" };

            if ((newTableName != null))
            {
                options = new[]
                { "-nln", newTableName, "-overwrite", tablename }
            }
            ;


            ExportImportCallback callback = new ExportImportCallback(MapControlTools);

            callback.GdalFinished   += Callback_GdalFinished;
            gdalUtils.GlobalCallback = callback;
            openTasks++;
            if (!gdalUtils.GdalVectorTranslate(inputFilename, outputFilename, options, true))
            {
                Events.MapControl_Error export_error = new Events.MapControl_Error()
                {
                    ErrorCode = Events.ErrorCodes.ImportExportError, InMethod = "ImportProject", AxMapError = gdalUtils.ErrorMsg[gdalUtils.LastErrorCode] + " Detailed error: " + gdalUtils.DetailedErrorMsg
                };
                On_Error(export_error);
                return(false);
            }
            return(true);
        }
Esempio n. 2
0
        private bool ExportProjectLayer(ResTBPostGISLayer projectLayer)
        {
            if (projectLayer == null)
            {
                return(true);
            }
            DBConnectionTool db = new DBConnectionTool(this.AxMap, this.MapControlTools);

            string outputFilename = localDirectory + "\\ExportMap\\" + projectLayer.ExportImportFileName + ".shp";
            string inputFilename  = db.GetGdalConnectionString();

            if (projectLayer.GetType() == typeof(ResTBDamagePotentialLayer))
            {
                if (!RunGdalTranslate(inputFilename, localDirectory + "\\ExportMap\\" + projectLayer.ExportImportFileName + "_Points.shp", "ESRI Shapefile", "select \"ID\", \"FreeFillParameter_ID\", \"Objectparameter_ID\", \"Project_Id\", point from \"MappedObject\" where point is not null and \"Project_Id\" = " + projectLayer.Project))
                {
                    return(false);
                }
                if (!RunGdalTranslate(inputFilename, localDirectory + "\\ExportMap\\" + projectLayer.ExportImportFileName + "_Lines.shp", "ESRI Shapefile", "select \"ID\", \"FreeFillParameter_ID\", \"Objectparameter_ID\", \"Project_Id\", line from \"MappedObject\" where line is not null and \"Project_Id\" = " + projectLayer.Project))
                {
                    return(false);
                }
                if (!RunGdalTranslate(inputFilename, localDirectory + "\\ExportMap\\" + projectLayer.ExportImportFileName + "_Polygones.shp", "ESRI Shapefile", "select \"ID\", \"FreeFillParameter_ID\", \"Objectparameter_ID\", \"Project_Id\", polygon from \"MappedObject\" where polygon is not null and \"Project_Id\" = " + projectLayer.Project))
                {
                    return(false);
                }
                return(true);
            }
            else if (projectLayer.GetType() == typeof(ResTBHazardMapLayer))
            {
                if (!RunGdalTranslate(inputFilename, localDirectory + "\\ExportMap\\" + projectLayer.ExportImportFileName + ".shp", "ESRI Shapefile", "select * from \"HazardMap\" where \"Project_Id\" =" + projectLayer.Project))
                {
                    return(false);
                }
                return(true);
            }
            return(RunGdalTranslate(inputFilename, outputFilename, "ESRI Shapefile", projectLayer.SQL));
        }
 public AddLayersTool(AxMapWinGIS.AxMap axMap, MapControlTools mapControlTool) : base(axMap, mapControlTool)
 {
     dBConnection = new DBConnectionTool(AxMap, MapControlTools);
 }
        public bool StartEditingLayer(string name, bool saveAndStopWhenFinish = false, FeatureType featureType = FeatureType.Any)
        {
            ILayer editLayer = MapControlTools.Layers.Where(m => m.Name == name).FirstOrDefault();

            if (editLayer == null)
            {
                return(false);
            }

            this.saveAndCloseWhenFinish = saveAndStopWhenFinish;

            Shapefile sf;

            var ogrLayer = AxMap.get_OgrLayer(editLayer.Handle);

            sf = AxMap.get_Shapefile(editLayer.Handle);

            _currentEditingLayer = editLayer;

            if (ogrLayer != null)
            {
                // Add the editing layer
                DBConnectionTool dBConnection = new DBConnectionTool(AxMap, MapControlTools);
                OgrDatasource    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(false);
                }
                OgrLayer editlayer = null;
                if (featureType == FeatureType.Point)
                {
                    editlayer = ds.GetLayerByName(((ResTBPostGISLayer)editLayer).SQL_Layer + "(point)", true);
                    if (editLayer.GetType() == typeof(ResTBDamagePotentialLayer))
                    {
                        editLayer.Handle = ((ResTBDamagePotentialLayer)editLayer).PointHandle;
                    }
                }
                else if (featureType == FeatureType.Line)
                {
                    editlayer = ds.GetLayerByName(((ResTBPostGISLayer)editLayer).SQL_Layer + "(line)", true);
                    if (editLayer.GetType() == typeof(ResTBDamagePotentialLayer))
                    {
                        editLayer.Handle = ((ResTBDamagePotentialLayer)editLayer).LineHandle;
                    }
                }
                else if (featureType == FeatureType.Polygon)
                {
                    editlayer = ds.GetLayerByName(((ResTBPostGISLayer)editLayer).SQL_Layer + "(polygon)", true);
                    if (editLayer.GetType() == typeof(ResTBDamagePotentialLayer))
                    {
                        editLayer.Handle = ((ResTBDamagePotentialLayer)editLayer).PolygonHandle;
                    }
                }
                else
                {
                    editlayer = ds.GetLayerByName(((ResTBPostGISLayer)editLayer).SQL_Layer, true);
                }

                int editinghandle = AxMap.AddLayer(editlayer, false);
                ((ResTBPostGISLayer)editLayer).EditingLayer       = editlayer;
                ((ResTBPostGISLayer)editLayer).EditingLayerHandle = editinghandle;

                _currentEditingLayer = editLayer;

                OgrLayer ogrLayer2 = ((ResTBPostGISLayer)editLayer).EditingLayer;
                if (ogrLayer2.DynamicLoading)
                {
                    Events.MapControl_Error error = new Events.MapControl_Error()
                    {
                        ErrorCode = Events.ErrorCodes.EditingNotAllowed, InMethod = "StartEditingLayer", AxMapError = ""
                    };
                    On_Error(error);
                    return(false);
                }
                if (!ogrLayer2.SupportsEditing[tkOgrSaveType.ostSaveAll])
                {
                    Events.MapControl_Error error = new Events.MapControl_Error()
                    {
                        ErrorCode = Events.ErrorCodes.EditingNotSupported, InMethod = "StartEditingLayer", AxMapError = ogrLayer2.ErrorMsg[ogrLayer.LastErrorCode]
                    };
                    On_Error(error);
                    return(false);
                }

                AxMap.set_LayerVisible(editLayer.Handle, false);

                AxMap.set_LayerVisible(((ResTBPostGISLayer)editLayer).EditingLayerHandle, true);
                sf = AxMap.get_Shapefile(((ResTBPostGISLayer)editLayer).EditingLayerHandle);

                Utils utils = new Utils();
                if (featureType == FeatureType.Point)
                {
                    sf.DefaultDrawingOptions.PointSize = 15;
                    sf.DefaultDrawingOptions.FillColor = utils.ColorByName(tkMapColor.Blue);
                }
                else
                {
                    sf.DefaultDrawingOptions.LineWidth = 7;
                    sf.DefaultDrawingOptions.FillColor = utils.ColorByName(tkMapColor.Blue);
                    sf.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.Blue);
                    AxMap.ShapeEditor.FillColor        = utils.ColorByName(tkMapColor.Blue);
                }

                //AxMap.ShapeEditor.LineWidth = 20;
                AxMap.ShapeEditor.LineColor = utils.ColorByName(tkMapColor.Blue);
                sf.VisibilityExpression     = ((ResTBPostGISLayer)editLayer).VisibilityExpression;
            }

            AxMap.SendMouseDown = true;
            AxMap.SendMouseUp   = true;

            AxMap.ChooseLayer           += AxMap_ChooseLayer;
            AxMap.AfterShapeEdit        += _map_AfterShapeEdit;
            AxMap.BeforeDeleteShape     += _map_BeforeDeleteShape;
            AxMap.BeforeShapeEdit       += _map_BeforeShapeEdit;
            AxMap.ShapeValidationFailed += _map_ShapeValidationFailed;
            AxMap.ValidateShape         += _map_ValidateShape;

            AxMap.ShapeEditor.IndicesVisible = false;
            AxMap.ShapeEditor.ShowLength     = false;
            AxMap.ShapeEditor.ShowArea       = false;
            AxMap.ShapeEditor.ValidationMode = tkEditorValidation.evFixWithGeos;

            if ((featureType == FeatureType.Point) || (featureType == FeatureType.Line))
            {
                AxMap.ShapeEditor.SnapBehavior = tkLayerSelection.lsNoLayer;
            }
            else
            {
                AxMap.ShapeEditor.SnapBehavior = tkLayerSelection.lsAllLayers;
            }

            sf.InteractiveEditing = true;
            Events.MapControl_EditingStateChange editingStateChange = new Events.MapControl_EditingStateChange()
            {
                EditingState = Events.EditingState.StartEditing, EditingLayer = editLayer
            };
            On_EditingStateChange(editingStateChange);



            return(true);
        }
Esempio n. 5
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;
        }