Importacion estable

ConfiguracionSimplificada
Gerardo 2021-11-14 17:08:06 +01:00
parent 3885fa83b7
commit b0e389aada
4 changed files with 172 additions and 116 deletions

View File

@ -18,6 +18,8 @@ using ArcGIS.Desktop.Core.Geoprocessing;
using ArcGIS.Desktop.Framework.Threading.Tasks; using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Editing; using ArcGIS.Desktop.Editing;
using OliviaAddInPro.Model; using OliviaAddInPro.Model;
using System.IO;
using System.Diagnostics;
namespace OliviaAddInPro.Helper namespace OliviaAddInPro.Helper
{ {
@ -599,91 +601,102 @@ namespace OliviaAddInPro.Helper
Free(fc); Free(fc);
return n; 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<bool> 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<bool> ExportShp(string pathLayerIn, SpatialQueryFilter filter, string nameShp, string outpath, ProgressorSource progrDialog = null)
{ {
if (!System.IO.Directory.Exists(outpath)) if (!System.IO.Directory.Exists(outpath))
System.IO.Directory.CreateDirectory(outpath); System.IO.Directory.CreateDirectory(outpath);
ReiniciaOutStr(); ReiniciaOutStr();
string dirPath; System.Threading.CancellationTokenSource _cts= new System.Threading.CancellationTokenSource();
string fclassName = System.IO.Path.GetFileName(pathLayerIn); var fin = ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run((Func<string>)(() =>
IReadOnlyList<string> parameters = null;
IReadOnlyList<string> parameters2 = null;
FeatureClass featureClass = null;
var fin = ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run((Func<bool>)(() =>
{ {
if (string.IsNullOrEmpty(fclassName)) var progSrc = new CancelableProgressorSource();
return false; string[] args = { pathLayerIn, outpath };
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<FeatureClass>(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<FeatureClass>(fclassName);
}
}
if (featureClass == null)
return false;
//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);
}
catch
{
OutStr = "Ha ocurrido un error al convertir a shape";
return false;
}
return true;
}));
fin.Wait();
if(!fin.Result)// || parameters==null)
{
OutStr = "Ha ocurrido un error al convertir a shape";
return false;
}
var progSrc = new CancelableProgressorSource(progrDialog);
// execute the tool // execute the tool
IGPResult gpResult = Geoprocessing.ExecuteToolAsync("FeatureClassToShapefile_conversion", parameters, IGPResult gpResult = Geoprocessing.ExecuteToolAsync("FeatureClassToShapefile_conversion", args,
null, progSrc.Progressor, GPExecuteToolFlags.GPThread).Result; null, _cts.Token,
//renombrar shp (event_name, o) => // implement delegate and handle events
//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; switch (event_name)
{
case "OnValidate": // stop execute if any warnings
//if ((o as IGPMessage[]).Any(it => it.Type == GPMessageType.Warning))
//_cts.Cancel();
break;
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;
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 (string.IsNullOrEmpty(fin.Result))
{
OutStr = "Ha ocurrido un error en la herramienta de GeoProcesamiento";
return false; return false;
} }
else else
{ {
OutStr = "Ok: " + gpResult.ReturnValue; OutStr = "Ok: " + fin.Result;
return true; return true;
} }
} }

View File

@ -7,6 +7,7 @@ using System.Threading.Tasks;
using ArcGIS.Desktop.Internal.Framework.Controls; using ArcGIS.Desktop.Internal.Framework.Controls;
using ArcGIS.Desktop.Framework.Dialogs; using ArcGIS.Desktop.Framework.Dialogs;
using System.Windows; using System.Windows;
using System.IO;
namespace OliviaAddInPro.Helper namespace OliviaAddInPro.Helper
{ {
@ -50,5 +51,33 @@ namespace OliviaAddInPro.Helper
return false; 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
{
}
}
}
}
} }
} }

View File

@ -7,6 +7,7 @@ using System.Threading.Tasks;
using OliviaAddInPro.Services; using OliviaAddInPro.Services;
using ArcGIS.Core.Geometry; using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Framework.Threading.Tasks; using ArcGIS.Desktop.Framework.Threading.Tasks;
using OliviaAddInPro.Helper;
namespace OliviaAddInPro.Model namespace OliviaAddInPro.Model
{ {
@ -132,6 +133,9 @@ namespace OliviaAddInPro.Model
Paths.PathTemp = c.path_temp; Paths.PathTemp = c.path_temp;
c.PathCartela = c.PathCartela; c.PathCartela = c.PathCartela;
HelperGlobal.create_folder(Paths.DirData);
HelperGlobal.create_folder(Paths.PathTemp);
Paths.PathGdbGen = c.PathGdbGen; Paths.PathGdbGen = c.PathGdbGen;
Paths.PathGdbNw = c.red_carreteras; Paths.PathGdbNw = c.red_carreteras;
Paths.PathSimbVSM = c.PathSimbVSM; Paths.PathSimbVSM = c.PathSimbVSM;

View File

@ -37,15 +37,23 @@ namespace OliviaAddInPro.Services
*/ */
public bool ComienzaEjec(ModosEjec modo) public bool ComienzaEjec(ModosEjec modo)
{ {
bool fue_mal = false; using (OliviaGlob.progrDialog = new ProgressDialog("Exportando Datos", "Cancelar", 100, 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 //esconde el pane
OliviaGlob.ShowHidePane(false); OliviaGlob.ShowHidePane(false);
ErrStr = string.Empty; ErrStr = string.Empty;
//Cuenta las filas que cumplen la consulta //Cuenta las filas que cumplen la consulta
int nelems = HelperGdb.GetNumElems(com.CapaElems, com.ConsultaAmbs); int nelems = HelperGdb.GetNumElems(com.CapaElems, com.ConsultaAmbs);
if(nelems<=0) if (nelems <= 0)
{ {
ErrStr = "No existen ámbitos que cumplan las condiciones introducidas para la exportación " + com.ConsultaAmbs; ErrStr = "No existen ámbitos que cumplan las condiciones introducidas para la exportación " + com.ConsultaAmbs;
fue_mal = true; fue_mal = true;
@ -82,16 +90,19 @@ namespace OliviaAddInPro.Services
if (!fue_mal) if (!fue_mal)
{ {
Ejecuta(modo); Ejecuta(modo, status);
OliviaGlob.progrDialog.Hide();
return true; return true;
} }
OliviaGlob.progrDialog.Hide();
//fue mal //fue mal
//muestra pane //muestra pane
OliviaGlob.ShowHidePane(true); OliviaGlob.ShowHidePane(true);
return false; return false;
} }
}
/** /**
@ -192,14 +203,13 @@ namespace OliviaAddInPro.Services
/** /**
* Exporta y lanza proceso y ventana de proceso * Exporta y lanza proceso y ventana de proceso
*/ */
public void Ejecuta(ModosEjec modo) public void Ejecuta(ModosEjec modo, ProgressorSource dlgProcess)
{ {
//lanza ventana de progreso //lanza ventana de progreso
OliviaGlob.progrDialog = new ProgressDialog("Exportando Datos", "Cancelar", 100);
OliviaGlob.progrDialog.Show();
//exporta los datos de entrada //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) if(!res)
{ {
string msg = HelperGdb.OutStr; string msg = HelperGdb.OutStr;