From b0e389aada3dec96740c6ad42206963ee234bba2 Mon Sep 17 00:00:00 2001 From: Gerardo Date: Sun, 14 Nov 2021 17:08:06 +0100 Subject: [PATCH] Importacion estable --- Helper/HelperGdb.cs | 151 ++++++++++++++++++++++------------------- Helper/HelperGlobal.cs | 29 ++++++++ Model/OliviaGlob.cs | 4 ++ Services/EjecServ.cs | 104 +++++++++++++++------------- 4 files changed, 172 insertions(+), 116 deletions(-) diff --git a/Helper/HelperGdb.cs b/Helper/HelperGdb.cs index 1555382..664526c 100644 --- a/Helper/HelperGdb.cs +++ b/Helper/HelperGdb.cs @@ -18,6 +18,8 @@ using ArcGIS.Desktop.Core.Geoprocessing; using ArcGIS.Desktop.Framework.Threading.Tasks; using ArcGIS.Desktop.Editing; using OliviaAddInPro.Model; +using System.IO; +using System.Diagnostics; namespace OliviaAddInPro.Helper { @@ -599,91 +601,102 @@ namespace OliviaAddInPro.Helper Free(fc); return n; } + static public bool RenameSHP(string dir, string path, string nameNew) + { + var extensions = new[] { "cpg", "dbf", "prj", "sbn", "sbx", "shp", "shp.xml", "shx" }; + string old_ext = System.IO.Path.GetExtension(path); - public static async Task ExportShp(string pathLayerIn, SpatialQueryFilter filter, string nameShp, string outpath, ProgressDialog progrDialog = null) + string name = System.IO.Path.GetFileName(path); + if (!string.IsNullOrEmpty(old_ext)) + name.Replace(old_ext,""); + string _ex =System.IO.Path.GetExtension(nameNew); + string nn = nameNew; + if (!string.IsNullOrEmpty(_ex)) + nn.Replace(_ex, ""); + var res = true; + foreach(var ex in extensions) + { + try + { + var po = string.Format("{0}{1}.{2}", dir, name, ex); + var pn = string.Format("{0}{1}.{2}", dir, nn, ex); + + if (!File.Exists(po)) + res = false;//no existe + if (!File.Exists(pn)) + File.Delete(pn); + File.Move(po,pn ); + } + catch + { + res = false; + } + } + + return res; + } + + public static async Task ExportShp(string pathLayerIn, SpatialQueryFilter filter, string nameShp, string outpath, ProgressorSource progrDialog = null) { if (!System.IO.Directory.Exists(outpath)) System.IO.Directory.CreateDirectory(outpath); ReiniciaOutStr(); - string dirPath; - string fclassName = System.IO.Path.GetFileName(pathLayerIn); - IReadOnlyList parameters = null; - IReadOnlyList parameters2 = null; - - FeatureClass featureClass = null; - var fin = ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run((Func)(() => + System.Threading.CancellationTokenSource _cts= new System.Threading.CancellationTokenSource(); + var fin = ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run((Func)(() => { - if (string.IsNullOrEmpty(fclassName)) - return false; + var progSrc = new CancelableProgressorSource(); + string[] args = { pathLayerIn, outpath }; + // execute the tool + IGPResult gpResult = Geoprocessing.ExecuteToolAsync("FeatureClassToShapefile_conversion", args, + null, _cts.Token, - try - { - if (System.IO.Path.GetExtension(pathLayerIn).Equals(".shp")) - { - dirPath = System.IO.Path.GetDirectoryName(pathLayerIn); - //es un shape de entrada - FileSystemConnectionPath fileConnection = new FileSystemConnectionPath(new Uri(dirPath), FileSystemDatastoreType.Shapefile); - using (FileSystemDatastore shapefile = new FileSystemDatastore(fileConnection)) - { - featureClass = shapefile.OpenDataset(fclassName); - } - } - else - { - dirPath = GetPathGdb(pathLayerIn); - //es una feature class de una gdb - using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(dirPath)))) - { - featureClass = fileGeodatabase.OpenDataset(fclassName); - } - } - if (featureClass == null) - return false; + (event_name, o) => // implement delegate and handle events + { + switch (event_name) + { + case "OnValidate": // stop execute if any warnings + //if ((o as IGPMessage[]).Any(it => it.Type == GPMessageType.Warning)) + //_cts.Cancel(); + break; - //Selection selFeatures = featureClass.Select(filter, SelectionType.ObjectID, SelectionOption.Normal); - // make a value array of strings to be passed to ExecuteToolAsync - parameters = Geoprocessing.MakeValueArray(pathLayerIn, outpath);//, nameShp); - var aux = pathLayerIn.Split('\\'); - parameters2 = Geoprocessing.MakeValueArray(outpath + aux[aux.Length - 1]+".shp", outpath, nameShp); + case "OnProgressMessage": + { + string msg = string.Format("{0}: {1}", new object[] { event_name, (string)o }); + progrDialog.Message = (string)o; + Debug.WriteLine(msg); + //System.Windows.MessageBox.Show(msg); + //_cts.Cancel(); + } + break; - } - catch - { - OutStr = "Ha ocurrido un error al convertir a shape"; - return false; - } - return true; + case "OnProgressPos": + { + string msg2 = string.Format("{0}: {1} %", new object[] { event_name, (int)o }); + Debug.WriteLine(msg2); + // if ((int)o < 0) + //System.Windows.MessageBox.Show(msg2); + //_cts.Cancel(); + break; + } + } + }).Result; + + //renombrado de ficheros: + if (!RenameSHP(outpath, pathLayerIn, nameShp)) + return null; + + return gpResult.ReturnValue; })); + fin.Wait(); - if(!fin.Result)// || parameters==null) + if (string.IsNullOrEmpty(fin.Result)) { - OutStr = "Ha ocurrido un error al convertir a shape"; - return false; - } - - - var progSrc = new CancelableProgressorSource(progrDialog); - - - // execute the tool - IGPResult gpResult = Geoprocessing.ExecuteToolAsync("FeatureClassToShapefile_conversion", parameters, - null, progSrc.Progressor, GPExecuteToolFlags.GPThread).Result; - - //renombrar shp - //param2.Add(nameShp); - IGPResult gpResult2 = Geoprocessing.ExecuteToolAsync("FeatureClassToFeatureClass", parameters2, - null, progSrc.Progressor, GPExecuteToolFlags.GPThread).Result; - - - Free(featureClass); - if (string.IsNullOrEmpty(gpResult.ReturnValue)) - { - OutStr = "Ha ocurrido un error en la herramienta de GeoProcesamiento: " + gpResult.ErrorMessages; + OutStr = "Ha ocurrido un error en la herramienta de GeoProcesamiento"; return false; } else { - OutStr = "Ok: " + gpResult.ReturnValue; + OutStr = "Ok: " + fin.Result; return true; } } diff --git a/Helper/HelperGlobal.cs b/Helper/HelperGlobal.cs index 8bb9079..aae50a3 100644 --- a/Helper/HelperGlobal.cs +++ b/Helper/HelperGlobal.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using ArcGIS.Desktop.Internal.Framework.Controls; using ArcGIS.Desktop.Framework.Dialogs; using System.Windows; +using System.IO; namespace OliviaAddInPro.Helper { @@ -50,5 +51,33 @@ namespace OliviaAddInPro.Helper return false; } } + + public static void create_folder(string dir) + { + string[] pathParts = dir.Split('\\'); + string path = ""; + foreach (var d in pathParts) + { + if(string.IsNullOrEmpty(path)) + path = d; + + else + path = path + '\\'+d; + + if (!Directory.Exists(path)) + { + try + { + Directory.CreateDirectory(path); + + } + catch + { + + } + } + + } + } } } diff --git a/Model/OliviaGlob.cs b/Model/OliviaGlob.cs index c1b2853..44fb945 100644 --- a/Model/OliviaGlob.cs +++ b/Model/OliviaGlob.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using OliviaAddInPro.Services; using ArcGIS.Core.Geometry; using ArcGIS.Desktop.Framework.Threading.Tasks; +using OliviaAddInPro.Helper; namespace OliviaAddInPro.Model { @@ -132,6 +133,9 @@ namespace OliviaAddInPro.Model Paths.PathTemp = c.path_temp; c.PathCartela = c.PathCartela; + HelperGlobal.create_folder(Paths.DirData); + HelperGlobal.create_folder(Paths.PathTemp); + Paths.PathGdbGen = c.PathGdbGen; Paths.PathGdbNw = c.red_carreteras; Paths.PathSimbVSM = c.PathSimbVSM; diff --git a/Services/EjecServ.cs b/Services/EjecServ.cs index 46ab33c..aa6a104 100644 --- a/Services/EjecServ.cs +++ b/Services/EjecServ.cs @@ -37,60 +37,71 @@ namespace OliviaAddInPro.Services */ public bool ComienzaEjec(ModosEjec modo) { - bool fue_mal = false; - - //esconde el pane - OliviaGlob.ShowHidePane(false); - ErrStr = string.Empty; - - //Cuenta las filas que cumplen la consulta - int nelems = HelperGdb.GetNumElems(com.CapaElems, com.ConsultaAmbs); - if(nelems<=0) + using (OliviaGlob.progrDialog = new ProgressDialog("Exportando Datos", "Cancelar", 100, true)) { - ErrStr = "No existen ámbitos que cumplan las condiciones introducidas para la exportación " + com.ConsultaAmbs; - fue_mal = true; - } + var status = new ProgressorSource(OliviaGlob.progrDialog); + + OliviaGlob.progrDialog.Show(); + bool fue_mal = false; + status.Message = "Exportando Datos"; + status.Value = 0; + status.Status = "Exportando Datos"; + status.Max = 100; + //esconde el pane + OliviaGlob.ShowHidePane(false); + ErrStr = string.Empty; - //Obtiene la geometría que envuelve a los ámbitos - Geometry geom_export = null; - if (!fue_mal) - { - geom_export = GetGeomAmbitsExport(); - if (geom_export == null || geom_export.IsEmpty) + //Cuenta las filas que cumplen la consulta + int nelems = HelperGdb.GetNumElems(com.CapaElems, com.ConsultaAmbs); + if (nelems <= 0) { - ErrStr = "No se ha podido generar geometría de los ámbitos" + com.ConsultaAmbs + ErrStr; + ErrStr = "No existen ámbitos que cumplan las condiciones introducidas para la exportación " + com.ConsultaAmbs; fue_mal = true; } - if (geom_export == null || geom_export.IsEmpty) + //Obtiene la geometría que envuelve a los ámbitos + Geometry geom_export = null; + if (!fue_mal) { - fue_mal = true; - } - } + geom_export = GetGeomAmbitsExport(); + if (geom_export == null || geom_export.IsEmpty) + { + ErrStr = "No se ha podido generar geometría de los ámbitos" + com.ConsultaAmbs + ErrStr; + fue_mal = true; + } - //crea el filtro de exportación - if (!fue_mal) - { - //mira spatialreference de los datos de entrada - spatRef = geom_export.SpatialReference; - filtroEspacial = CreaFiltro(com.ConsultaAmbs, geom_export); - if (filtroEspacial == null) + if (geom_export == null || geom_export.IsEmpty) + { + fue_mal = true; + } + } + + //crea el filtro de exportación + if (!fue_mal) { - fue_mal = true; + //mira spatialreference de los datos de entrada + spatRef = geom_export.SpatialReference; + filtroEspacial = CreaFiltro(com.ConsultaAmbs, geom_export); + if (filtroEspacial == null) + { + fue_mal = true; + } } - } - if (!fue_mal) - { - Ejecuta(modo); - return true; + if (!fue_mal) + { + Ejecuta(modo, status); + OliviaGlob.progrDialog.Hide(); + return true; + } + + OliviaGlob.progrDialog.Hide(); + //fue mal + //muestra pane + OliviaGlob.ShowHidePane(true); + + return false; } - - //fue mal - //muestra pane - OliviaGlob.ShowHidePane(true); - - return false; } @@ -192,14 +203,13 @@ namespace OliviaAddInPro.Services /** * Exporta y lanza proceso y ventana de proceso */ - public void Ejecuta(ModosEjec modo) + public void Ejecuta(ModosEjec modo, ProgressorSource dlgProcess) { //lanza ventana de progreso - OliviaGlob.progrDialog = new ProgressDialog("Exportando Datos", "Cancelar", 100); - OliviaGlob.progrDialog.Show(); - + + //exporta los datos de entrada - bool res=HelperGdb.ExportShp(com.CapaElems, filtroEspacial, com.NombreShpExport, OliviaGlob.Paths.DirData, OliviaGlob.progrDialog).Result; + bool res=HelperGdb.ExportShp(com.CapaElems, filtroEspacial, com.NombreShpExport, OliviaGlob.Paths.DirData, dlgProcess).Result; if(!res) { string msg = HelperGdb.OutStr;